at v6.4 233 lines 6.4 kB view raw
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/* cmd for reboot() */ 90#define LINUX_REBOOT_MAGIC1 0xfee1dead 91#define LINUX_REBOOT_MAGIC2 0x28121969 92#define LINUX_REBOOT_CMD_HALT 0xcdef0123 93#define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc 94#define LINUX_REBOOT_CMD_RESTART 0x01234567 95#define LINUX_REBOOT_CMD_SW_SUSPEND 0xd000fce2 96 97/* Macros used on waitpid()'s return status */ 98#define WEXITSTATUS(status) (((status) & 0xff00) >> 8) 99#define WIFEXITED(status) (((status) & 0x7f) == 0) 100#define WTERMSIG(status) ((status) & 0x7f) 101#define WIFSIGNALED(status) ((status) - 1 < 0xff) 102 103/* waitpid() flags */ 104#define WNOHANG 1 105 106/* standard exit() codes */ 107#define EXIT_SUCCESS 0 108#define EXIT_FAILURE 1 109 110#define FD_SETIDXMASK (8 * sizeof(unsigned long)) 111#define FD_SETBITMASK (8 * sizeof(unsigned long)-1) 112 113/* for select() */ 114typedef struct { 115 unsigned long fds[(FD_SETSIZE + FD_SETBITMASK) / FD_SETIDXMASK]; 116} fd_set; 117 118#define FD_CLR(fd, set) do { \ 119 fd_set *__set = (set); \ 120 int __fd = (fd); \ 121 if (__fd >= 0) \ 122 __set->fds[__fd / FD_SETIDXMASK] &= \ 123 ~(1U << (__fd & FX_SETBITMASK)); \ 124 } while (0) 125 126#define FD_SET(fd, set) do { \ 127 fd_set *__set = (set); \ 128 int __fd = (fd); \ 129 if (__fd >= 0) \ 130 __set->fds[__fd / FD_SETIDXMASK] |= \ 131 1 << (__fd & FD_SETBITMASK); \ 132 } while (0) 133 134#define FD_ISSET(fd, set) ({ \ 135 fd_set *__set = (set); \ 136 int __fd = (fd); \ 137 int __r = 0; \ 138 if (__fd >= 0) \ 139 __r = !!(__set->fds[__fd / FD_SETIDXMASK] & \ 1401U << (__fd & FD_SET_BITMASK)); \ 141 __r; \ 142 }) 143 144#define FD_ZERO(set) do { \ 145 fd_set *__set = (set); \ 146 int __idx; \ 147 int __size = (FD_SETSIZE+FD_SETBITMASK) / FD_SETIDXMASK;\ 148 for (__idx = 0; __idx < __size; __idx++) \ 149 __set->fds[__idx] = 0; \ 150 } while (0) 151 152/* for poll() */ 153#define POLLIN 0x0001 154#define POLLPRI 0x0002 155#define POLLOUT 0x0004 156#define POLLERR 0x0008 157#define POLLHUP 0x0010 158#define POLLNVAL 0x0020 159 160struct pollfd { 161 int fd; 162 short int events; 163 short int revents; 164}; 165 166/* for getdents64() */ 167struct linux_dirent64 { 168 uint64_t d_ino; 169 int64_t d_off; 170 unsigned short d_reclen; 171 unsigned char d_type; 172 char d_name[]; 173}; 174 175/* needed by wait4() */ 176struct rusage { 177 struct timeval ru_utime; 178 struct timeval ru_stime; 179 long ru_maxrss; 180 long ru_ixrss; 181 long ru_idrss; 182 long ru_isrss; 183 long ru_minflt; 184 long ru_majflt; 185 long ru_nswap; 186 long ru_inblock; 187 long ru_oublock; 188 long ru_msgsnd; 189 long ru_msgrcv; 190 long ru_nsignals; 191 long ru_nvcsw; 192 long ru_nivcsw; 193}; 194 195/* The format of the struct as returned by the libc to the application, which 196 * significantly differs from the format returned by the stat() syscall flavours. 197 */ 198struct stat { 199 dev_t st_dev; /* ID of device containing file */ 200 ino_t st_ino; /* inode number */ 201 mode_t st_mode; /* protection */ 202 nlink_t st_nlink; /* number of hard links */ 203 uid_t st_uid; /* user ID of owner */ 204 gid_t st_gid; /* group ID of owner */ 205 dev_t st_rdev; /* device ID (if special file) */ 206 off_t st_size; /* total size, in bytes */ 207 blksize_t st_blksize; /* blocksize for file system I/O */ 208 blkcnt_t st_blocks; /* number of 512B blocks allocated */ 209 time_t st_atime; /* time of last access */ 210 time_t st_mtime; /* time of last modification */ 211 time_t st_ctime; /* time of last status change */ 212}; 213 214/* WARNING, it only deals with the 4096 first majors and 256 first minors */ 215#define makedev(major, minor) ((dev_t)((((major) & 0xfff) << 8) | ((minor) & 0xff))) 216#define major(dev) ((unsigned int)(((dev) >> 8) & 0xfff)) 217#define minor(dev) ((unsigned int)(((dev) & 0xff)) 218 219#ifndef offsetof 220#define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD) 221#endif 222 223#ifndef container_of 224#define container_of(PTR, TYPE, FIELD) ({ \ 225 __typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR = (PTR); \ 226 (TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD)); \ 227}) 228#endif 229 230/* make sure to include all global symbols */ 231#include "nolibc.h" 232 233#endif /* _NOLIBC_TYPES_H */