runshare 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,479 @@
1
+ /*
2
+ * Fundamental C definitions.
3
+ *
4
+ * No copyright is claimed. This code is in the public domain; do with
5
+ * it what you wish.
6
+ */
7
+ #ifndef UTIL_LINUX_C_H
8
+ #define UTIL_LINUX_C_H
9
+
10
+ #include <limits.h>
11
+ #include <stddef.h>
12
+ #include <stdint.h>
13
+ #include <stdio.h>
14
+ #include <unistd.h>
15
+ #include <stdarg.h>
16
+ #include <stdlib.h>
17
+ #include <string.h>
18
+ #include <errno.h>
19
+ #include <sys/types.h>
20
+ #include <grp.h>
21
+
22
+ #include <assert.h>
23
+
24
+ #ifdef HAVE_ERR_H
25
+ # include <err.h>
26
+ #endif
27
+
28
+ #ifdef HAVE_SYS_SYSMACROS_H
29
+ # include <sys/sysmacros.h> /* for major, minor */
30
+ #endif
31
+
32
+ #ifndef LOGIN_NAME_MAX
33
+ # define LOGIN_NAME_MAX 256
34
+ #endif
35
+
36
+ #ifndef NAME_MAX
37
+ # define NAME_MAX PATH_MAX
38
+ #endif
39
+
40
+ /*
41
+ * __GNUC_PREREQ is deprecated in favour of __has_attribute() and
42
+ * __has_feature(). The __has macros are supported by clang and gcc>=5.
43
+ */
44
+ #ifndef __GNUC_PREREQ
45
+ # if defined __GNUC__ && defined __GNUC_MINOR__
46
+ # define __GNUC_PREREQ(maj, min) \
47
+ ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
48
+ # else
49
+ # define __GNUC_PREREQ(maj, min) 0
50
+ # endif
51
+ #endif
52
+
53
+ #ifdef __GNUC__
54
+
55
+ /* &a[0] degrades to a pointer: a different type from an array */
56
+ # define __must_be_array(a) \
57
+ UL_BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(__typeof__(a), __typeof__(&a[0])))
58
+
59
+ # define ignore_result(x) __extension__ ({ \
60
+ __typeof__(x) __dummy __attribute__((__unused__)) = (x); (void) __dummy; \
61
+ })
62
+
63
+ #else /* !__GNUC__ */
64
+ # define __must_be_array(a) 0
65
+ # define __attribute__(_arg_)
66
+ # define ignore_result(x) ((void) (x))
67
+ #endif /* !__GNUC__ */
68
+
69
+
70
+ /* "restrict" keyword fallback */
71
+ #if __STDC__ != 1
72
+ # define restrict __restrict /* use implementation __ format */
73
+ #else
74
+ # ifndef __STDC_VERSION__
75
+ # define restrict __restrict /* use implementation __ format */
76
+ # else
77
+ # if __STDC_VERSION__ < 199901L
78
+ # define restrict __restrict /* use implementation __ format */
79
+ # endif
80
+ # endif
81
+ #endif
82
+
83
+
84
+ /*
85
+ * It evaluates to 1 if the attribute/feature is supported by the current
86
+ * compilation target. Fallback for old compilers.
87
+ */
88
+ #ifndef __has_attribute
89
+ #define __has_attribute(x) 0
90
+ #endif
91
+
92
+ #ifndef __has_feature
93
+ #define __has_feature(x) 0
94
+ #endif
95
+
96
+ /*
97
+ * Function attributes
98
+ */
99
+ #ifndef __ul_alloc_size
100
+ # if (__has_attribute(alloc_size) && __has_attribute(warn_unused_result)) || __GNUC_PREREQ (4, 3)
101
+ # define __ul_alloc_size(s) __attribute__((alloc_size(s), warn_unused_result))
102
+ # else
103
+ # define __ul_alloc_size(s)
104
+ # endif
105
+ #endif
106
+
107
+ #ifndef __ul_calloc_size
108
+ # if (__has_attribute(alloc_size) && __has_attribute(warn_unused_result)) || __GNUC_PREREQ (4, 3)
109
+ # define __ul_calloc_size(n, s) __attribute__((alloc_size(n, s), warn_unused_result))
110
+ # else
111
+ # define __ul_calloc_size(n, s)
112
+ # endif
113
+ #endif
114
+
115
+ #if __has_attribute(returns_nonnull) || __GNUC_PREREQ (4, 9)
116
+ # define __ul_returns_nonnull __attribute__((returns_nonnull))
117
+ #else
118
+ # define __ul_returns_nonnull
119
+ #endif
120
+
121
+ /*
122
+ * Force a compilation error if condition is true, but also produce a
123
+ * result (of value 0 and type size_t), so the expression can be used
124
+ * e.g. in a structure initializer (or wherever else comma expressions
125
+ * aren't permitted).
126
+ */
127
+ #define UL_BUILD_BUG_ON_ZERO(e) __extension__ (sizeof(struct { int:-!!(e); }))
128
+ #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
129
+
130
+ #ifndef ARRAY_SIZE
131
+ # define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
132
+ #endif
133
+
134
+ #ifndef PATH_MAX
135
+ # define PATH_MAX 4096
136
+ #endif
137
+
138
+ #ifndef TRUE
139
+ # define TRUE 1
140
+ #endif
141
+
142
+ #ifndef FALSE
143
+ # define FALSE 0
144
+ #endif
145
+
146
+ #ifndef min
147
+ # define min(x, y) __extension__ ({ \
148
+ __typeof__(x) _min1 = (x); \
149
+ __typeof__(y) _min2 = (y); \
150
+ (void) (&_min1 == &_min2); \
151
+ _min1 < _min2 ? _min1 : _min2; })
152
+ #endif
153
+
154
+ #ifndef max
155
+ # define max(x, y) __extension__ ({ \
156
+ __typeof__(x) _max1 = (x); \
157
+ __typeof__(y) _max2 = (y); \
158
+ (void) (&_max1 == &_max2); \
159
+ _max1 > _max2 ? _max1 : _max2; })
160
+ #endif
161
+
162
+ #ifndef cmp_numbers
163
+ # define cmp_numbers(x, y) __extension__ ({ \
164
+ __typeof__(x) _a = (x); \
165
+ __typeof__(y) _b = (y); \
166
+ (void) (&_a == &_b); \
167
+ _a == _b ? 0 : _a > _b ? 1 : -1; })
168
+ #endif
169
+
170
+ #ifndef offsetof
171
+ #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
172
+ #endif
173
+
174
+ /*
175
+ * container_of - cast a member of a structure out to the containing structure
176
+ * @ptr: the pointer to the member.
177
+ * @type: the type of the container struct this is embedded in.
178
+ * @member: the name of the member within the struct.
179
+ */
180
+ #ifndef container_of
181
+ #define container_of(ptr, type, member) __extension__ ({ \
182
+ const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
183
+ (type *)( (char *)__mptr - offsetof(type,member) );})
184
+ #endif
185
+
186
+ #ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
187
+ # ifdef HAVE___PROGNAME
188
+ extern char *__progname;
189
+ # define program_invocation_short_name __progname
190
+ # else
191
+ # ifdef HAVE_GETEXECNAME
192
+ # define program_invocation_short_name \
193
+ prog_inv_sh_nm_from_file(getexecname(), 0)
194
+ # else
195
+ # define program_invocation_short_name \
196
+ prog_inv_sh_nm_from_file(__FILE__, 1)
197
+ # endif
198
+ static char prog_inv_sh_nm_buf[256];
199
+ static inline char *
200
+ prog_inv_sh_nm_from_file(char *f, char stripext)
201
+ {
202
+ char *t;
203
+
204
+ if ((t = strrchr(f, '/')) != NULL)
205
+ t++;
206
+ else
207
+ t = f;
208
+
209
+ strncpy(prog_inv_sh_nm_buf, t, sizeof(prog_inv_sh_nm_buf) - 1);
210
+ prog_inv_sh_nm_buf[sizeof(prog_inv_sh_nm_buf) - 1] = '\0';
211
+
212
+ if (stripext && (t = strrchr(prog_inv_sh_nm_buf, '.')) != NULL)
213
+ *t = '\0';
214
+
215
+ return prog_inv_sh_nm_buf;
216
+ }
217
+ # endif
218
+ #endif
219
+
220
+
221
+ #ifndef HAVE_ERR_H
222
+ static inline void __attribute__ ((__format__ (__printf__, 4, 5)))
223
+ errmsg(char doexit, int excode, char adderr, const char *fmt, ...)
224
+ {
225
+ fprintf(stderr, "%s: ", program_invocation_short_name);
226
+ if (fmt != NULL) {
227
+ va_list argp;
228
+ va_start(argp, fmt);
229
+ vfprintf(stderr, fmt, argp);
230
+ va_end(argp);
231
+ if (adderr)
232
+ fprintf(stderr, ": ");
233
+ }
234
+ if (adderr)
235
+ fprintf(stderr, "%m");
236
+ fprintf(stderr, "\n");
237
+ if (doexit)
238
+ exit(excode);
239
+ }
240
+
241
+ #ifndef HAVE_ERR
242
+ # define err(E, FMT...) errmsg(1, E, 1, FMT)
243
+ #endif
244
+
245
+ #ifndef HAVE_ERRX
246
+ # define errx(E, FMT...) errmsg(1, E, 0, FMT)
247
+ #endif
248
+
249
+ #ifndef HAVE_WARN
250
+ # define warn(FMT...) errmsg(0, 0, 1, FMT)
251
+ #endif
252
+
253
+ #ifndef HAVE_WARNX
254
+ # define warnx(FMT...) errmsg(0, 0, 0, FMT)
255
+ #endif
256
+ #endif /* !HAVE_ERR_H */
257
+
258
+
259
+ /* Don't use inline function to avoid '#include "nls.h"' in c.h
260
+ */
261
+ #define errtryhelp(eval) __extension__ ({ \
262
+ fprintf(stderr, _("Try '%s --help' for more information.\n"), \
263
+ program_invocation_short_name); \
264
+ exit(eval); \
265
+ })
266
+
267
+ /* After failed execvp() */
268
+ #define EX_EXEC_FAILED 126 /* Program located, but not usable. */
269
+ #define EX_EXEC_ENOENT 127 /* Could not find program to exec. */
270
+ #define errexec(name) err(errno == ENOENT ? EX_EXEC_ENOENT : EX_EXEC_FAILED, \
271
+ _("failed to execute %s"), name)
272
+
273
+
274
+ static inline __attribute__((const)) int is_power_of_2(unsigned long num)
275
+ {
276
+ return (num != 0 && ((num & (num - 1)) == 0));
277
+ }
278
+
279
+ #ifndef HAVE_LOFF_T
280
+ typedef int64_t loff_t;
281
+ #endif
282
+
283
+ #if !defined(HAVE_DIRFD) && (!defined(HAVE_DECL_DIRFD) || HAVE_DECL_DIRFD == 0) && defined(HAVE_DIR_DD_FD)
284
+ #include <sys/types.h>
285
+ #include <dirent.h>
286
+ static inline int dirfd(DIR *d)
287
+ {
288
+ return d->dd_fd;
289
+ }
290
+ #endif
291
+
292
+ /*
293
+ * Fallback defines for old versions of glibc
294
+ */
295
+ #include <fcntl.h>
296
+
297
+ #ifdef O_CLOEXEC
298
+ #define UL_CLOEXECSTR "e"
299
+ #else
300
+ #define UL_CLOEXECSTR ""
301
+ #endif
302
+
303
+ #ifndef O_CLOEXEC
304
+ #define O_CLOEXEC 0
305
+ #endif
306
+
307
+ #ifdef __FreeBSD_kernel__
308
+ #ifndef F_DUPFD_CLOEXEC
309
+ #define F_DUPFD_CLOEXEC 17 /* Like F_DUPFD, but FD_CLOEXEC is set */
310
+ #endif
311
+ #endif
312
+
313
+
314
+ #ifndef AI_ADDRCONFIG
315
+ #define AI_ADDRCONFIG 0x0020
316
+ #endif
317
+
318
+ #ifndef IUTF8
319
+ #define IUTF8 0040000
320
+ #endif
321
+
322
+ /*
323
+ * MAXHOSTNAMELEN replacement
324
+ */
325
+ static inline size_t get_hostname_max(void)
326
+ {
327
+ long len = sysconf(_SC_HOST_NAME_MAX);
328
+
329
+ if (0 < len)
330
+ return len;
331
+
332
+ #ifdef MAXHOSTNAMELEN
333
+ return MAXHOSTNAMELEN;
334
+ #elif HOST_NAME_MAX
335
+ return HOST_NAME_MAX;
336
+ #endif
337
+ return 64;
338
+ }
339
+
340
+
341
+ static inline int drop_permissions(void)
342
+ {
343
+ errno = 0;
344
+
345
+ /* drop GID */
346
+ if (setgid(getgid()) < 0)
347
+ goto fail;
348
+
349
+ /* drop UID */
350
+ if (setuid(getuid()) < 0)
351
+ goto fail;
352
+
353
+ return 0;
354
+ fail:
355
+ return errno ? -errno : -1;
356
+ }
357
+
358
+ /*
359
+ * The usleep function was marked obsolete in POSIX.1-2001 and was removed
360
+ * in POSIX.1-2008. It was replaced with nanosleep() that provides more
361
+ * advantages (like no interaction with signals and other timer functions).
362
+ */
363
+ #include <time.h>
364
+
365
+ static inline int xusleep(useconds_t usec)
366
+ {
367
+ #ifdef HAVE_NANOSLEEP
368
+ struct timespec waittime = {
369
+ .tv_sec = usec / 1000000L,
370
+ .tv_nsec = (usec % 1000000L) * 1000
371
+ };
372
+ return nanosleep(&waittime, NULL);
373
+ #elif defined(HAVE_USLEEP)
374
+ return usleep(usec);
375
+ #else
376
+ # error "System with usleep() or nanosleep() required!"
377
+ #endif
378
+ }
379
+
380
+ /*
381
+ * Constant strings for usage() functions. For more info see
382
+ * Documentation/{howto-usage-function.txt,boilerplate.c}
383
+ */
384
+ #define USAGE_HEADER _("\nUsage:\n")
385
+ #define USAGE_OPTIONS _("\nOptions:\n")
386
+ #define USAGE_FUNCTIONS _("\nFunctions:\n")
387
+ #define USAGE_COMMANDS _("\nCommands:\n")
388
+ #define USAGE_ARGUMENTS _("\nArguments:\n")
389
+ #define USAGE_COLUMNS _("\nAvailable output columns:\n")
390
+ #define USAGE_SEPARATOR "\n"
391
+
392
+ #define USAGE_OPTSTR_HELP _("display this help")
393
+ #define USAGE_OPTSTR_VERSION _("display version")
394
+
395
+ #define USAGE_HELP_OPTIONS(marg_dsc) \
396
+ "%-" #marg_dsc "s%s\n" \
397
+ "%-" #marg_dsc "s%s\n" \
398
+ , " -h, --help", USAGE_OPTSTR_HELP \
399
+ , " -V, --version", USAGE_OPTSTR_VERSION
400
+
401
+ #define USAGE_ARG_SEPARATOR "\n"
402
+ #define USAGE_ARG_SIZE(_name) \
403
+ _(" %s arguments may be followed by the suffixes for\n" \
404
+ " GiB, TiB, PiB, EiB, ZiB, and YiB (the \"iB\" is optional)\n"), _name
405
+
406
+ #define USAGE_MAN_TAIL(_man) _("\nFor more details see %s.\n"), _man
407
+
408
+ #define UTIL_LINUX_VERSION _("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING
409
+
410
+ #define print_version(eval) __extension__ ({ \
411
+ printf(UTIL_LINUX_VERSION); \
412
+ exit(eval); \
413
+ })
414
+
415
+ /*
416
+ * seek stuff
417
+ */
418
+ #ifndef SEEK_DATA
419
+ # define SEEK_DATA 3
420
+ #endif
421
+ #ifndef SEEK_HOLE
422
+ # define SEEK_HOLE 4
423
+ #endif
424
+
425
+
426
+ /*
427
+ * Macros to convert #define'itions to strings, for example
428
+ * #define XYXXY 42
429
+ * printf ("%s=%s\n", stringify(XYXXY), stringify_value(XYXXY));
430
+ */
431
+ #define stringify_value(s) stringify(s)
432
+ #define stringify(s) #s
433
+
434
+ /* Detect if we're compiled with Address Sanitizer
435
+ * - gcc (__SANITIZE_ADDRESS__)
436
+ * - clang (__has_feature(address_sanitizer))
437
+ */
438
+ #if !defined(HAS_FEATURE_ADDRESS_SANITIZER)
439
+ # ifdef __SANITIZE_ADDRESS__
440
+ # define HAS_FEATURE_ADDRESS_SANITIZER 1
441
+ # elif defined(__has_feature)
442
+ # if __has_feature(address_sanitizer)
443
+ # define HAS_FEATURE_ADDRESS_SANITIZER 1
444
+ # endif
445
+ # endif
446
+ # if !defined(HAS_FEATURE_ADDRESS_SANITIZER)
447
+ # define HAS_FEATURE_ADDRESS_SANITIZER 0
448
+ # endif
449
+ #endif
450
+
451
+ /*
452
+ * UL_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time
453
+ * instrumentation shipped with Clang and GCC) to not instrument the
454
+ * annotated function. Furthermore, it will prevent the compiler from
455
+ * inlining the function because inlining currently breaks the blacklisting
456
+ * mechanism of AddressSanitizer.
457
+ */
458
+ #if __has_feature(address_sanitizer) && __has_attribute(no_sanitize_memory) && __has_attribute(no_sanitize_address)
459
+ # define UL_ASAN_BLACKLIST __attribute__((noinline)) __attribute__((no_sanitize_memory)) __attribute__((no_sanitize_address))
460
+ #else
461
+ # define UL_ASAN_BLACKLIST /* nothing */
462
+ #endif
463
+
464
+ /*
465
+ * Note that sysconf(_SC_GETPW_R_SIZE_MAX) returns *initial* suggested size for
466
+ * pwd buffer and in some cases it is not large enough. See POSIX and
467
+ * getpwnam_r man page for more details.
468
+ */
469
+ #define UL_GETPW_BUFSIZ (16 * 1024)
470
+
471
+ /*
472
+ * Darwin or other BSDs may only have MAP_ANON. To get it on Darwin we must
473
+ * define _DARWIN_C_SOURCE before including sys/mman.h. We do this in config.h.
474
+ */
475
+ #if !defined MAP_ANONYMOUS && defined MAP_ANON
476
+ # define MAP_ANONYMOUS (MAP_ANON)
477
+ #endif
478
+
479
+ #endif /* UTIL_LINUX_C_H */
@@ -0,0 +1,34 @@
1
+ /*
2
+ * This program is free software; you can redistribute it and/or modify it
3
+ * under the terms of the GNU General Public License as published by the
4
+ * Free Software Foundation; either version 2, or (at your option) any
5
+ * later version.
6
+ *
7
+ * This program is distributed in the hope that it will be useful, but
8
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
+ * General Public License for more details.
11
+ *
12
+ * You should have received a copy of the GNU General Public License along
13
+ * with this program; if not, write to the Free Software Foundation, Inc.,
14
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15
+ */
16
+
17
+ #ifndef CAPUTILS_H
18
+ #define CAPUTILS_H
19
+
20
+ #include <linux/capability.h>
21
+
22
+ #ifndef PR_CAP_AMBIENT
23
+ # define PR_CAP_AMBIENT 47
24
+ # define PR_CAP_AMBIENT_IS_SET 1
25
+ # define PR_CAP_AMBIENT_RAISE 2
26
+ # define PR_CAP_AMBIENT_LOWER 3
27
+ #endif
28
+
29
+ extern int capset(cap_user_header_t header, cap_user_data_t data);
30
+ extern int capget(cap_user_header_t header, const cap_user_data_t data);
31
+
32
+ extern int cap_last_cap(void);
33
+
34
+ #endif /* CAPUTILS_H */
@@ -0,0 +1,56 @@
1
+
2
+ /*
3
+ * No copyright is claimed. This code is in the public domain; do with
4
+ * it what you wish.
5
+ *
6
+ * Compat code so unshare and setns can be used with older libcs
7
+ */
8
+ #ifndef UTIL_LINUX_NAMESPACE_H
9
+ # define UTIL_LINUX_NAMESPACE_H
10
+
11
+ # include <sched.h>
12
+
13
+ # ifndef CLONE_NEWNS
14
+ # define CLONE_NEWNS 0x00020000
15
+ # endif
16
+ # ifndef CLONE_NEWCGROUP
17
+ # define CLONE_NEWCGROUP 0x02000000
18
+ # endif
19
+ # ifndef CLONE_NEWUTS
20
+ # define CLONE_NEWUTS 0x04000000
21
+ # endif
22
+ # ifndef CLONE_NEWIPC
23
+ # define CLONE_NEWIPC 0x08000000
24
+ # endif
25
+ # ifndef CLONE_NEWNET
26
+ # define CLONE_NEWNET 0x40000000
27
+ # endif
28
+ # ifndef CLONE_NEWUSER
29
+ # define CLONE_NEWUSER 0x10000000
30
+ # endif
31
+ # ifndef CLONE_NEWPID
32
+ # define CLONE_NEWPID 0x20000000
33
+ # endif
34
+ # ifndef CLONE_NEWTIME
35
+ # define CLONE_NEWTIME 0x00000080
36
+ # endif
37
+
38
+ # if !defined(HAVE_UNSHARE) || !defined(HAVE_SETNS)
39
+ # include <sys/syscall.h>
40
+ # endif
41
+
42
+ # if !defined(HAVE_UNSHARE) && defined(SYS_unshare)
43
+ static inline int unshare(int flags)
44
+ {
45
+ return syscall(SYS_unshare, flags);
46
+ }
47
+ # endif
48
+
49
+ # if !defined(HAVE_SETNS) && defined(SYS_setns)
50
+ static inline int setns(int fd, int nstype)
51
+ {
52
+ return syscall(SYS_setns, fd, nstype);
53
+ }
54
+ # endif
55
+
56
+ #endif /* UTIL_LINUX_NAMESPACE_H */