chiark / gitweb /
dbus: add 'Tainted' property to Manager object
[elogind.git] / src / 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 General Public License as published by
10   the Free Software Foundation; either version 2 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   General Public License for more details.
17
18   You should have received a copy of the GNU 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         }
121
122         if (level == EXIT_STATUS_LSB) {
123                 switch ((int) status) {
124
125                 case EXIT_INVALIDARGUMENT:
126                         return "INVALIDARGUMENT";
127
128                 case EXIT_NOTIMPLEMENTED:
129                         return "NOTIMPLEMENTED";
130
131                 case EXIT_NOPERMISSION:
132                         return "NOPERMISSION";
133
134                 case EXIT_NOTINSTALLED:
135                         return "NOTINSSTALLED";
136
137                 case EXIT_NOTCONFIGURED:
138                         return "NOTCONFIGURED";
139
140                 case EXIT_NOTRUNNING:
141                         return "NOTRUNNING";
142                 }
143         }
144
145         return NULL;
146 }
147
148
149 bool is_clean_exit(int code, int status) {
150
151         if (code == CLD_EXITED)
152                 return status == 0;
153
154         /* If a daemon does not implement handlers for some of the
155          * signals that's not considered an unclean shutdown */
156         if (code == CLD_KILLED)
157                 return
158                         status == SIGHUP ||
159                         status == SIGINT ||
160                         status == SIGTERM ||
161                         status == SIGPIPE;
162
163         return false;
164 }
165
166 bool is_clean_exit_lsb(int code, int status) {
167
168         if (is_clean_exit(code, status))
169                 return true;
170
171         return
172                 code == CLD_EXITED &&
173                 (status == EXIT_NOTINSTALLED || status == EXIT_NOTCONFIGURED);
174 }