xref: /third_party/eudev/src/shared/missing.h (revision 99ca880a)
1/***
2  This file is part of eudev, forked from systemd.
3
4  Copyright 2010 Lennart Poettering
5
6  systemd is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation; either version 2.1 of the License, or
9  (at your option) any later version.
10
11  systemd is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15
16  You should have received a copy of the GNU Lesser General Public License
17  along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#pragma once
21
22/* Missing glibc definitions to access certain kernel APIs */
23
24#include <sys/resource.h>
25#include <sys/syscall.h>
26#include <fcntl.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <linux/oom.h>
30
31#include "macro.h"
32#include "config.h"
33#include "hm-missing.h"
34
35#ifdef ARCH_MIPS
36#include <asm/sgidefs.h>
37#endif
38
39#ifndef RLIMIT_RTTIME
40#define RLIMIT_RTTIME 15
41#endif
42
43/* If RLIMIT_RTTIME is not defined, then we cannot use RLIMIT_NLIMITS as is */
44#define _RLIMIT_MAX (RLIMIT_RTTIME+1 > RLIMIT_NLIMITS ? RLIMIT_RTTIME+1 : RLIMIT_NLIMITS)
45
46#ifndef __NR_getrandom
47#  if defined __x86_64__
48#    define __NR_getrandom 318
49#  elif defined(__i386__)
50#    define __NR_getrandom 355
51#  elif defined(__arm__)
52#    define __NR_getrandom 384
53#  elif defined(__aarch64__)
54#    define __NR_getrandom 278
55#  elif defined(__ia64__)
56#    define __NR_getrandom 1339
57#  elif defined(__m68k__)
58#    define __NR_getrandom 352
59#  elif defined(__s390x__)
60#    define __NR_getrandom 349
61#  else
62#    warning "__NR_getrandom unknown for your architecture"
63#    define __NR_getrandom 0xffffffff
64#  endif
65#endif
66
67#if !HAVE_DECL_GETRANDOM
68static inline int getrandom(void *buffer, size_t count, unsigned flags) {
69        return syscall(__NR_getrandom, buffer, count, flags);
70}
71#endif
72
73#ifndef GRND_NONBLOCK
74#define GRND_NONBLOCK 0x0001
75#endif
76
77#ifndef GRND_RANDOM
78#define GRND_RANDOM 0x0002
79#endif
80
81#ifndef BTRFS_IOCTL_MAGIC
82#define BTRFS_IOCTL_MAGIC 0x94
83#endif
84
85#ifndef BTRFS_PATH_NAME_MAX
86#define BTRFS_PATH_NAME_MAX 4087
87#endif
88
89#ifndef HAVE_LINUX_BTRFS_H
90struct btrfs_ioctl_vol_args {
91        int64_t fd;
92        char name[BTRFS_PATH_NAME_MAX + 1];
93};
94#endif
95
96#ifndef BTRFS_IOC_DEVICES_READY
97#define BTRFS_IOC_DEVICES_READY _IOR(BTRFS_IOCTL_MAGIC, 39, \
98                                 struct btrfs_ioctl_vol_args)
99#endif
100
101#ifndef MS_MOVE
102#define MS_MOVE 8192
103#endif
104
105#ifndef MS_PRIVATE
106#define MS_PRIVATE  (1 << 18)
107#endif
108
109#if !HAVE_DECL_GETTID
110static inline pid_t gettid(void) {
111        return (pid_t) syscall(SYS_gettid);
112}
113#endif
114
115#ifndef MS_REC
116#define MS_REC 16384
117#endif
118
119#ifndef MAX_HANDLE_SZ
120#define MAX_HANDLE_SZ 128
121#endif
122
123#ifdef __MUSL__
124#define HAVE_DECL_NAME_TO_HANDLE_AT 1
125#endif
126
127#ifndef __NR_name_to_handle_at
128#  if defined(__x86_64__)
129#    define __NR_name_to_handle_at 303
130#  elif defined(__i386__)
131#    define __NR_name_to_handle_at 341
132#  elif defined(__arm__)
133#    define __NR_name_to_handle_at 370
134#  elif defined(__powerpc__)
135#    define __NR_name_to_handle_at 345
136#  else
137#    error "__NR_name_to_handle_at is not defined"
138#  endif
139#endif
140
141#if !HAVE_DECL_NAME_TO_HANDLE_AT
142struct file_handle {
143        unsigned int handle_bytes;
144        int handle_type;
145        unsigned char f_handle[0];
146};
147
148static inline int name_to_handle_at(int fd, const char *name, struct file_handle *handle, int *mnt_id, int flags) {
149        return syscall(__NR_name_to_handle_at, fd, name, handle, mnt_id, flags);
150}
151#endif
152
153#ifndef INPUT_PROP_POINTING_STICK
154#define INPUT_PROP_POINTING_STICK 0x05
155#endif
156
157#ifndef INPUT_PROP_ACCELEROMETER
158#define INPUT_PROP_ACCELEROMETER  0x06
159#endif
160
161#ifndef O_PATH
162#define O_PATH    010000000
163#endif
164
165#ifndef AT_EMPTY_PATH
166#define AT_EMPTY_PATH 0x1000
167#endif
168
169#if !HAVE_DECL_STRNDUPA
170#define strndupa(s, n) \
171  ({ \
172    const char *__old = (s); \
173    size_t __len = strnlen(__old, (n)); \
174    char *__new = (char *)alloca(__len + 1); \
175    __new[__len] = '\0'; \
176    (char *)memcpy(__new, __old, __len); \
177  })
178#endif
179
180#ifndef BTN_TRIGGER_HAPPY
181#define BTN_TRIGGER_HAPPY 0x2c0
182#endif
183
184#ifndef INPUT_PROP_MAX
185#define INPUT_PROP_MAX 0x1f
186#endif
187
188#ifndef KEY_ALS_TOGGLE
189#define KEY_ALS_TOGGLE 0x7a
190#endif
191
192#ifndef BTN_DPAD_UP
193#define BTN_DPAD_UP 0x220
194#define BTN_DPAD_RIGHT 0x223
195#endif
196