]>
Commit | Line | Data |
---|---|---|
1afbdcb0 LP |
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 | |
5430f7f2 LP |
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 | |
1afbdcb0 LP |
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 | |
5430f7f2 | 14 | Lesser General Public License for more details. |
1afbdcb0 | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
1afbdcb0 LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
c43516e0 | 20 | #include <signal.h> |
cf0fbc49 | 21 | #include <stdlib.h> |
1afbdcb0 LP |
22 | |
23 | #include "exit-status.h" | |
96342de6 | 24 | #include "macro.h" |
cf0fbc49 | 25 | #include "set.h" |
1afbdcb0 | 26 | |
65e3fd83 | 27 | const char* exit_status_to_string(int status, ExitStatusLevel level) { |
1afbdcb0 LP |
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 | ||
65e3fd83 | 32 | switch (status) { |
1afbdcb0 LP |
33 | |
34 | case EXIT_SUCCESS: | |
35 | return "SUCCESS"; | |
36 | ||
37 | case EXIT_FAILURE: | |
38 | return "FAILURE"; | |
39 | } | |
40 | ||
5f94b4e6 | 41 | if (IN_SET(level, EXIT_STATUS_SYSTEMD, EXIT_STATUS_LSB)) { |
65e3fd83 | 42 | switch (status) { |
1afbdcb0 LP |
43 | |
44 | case EXIT_CHDIR: | |
45 | return "CHDIR"; | |
46 | ||
47 | case EXIT_NICE: | |
48 | return "NICE"; | |
49 | ||
50 | case EXIT_FDS: | |
51 | return "FDS"; | |
52 | ||
53 | case EXIT_EXEC: | |
54 | return "EXEC"; | |
55 | ||
56 | case EXIT_MEMORY: | |
57 | return "MEMORY"; | |
58 | ||
59 | case EXIT_LIMITS: | |
60 | return "LIMITS"; | |
61 | ||
62 | case EXIT_OOM_ADJUST: | |
63 | return "OOM_ADJUST"; | |
64 | ||
65 | case EXIT_SIGNAL_MASK: | |
66 | return "SIGNAL_MASK"; | |
67 | ||
68 | case EXIT_STDIN: | |
69 | return "STDIN"; | |
70 | ||
71 | case EXIT_STDOUT: | |
72 | return "STDOUT"; | |
73 | ||
74 | case EXIT_CHROOT: | |
75 | return "CHROOT"; | |
76 | ||
77 | case EXIT_IOPRIO: | |
78 | return "IOPRIO"; | |
79 | ||
80 | case EXIT_TIMERSLACK: | |
81 | return "TIMERSLACK"; | |
82 | ||
83 | case EXIT_SECUREBITS: | |
84 | return "SECUREBITS"; | |
85 | ||
86 | case EXIT_SETSCHEDULER: | |
87 | return "SETSCHEDULER"; | |
88 | ||
89 | case EXIT_CPUAFFINITY: | |
90 | return "CPUAFFINITY"; | |
91 | ||
92 | case EXIT_GROUP: | |
93 | return "GROUP"; | |
94 | ||
95 | case EXIT_USER: | |
96 | return "USER"; | |
97 | ||
98 | case EXIT_CAPABILITIES: | |
99 | return "CAPABILITIES"; | |
100 | ||
101 | case EXIT_CGROUP: | |
102 | return "CGROUP"; | |
103 | ||
104 | case EXIT_SETSID: | |
105 | return "SETSID"; | |
106 | ||
107 | case EXIT_CONFIRM: | |
108 | return "CONFIRM"; | |
109 | ||
110 | case EXIT_STDERR: | |
111 | return "STDERR"; | |
112 | ||
1afbdcb0 LP |
113 | case EXIT_PAM: |
114 | return "PAM"; | |
ff01d048 LP |
115 | |
116 | case EXIT_NETWORK: | |
117 | return "NETWORK"; | |
4c2630eb MS |
118 | |
119 | case EXIT_NAMESPACE: | |
120 | return "NAMESPACE"; | |
8351ceae LP |
121 | |
122 | case EXIT_NO_NEW_PRIVILEGES: | |
123 | return "NO_NEW_PRIVILEGES"; | |
124 | ||
125 | case EXIT_SECCOMP: | |
126 | return "SECCOMP"; | |
7b52a628 MS |
127 | |
128 | case EXIT_SELINUX_CONTEXT: | |
129 | return "SELINUX_CONTEXT"; | |
ac45f971 LP |
130 | |
131 | case EXIT_PERSONALITY: | |
132 | return "PERSONALITY"; | |
eef65bf3 MS |
133 | |
134 | case EXIT_APPARMOR_PROFILE: | |
135 | return "APPARMOR"; | |
4298d0b5 LP |
136 | |
137 | case EXIT_ADDRESS_FAMILIES: | |
138 | return "ADDRESS_FAMILIES"; | |
e66cf1a3 LP |
139 | |
140 | case EXIT_RUNTIME_DIRECTORY: | |
141 | return "RUNTIME_DIRECTORY"; | |
3900e5fd | 142 | |
a4152e3f LP |
143 | case EXIT_MAKE_STARTER: |
144 | return "MAKE_STARTER"; | |
060e088e | 145 | |
56ecbcc0 LP |
146 | case EXIT_CHOWN: |
147 | return "CHOWN"; | |
148 | ||
94b573bd SY |
149 | case EXIT_SMACK_PROCESS_LABEL: |
150 | return "SMACK_PROCESS_LABEL"; | |
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 && | |
3e2d435b | 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 | } |