221 lines · c
1/* SPDX-License-Identifier: LGPL-2.1 OR MIT */2/*3 * Special types used by various syscalls for NOLIBC4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>5 */6 7#ifndef _NOLIBC_TYPES_H8#define _NOLIBC_TYPES_H9 10#include "std.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>17 18 19/* Only the generic macros and types may be defined here. The arch-specific20 * ones such as the O_RDONLY and related macros used by fcntl() and open()21 * must not be defined here.22 */23 24/* stat flags (WARNING, octal here). We need to check for an existing25 * definition because linux/stat.h may omit to define those if it finds26 * that any glibc header was already included.27 */28#if !defined(S_IFMT)29#define S_IFDIR 004000030#define S_IFCHR 002000031#define S_IFBLK 006000032#define S_IFREG 010000033#define S_IFIFO 001000034#define S_IFLNK 012000035#define S_IFSOCK 014000036#define S_IFMT 017000037 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)45 46#define S_IRWXU 0070047#define S_IRUSR 0040048#define S_IWUSR 0020049#define S_IXUSR 0010050 51#define S_IRWXG 0007052#define S_IRGRP 0004053#define S_IWGRP 0002054#define S_IXGRP 0001055 56#define S_IRWXO 0000757#define S_IROTH 0000458#define S_IWOTH 0000259#define S_IXOTH 0000160#endif61 62/* dirent types */63#define DT_UNKNOWN 0x064#define DT_FIFO 0x165#define DT_CHR 0x266#define DT_DIR 0x467#define DT_BLK 0x668#define DT_REG 0x869#define DT_LNK 0xa70#define DT_SOCK 0xc71 72/* commonly an fd_set represents 256 FDs */73#ifndef FD_SETSIZE74#define FD_SETSIZE 25675#endif76 77/* PATH_MAX and MAXPATHLEN are often used and found with plenty of different78 * values.79 */80#ifndef PATH_MAX81#define PATH_MAX 409682#endif83 84#ifndef MAXPATHLEN85#define MAXPATHLEN (PATH_MAX)86#endif87 88/* flags for mmap */89#ifndef MAP_FAILED90#define MAP_FAILED ((void *)-1)91#endif92 93/* whence values for lseek() */94#define SEEK_SET 095#define SEEK_CUR 196#define SEEK_END 297 98/* flags for reboot */99#define RB_AUTOBOOT LINUX_REBOOT_CMD_RESTART100#define RB_HALT_SYSTEM LINUX_REBOOT_CMD_HALT101#define RB_ENABLE_CAD LINUX_REBOOT_CMD_CAD_ON102#define RB_DISABLE_CAD LINUX_REBOOT_CMD_CAD_OFF103#define RB_POWER_OFF LINUX_REBOOT_CMD_POWER_OFF104#define RB_SW_SUSPEND LINUX_REBOOT_CMD_SW_SUSPEND105#define RB_KEXEC LINUX_REBOOT_CMD_KEXEC106 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)112 113/* standard exit() codes */114#define EXIT_SUCCESS 0115#define EXIT_FAILURE 1116 117#define FD_SETIDXMASK (8 * sizeof(unsigned long))118#define FD_SETBITMASK (8 * sizeof(unsigned long)-1)119 120/* for select() */121typedef struct {122 unsigned long fds[(FD_SETSIZE + FD_SETBITMASK) / FD_SETIDXMASK];123} fd_set;124 125#define FD_CLR(fd, set) do { \126 fd_set *__set = (set); \127 int __fd = (fd); \128 if (__fd >= 0) \129 __set->fds[__fd / FD_SETIDXMASK] &= \130 ~(1U << (__fd & FX_SETBITMASK)); \131 } while (0)132 133#define FD_SET(fd, set) do { \134 fd_set *__set = (set); \135 int __fd = (fd); \136 if (__fd >= 0) \137 __set->fds[__fd / FD_SETIDXMASK] |= \138 1 << (__fd & FD_SETBITMASK); \139 } while (0)140 141#define FD_ISSET(fd, set) ({ \142 fd_set *__set = (set); \143 int __fd = (fd); \144 int __r = 0; \145 if (__fd >= 0) \146 __r = !!(__set->fds[__fd / FD_SETIDXMASK] & \1471U << (__fd & FD_SET_BITMASK)); \148 __r; \149 })150 151#define FD_ZERO(set) do { \152 fd_set *__set = (set); \153 int __idx; \154 int __size = (FD_SETSIZE+FD_SETBITMASK) / FD_SETIDXMASK;\155 for (__idx = 0; __idx < __size; __idx++) \156 __set->fds[__idx] = 0; \157 } while (0)158 159/* for poll() */160#define POLLIN 0x0001161#define POLLPRI 0x0002162#define POLLOUT 0x0004163#define POLLERR 0x0008164#define POLLHUP 0x0010165#define POLLNVAL 0x0020166 167struct pollfd {168 int fd;169 short int events;170 short int revents;171};172 173/* for getdents64() */174struct linux_dirent64 {175 uint64_t d_ino;176 int64_t d_off;177 unsigned short d_reclen;178 unsigned char d_type;179 char d_name[];180};181 182/* The format of the struct as returned by the libc to the application, which183 * significantly differs from the format returned by the stat() syscall flavours.184 */185struct stat {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 */199};200 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))205 206#ifndef offsetof207#define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD)208#endif209 210#ifndef container_of211#define container_of(PTR, TYPE, FIELD) ({ \212 __typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR = (PTR); \213 (TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD)); \214})215#endif216 217/* make sure to include all global symbols */218#include "nolibc.h"219 220#endif /* _NOLIBC_TYPES_H */221