xref: /linux/tools/include/nolibc/types.h (revision f2310b6271dc54278337987ec8140175e8df16f4)
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 /* 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 /* flags for mmap */
85 #ifndef MAP_FAILED
86 #define MAP_FAILED ((void *)-1)
87 #endif
88 
89 /* whence values for lseek() */
90 #define SEEK_SET       0
91 #define SEEK_CUR       1
92 #define SEEK_END       2
93 
94 /* flags for reboot */
95 #define RB_AUTOBOOT     LINUX_REBOOT_CMD_RESTART
96 #define RB_HALT_SYSTEM  LINUX_REBOOT_CMD_HALT
97 #define RB_ENABLE_CAD   LINUX_REBOOT_CMD_CAD_ON
98 #define RB_DISABLE_CAD  LINUX_REBOOT_CMD_CAD_OFF
99 #define RB_POWER_OFF    LINUX_REBOOT_CMD_POWER_OFF
100 #define RB_SW_SUSPEND   LINUX_REBOOT_CMD_SW_SUSPEND
101 #define RB_KEXEC        LINUX_REBOOT_CMD_KEXEC
102 
103 /* Macros used on waitpid()'s return status */
104 #define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
105 #define WIFEXITED(status)   (((status) & 0x7f) == 0)
106 #define WTERMSIG(status)    ((status) & 0x7f)
107 #define WIFSIGNALED(status) ((status) - 1 < 0xff)
108 
109 /* standard exit() codes */
110 #define EXIT_SUCCESS 0
111 #define EXIT_FAILURE 1
112 
113 /* for getdents64() */
114 struct linux_dirent64 {
115 	uint64_t       d_ino;
116 	int64_t        d_off;
117 	unsigned short d_reclen;
118 	unsigned char  d_type;
119 	char           d_name[];
120 };
121 
122 /* The format of the struct as returned by the libc to the application, which
123  * significantly differs from the format returned by the stat() syscall flavours.
124  */
125 struct stat {
126 	dev_t     st_dev;     /* ID of device containing file */
127 	ino_t     st_ino;     /* inode number */
128 	mode_t    st_mode;    /* protection */
129 	nlink_t   st_nlink;   /* number of hard links */
130 	uid_t     st_uid;     /* user ID of owner */
131 	gid_t     st_gid;     /* group ID of owner */
132 	dev_t     st_rdev;    /* device ID (if special file) */
133 	off_t     st_size;    /* total size, in bytes */
134 	blksize_t st_blksize; /* blocksize for file system I/O */
135 	blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
136 	union { time_t st_atime; struct timespec st_atim; }; /* time of last access */
137 	union { time_t st_mtime; struct timespec st_mtim; }; /* time of last modification */
138 	union { time_t st_ctime; struct timespec st_ctim; }; /* time of last status change */
139 };
140 
141 typedef __kernel_clockid_t clockid_t;
142 typedef int timer_t;
143 
144 #ifndef container_of
145 #define container_of(PTR, TYPE, FIELD) ({			\
146 	__typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR = (PTR);	\
147 	(TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD));	\
148 })
149 #endif
150 
151 #endif /* _NOLIBC_TYPES_H */
152