1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <stdbool.h>
5 
6 #include "bitmap.h"
7 #include "hashmap.h"
8 #include "macro.h"
9 
10 /* This defines pretty names for the LSB 'start' verb exit codes. Note that they shouldn't be confused with
11  * the LSB 'status' verb exit codes which are defined very differently. For details see:
12  *
13  * https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
14  */
15 
16 enum {
17         /* EXIT_SUCCESS defined by libc */
18         /* EXIT_FAILURE defined by libc */
19         EXIT_INVALIDARGUMENT = 2,
20         EXIT_NOTIMPLEMENTED = 3,
21         EXIT_NOPERMISSION = 4,
22         EXIT_NOTINSTALLED = 5,
23         EXIT_NOTCONFIGURED = 6,
24         EXIT_NOTRUNNING = 7,
25 
26         /* BSD's sysexits.h defines a couple EX_xyz exit codes in the range 64 … 78 */
27 
28         /* The LSB suggests that error codes >= 200 are "reserved". We use them here under the assumption
29          * that they hence are unused by init scripts. */
30         EXIT_CHDIR = 200,
31         EXIT_NICE,
32         EXIT_FDS,
33         EXIT_EXEC,
34         EXIT_MEMORY,
35         EXIT_LIMITS,
36         EXIT_OOM_ADJUST,
37         EXIT_SIGNAL_MASK,
38         EXIT_STDIN,
39         EXIT_STDOUT,
40         EXIT_CHROOT,   /* 210 */
41         EXIT_IOPRIO,
42         EXIT_TIMERSLACK,
43         EXIT_SECUREBITS,
44         EXIT_SETSCHEDULER,
45         EXIT_CPUAFFINITY,
46         EXIT_GROUP,
47         EXIT_USER,
48         EXIT_CAPABILITIES,
49         EXIT_CGROUP,
50         EXIT_SETSID,   /* 220 */
51         EXIT_CONFIRM,
52         EXIT_STDERR,
53         _EXIT_RESERVED, /* used to be tcpwrap, don't reuse! */
54         EXIT_PAM,
55         EXIT_NETWORK,
56         EXIT_NAMESPACE,
57         EXIT_NO_NEW_PRIVILEGES,
58         EXIT_SECCOMP,
59         EXIT_SELINUX_CONTEXT,
60         EXIT_PERSONALITY,  /* 230 */
61         EXIT_APPARMOR_PROFILE,
62         EXIT_ADDRESS_FAMILIES,
63         EXIT_RUNTIME_DIRECTORY,
64         _EXIT_RESERVED2, /* used to be used by kdbus, don't reuse */
65         EXIT_CHOWN,
66         EXIT_SMACK_PROCESS_LABEL,
67         EXIT_KEYRING,
68         EXIT_STATE_DIRECTORY,
69         EXIT_CACHE_DIRECTORY,
70         EXIT_LOGS_DIRECTORY, /* 240 */
71         EXIT_CONFIGURATION_DIRECTORY,
72         EXIT_NUMA_POLICY,
73         EXIT_CREDENTIALS,
74         EXIT_BPF,
75 
76         EXIT_EXCEPTION = 255,  /* Whenever we want to propagate an abnormal/signal exit, in line with bash */
77 };
78 
79 typedef enum ExitStatusClass {
80         EXIT_STATUS_LIBC    = 1 << 0,  /* libc EXIT_STATUS/EXIT_FAILURE */
81         EXIT_STATUS_SYSTEMD = 1 << 1,  /* systemd's own exit codes */
82         EXIT_STATUS_LSB     = 1 << 2,  /* LSB exit codes */
83         EXIT_STATUS_BSD     = 1 << 3,  /* BSD (EX_xyz) exit codes */
84         EXIT_STATUS_FULL    = EXIT_STATUS_LIBC | EXIT_STATUS_SYSTEMD | EXIT_STATUS_LSB | EXIT_STATUS_BSD,
85 } ExitStatusClass;
86 
87 typedef struct ExitStatusSet {
88         Bitmap status;
89         Bitmap signal;
90 } ExitStatusSet;
91 
92 const char* exit_status_to_string(int code, ExitStatusClass class) _const_;
93 const char* exit_status_class(int code) _const_;
94 int exit_status_from_string(const char *s) _pure_;
95 
96 typedef struct ExitStatusMapping {
97         const char *name;
98         ExitStatusClass class;
99 } ExitStatusMapping;
100 
101 extern const ExitStatusMapping exit_status_mappings[256];
102 
103 typedef enum ExitClean {
104         EXIT_CLEAN_DAEMON,
105         EXIT_CLEAN_COMMAND,
106 } ExitClean;
107 
108 bool is_clean_exit(int code, int status, ExitClean clean, const ExitStatusSet *success_status);
109 
110 void exit_status_set_free(ExitStatusSet *x);
111 bool exit_status_set_is_empty(const ExitStatusSet *x);
112 bool exit_status_set_test(const ExitStatusSet *x, int code, int status);
113