]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/exit-status.c
0dc82b2e130e51e49ae14cd8dabeab12fbafca2e
[thirdparty/systemd.git] / src / shared / exit-status.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdlib.h>
23 #include <sys/wait.h>
24
25 #include "exit-status.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_TCPWRAP:
115 return "TCPWRAP";
116
117 case EXIT_PAM:
118 return "PAM";
119
120 case EXIT_NETWORK:
121 return "NETWORK";
122
123 case EXIT_NAMESPACE:
124 return "NAMESPACE";
125
126 case EXIT_NO_NEW_PRIVILEGES:
127 return "NO_NEW_PRIVILEGES";
128
129 case EXIT_SECCOMP:
130 return "SECCOMP";
131 }
132 }
133
134 if (level == EXIT_STATUS_LSB) {
135 switch ((int) status) {
136
137 case EXIT_INVALIDARGUMENT:
138 return "INVALIDARGUMENT";
139
140 case EXIT_NOTIMPLEMENTED:
141 return "NOTIMPLEMENTED";
142
143 case EXIT_NOPERMISSION:
144 return "NOPERMISSION";
145
146 case EXIT_NOTINSTALLED:
147 return "NOTINSSTALLED";
148
149 case EXIT_NOTCONFIGURED:
150 return "NOTCONFIGURED";
151
152 case EXIT_NOTRUNNING:
153 return "NOTRUNNING";
154 }
155 }
156
157 return NULL;
158 }
159
160
161 bool is_clean_exit(int code, int status) {
162
163 if (code == CLD_EXITED)
164 return status == 0;
165
166 /* If a daemon does not implement handlers for some of the
167 * signals that's not considered an unclean shutdown */
168 if (code == CLD_KILLED)
169 return
170 status == SIGHUP ||
171 status == SIGINT ||
172 status == SIGTERM ||
173 status == SIGPIPE;
174
175 return false;
176 }
177
178 bool is_clean_exit_lsb(int code, int status) {
179
180 if (is_clean_exit(code, status))
181 return true;
182
183 return
184 code == CLD_EXITED &&
185 (status == EXIT_NOTINSTALLED || status == EXIT_NOTCONFIGURED);
186 }