]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/exit-status.c
tree-wide: remove Emacs lines from all files
[thirdparty/systemd.git] / src / basic / exit-status.c
1 /***
2 This file is part of 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 #include <signal.h>
21 #include <stdlib.h>
22
23 #include "exit-status.h"
24 #include "macro.h"
25 #include "set.h"
26
27 const char* exit_status_to_string(ExitStatus status, ExitStatusLevel level) {
28
29 /* We cast to int here, so that -Wenum doesn't complain that
30 * EXIT_SUCCESS/EXIT_FAILURE aren't in the enum */
31
32 switch ((int) status) {
33
34 case EXIT_SUCCESS:
35 return "SUCCESS";
36
37 case EXIT_FAILURE:
38 return "FAILURE";
39 }
40
41
42 if (level == EXIT_STATUS_SYSTEMD || level == EXIT_STATUS_LSB) {
43 switch ((int) status) {
44
45 case EXIT_CHDIR:
46 return "CHDIR";
47
48 case EXIT_NICE:
49 return "NICE";
50
51 case EXIT_FDS:
52 return "FDS";
53
54 case EXIT_EXEC:
55 return "EXEC";
56
57 case EXIT_MEMORY:
58 return "MEMORY";
59
60 case EXIT_LIMITS:
61 return "LIMITS";
62
63 case EXIT_OOM_ADJUST:
64 return "OOM_ADJUST";
65
66 case EXIT_SIGNAL_MASK:
67 return "SIGNAL_MASK";
68
69 case EXIT_STDIN:
70 return "STDIN";
71
72 case EXIT_STDOUT:
73 return "STDOUT";
74
75 case EXIT_CHROOT:
76 return "CHROOT";
77
78 case EXIT_IOPRIO:
79 return "IOPRIO";
80
81 case EXIT_TIMERSLACK:
82 return "TIMERSLACK";
83
84 case EXIT_SECUREBITS:
85 return "SECUREBITS";
86
87 case EXIT_SETSCHEDULER:
88 return "SETSCHEDULER";
89
90 case EXIT_CPUAFFINITY:
91 return "CPUAFFINITY";
92
93 case EXIT_GROUP:
94 return "GROUP";
95
96 case EXIT_USER:
97 return "USER";
98
99 case EXIT_CAPABILITIES:
100 return "CAPABILITIES";
101
102 case EXIT_CGROUP:
103 return "CGROUP";
104
105 case EXIT_SETSID:
106 return "SETSID";
107
108 case EXIT_CONFIRM:
109 return "CONFIRM";
110
111 case EXIT_STDERR:
112 return "STDERR";
113
114 case EXIT_PAM:
115 return "PAM";
116
117 case EXIT_NETWORK:
118 return "NETWORK";
119
120 case EXIT_NAMESPACE:
121 return "NAMESPACE";
122
123 case EXIT_NO_NEW_PRIVILEGES:
124 return "NO_NEW_PRIVILEGES";
125
126 case EXIT_SECCOMP:
127 return "SECCOMP";
128
129 case EXIT_SELINUX_CONTEXT:
130 return "SELINUX_CONTEXT";
131
132 case EXIT_PERSONALITY:
133 return "PERSONALITY";
134
135 case EXIT_APPARMOR_PROFILE:
136 return "APPARMOR";
137
138 case EXIT_ADDRESS_FAMILIES:
139 return "ADDRESS_FAMILIES";
140
141 case EXIT_RUNTIME_DIRECTORY:
142 return "RUNTIME_DIRECTORY";
143
144 case EXIT_CHOWN:
145 return "CHOWN";
146
147 case EXIT_MAKE_STARTER:
148 return "MAKE_STARTER";
149
150 case EXIT_BUS_ENDPOINT:
151 return "BUS_ENDPOINT";
152
153 case EXIT_SMACK_PROCESS_LABEL:
154 return "SMACK_PROCESS_LABEL";
155 }
156 }
157
158 if (level == EXIT_STATUS_LSB) {
159 switch ((int) status) {
160
161 case EXIT_INVALIDARGUMENT:
162 return "INVALIDARGUMENT";
163
164 case EXIT_NOTIMPLEMENTED:
165 return "NOTIMPLEMENTED";
166
167 case EXIT_NOPERMISSION:
168 return "NOPERMISSION";
169
170 case EXIT_NOTINSTALLED:
171 return "NOTINSTALLED";
172
173 case EXIT_NOTCONFIGURED:
174 return "NOTCONFIGURED";
175
176 case EXIT_NOTRUNNING:
177 return "NOTRUNNING";
178 }
179 }
180
181 return NULL;
182 }
183
184
185 bool is_clean_exit(int code, int status, ExitStatusSet *success_status) {
186
187 if (code == CLD_EXITED)
188 return status == 0 ||
189 (success_status &&
190 set_contains(success_status->status, INT_TO_PTR(status)));
191
192 /* If a daemon does not implement handlers for some of the
193 * signals that's not considered an unclean shutdown */
194 if (code == CLD_KILLED)
195 return
196 status == SIGHUP ||
197 status == SIGINT ||
198 status == SIGTERM ||
199 status == SIGPIPE ||
200 (success_status &&
201 set_contains(success_status->signal, INT_TO_PTR(status)));
202
203 return false;
204 }
205
206 bool is_clean_exit_lsb(int code, int status, ExitStatusSet *success_status) {
207
208 if (is_clean_exit(code, status, success_status))
209 return true;
210
211 return
212 code == CLD_EXITED &&
213 (status == EXIT_NOTINSTALLED || status == EXIT_NOTCONFIGURED);
214 }
215
216 void exit_status_set_free(ExitStatusSet *x) {
217 assert(x);
218
219 set_free(x->status);
220 set_free(x->signal);
221 x->status = x->signal = NULL;
222 }
223
224 bool exit_status_set_is_empty(ExitStatusSet *x) {
225 if (!x)
226 return true;
227
228 return set_isempty(x->status) && set_isempty(x->signal);
229 }
230
231 bool exit_status_set_test(ExitStatusSet *x, int code, int status) {
232
233 if (exit_status_set_is_empty(x))
234 return false;
235
236 if (code == CLD_EXITED && set_contains(x->status, INT_TO_PTR(status)))
237 return true;
238
239 if (IN_SET(code, CLD_KILLED, CLD_DUMPED) && set_contains(x->signal, INT_TO_PTR(status)))
240 return true;
241
242 return false;
243 }