xref: /linux/tools/include/nolibc/types.h (revision c17ee635fd3a482b2ad2bf5e269755c2eae5f25e)
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_types.h>
17 #include <linux/wait.h>
18 
19 struct timespec {
20 	time_t	tv_sec;
21 	int64_t	tv_nsec;
22 };
23 #define _STRUCT_TIMESPEC
24 
25 /* Never use with system calls */
26 struct timeval {
27 	time_t	tv_sec;
28 	int64_t	tv_usec;
29 };
30 
31 #define timeval __nolibc_kernel_timeval
32 #include <linux/time.h>
33 #undef timeval
34 
35 /* Only the generic macros and types may be defined here. The arch-specific
36  * ones such as the O_RDONLY and related macros used by fcntl() and open()
37  * must not be defined here.
38  */
39 
40 /* stat flags (WARNING, octal here). We need to check for an existing
41  * definition because linux/stat.h may omit to define those if it finds
42  * that any glibc header was already included.
43  */
44 #if !defined(S_IFMT)
45 #define S_IFDIR        0040000
46 #define S_IFCHR        0020000
47 #define S_IFBLK        0060000
48 #define S_IFREG        0100000
49 #define S_IFIFO        0010000
50 #define S_IFLNK        0120000
51 #define S_IFSOCK       0140000
52 #define S_IFMT         0170000
53 
54 #define S_ISDIR(mode)  (((mode) & S_IFMT) == S_IFDIR)
55 #define S_ISCHR(mode)  (((mode) & S_IFMT) == S_IFCHR)
56 #define S_ISBLK(mode)  (((mode) & S_IFMT) == S_IFBLK)
57 #define S_ISREG(mode)  (((mode) & S_IFMT) == S_IFREG)
58 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
59 #define S_ISLNK(mode)  (((mode) & S_IFMT) == S_IFLNK)
60 #define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
61 
62 #define S_IRWXU 00700
63 #define S_IRUSR 00400
64 #define S_IWUSR 00200
65 #define S_IXUSR 00100
66 
67 #define S_IRWXG 00070
68 #define S_IRGRP 00040
69 #define S_IWGRP 00020
70 #define S_IXGRP 00010
71 
72 #define S_IRWXO 00007
73 #define S_IROTH 00004
74 #define S_IWOTH 00002
75 #define S_IXOTH 00001
76 #endif
77 
78 /* dirent types */
79 #define DT_UNKNOWN     0x0
80 #define DT_FIFO        0x1
81 #define DT_CHR         0x2
82 #define DT_DIR         0x4
83 #define DT_BLK         0x6
84 #define DT_REG         0x8
85 #define DT_LNK         0xa
86 #define DT_SOCK        0xc
87 
88 /* PATH_MAX and MAXPATHLEN are often used and found with plenty of different
89  * values.
90  */
91 #ifndef PATH_MAX
92 #define PATH_MAX       4096
93 #endif
94 
95 #ifndef MAXPATHLEN
96 #define MAXPATHLEN     (PATH_MAX)
97 #endif
98 
99 /* flags for mmap */
100 #ifndef MAP_FAILED
101 #define MAP_FAILED ((void *)-1)
102 #endif
103 
104 /* whence values for lseek() */
105 #define SEEK_SET       0
106 #define SEEK_CUR       1
107 #define SEEK_END       2
108 
109 /* flags for reboot */
110 #define RB_AUTOBOOT     LINUX_REBOOT_CMD_RESTART
111 #define RB_HALT_SYSTEM  LINUX_REBOOT_CMD_HALT
112 #define RB_ENABLE_CAD   LINUX_REBOOT_CMD_CAD_ON
113 #define RB_DISABLE_CAD  LINUX_REBOOT_CMD_CAD_OFF
114 #define RB_POWER_OFF    LINUX_REBOOT_CMD_POWER_OFF
115 #define RB_SW_SUSPEND   LINUX_REBOOT_CMD_SW_SUSPEND
116 #define RB_KEXEC        LINUX_REBOOT_CMD_KEXEC
117 
118 /* Macros used on waitpid()'s return status */
119 #define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
120 #define WIFEXITED(status)   (((status) & 0x7f) == 0)
121 #define WTERMSIG(status)    ((status) & 0x7f)
122 #define WIFSIGNALED(status) ((status) - 1 < 0xff)
123 
124 /* standard exit() codes */
125 #define EXIT_SUCCESS 0
126 #define EXIT_FAILURE 1
127 
128 /* for getdents64() */
129 struct linux_dirent64 {
130 	uint64_t       d_ino;
131 	int64_t        d_off;
132 	unsigned short d_reclen;
133 	unsigned char  d_type;
134 	char           d_name[];
135 };
136 
137 /* The format of the struct as returned by the libc to the application, which
138  * significantly differs from the format returned by the stat() syscall flavours.
139  */
140 struct stat {
141 	dev_t     st_dev;     /* ID of device containing file */
142 	ino_t     st_ino;     /* inode number */
143 	mode_t    st_mode;    /* protection */
144 	nlink_t   st_nlink;   /* number of hard links */
145 	uid_t     st_uid;     /* user ID of owner */
146 	gid_t     st_gid;     /* group ID of owner */
147 	dev_t     st_rdev;    /* device ID (if special file) */
148 	off_t     st_size;    /* total size, in bytes */
149 	blksize_t st_blksize; /* blocksize for file system I/O */
150 	blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
151 	union { time_t st_atime; struct timespec st_atim; }; /* time of last access */
152 	union { time_t st_mtime; struct timespec st_mtim; }; /* time of last modification */
153 	union { time_t st_ctime; struct timespec st_ctim; }; /* time of last status change */
154 };
155 
156 typedef __kernel_clockid_t clockid_t;
157 typedef int timer_t;
158 
159 #ifndef container_of
160 #define container_of(PTR, TYPE, FIELD) ({			\
161 	__typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR = (PTR);	\
162 	(TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD));	\
163 })
164 #endif
165 
166 #endif /* _NOLIBC_TYPES_H */
167