]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
1afbdcb0 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
1afbdcb0 LP |
6 | ***/ |
7 | ||
c43516e0 | 8 | #include <signal.h> |
cf0fbc49 | 9 | #include <stdlib.h> |
1afbdcb0 LP |
10 | |
11 | #include "exit-status.h" | |
96342de6 | 12 | #include "macro.h" |
cf0fbc49 | 13 | #include "set.h" |
1afbdcb0 | 14 | |
65e3fd83 | 15 | const char* exit_status_to_string(int status, ExitStatusLevel level) { |
1afbdcb0 LP |
16 | |
17 | /* We cast to int here, so that -Wenum doesn't complain that | |
18 | * EXIT_SUCCESS/EXIT_FAILURE aren't in the enum */ | |
19 | ||
65e3fd83 | 20 | switch (status) { |
1afbdcb0 LP |
21 | |
22 | case EXIT_SUCCESS: | |
23 | return "SUCCESS"; | |
24 | ||
25 | case EXIT_FAILURE: | |
26 | return "FAILURE"; | |
27 | } | |
28 | ||
5f94b4e6 | 29 | if (IN_SET(level, EXIT_STATUS_SYSTEMD, EXIT_STATUS_LSB)) { |
65e3fd83 | 30 | switch (status) { |
1afbdcb0 LP |
31 | |
32 | case EXIT_CHDIR: | |
33 | return "CHDIR"; | |
34 | ||
35 | case EXIT_NICE: | |
36 | return "NICE"; | |
37 | ||
38 | case EXIT_FDS: | |
39 | return "FDS"; | |
40 | ||
41 | case EXIT_EXEC: | |
42 | return "EXEC"; | |
43 | ||
44 | case EXIT_MEMORY: | |
45 | return "MEMORY"; | |
46 | ||
47 | case EXIT_LIMITS: | |
48 | return "LIMITS"; | |
49 | ||
50 | case EXIT_OOM_ADJUST: | |
51 | return "OOM_ADJUST"; | |
52 | ||
53 | case EXIT_SIGNAL_MASK: | |
54 | return "SIGNAL_MASK"; | |
55 | ||
56 | case EXIT_STDIN: | |
57 | return "STDIN"; | |
58 | ||
59 | case EXIT_STDOUT: | |
60 | return "STDOUT"; | |
61 | ||
62 | case EXIT_CHROOT: | |
63 | return "CHROOT"; | |
64 | ||
65 | case EXIT_IOPRIO: | |
66 | return "IOPRIO"; | |
67 | ||
68 | case EXIT_TIMERSLACK: | |
69 | return "TIMERSLACK"; | |
70 | ||
71 | case EXIT_SECUREBITS: | |
72 | return "SECUREBITS"; | |
73 | ||
74 | case EXIT_SETSCHEDULER: | |
75 | return "SETSCHEDULER"; | |
76 | ||
77 | case EXIT_CPUAFFINITY: | |
78 | return "CPUAFFINITY"; | |
79 | ||
80 | case EXIT_GROUP: | |
81 | return "GROUP"; | |
82 | ||
83 | case EXIT_USER: | |
84 | return "USER"; | |
85 | ||
86 | case EXIT_CAPABILITIES: | |
87 | return "CAPABILITIES"; | |
88 | ||
89 | case EXIT_CGROUP: | |
90 | return "CGROUP"; | |
91 | ||
92 | case EXIT_SETSID: | |
93 | return "SETSID"; | |
94 | ||
95 | case EXIT_CONFIRM: | |
96 | return "CONFIRM"; | |
97 | ||
98 | case EXIT_STDERR: | |
99 | return "STDERR"; | |
100 | ||
1afbdcb0 LP |
101 | case EXIT_PAM: |
102 | return "PAM"; | |
ff01d048 LP |
103 | |
104 | case EXIT_NETWORK: | |
105 | return "NETWORK"; | |
4c2630eb MS |
106 | |
107 | case EXIT_NAMESPACE: | |
108 | return "NAMESPACE"; | |
8351ceae LP |
109 | |
110 | case EXIT_NO_NEW_PRIVILEGES: | |
111 | return "NO_NEW_PRIVILEGES"; | |
112 | ||
113 | case EXIT_SECCOMP: | |
114 | return "SECCOMP"; | |
7b52a628 MS |
115 | |
116 | case EXIT_SELINUX_CONTEXT: | |
117 | return "SELINUX_CONTEXT"; | |
ac45f971 LP |
118 | |
119 | case EXIT_PERSONALITY: | |
120 | return "PERSONALITY"; | |
eef65bf3 MS |
121 | |
122 | case EXIT_APPARMOR_PROFILE: | |
123 | return "APPARMOR"; | |
4298d0b5 LP |
124 | |
125 | case EXIT_ADDRESS_FAMILIES: | |
126 | return "ADDRESS_FAMILIES"; | |
e66cf1a3 LP |
127 | |
128 | case EXIT_RUNTIME_DIRECTORY: | |
129 | return "RUNTIME_DIRECTORY"; | |
3900e5fd | 130 | |
56ecbcc0 LP |
131 | case EXIT_CHOWN: |
132 | return "CHOWN"; | |
133 | ||
94b573bd SY |
134 | case EXIT_SMACK_PROCESS_LABEL: |
135 | return "SMACK_PROCESS_LABEL"; | |
74dd6b51 LP |
136 | |
137 | case EXIT_KEYRING: | |
138 | return "KEYRING"; | |
3536f49e YW |
139 | |
140 | case EXIT_STATE_DIRECTORY: | |
141 | return "STATE_DIRECTORY"; | |
142 | ||
143 | case EXIT_CACHE_DIRECTORY: | |
144 | return "CACHE_DIRECTORY"; | |
145 | ||
146 | case EXIT_LOGS_DIRECTORY: | |
147 | return "LOGS_DIRECTORY"; | |
148 | ||
149 | case EXIT_CONFIGURATION_DIRECTORY: | |
150 | return "CONFIGURATION_DIRECTORY"; | |
1afbdcb0 LP |
151 | } |
152 | } | |
153 | ||
154 | if (level == EXIT_STATUS_LSB) { | |
65e3fd83 | 155 | switch (status) { |
1afbdcb0 LP |
156 | |
157 | case EXIT_INVALIDARGUMENT: | |
158 | return "INVALIDARGUMENT"; | |
159 | ||
160 | case EXIT_NOTIMPLEMENTED: | |
161 | return "NOTIMPLEMENTED"; | |
162 | ||
163 | case EXIT_NOPERMISSION: | |
164 | return "NOPERMISSION"; | |
165 | ||
166 | case EXIT_NOTINSTALLED: | |
9f8f87e3 | 167 | return "NOTINSTALLED"; |
1afbdcb0 LP |
168 | |
169 | case EXIT_NOTCONFIGURED: | |
170 | return "NOTCONFIGURED"; | |
171 | ||
172 | case EXIT_NOTRUNNING: | |
173 | return "NOTRUNNING"; | |
174 | } | |
175 | } | |
176 | ||
177 | return NULL; | |
178 | } | |
9a57c629 | 179 | |
1f0958f6 | 180 | bool is_clean_exit(int code, int status, ExitClean clean, ExitStatusSet *success_status) { |
9a57c629 LP |
181 | |
182 | if (code == CLD_EXITED) | |
96342de6 LN |
183 | return status == 0 || |
184 | (success_status && | |
6a2b82a7 | 185 | set_contains(success_status->status, INT_TO_PTR(status))); |
9a57c629 | 186 | |
1f0958f6 | 187 | /* If a daemon does not implement handlers for some of the signals that's not considered an unclean shutdown */ |
9a57c629 | 188 | if (code == CLD_KILLED) |
1f0958f6 LP |
189 | return |
190 | (clean == EXIT_CLEAN_DAEMON && IN_SET(status, SIGHUP, SIGINT, SIGTERM, SIGPIPE)) || | |
96342de6 | 191 | (success_status && |
5f94b4e6 | 192 | set_contains(success_status->signal, INT_TO_PTR(status))); |
9a57c629 LP |
193 | |
194 | return false; | |
195 | } | |
196 | ||
37520c1b LP |
197 | void exit_status_set_free(ExitStatusSet *x) { |
198 | assert(x); | |
199 | ||
5f94b4e6 LP |
200 | x->status = set_free(x->status); |
201 | x->signal = set_free(x->signal); | |
37520c1b | 202 | } |
55ebf98c LP |
203 | |
204 | bool exit_status_set_is_empty(ExitStatusSet *x) { | |
205 | if (!x) | |
206 | return true; | |
207 | ||
208 | return set_isempty(x->status) && set_isempty(x->signal); | |
209 | } | |
597466f4 LP |
210 | |
211 | bool exit_status_set_test(ExitStatusSet *x, int code, int status) { | |
212 | ||
213 | if (exit_status_set_is_empty(x)) | |
214 | return false; | |
215 | ||
216 | if (code == CLD_EXITED && set_contains(x->status, INT_TO_PTR(status))) | |
217 | return true; | |
218 | ||
219 | if (IN_SET(code, CLD_KILLED, CLD_DUMPED) && set_contains(x->signal, INT_TO_PTR(status))) | |
220 | return true; | |
221 | ||
222 | return false; | |
223 | } |