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/mman.h>
12 #include <linux/reboot.h> /* for LINUX_REBOOT_* */
13 #include <linux/stat.h>
14 #include <linux/time.h>
15 #include <linux/wait.h>
16 #include <linux/resource.h>
19 /* Only the generic macros and types may be defined here. The arch-specific
20 * ones such as the O_RDONLY and related macros used by fcntl() and open()
21 * must not be defined here.
24 /* stat flags (WARNING, octal here). We need to check for an existing
25 * definition because linux/stat.h may omit to define those if it finds
26 * that any glibc header was already included.
29 #define S_IFDIR 0040000
30 #define S_IFCHR 0020000
31 #define S_IFBLK 0060000
32 #define S_IFREG 0100000
33 #define S_IFIFO 0010000
34 #define S_IFLNK 0120000
35 #define S_IFSOCK 0140000
36 #define S_IFMT 0170000
38 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
39 #define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
40 #define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
41 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
42 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
43 #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
44 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
63 #define DT_UNKNOWN 0x0
72 /* commonly an fd_set represents 256 FDs */
74 #define FD_SETSIZE 256
77 /* PATH_MAX and MAXPATHLEN are often used and found with plenty of different
85 #define MAXPATHLEN (PATH_MAX)
90 #define MAP_FAILED ((void *)-1)
93 /* whence values for lseek() */
98 /* flags for reboot */
99 #define RB_AUTOBOOT LINUX_REBOOT_CMD_RESTART
100 #define RB_HALT_SYSTEM LINUX_REBOOT_CMD_HALT
101 #define RB_ENABLE_CAD LINUX_REBOOT_CMD_CAD_ON
102 #define RB_DISABLE_CAD LINUX_REBOOT_CMD_CAD_OFF
103 #define RB_POWER_OFF LINUX_REBOOT_CMD_POWER_OFF
104 #define RB_SW_SUSPEND LINUX_REBOOT_CMD_SW_SUSPEND
105 #define RB_KEXEC LINUX_REBOOT_CMD_KEXEC
107 /* Macros used on waitpid()'s return status */
108 #define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
109 #define WIFEXITED(status) (((status) & 0x7f) == 0)
110 #define WTERMSIG(status) ((status) & 0x7f)
111 #define WIFSIGNALED(status) ((status) - 1 < 0xff)
113 /* standard exit() codes */
114 #define EXIT_SUCCESS 0
115 #define EXIT_FAILURE 1
117 #define FD_SETIDXMASK (8 * sizeof(unsigned long))
118 #define FD_SETBITMASK (8 * sizeof(unsigned long)-1)
122 unsigned long fds
[(FD_SETSIZE
+ FD_SETBITMASK
) / FD_SETIDXMASK
];
125 #define FD_CLR(fd, set) do { \
126 fd_set *__set = (set); \
129 __set->fds[__fd / FD_SETIDXMASK] &= \
130 ~(1U << (__fd & FX_SETBITMASK)); \
133 #define FD_SET(fd, set) do { \
134 fd_set *__set = (set); \
137 __set->fds[__fd / FD_SETIDXMASK] |= \
138 1 << (__fd & FD_SETBITMASK); \
141 #define FD_ISSET(fd, set) ({ \
142 fd_set *__set = (set); \
146 __r = !!(__set->fds[__fd / FD_SETIDXMASK] & \
147 1U << (__fd & FD_SET_BITMASK)); \
151 #define FD_ZERO(set) do { \
152 fd_set *__set = (set); \
154 int __size = (FD_SETSIZE+FD_SETBITMASK) / FD_SETIDXMASK;\
155 for (__idx = 0; __idx < __size; __idx++) \
156 __set->fds[__idx] = 0; \
160 #define POLLIN 0x0001
161 #define POLLPRI 0x0002
162 #define POLLOUT 0x0004
163 #define POLLERR 0x0008
164 #define POLLHUP 0x0010
165 #define POLLNVAL 0x0020
173 /* for getdents64() */
174 struct linux_dirent64
{
177 unsigned short d_reclen
;
178 unsigned char d_type
;
182 /* The format of the struct as returned by the libc to the application, which
183 * significantly differs from the format returned by the stat() syscall flavours.
186 dev_t st_dev
; /* ID of device containing file */
187 ino_t st_ino
; /* inode number */
188 mode_t st_mode
; /* protection */
189 nlink_t st_nlink
; /* number of hard links */
190 uid_t st_uid
; /* user ID of owner */
191 gid_t st_gid
; /* group ID of owner */
192 dev_t st_rdev
; /* device ID (if special file) */
193 off_t st_size
; /* total size, in bytes */
194 blksize_t st_blksize
; /* blocksize for file system I/O */
195 blkcnt_t st_blocks
; /* number of 512B blocks allocated */
196 union { time_t st_atime
; struct timespec st_atim
; }; /* time of last access */
197 union { time_t st_mtime
; struct timespec st_mtim
; }; /* time of last modification */
198 union { time_t st_ctime
; struct timespec st_ctim
; }; /* time of last status change */
201 /* WARNING, it only deals with the 4096 first majors and 256 first minors */
202 #define makedev(major, minor) ((dev_t)((((major) & 0xfff) << 8) | ((minor) & 0xff)))
203 #define major(dev) ((unsigned int)(((dev) >> 8) & 0xfff))
204 #define minor(dev) ((unsigned int)(((dev) & 0xff))
207 #define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD)
211 #define container_of(PTR, TYPE, FIELD) ({ \
212 __typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR = (PTR); \
213 (TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD)); \
217 /* make sure to include all global symbols */
220 #endif /* _NOLIBC_TYPES_H */