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_IFDIR) == S_IFDIR)
30 #define S_ISCHR(mode) (((mode) & S_IFCHR) == S_IFCHR)
31 #define S_ISBLK(mode) (((mode) & S_IFBLK) == S_IFBLK)
32 #define S_ISREG(mode) (((mode) & S_IFREG) == S_IFREG)
33 #define S_ISFIFO(mode) (((mode) & S_IFIFO) == S_IFIFO)
34 #define S_ISLNK(mode) (((mode) & S_IFLNK) == S_IFLNK)
35 #define S_ISSOCK(mode) (((mode) & S_IFSOCK) == 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
94 uint32_t fd32[(FD_SETSIZE + 31) / 32];
97 #define FD_CLR(fd, set) do { \
98 fd_set *__set = (set); \
101 __set->fd32[__fd / 32] &= ~(1U << (__fd & 31)); \
104 #define FD_SET(fd, set) do { \
105 fd_set *__set = (set); \
108 __set->fd32[__fd / 32] |= 1U << (__fd & 31); \
111 #define FD_ISSET(fd, set) ({ \
112 fd_set *__set = (set); \
116 __r = !!(__set->fd32[__fd / 32] & 1U << (__fd & 31)); \
120 #define FD_ZERO(set) do { \
121 fd_set *__set = (set); \
123 for (__idx = 0; __idx < (FD_SETSIZE+31) / 32; __idx ++) \
124 __set->fd32[__idx] = 0; \
128 #define POLLIN 0x0001
129 #define POLLPRI 0x0002
130 #define POLLOUT 0x0004
131 #define POLLERR 0x0008
132 #define POLLHUP 0x0010
133 #define POLLNVAL 0x0020
141 /* for getdents64() */
142 struct linux_dirent64 {
145 unsigned short d_reclen;
146 unsigned char d_type;
150 /* needed by wait4() */
152 struct timeval ru_utime;
153 struct timeval ru_stime;
170 /* The format of the struct as returned by the libc to the application, which
171 * significantly differs from the format returned by the stat() syscall flavours.
174 dev_t st_dev; /* ID of device containing file */
175 ino_t st_ino; /* inode number */
176 mode_t st_mode; /* protection */
177 nlink_t st_nlink; /* number of hard links */
178 uid_t st_uid; /* user ID of owner */
179 gid_t st_gid; /* group ID of owner */
180 dev_t st_rdev; /* device ID (if special file) */
181 off_t st_size; /* total size, in bytes */
182 blksize_t st_blksize; /* blocksize for file system I/O */
183 blkcnt_t st_blocks; /* number of 512B blocks allocated */
184 time_t st_atime; /* time of last access */
185 time_t st_mtime; /* time of last modification */
186 time_t st_ctime; /* time of last status change */
189 /* WARNING, it only deals with the 4096 first majors and 256 first minors */
190 #define makedev(major, minor) ((dev_t)((((major) & 0xfff) << 8) | ((minor) & 0xff)))
191 #define major(dev) ((unsigned int)(((dev) >> 8) & 0xfff))
192 #define minor(dev) ((unsigned int)(((dev) & 0xff))
195 #define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD)
199 #define container_of(PTR, TYPE, FIELD) ({ \
200 __typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR = (PTR); \
201 (TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD)); \
205 #endif /* _NOLIBC_TYPES_H */