1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
3 * Special types used by various syscalls for NOLIBC
4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
7 #ifndef _NOLIBC_TYPES_H
8 #define _NOLIBC_TYPES_H
11 #include <linux/time.h>
14 /* Only the generic macros and types may be defined here. The arch-specific
15 * ones such as the O_RDONLY and related macros used by fcntl() and open(), or
16 * the layout of sys_stat_struct must not be defined here.
19 /* stat flags (WARNING, octal here) */
20 #define S_IFDIR 0040000
21 #define S_IFCHR 0020000
22 #define S_IFBLK 0060000
23 #define S_IFREG 0100000
24 #define S_IFIFO 0010000
25 #define S_IFLNK 0120000
26 #define S_IFSOCK 0140000
27 #define S_IFMT 0170000
29 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
30 #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
31 #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
32 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
33 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
34 #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
35 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
38 #define DT_UNKNOWN 0x0
47 /* commonly an fd_set represents 256 FDs */
49 #define FD_SETSIZE 256
52 /* PATH_MAX and MAXPATHLEN are often used and found with plenty of different
60 #define MAXPATHLEN (PATH_MAX)
63 /* Special FD used by all the *at functions */
65 #define AT_FDCWD (-100)
68 /* whence values for lseek() */
73 /* cmd for reboot() */
74 #define LINUX_REBOOT_MAGIC1 0xfee1dead
75 #define LINUX_REBOOT_MAGIC2 0x28121969
76 #define LINUX_REBOOT_CMD_HALT 0xcdef0123
77 #define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc
78 #define LINUX_REBOOT_CMD_RESTART 0x01234567
79 #define LINUX_REBOOT_CMD_SW_SUSPEND 0xd000fce2
81 /* Macros used on waitpid()'s return status */
82 #define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
83 #define WIFEXITED(status) (((status) & 0x7f) == 0)
88 /* standard exit() codes */
89 #define EXIT_SUCCESS 0
90 #define EXIT_FAILURE 1
92 #define FD_SETIDXMASK (8 * sizeof(unsigned long))
93 #define FD_SETBITMASK (8 * sizeof(unsigned long)-1)
97 unsigned long fds[(FD_SETSIZE + FD_SETBITMASK) / FD_SETIDXMASK];
100 #define FD_CLR(fd, set) do { \
101 fd_set *__set = (set); \
104 __set->fds[__fd / FD_SETIDXMASK] &= \
105 ~(1U << (__fd & FX_SETBITMASK)); \
108 #define FD_SET(fd, set) do { \
109 fd_set *__set = (set); \
112 __set->fds[__fd / FD_SETIDXMASK] |= \
113 1 << (__fd & FD_SETBITMASK); \
116 #define FD_ISSET(fd, set) ({ \
117 fd_set *__set = (set); \
121 __r = !!(__set->fds[__fd / FD_SETIDXMASK] & \
122 1U << (__fd & FD_SET_BITMASK)); \
126 #define FD_ZERO(set) do { \
127 fd_set *__set = (set); \
129 int __size = (FD_SETSIZE+FD_SETBITMASK) / FD_SETIDXMASK;\
130 for (__idx = 0; __idx < __size; __idx++) \
131 __set->fds[__idx] = 0; \
135 #define POLLIN 0x0001
136 #define POLLPRI 0x0002
137 #define POLLOUT 0x0004
138 #define POLLERR 0x0008
139 #define POLLHUP 0x0010
140 #define POLLNVAL 0x0020
148 /* for getdents64() */
149 struct linux_dirent64 {
152 unsigned short d_reclen;
153 unsigned char d_type;
157 /* needed by wait4() */
159 struct timeval ru_utime;
160 struct timeval ru_stime;
177 /* The format of the struct as returned by the libc to the application, which
178 * significantly differs from the format returned by the stat() syscall flavours.
181 dev_t st_dev; /* ID of device containing file */
182 ino_t st_ino; /* inode number */
183 mode_t st_mode; /* protection */
184 nlink_t st_nlink; /* number of hard links */
185 uid_t st_uid; /* user ID of owner */
186 gid_t st_gid; /* group ID of owner */
187 dev_t st_rdev; /* device ID (if special file) */
188 off_t st_size; /* total size, in bytes */
189 blksize_t st_blksize; /* blocksize for file system I/O */
190 blkcnt_t st_blocks; /* number of 512B blocks allocated */
191 time_t st_atime; /* time of last access */
192 time_t st_mtime; /* time of last modification */
193 time_t st_ctime; /* time of last status change */
196 /* WARNING, it only deals with the 4096 first majors and 256 first minors */
197 #define makedev(major, minor) ((dev_t)((((major) & 0xfff) << 8) | ((minor) & 0xff)))
198 #define major(dev) ((unsigned int)(((dev) >> 8) & 0xfff))
199 #define minor(dev) ((unsigned int)(((dev) & 0xff))
202 #define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD)
206 #define container_of(PTR, TYPE, FIELD) ({ \
207 __typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR = (PTR); \
208 (TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD)); \
212 /* make sure to include all global symbols */
215 #endif /* _NOLIBC_TYPES_H */