1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */ 2 /* 3 * Special types used by various syscalls for NOLIBC 4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu> 5 */ 6 7 #ifndef _NOLIBC_TYPES_H 8 #define _NOLIBC_TYPES_H 9 10 #include "std.h" 11 #include <linux/time.h> 12 #include <linux/stat.h> 13 14 15 /* Only the generic macros and types may be defined here. The arch-specific 16 * ones such as the O_RDONLY and related macros used by fcntl() and open(), or 17 * the layout of sys_stat_struct must not be defined here. 18 */ 19 20 /* stat flags (WARNING, octal here). We need to check for an existing 21 * definition because linux/stat.h may omit to define those if it finds 22 * that any glibc header was already included. 23 */ 24 #if !defined(S_IFMT) 25 #define S_IFDIR 0040000 26 #define S_IFCHR 0020000 27 #define S_IFBLK 0060000 28 #define S_IFREG 0100000 29 #define S_IFIFO 0010000 30 #define S_IFLNK 0120000 31 #define S_IFSOCK 0140000 32 #define S_IFMT 0170000 33 34 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) 35 #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR) 36 #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK) 37 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) 38 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO) 39 #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK) 40 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK) 41 42 #define S_IRWXU 00700 43 #define S_IRUSR 00400 44 #define S_IWUSR 00200 45 #define S_IXUSR 00100 46 47 #define S_IRWXG 00070 48 #define S_IRGRP 00040 49 #define S_IWGRP 00020 50 #define S_IXGRP 00010 51 52 #define S_IRWXO 00007 53 #define S_IROTH 00004 54 #define S_IWOTH 00002 55 #define S_IXOTH 00001 56 #endif 57 58 /* dirent types */ 59 #define DT_UNKNOWN 0x0 60 #define DT_FIFO 0x1 61 #define DT_CHR 0x2 62 #define DT_DIR 0x4 63 #define DT_BLK 0x6 64 #define DT_REG 0x8 65 #define DT_LNK 0xa 66 #define DT_SOCK 0xc 67 68 /* commonly an fd_set represents 256 FDs */ 69 #ifndef FD_SETSIZE 70 #define FD_SETSIZE 256 71 #endif 72 73 /* PATH_MAX and MAXPATHLEN are often used and found with plenty of different 74 * values. 75 */ 76 #ifndef PATH_MAX 77 #define PATH_MAX 4096 78 #endif 79 80 #ifndef MAXPATHLEN 81 #define MAXPATHLEN (PATH_MAX) 82 #endif 83 84 /* whence values for lseek() */ 85 #define SEEK_SET 0 86 #define SEEK_CUR 1 87 #define SEEK_END 2 88 89 /* Macros used on waitpid()'s return status */ 90 #define WEXITSTATUS(status) (((status) & 0xff00) >> 8) 91 #define WIFEXITED(status) (((status) & 0x7f) == 0) 92 #define WTERMSIG(status) ((status) & 0x7f) 93 #define WIFSIGNALED(status) ((status) - 1 < 0xff) 94 95 /* waitpid() flags */ 96 #define WNOHANG 1 97 98 /* standard exit() codes */ 99 #define EXIT_SUCCESS 0 100 #define EXIT_FAILURE 1 101 102 #define FD_SETIDXMASK (8 * sizeof(unsigned long)) 103 #define FD_SETBITMASK (8 * sizeof(unsigned long)-1) 104 105 /* for select() */ 106 typedef struct { 107 unsigned long fds[(FD_SETSIZE + FD_SETBITMASK) / FD_SETIDXMASK]; 108 } fd_set; 109 110 #define FD_CLR(fd, set) do { \ 111 fd_set *__set = (set); \ 112 int __fd = (fd); \ 113 if (__fd >= 0) \ 114 __set->fds[__fd / FD_SETIDXMASK] &= \ 115 ~(1U << (__fd & FX_SETBITMASK)); \ 116 } while (0) 117 118 #define FD_SET(fd, set) do { \ 119 fd_set *__set = (set); \ 120 int __fd = (fd); \ 121 if (__fd >= 0) \ 122 __set->fds[__fd / FD_SETIDXMASK] |= \ 123 1 << (__fd & FD_SETBITMASK); \ 124 } while (0) 125 126 #define FD_ISSET(fd, set) ({ \ 127 fd_set *__set = (set); \ 128 int __fd = (fd); \ 129 int __r = 0; \ 130 if (__fd >= 0) \ 131 __r = !!(__set->fds[__fd / FD_SETIDXMASK] & \ 132 1U << (__fd & FD_SET_BITMASK)); \ 133 __r; \ 134 }) 135 136 #define FD_ZERO(set) do { \ 137 fd_set *__set = (set); \ 138 int __idx; \ 139 int __size = (FD_SETSIZE+FD_SETBITMASK) / FD_SETIDXMASK;\ 140 for (__idx = 0; __idx < __size; __idx++) \ 141 __set->fds[__idx] = 0; \ 142 } while (0) 143 144 /* for poll() */ 145 #define POLLIN 0x0001 146 #define POLLPRI 0x0002 147 #define POLLOUT 0x0004 148 #define POLLERR 0x0008 149 #define POLLHUP 0x0010 150 #define POLLNVAL 0x0020 151 152 struct pollfd { 153 int fd; 154 short int events; 155 short int revents; 156 }; 157 158 /* for getdents64() */ 159 struct linux_dirent64 { 160 uint64_t d_ino; 161 int64_t d_off; 162 unsigned short d_reclen; 163 unsigned char d_type; 164 char d_name[]; 165 }; 166 167 /* needed by wait4() */ 168 struct rusage { 169 struct timeval ru_utime; 170 struct timeval ru_stime; 171 long ru_maxrss; 172 long ru_ixrss; 173 long ru_idrss; 174 long ru_isrss; 175 long ru_minflt; 176 long ru_majflt; 177 long ru_nswap; 178 long ru_inblock; 179 long ru_oublock; 180 long ru_msgsnd; 181 long ru_msgrcv; 182 long ru_nsignals; 183 long ru_nvcsw; 184 long ru_nivcsw; 185 }; 186 187 /* The format of the struct as returned by the libc to the application, which 188 * significantly differs from the format returned by the stat() syscall flavours. 189 */ 190 struct stat { 191 dev_t st_dev; /* ID of device containing file */ 192 ino_t st_ino; /* inode number */ 193 mode_t st_mode; /* protection */ 194 nlink_t st_nlink; /* number of hard links */ 195 uid_t st_uid; /* user ID of owner */ 196 gid_t st_gid; /* group ID of owner */ 197 dev_t st_rdev; /* device ID (if special file) */ 198 off_t st_size; /* total size, in bytes */ 199 blksize_t st_blksize; /* blocksize for file system I/O */ 200 blkcnt_t st_blocks; /* number of 512B blocks allocated */ 201 union { time_t st_atime; struct timespec st_atim; }; /* time of last access */ 202 union { time_t st_mtime; struct timespec st_mtim; }; /* time of last modification */ 203 union { time_t st_ctime; struct timespec st_ctim; }; /* time of last status change */ 204 }; 205 206 /* WARNING, it only deals with the 4096 first majors and 256 first minors */ 207 #define makedev(major, minor) ((dev_t)((((major) & 0xfff) << 8) | ((minor) & 0xff))) 208 #define major(dev) ((unsigned int)(((dev) >> 8) & 0xfff)) 209 #define minor(dev) ((unsigned int)(((dev) & 0xff)) 210 211 #ifndef offsetof 212 #define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD) 213 #endif 214 215 #ifndef container_of 216 #define container_of(PTR, TYPE, FIELD) ({ \ 217 __typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR = (PTR); \ 218 (TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD)); \ 219 }) 220 #endif 221 222 /* make sure to include all global symbols */ 223 #include "nolibc.h" 224 225 #endif /* _NOLIBC_TYPES_H */ 226