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