File: options.h

package info (click to toggle)
xchpst 0.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 304 kB
  • sloc: ansic: 2,792; sh: 75; makefile: 47
file content (280 lines) | stat: -rw-r--r-- 6,025 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* SPDX-License-Identifier: MIT */
/* SPDX-FileCopyrightText: (c) Copyright 2024,2025 Andrew Bower <andrew@bower.uk> */

#ifndef _OPTIONS_H
#define _OPTIONS_H

#include <assert.h>
#include <ctype.h>
#include <getopt.h>
#include <errno.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/capability.h>
#include <sys/resource.h>
#include <linux/ioprio.h>

#include "xchpst.h"
#include "usrgrp.h"

enum compat_level {
  COMPAT_CHPST             = 00001,
  COMPAT_XCHPST            = 00002,
  COMPAT_SOFTLIMIT         = 00004,
  COMPAT_ENVDIR            = 00010,
  COMPAT_PGRPHACK          = 00020,
  COMPAT_SETUIDGID         = 00040,
  COMPAT_ENVUIDGID         = 00100,
  COMPAT_SETLOCK           = 00200,
  COMPAT_APPLYUIDGID       = 00400,
  COMPAT_SETUIDGID_FROMENV = 01000,
};


enum verbosity {
  LOG_LEVEL_NONE = 0,
  LOG_LEVEL_VERBOSE = 1,
  LOG_LEVEL_DEBUG = 2,
};

static const/*expr*/ enum compat_level C_X = COMPAT_XCHPST;
static const/*expr*/ enum compat_level C_R = COMPAT_XCHPST | COMPAT_CHPST;
static const/*expr*/ enum compat_level C_0 = COMPAT_CHPST;

static const/*expr*/ enum compat_level C_S = COMPAT_SOFTLIMIT;
static const/*expr*/ enum compat_level C_RS = C_R | C_S;
static const/*expr*/ enum compat_level C_XS = C_X | C_S;

static const/*expr*/ enum compat_level C_L = COMPAT_SETLOCK;

static const/*expr*/ enum compat_level C_A = COMPAT_APPLYUIDGID;

static const/*expr*/ enum compat_level C_ALL = 01777;

enum opt /* C23: :int */ {
  OPT_BASE = 0x1000,
  OPT_SETUIDGID = OPT_BASE,
  OPT_ENVUIDGID,
  OPT_ARGV0,
  OPT_ENVDIR,
  OPT_CHROOT,
  OPT_CHDIR,
  OPT_NICE,
  OPT_LOCK_WAIT,
  OPT_LOCK,
  OPT_LOCKOPT_WAIT,
  OPT_LOCKOPT_TRY,
  OPT_LOCKOPT_NOISY,
  OPT_LOCKOPT_QUIET,
  OPT_LIMIT_MEM,
  OPT_RLIMIT_DATA,
  OPT_RLIMIT_STACK,
  OPT_RLIMIT_MEMLOCK,
  OPT_RLIMIT_AS,
  OPT_RLIMIT_NOFILE,
  OPT_RLIMIT_NPROC,
  OPT_RLIMIT_FSIZE,
  OPT_RLIMIT_CORE,
  OPT_RLIMIT_RSS,
  OPT_RLIMIT_CPU,
  OPT_RLIMIT_MSGQUEUE,
  OPT_RLIMIT_NICE,
  OPT_RLIMIT_RTPRIO,
  OPT_RLIMIT_RTTIME,
  OPT_RLIMIT_SIGPENDING,
  OPT_RLIMIT_LOCKS,
  OPT_CLOSE_STDIN,
  OPT_CLOSE_STDOUT = OPT_CLOSE_STDIN + STDOUT_FILENO - STDIN_FILENO,
  OPT_CLOSE_STDERR = OPT_CLOSE_STDIN + STDERR_FILENO - STDIN_FILENO,
  OPT_VERBOSE,
  OPT_VERSION,
  OPT_PGRPHACK,
  OPT_LEGACY,
  OPT_HELP,
  OPT_FILE,
  OPT_MOUNT_NS,
  OPT_NET_NS,
  OPT_USER_NS,
  OPT_PID_NS,
  OPT_UTS_NS,
  OPT_NET_ADOPT,
  OPT_PRIVATE_RUN,
  OPT_PRIVATE_TMP,
  OPT_PROTECT_HOME,
  OPT_RO_HOME,
  OPT_RO_SYS,
  OPT_RO_ETC,
#ifndef NO_CAP
  OPT_CAPBS_KEEP,
  OPT_CAPBS_DROP,
  OPT_CAPS_KEEP,
  OPT_CAPS_DROP,
#endif
  OPT_FORK_JOIN,
  OPT_NEW_ROOT,
  OPT_NO_NEW_PRIVS,
  OPT_CPUS,
  OPT_CPU_SCHED,
  OPT_IO_SCHED,
  OPT_UMASK,
  OPT_APP,
  OPT_RUN_DIR,
  OPT_STATE_DIR,
  OPT_CACHE_DIR,
  OPT_LOG_DIR,
  OPT_LOGIN,
  OPT_OOM,
  OPT_HARDLIMIT,
  OPT_UIDNUM,
  OPT_GIDNUM,
  OPT_GIDSNUM,
  OPT_UGFROMENV,
  OPT_UGCLEARENV,

