Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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/* make sure to include all global symbols */
8#include "nolibc.h"
9
10#ifndef _NOLIBC_TYPES_H
11#define _NOLIBC_TYPES_H
12
13#include "std.h"
14#include <linux/mman.h>
15#include <linux/stat.h>
16#include <linux/time.h>
17#include <linux/wait.h>
18
19
20/* Only the generic macros and types may be defined here. The arch-specific
21 * ones such as the O_RDONLY and related macros used by fcntl() and open()
22 * must not be defined here.
23 */
24
25/* stat flags (WARNING, octal here). We need to check for an existing
26 * definition because linux/stat.h may omit to define those if it finds
27 * that any glibc header was already included.
28 */
29#if !defined(S_IFMT)
30#define S_IFDIR 0040000
31#define S_IFCHR 0020000
32#define S_IFBLK 0060000
33#define S_IFREG 0100000
34#define S_IFIFO 0010000
35#define S_IFLNK 0120000
36#define S_IFSOCK 0140000
37#define S_IFMT 0170000
38
39#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
40#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
41#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
42#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
43#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
44#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
45#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
46
47#define S_IRWXU 00700
48#define S_IRUSR 00400
49#define S_IWUSR 00200
50#define S_IXUSR 00100
51
52#define S_IRWXG 00070
53#define S_IRGRP 00040
54#define S_IWGRP 00020
55#define S_IXGRP 00010
56
57#define S_IRWXO 00007
58#define S_IROTH 00004
59#define S_IWOTH 00002
60#define S_IXOTH 00001
61#endif
62
63/* dirent types */
64#define DT_UNKNOWN 0x0
65#define DT_FIFO 0x1
66#define DT_CHR 0x2
67#define DT_DIR 0x4
68#define DT_BLK 0x6
69#define DT_REG 0x8
70#define DT_LNK 0xa
71#define DT_SOCK 0xc
72
73/* commonly an fd_set represents 256 FDs */
74#ifndef FD_SETSIZE
75#define FD_SETSIZE 256
76#endif
77
78/* PATH_MAX and MAXPATHLEN are often used and found with plenty of different
79 * values.
80 */
81#ifndef PATH_MAX
82#define PATH_MAX 4096
83#endif
84
85#ifndef MAXPATHLEN
86#define MAXPATHLEN (PATH_MAX)
87#endif
88
89/* flags for mmap */
90#ifndef MAP_FAILED
91#define MAP_FAILED ((void *)-1)
92#endif
93
94/* whence values for lseek() */
95#define SEEK_SET 0
96#define SEEK_CUR 1
97#define SEEK_END 2
98
99/* flags for reboot */
100#define RB_AUTOBOOT LINUX_REBOOT_CMD_RESTART
101#define RB_HALT_SYSTEM LINUX_REBOOT_CMD_HALT
102#define RB_ENABLE_CAD LINUX_REBOOT_CMD_CAD_ON
103#define RB_DISABLE_CAD LINUX_REBOOT_CMD_CAD_OFF
104#define RB_POWER_OFF LINUX_REBOOT_CMD_POWER_OFF
105#define RB_SW_SUSPEND LINUX_REBOOT_CMD_SW_SUSPEND
106#define RB_KEXEC LINUX_REBOOT_CMD_KEXEC
107
108/* Macros used on waitpid()'s return status */
109#define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
110#define WIFEXITED(status) (((status) & 0x7f) == 0)
111#define WTERMSIG(status) ((status) & 0x7f)
112#define WIFSIGNALED(status) ((status) - 1 < 0xff)
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() */
122typedef 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 & FD_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] & \
1481U << (__fd & FD_SETBITMASK)); \
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 getdents64() */
161struct linux_dirent64 {
162 uint64_t d_ino;
163 int64_t d_off;
164 unsigned short d_reclen;
165 unsigned char d_type;
166 char d_name[];
167};
168
169/* The format of the struct as returned by the libc to the application, which
170 * significantly differs from the format returned by the stat() syscall flavours.
171 */
172struct stat {
173 dev_t st_dev; /* ID of device containing file */
174 ino_t st_ino; /* inode number */
175 mode_t st_mode; /* protection */
176 nlink_t st_nlink; /* number of hard links */
177 uid_t st_uid; /* user ID of owner */
178 gid_t st_gid; /* group ID of owner */
179 dev_t st_rdev; /* device ID (if special file) */
180 off_t st_size; /* total size, in bytes */
181 blksize_t st_blksize; /* blocksize for file system I/O */
182 blkcnt_t st_blocks; /* number of 512B blocks allocated */
183 union { time_t st_atime; struct timespec st_atim; }; /* time of last access */
184 union { time_t st_mtime; struct timespec st_mtim; }; /* time of last modification */
185 union { time_t st_ctime; struct timespec st_ctim; }; /* time of last status change */
186};
187
188typedef __kernel_clockid_t clockid_t;
189typedef int timer_t;
190
191#ifndef container_of
192#define container_of(PTR, TYPE, FIELD) ({ \
193 __typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR = (PTR); \
194 (TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD)); \
195})
196#endif
197
198#endif /* _NOLIBC_TYPES_H */