chiark / gitweb /
c1b04a38688cd96055f74c39a4a1555552ab3da5
[elogind.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 #include "set.h"
27 #include "macro.h"
28
29 const char* exit_status_to_string(ExitStatus status, ExitStatusLevel level) {
30
31         /* We cast to int here, so that -Wenum doesn't complain that
32          * EXIT_SUCCESS/EXIT_FAILURE aren't in the enum */
33
34         switch ((int) status) {
35
36         case EXIT_SUCCESS:
37                 return "SUCCESS";
38
39         case EXIT_FAILURE:
40                 return "FAILURE";
41         }
42
43
44         if (level == EXIT_STATUS_SYSTEMD || level == EXIT_STATUS_LSB) {
45                 switch ((int) status) {
46
47                 case EXIT_CHDIR:
48                         return "CHDIR";
49
50                 case EXIT_NICE:
51                         return "NICE";
52
53                 case EXIT_FDS:
54                         return "FDS";
55
56                 case EXIT_EXEC:
57                         return "EXEC";
58
59                 case EXIT_MEMORY:
60                         return "MEMORY";
61
62                 case EXIT_LIMITS:
63                         return "LIMITS";
64
65                 case EXIT_OOM_ADJUST:
66                         return "OOM_ADJUST";
67
68                 case EXIT_SIGNAL_MASK:
69                         return "SIGNAL_MASK";
70
71                 case EXIT_STDIN:
72                         return "STDIN";
73
74                 case EXIT_STDOUT:
75                         return "STDOUT";
76
77                 case EXIT_CHROOT:
78                         return "CHROOT";
79
80                 case EXIT_IOPRIO:
81                         return "IOPRIO";
82
83                 case EXIT_TIMERSLACK:
84                         return "TIMERSLACK";
85
86                 case EXIT_SECUREBITS:
87                         return "SECUREBITS";
88
89                 case EXIT_SETSCHEDULER:
90                         return "SETSCHEDULER";
91
92                 case EXIT_CPUAFFINITY:
93                         return "CPUAFFINITY";
94
95                 case EXIT_GROUP:
96                         return "GROUP";
97
98                 case EXIT_USER:
99                         return "USER";
100
101                 case EXIT_CAPABILITIES:
102                         return "CAPABILITIES";
103
104                 case EXIT_CGROUP:
105                         return "CGROUP";
106
107                 case EXIT_SETSID:
108                         return "SETSID";
109
110                 case EXIT_CONFIRM:
111                         return "CONFIRM";
112
113                 case EXIT_STDERR:
114                         return "STDERR";
115
116                 case EXIT_TCPWRAP:
117                         return "TCPWRAP";
118
119                 case EXIT_PAM:
120                         return "PAM";
121
122                 case EXIT_NETWORK:
123                         return "NETWORK";
124
125                 case EXIT_NAMESPACE:
126                         return "NAMESPACE";
127
128                 case EXIT_NO_NEW_PRIVILEGES:
129                         return "NO_NEW_PRIVILEGES";
130
131                 case EXIT_SECCOMP:
132                         return "SECCOMP";
133
134                 case EXIT_SELINUX_CONTEXT:
135                         return "SELINUX_CONTEXT";
136
137                 case EXIT_PERSONALITY:
138                         return "PERSONALITY";
139
140                 case EXIT_APPARMOR_PROFILE:
141                         return "APPARMOR";
142
143                 case EXIT_ADDRESS_FAMILIES:
144                         return "ADDRESS_FAMILIES";
145                 }
146         }
147
148         if (level == EXIT_STATUS_LSB) {
149                 switch ((int) status) {
150
151                 case EXIT_INVALIDARGUMENT:
152                         return "INVALIDARGUMENT";
153
154                 case EXIT_NOTIMPLEMENTED:
155                         return "NOTIMPLEMENTED";
156
157                 case EXIT_NOPERMISSION:
158                         return "NOPERMISSION";
159
160                 case EXIT_NOTINSTALLED:
161                         return "NOTINSSTALLED";
162
163                 case EXIT_NOTCONFIGURED:
164                         return "NOTCONFIGURED";
165
166                 case EXIT_NOTRUNNING:
167                         return "NOTRUNNING";
168                 }
169         }
170
171         return NULL;
172 }
173
174
175 bool is_clean_exit(int code, int status, ExitStatusSet *success_status) {
176
177         if (code == CLD_EXITED)
178                 return status == 0 ||
179                        (success_status &&
180                        set_contains(success_status->code, INT_TO_PTR(status)));
181
182         /* If a daemon does not implement handlers for some of the
183          * signals that's not considered an unclean shutdown */
184         if (code == CLD_KILLED)
185                 return
186                         status == SIGHUP ||
187                         status == SIGINT ||
188                         status == SIGTERM ||
189                         status == SIGPIPE ||
190                         (success_status &&
191                         set_contains(success_status->signal, INT_TO_PTR(status)));
192
193         return false;
194 }
195
196 bool is_clean_exit_lsb(int code, int status, ExitStatusSet *success_status) {
197
198         if (is_clean_exit(code, status, success_status))
199                 return true;
200
201         return
202                 code == CLD_EXITED &&
203                 (status == EXIT_NOTINSTALLED || status == EXIT_NOTCONFIGURED);
204 }
205
206 int parse_show_status(const char *v, ShowStatus *ret) {
207         int r;
208
209         assert(v);
210         assert(ret);
211
212         if (streq(v, "auto")) {
213                 *ret = SHOW_STATUS_AUTO;
214                 return 0;
215         }
216         r = parse_boolean(v);
217         if (r < 0)
218                 return r;
219         *ret = r ? SHOW_STATUS_YES : SHOW_STATUS_NO;
220         return 0;
221 }