  /* Keep at end */
  OPT_EXIT,
  _OPT_LAST = OPT_EXIT
};
static_assert(STDIN_FILENO == 0);

static const/*expr*/ int MAX_POSITIONAL_OPTS = 1;
struct app {
  enum compat_level compat_level;
  const char *name;
  bool long_opts;
  int takes_positional_opts;
  enum opt positional_opts[1 /* MAX_POSITIONAL_OPTS */];
};

#define NAME_STR STRINGIFY(PROG_NAME)

struct option_info {
  enum compat_level compat_level;
  enum opt option;
  char short_name;
  char *long_name;
  int has_arg;
  const char *help;
  const char *arg;
};

struct limit {
  struct rlimit limits;
  bool soft_specified:1;
  bool hard_specified:1;
};

typedef uint64_t cap_bits_t;

enum cap_op {
  CAP_OP_NONE = 0,
  CAP_OP_KEEP,
  CAP_OP_DROP,
};

struct options_file {
  struct options_file *next;
  char content[];
};

struct options {
  /* Linked list of options files */
  struct options_file *opt_files;

  /* Bitfield of specified options */
  uint32_t specified[(_OPT_LAST - OPT_BASE + 32) / 32];

  /* Which type of application we are launched as */
  const struct app *app;

  /* Meta stuff */
  bool error;
  bool version;
  bool help;
  bool exit;
  int retcode;
  int verbosity;

  /* The interesting (x)chpsty stuff here */
  char *argv0;
  char *app_name;
  int new_ns;
  int niceness;
  bool lock_wait;
  bool lock_nowait_override;
  bool lock_quiet;
  int sched_policy;
  int ionice_prio;
  const char *lock_file;
  const char *env_dir;
  const char *chroot;
  const char *chdir;
  const char *net_adopt;
  struct users_groups users_groups;
  struct users_groups env_users_groups;
  struct limit rlimit_data;
  struct limit rlimit_stack;
  struct limit rlimit_as;
  struct limit rlimit_memlock;
  struct limit rlimit_rss;
  struct limit rlimit_nofile;
  struct limit rlimit_nproc;
  struct limit rlimit_fsize;
  struct limit rlimit_core;
  struct limit rlimit_cpu;
  struct limit rlimit_msgqueue;
  struct limit rlimit_nice;
  struct limit rlimit_rtprio;
  struct limit rlimit_rttime;
  struct limit rlimit_sigpending;
  struct limit rlimit_locks;
  unsigned close_fds;
#ifndef NO_CAP
  enum cap_op cap_bounds_op;
  cap_bits_t cap_bounds;
  cap_bits_t caps_op;
  cap_bits_t caps;
#endif
  unsigned int umask;
  long oom_adjust;

  struct {
    cpu_set_t *mask;
    int size;
  } cpu_affinity;
};

extern struct options opt;

static inline bool is_verbose(void) {
  return opt.verbosity >= LOG_LEVEL_VERBOSE;
}

static inline bool is_debug(void) {
  return opt.verbosity >= LOG_LEVEL_DEBUG;
}

static inline bool set(enum opt option) {
  int index = option - OPT_BASE;
  return opt.specified[index / 32] & (1 << (index & 31));
}

static inline void enable(enum opt option) {
  int index = option - OPT_BASE;
  opt.specified[index / 32] |= (1 << (index & 31));
}

bool options_init(void);
void options_print(FILE *out);
void options_print_positional(FILE *out);
void options_explain_positional(FILE *out);
int options_parse(int argc, char *argv[]);
void options_free(void);
const struct option_info *find_option(int by_code,
                                      const char *by_name);

#endif