LCOV - code coverage report
Current view: top level - basic - missing_syscall.h (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 10 22 45.5 %
Date: 2019-08-22 15:41:25 Functions: 5 10 50.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : #pragma once
       3             : 
       4             : /* Missing glibc definitions to access certain kernel APIs */
       5             : 
       6             : #include <errno.h>
       7             : #include <fcntl.h>
       8             : #include <sys/syscall.h>
       9             : #include <sys/types.h>
      10             : #include <unistd.h>
      11             : 
      12             : #ifdef ARCH_MIPS
      13             : #include <asm/sgidefs.h>
      14             : #endif
      15             : 
      16             : #include "missing_keyctl.h"
      17             : #include "missing_stat.h"
      18             : 
      19             : /* linux/kcmp.h */
      20             : #ifndef KCMP_FILE /* 3f4994cfc15f38a3159c6e3a4b3ab2e1481a6b02 (3.19) */
      21             : #define KCMP_FILE 0
      22             : #endif
      23             : 
      24             : #if !HAVE_PIVOT_ROOT
      25           0 : static inline int missing_pivot_root(const char *new_root, const char *put_old) {
      26           0 :         return syscall(__NR_pivot_root, new_root, put_old);
      27             : }
      28             : 
      29             : #  define pivot_root missing_pivot_root
      30             : #endif
      31             : 
      32             : /* ======================================================================= */
      33             : 
      34             : #if !HAVE_MEMFD_CREATE
      35             : /* may be (invalid) negative number due to libseccomp, see PR 13319 */
      36             : #  if ! (defined __NR_memfd_create && __NR_memfd_create > 0)
      37             : #    if defined __NR_memfd_create
      38             : #      undef __NR_memfd_create
      39             : #    endif
      40             : #    if defined __x86_64__
      41             : #      define __NR_memfd_create 319
      42             : #    elif defined __arm__
      43             : #      define __NR_memfd_create 385
      44             : #    elif defined __aarch64__
      45             : #      define __NR_memfd_create 279
      46             : #    elif defined __s390__
      47             : #      define __NR_memfd_create 350
      48             : #    elif defined _MIPS_SIM
      49             : #      if _MIPS_SIM == _MIPS_SIM_ABI32
      50             : #        define __NR_memfd_create 4354
      51             : #      endif
      52             : #      if _MIPS_SIM == _MIPS_SIM_NABI32
      53             : #        define __NR_memfd_create 6318
      54             : #      endif
      55             : #      if _MIPS_SIM == _MIPS_SIM_ABI64
      56             : #        define __NR_memfd_create 5314
      57             : #      endif
      58             : #    elif defined __i386__
      59             : #      define __NR_memfd_create 356
      60             : #    elif defined __arc__
      61             : #      define __NR_memfd_create 279
      62             : #    else
      63             : #      warning "__NR_memfd_create unknown for your architecture"
      64             : #    endif
      65             : #  endif
      66             : 
      67             : static inline int missing_memfd_create(const char *name, unsigned int flags) {
      68             : #  ifdef __NR_memfd_create
      69             :         return syscall(__NR_memfd_create, name, flags);
      70             : #  else
      71             :         errno = ENOSYS;
      72             :         return -1;
      73             : #  endif
      74             : }
      75             : 
      76             : #  define memfd_create missing_memfd_create
      77             : #endif
      78             : 
      79             : /* ======================================================================= */
      80             : 
      81             : #if !HAVE_GETRANDOM
      82             : /* may be (invalid) negative number due to libseccomp, see PR 13319 */
      83             : #  if ! (defined __NR_getrandom && __NR_getrandom > 0)
      84             : #    if defined __NR_getrandom
      85             : #      undef __NR_getrandom
      86             : #    endif
      87             : #    if defined __x86_64__
      88             : #      define __NR_getrandom 318
      89             : #    elif defined(__i386__)
      90             : #      define __NR_getrandom 355
      91             : #    elif defined(__arm__)
      92             : #      define __NR_getrandom 384
      93             : #   elif defined(__aarch64__)
      94             : #      define __NR_getrandom 278
      95             : #    elif defined(__ia64__)
      96             : #      define __NR_getrandom 1339
      97             : #    elif defined(__m68k__)
      98             : #      define __NR_getrandom 352
      99             : #    elif defined(__s390x__)
     100             : #      define __NR_getrandom 349
     101             : #    elif defined(__powerpc__)
     102             : #      define __NR_getrandom 359
     103             : #    elif defined _MIPS_SIM
     104             : #      if _MIPS_SIM == _MIPS_SIM_ABI32
     105             : #        define __NR_getrandom 4353
     106             : #      endif
     107             : #      if _MIPS_SIM == _MIPS_SIM_NABI32
     108             : #        define __NR_getrandom 6317
     109             : #      endif
     110             : #      if _MIPS_SIM == _MIPS_SIM_ABI64
     111             : #        define __NR_getrandom 5313
     112             : #      endif
     113             : #    elif defined(__arc__)
     114             : #      define __NR_getrandom 278
     115             : #    else
     116             : #      warning "__NR_getrandom unknown for your architecture"
     117             : #    endif
     118             : #  endif
     119             : 
     120             : static inline int missing_getrandom(void *buffer, size_t count, unsigned flags) {
     121             : #  ifdef __NR_getrandom
     122             :         return syscall(__NR_getrandom, buffer, count, flags);
     123             : #  else
     124             :         errno = ENOSYS;
     125             :         return -1;
     126             : #  endif
     127             : }
     128             : 
     129             : #  define getrandom missing_getrandom
     130             : #endif
     131             : 
     132             : /* ======================================================================= */
     133             : 
     134             : #if !HAVE_GETTID
     135         212 : static inline pid_t missing_gettid(void) {
     136         212 :         return (pid_t) syscall(__NR_gettid);
     137             : }
     138             : 
     139             : #  define gettid missing_gettid
     140             : #endif
     141             : 
     142             : /* ======================================================================= */
     143             : 
     144             : #if !HAVE_NAME_TO_HANDLE_AT
     145             : /* may be (invalid) negative number due to libseccomp, see PR 13319 */
     146             : #  if ! (defined __NR_name_to_handle_at && __NR_name_to_handle_at > 0)
     147             : #    if defined __NR_name_to_handle_at
     148             : #      undef __NR_name_to_handle_at
     149             : #    endif
     150             : #    if defined(__x86_64__)
     151             : #      define __NR_name_to_handle_at 303
     152             : #    elif defined(__i386__)
     153             : #      define __NR_name_to_handle_at 341
     154             : #    elif defined(__arm__)
     155             : #      define __NR_name_to_handle_at 370
     156             : #    elif defined(__powerpc__)
     157             : #      define __NR_name_to_handle_at 345
     158             : #    elif defined(__arc__)
     159             : #      define __NR_name_to_handle_at 264
     160             : #    else
     161             : #      error "__NR_name_to_handle_at is not defined"
     162             : #    endif
     163             : #  endif
     164             : 
     165             : struct file_handle {
     166             :         unsigned int handle_bytes;
     167             :         int handle_type;
     168             :         unsigned char f_handle[0];
     169             : };
     170             : 
     171             : static inline int missing_name_to_handle_at(int fd, const char *name, struct file_handle *handle, int *mnt_id, int flags) {
     172             : #  ifdef __NR_name_to_handle_at
     173             :         return syscall(__NR_name_to_handle_at, fd, name, handle, mnt_id, flags);
     174             : #  else
     175             :         errno = ENOSYS;
     176             :         return -1;
     177             : #  endif
     178             : }
     179             : 
     180             : #  define name_to_handle_at missing_name_to_handle_at
     181             : #endif
     182             : 
     183             : /* ======================================================================= */
     184             : 
     185             : #if !HAVE_SETNS
     186             : /* may be (invalid) negative number due to libseccomp, see PR 13319 */
     187             : #  if ! (defined __NR_setns && __NR_setns > 0)
     188             : #    if defined __NR_setns
     189             : #      undef __NR_setns
     190             : #    endif
     191             : #    if defined(__x86_64__)
     192             : #      define __NR_setns 308
     193             : #    elif defined(__i386__)
     194             : #      define __NR_setns 346
     195             : #    elif defined(__arc__)
     196             : #      define __NR_setns 268
     197             : #    else
     198             : #      error "__NR_setns is not defined"
     199             : #    endif
     200             : #  endif
     201             : 
     202             : static inline int missing_setns(int fd, int nstype) {
     203             : #  ifdef __NR_setns
     204             :         return syscall(__NR_setns, fd, nstype);
     205             : #  else
     206             :         errno = ENOSYS;
     207             :         return -1;
     208             : #  endif
     209             : }
     210             : 
     211             : #  define setns missing_setns
     212             : #endif
     213             : 
     214             : /* ======================================================================= */
     215             : 
     216         461 : static inline pid_t raw_getpid(void) {
     217             : #if defined(__alpha__)
     218             :         return (pid_t) syscall(__NR_getxpid);
     219             : #else
     220         461 :         return (pid_t) syscall(__NR_getpid);
     221             : #endif
     222             : }
     223             : 
     224             : /* ======================================================================= */
     225             : 
     226             : #if !HAVE_RENAMEAT2
     227             : /* may be (invalid) negative number due to libseccomp, see PR 13319 */
     228             : #  if ! (defined __NR_renameat2 && __NR_renameat2 > 0)
     229             : #    if defined __NR_renameat2
     230             : #      undef __NR_renameat2
     231             : #    endif
     232             : #    if defined __x86_64__
     233             : #      define __NR_renameat2 316
     234             : #    elif defined __arm__
     235             : #      define __NR_renameat2 382
     236             : #    elif defined __aarch64__
     237             : #      define __NR_renameat2 276
     238             : #    elif defined _MIPS_SIM
     239             : #      if _MIPS_SIM == _MIPS_SIM_ABI32
     240             : #        define __NR_renameat2 4351
     241             : #      endif
     242             : #      if _MIPS_SIM == _MIPS_SIM_NABI32
     243             : #        define __NR_renameat2 6315
     244             : #      endif
     245             : #      if _MIPS_SIM == _MIPS_SIM_ABI64
     246             : #        define __NR_renameat2 5311
     247             : #      endif
     248             : #    elif defined __i386__
     249             : #      define __NR_renameat2 353
     250             : #    elif defined __powerpc64__
     251             : #      define __NR_renameat2 357
     252             : #    elif defined __s390__ || defined __s390x__
     253             : #      define __NR_renameat2 347
     254             : #    elif defined __arc__
     255             : #      define __NR_renameat2 276
     256             : #    else
     257             : #      warning "__NR_renameat2 unknown for your architecture"
     258             : #    endif
     259             : #  endif
     260             : 
     261             : static inline int missing_renameat2(int oldfd, const char *oldname, int newfd, const char *newname, unsigned flags) {
     262             : #  ifdef __NR_renameat2
     263             :         return syscall(__NR_renameat2, oldfd, oldname, newfd, newname, flags);
     264             : #  else
     265             :         errno = ENOSYS;
     266             :         return -1;
     267             : #  endif
     268             : }
     269             : 
     270             : #  define renameat2 missing_renameat2
     271             : #endif
     272             : 
     273             : /* ======================================================================= */
     274             : 
     275             : #if !HAVE_KCMP
     276          16 : static inline int missing_kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1, unsigned long idx2) {
     277             : #  ifdef __NR_kcmp
     278          16 :         return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
     279             : #  else
     280             :         errno = ENOSYS;
     281             :         return -1;
     282             : #  endif
     283             : }
     284             : 
     285             : #  define kcmp missing_kcmp
     286             : #endif
     287             : 
     288             : /* ======================================================================= */
     289             : 
     290             : #if !HAVE_KEYCTL
     291           0 : static inline long missing_keyctl(int cmd, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5) {
     292             : #  ifdef __NR_keyctl
     293           0 :         return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
     294             : #  else
     295             :         errno = ENOSYS;
     296             :         return -1;
     297             : #  endif
     298             : 
     299             : #  define keyctl missing_keyctl
     300             : }
     301             : 
     302           0 : static inline key_serial_t missing_add_key(const char *type, const char *description, const void *payload, size_t plen, key_serial_t ringid) {
     303             : #  ifdef __NR_add_key
     304           0 :         return syscall(__NR_add_key, type, description, payload, plen, ringid);
     305             : #  else
     306             :         errno = ENOSYS;
     307             :         return -1;
     308             : #  endif
     309             : 
     310             : #  define add_key missing_add_key
     311             : }
     312             : 
     313           1 : static inline key_serial_t missing_request_key(const char *type, const char *description, const char * callout_info, key_serial_t destringid) {
     314             : #  ifdef __NR_request_key
     315           1 :         return syscall(__NR_request_key, type, description, callout_info, destringid);
     316             : #  else
     317             :         errno = ENOSYS;
     318             :         return -1;
     319             : #  endif
     320             : 
     321             : #  define request_key missing_request_key
     322             : }
     323             : #endif
     324             : 
     325             : /* ======================================================================= */
     326             : 
     327             : #if !HAVE_COPY_FILE_RANGE
     328             : /* may be (invalid) negative number due to libseccomp, see PR 13319 */
     329             : #  if ! (defined __NR_copy_file_range && __NR_copy_file_range > 0)
     330             : #    if defined __NR_copy_file_range
     331             : #      undef __NR_copy_file_range
     332             : #    endif
     333             : #    if defined(__x86_64__)
     334             : #      define __NR_copy_file_range 326
     335             : #    elif defined(__i386__)
     336             : #      define __NR_copy_file_range 377
     337             : #    elif defined __s390__
     338             : #      define __NR_copy_file_range 375
     339             : #    elif defined __arm__
     340             : #      define __NR_copy_file_range 391
     341             : #    elif defined __aarch64__
     342             : #      define __NR_copy_file_range 285
     343             : #    elif defined __powerpc__
     344             : #      define __NR_copy_file_range 379
     345             : #    elif defined __arc__
     346             : #      define __NR_copy_file_range 285
     347             : #    else
     348             : #      warning "__NR_copy_file_range not defined for your architecture"
     349             : #    endif
     350             : #  endif
     351             : 
     352             : static inline ssize_t missing_copy_file_range(int fd_in, loff_t *off_in,
     353             :                                               int fd_out, loff_t *off_out,
     354             :                                               size_t len,
     355             :                                               unsigned int flags) {
     356             : #  ifdef __NR_copy_file_range
     357             :         return syscall(__NR_copy_file_range, fd_in, off_in, fd_out, off_out, len, flags);
     358             : #  else
     359             :         errno = ENOSYS;
     360             :         return -1;
     361             : #  endif
     362             : }
     363             : 
     364             : #  define copy_file_range missing_copy_file_range
     365             : #endif
     366             : 
     367             : /* ======================================================================= */
     368             : 
     369             : #if !HAVE_BPF
     370             : /* may be (invalid) negative number due to libseccomp, see PR 13319 */
     371             : #  if ! (defined __NR_bpf && __NR_bpf > 0)
     372             : #    if defined __NR_bpf
     373             : #      undef __NR_bpf
     374             : #    endif
     375             : #    if defined __i386__
     376             : #      define __NR_bpf 357
     377             : #    elif defined __x86_64__
     378             : #      define __NR_bpf 321
     379             : #    elif defined __aarch64__
     380             : #      define __NR_bpf 280
     381             : #    elif defined __arm__
     382             : #      define __NR_bpf 386
     383             : #    elif defined __sparc__
     384             : #      define __NR_bpf 349
     385             : #    elif defined __s390__
     386             : #      define __NR_bpf 351
     387             : #    elif defined __tilegx__
     388             : #      define __NR_bpf 280
     389             : #    else
     390             : #      warning "__NR_bpf not defined for your architecture"
     391             : #    endif
     392             : #  endif
     393             : 
     394             : union bpf_attr;
     395             : 
     396          10 : static inline int missing_bpf(int cmd, union bpf_attr *attr, size_t size) {
     397             : #ifdef __NR_bpf
     398          10 :         return (int) syscall(__NR_bpf, cmd, attr, size);
     399             : #else
     400             :         errno = ENOSYS;
     401             :         return -1;
     402             : #endif
     403             : }
     404             : 
     405             : #  define bpf missing_bpf
     406             : #endif
     407             : 
     408             : /* ======================================================================= */
     409             : 
     410             : #ifndef __IGNORE_pkey_mprotect
     411             : /* may be (invalid) negative number due to libseccomp, see PR 13319 */
     412             : #  if ! (defined __NR_pkey_mprotect && __NR_pkey_mprotect > 0)
     413             : #    if defined __NR_pkey_mprotect
     414             : #      undef __NR_pkey_mprotect
     415             : #    endif
     416             : #    if defined __i386__
     417             : #      define __NR_pkey_mprotect 380
     418             : #    elif defined __x86_64__
     419             : #      define __NR_pkey_mprotect 329
     420             : #    elif defined __arm__
     421             : #      define __NR_pkey_mprotect 394
     422             : #    elif defined __aarch64__
     423             : #      define __NR_pkey_mprotect 394
     424             : #    elif defined __powerpc__
     425             : #      define __NR_pkey_mprotect 386
     426             : #    elif defined __s390__
     427             : #      define __NR_pkey_mprotect 384
     428             : #    elif defined _MIPS_SIM
     429             : #      if _MIPS_SIM == _MIPS_SIM_ABI32
     430             : #        define __NR_pkey_mprotect 4363
     431             : #      endif
     432             : #      if _MIPS_SIM == _MIPS_SIM_NABI32
     433             : #        define __NR_pkey_mprotect 6327
     434             : #      endif
     435             : #      if _MIPS_SIM == _MIPS_SIM_ABI64
     436             : #        define __NR_pkey_mprotect 5323
     437             : #      endif
     438             : #    else
     439             : #      warning "__NR_pkey_mprotect not defined for your architecture"
     440             : #    endif
     441             : #  endif
     442             : #endif
     443             : 
     444             : /* ======================================================================= */
     445             : 
     446             : #if !HAVE_STATX
     447             : /* may be (invalid) negative number due to libseccomp, see PR 13319 */
     448             : #  if ! (defined __NR_statx && __NR_statx > 0)
     449             : #    if defined __NR_statx
     450             : #      undef __NR_statx
     451             : #    endif
     452             : #    if defined __aarch64__ || defined __arm__
     453             : #      define __NR_statx 397
     454             : #    elif defined __alpha__
     455             : #      define __NR_statx 522
     456             : #    elif defined __i386__ || defined __powerpc64__
     457             : #      define __NR_statx 383
     458             : #    elif defined __sparc__
     459             : #      define __NR_statx 360
     460             : #    elif defined __x86_64__
     461             : #      define __NR_statx 332
     462             : #    else
     463             : #      warning "__NR_statx not defined for your architecture"
     464             : #    endif
     465             : #  endif
     466             : 
     467             : struct statx;
     468             : #endif
     469             : 
     470             : /* This typedef is supposed to be always defined. */
     471             : typedef struct statx struct_statx;
     472             : 
     473             : #if !HAVE_STATX
     474             : static inline ssize_t missing_statx(int dfd, const char *filename, unsigned flags, unsigned int mask, struct statx *buffer) {
     475             : #  ifdef __NR_statx
     476             :         return syscall(__NR_statx, dfd, filename, flags, mask, buffer);
     477             : #  else
     478             :         errno = ENOSYS;
     479             :         return -1;
     480             : #  endif
     481             : }
     482             : 
     483             : #  define statx missing_statx
     484             : #endif
     485             : 
     486             : #if !HAVE_SET_MEMPOLICY
     487             : 
     488             : enum {
     489             :         MPOL_DEFAULT,
     490             :         MPOL_PREFERRED,
     491             :         MPOL_BIND,
     492             :         MPOL_INTERLEAVE,
     493             :         MPOL_LOCAL,
     494             : };
     495             : 
     496           0 : static inline long missing_set_mempolicy(int mode, const unsigned long *nodemask,
     497             :                            unsigned long maxnode) {
     498             :         long i;
     499             : #  ifdef __NR_set_mempolicy
     500           0 :         i = syscall(__NR_set_mempolicy, mode, nodemask, maxnode);
     501             : #  else
     502             :         errno = ENOSYS;
     503             :         i = -1;
     504             : #  endif
     505           0 :         return i;
     506             : }
     507             : 
     508             : #  define set_mempolicy missing_set_mempolicy
     509             : #endif
     510             : 
     511             : #if !HAVE_GET_MEMPOLICY
     512           0 : static inline long missing_get_mempolicy(int *mode, unsigned long *nodemask,
     513             :                            unsigned long maxnode, void *addr,
     514             :                            unsigned long flags) {
     515             :         long i;
     516             : #  ifdef __NR_get_mempolicy
     517           0 :         i = syscall(__NR_get_mempolicy, mode, nodemask, maxnode, addr, flags);
     518             : #  else
     519             :         errno = ENOSYS;
     520             :         i = -1;
     521             : #  endif
     522           0 :         return i;
     523             : }
     524             : 
     525             : #define get_mempolicy missing_get_mempolicy
     526             : #endif

Generated by: LCOV version 1.14