Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
0002 /*
0003  * Special types used by various syscalls for NOLIBC
0004  * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
0005  */
0006 
0007 #ifndef _NOLIBC_TYPES_H
0008 #define _NOLIBC_TYPES_H
0009 
0010 #include "std.h"
0011 #include <linux/time.h>
0012 
0013 
0014 /* Only the generic macros and types may be defined here. The arch-specific
0015  * ones such as the O_RDONLY and related macros used by fcntl() and open(), or
0016  * the layout of sys_stat_struct must not be defined here.
0017  */
0018 
0019 /* stat flags (WARNING, octal here) */
0020 #define S_IFDIR        0040000
0021 #define S_IFCHR        0020000
0022 #define S_IFBLK        0060000
0023 #define S_IFREG        0100000
0024 #define S_IFIFO        0010000
0025 #define S_IFLNK        0120000
0026 #define S_IFSOCK       0140000
0027 #define S_IFMT         0170000
0028 
0029 #define S_ISDIR(mode)  (((mode) & S_IFDIR)  == S_IFDIR)
0030 #define S_ISCHR(mode)  (((mode) & S_IFCHR)  == S_IFCHR)
0031 #define S_ISBLK(mode)  (((mode) & S_IFBLK)  == S_IFBLK)
0032 #define S_ISREG(mode)  (((mode) & S_IFREG)  == S_IFREG)
0033 #define S_ISFIFO(mode) (((mode) & S_IFIFO)  == S_IFIFO)
0034 #define S_ISLNK(mode)  (((mode) & S_IFLNK)  == S_IFLNK)
0035 #define S_ISSOCK(mode) (((mode) & S_IFSOCK) == S_IFSOCK)
0036 
0037 /* dirent types */
0038 #define DT_UNKNOWN     0x0
0039 #define DT_FIFO        0x1
0040 #define DT_CHR         0x2
0041 #define DT_DIR         0x4
0042 #define DT_BLK         0x6
0043 #define DT_REG         0x8
0044 #define DT_LNK         0xa
0045 #define DT_SOCK        0xc
0046 
0047 /* commonly an fd_set represents 256 FDs */
0048 #ifndef FD_SETSIZE
0049 #define FD_SETSIZE     256
0050 #endif
0051 
0052 /* PATH_MAX and MAXPATHLEN are often used and found with plenty of different
0053  * values.
0054  */
0055 #ifndef PATH_MAX
0056 #define PATH_MAX       4096
0057 #endif
0058 
0059 #ifndef MAXPATHLEN
0060 #define MAXPATHLEN     (PATH_MAX)
0061 #endif
0062 
0063 /* Special FD used by all the *at functions */
0064 #ifndef AT_FDCWD
0065 #define AT_FDCWD       (-100)
0066 #endif
0067 
0068 /* whence values for lseek() */
0069 #define SEEK_SET       0
0070 #define SEEK_CUR       1
0071 #define SEEK_END       2
0072 
0073 /* cmd for reboot() */
0074 #define LINUX_REBOOT_MAGIC1         0xfee1dead
0075 #define LINUX_REBOOT_MAGIC2         0x28121969
0076 #define LINUX_REBOOT_CMD_HALT       0xcdef0123
0077 #define LINUX_REBOOT_CMD_POWER_OFF  0x4321fedc
0078 #define LINUX_REBOOT_CMD_RESTART    0x01234567
0079 #define LINUX_REBOOT_CMD_SW_SUSPEND 0xd000fce2
0080 
0081 /* Macros used on waitpid()'s return status */
0082 #define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
0083 #define WIFEXITED(status)   (((status) & 0x7f) == 0)
0084 
0085 /* waitpid() flags */
0086 #define WNOHANG      1
0087 
0088 /* standard exit() codes */
0089 #define EXIT_SUCCESS 0
0090 #define EXIT_FAILURE 1
0091 
0092 /* for select() */
0093 typedef struct {
0094     uint32_t fd32[(FD_SETSIZE + 31) / 32];
0095 } fd_set;
0096 
0097 #define FD_CLR(fd, set) do {                                            \
0098         fd_set *__set = (set);                                  \
0099         int __fd = (fd);                                        \
0100         if (__fd >= 0)                                          \
0101             __set->fd32[__fd / 32] &= ~(1U << (__fd & 31)); \
0102     } while (0)
0103 
0104 #define FD_SET(fd, set) do {                                            \
0105         fd_set *__set = (set);                                  \
0106         int __fd = (fd);                                        \
0107         if (__fd >= 0)                                          \
0108             __set->fd32[__fd / 32] |= 1U << (__fd & 31);    \
0109     } while (0)
0110 
0111 #define FD_ISSET(fd, set) ({                                                  \
0112         fd_set *__set = (set);                                        \
0113         int __fd = (fd);                                              \
0114         int __r = 0;                                                  \
0115         if (__fd >= 0)                                                \
0116             __r = !!(__set->fd32[__fd / 32] & 1U << (__fd & 31)); \
0117         __r;                                                          \
0118     })
0119 
0120 #define FD_ZERO(set) do {                                               \
0121         fd_set *__set = (set);                                  \
0122         int __idx;                                              \
0123         for (__idx = 0; __idx < (FD_SETSIZE+31) / 32; __idx ++) \
0124             __set->fd32[__idx] = 0;                         \
0125     } while (0)
0126 
0127 /* for poll() */
0128 #define POLLIN          0x0001
0129 #define POLLPRI         0x0002
0130 #define POLLOUT         0x0004
0131 #define POLLERR         0x0008
0132 #define POLLHUP         0x0010
0133 #define POLLNVAL        0x0020
0134 
0135 struct pollfd {
0136     int fd;
0137     short int events;
0138     short int revents;
0139 };
0140 
0141 /* for getdents64() */
0142 struct linux_dirent64 {
0143     uint64_t       d_ino;
0144     int64_t        d_off;
0145     unsigned short d_reclen;
0146     unsigned char  d_type;
0147     char           d_name[];
0148 };
0149 
0150 /* needed by wait4() */
0151 struct rusage {
0152     struct timeval ru_utime;
0153     struct timeval ru_stime;
0154     long   ru_maxrss;
0155     long   ru_ixrss;
0156     long   ru_idrss;
0157     long   ru_isrss;
0158     long   ru_minflt;
0159     long   ru_majflt;
0160     long   ru_nswap;
0161     long   ru_inblock;
0162     long   ru_oublock;
0163     long   ru_msgsnd;
0164     long   ru_msgrcv;
0165     long   ru_nsignals;
0166     long   ru_nvcsw;
0167     long   ru_nivcsw;
0168 };
0169 
0170 /* The format of the struct as returned by the libc to the application, which
0171  * significantly differs from the format returned by the stat() syscall flavours.
0172  */
0173 struct stat {
0174     dev_t     st_dev;     /* ID of device containing file */
0175     ino_t     st_ino;     /* inode number */
0176     mode_t    st_mode;    /* protection */
0177     nlink_t   st_nlink;   /* number of hard links */
0178     uid_t     st_uid;     /* user ID of owner */
0179     gid_t     st_gid;     /* group ID of owner */
0180     dev_t     st_rdev;    /* device ID (if special file) */
0181     off_t     st_size;    /* total size, in bytes */
0182     blksize_t st_blksize; /* blocksize for file system I/O */
0183     blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
0184     time_t    st_atime;   /* time of last access */
0185     time_t    st_mtime;   /* time of last modification */
0186     time_t    st_ctime;   /* time of last status change */
0187 };
0188 
0189 /* WARNING, it only deals with the 4096 first majors and 256 first minors */
0190 #define makedev(major, minor) ((dev_t)((((major) & 0xfff) << 8) | ((minor) & 0xff)))
0191 #define major(dev) ((unsigned int)(((dev) >> 8) & 0xfff))
0192 #define minor(dev) ((unsigned int)(((dev) & 0xff))
0193 
0194 #ifndef offsetof
0195 #define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD)
0196 #endif
0197 
0198 #ifndef container_of
0199 #define container_of(PTR, TYPE, FIELD) ({           \
0200     __typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR = (PTR);    \
0201     (TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD)); \
0202 })
0203 #endif
0204 
0205 #endif /* _NOLIBC_TYPES_H */