1 /*
2 * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Additionally licensed with:
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS,
28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 */
32 package org.spf4j.base;
33
34 import gnu.trove.map.TIntObjectMap;
35 import gnu.trove.map.hash.TIntObjectHashMap;
36 import javax.annotation.Nonnull;
37
38 /**
39 * some "standard" process exit codes from:
40 * http://tldp.org/LDP/abs/html/index.html
41 * https://www.freebsd.org/cgi/man.cgi?query=sysexits&apropos=0&sektion=0&manpath=FreeBSD+4.3-RELEASE&format=html
42 * http://journal.thobe.org/2013/02/jvms-and-kill-signals.html
43 * @author zoly
44 */
45 public enum SysExits {
46
47 /**
48 * Everything is OK.
49 */
50 OK(0),
51
52 /**
53 * Catch all for general errors.
54 */
55 EX_GENERAL(1),
56
57 /**
58 * Shell build in miss-use.
59 */
60 EX_SHELL_BUILTIN_MISSUSE(2),
61
62 /**
63 * The command was used incorrectly, e.g., with the wrong number of arguments, a bad flag, a bad syntax in a
64 * parameter, or whatever.
65 */
66 EX_USAGE(64),
67 /**
68 * The input data was incorrect in some way. This should only be used for user's data and not system files.
69 */
70 EX_DATAERR(65),
71 /**
72 * An input file (not a system file) did not exist or was not readable. This could also include errors like ``No
73 * message'' to a mailer (if it cared to catch it).
74 */
75 EX_NOINPUT(66),
76 /**
77 * The user specified did not exist. This might be used for mail addresses or remote logins.
78 */
79 EX_NOUSER(67),
80 /**
81 * The host specified did not exist. This is used in mail addresses or network requests.
82 */
83 EX_NOHOST(68),
84 /**
85 * A service is unavailable. This can occur if a support program or file does not exist. This can also be used as a
86 * catchall message when something you wanted to do doesn't work, but you don't know why.
87 */
88 EX_UNAVAILABLE(69),
89 /**
90 * An internal software error has been detected. This should be limited to non-operating system related errors as
91 * possible.
92 */
93 EX_SOFTWARE(70),
94 /**
95 * An operating system error has been detected. This is intended to be used for such things as ``cannot fork'',
96 * ``cannot create pipe'', or the like. It includes things like getuid returning a user that does not exist in the
97 * passwd file.
98 */
99 EX_OSERR(71),
100 /**
101 * Some system file (e.g., /etc/passwd, /var/run/utmp,etc.) does not exist, cannot be opened, or has some sort of
102 * error (e.g., syntax error).
103 */
104 EX_OSFILE(72),
105 /**
106 * A (user specified) output file cannot be created.
107 */
108 EX_CANTCREAT(73),
109 /**
110 * An error occurred while doing I/O on some file.
111 */
112 EX_IOERR(74),
113 /**
114 * Temporary failure, indicating something that is not really an error. In sendmail, this means that a mailer (e.g.)
115 * could not create a connection, and the request should be reattempted later.
116 */
117 EX_TEMPFAIL(75),
118 /**
119 * The remote system returned something that was ``not possible'' during a protocol exchange.
120 */
121 EX_PROTOCOL(76),
122 /**
123 * You did not have sufficient permission to perform the operation. This is not intended for file system problems,
124 * which should use EX_NOINPUT or EX_CANTCREAT, but rather for higher level permissions.
125 */
126 EX_NOPERM(77),
127 /**
128 * Something was found in an unconfigured or misconfigured state.
129 */
130 EX_CONFIG(78),
131
132
133 /**
134 * cannot execute invoked command.
135 */
136 EX_CANNOT_EXEC_CMD(126),
137
138 /**
139 * Command not found.
140 */
141 EX_CMD_NOT_FOUND(127),
142
143 /**
144 * Invalid argument to exit.
145 */
146 EX_INVALID_ARG_TO_EXIT(128),
147
148 /**
149 * Section caused by exit due to signal.
150 * where signal name is same on Linux, Solaris and MacOS enum has the appropriate name.
151 * for linux see: http://man7.org/linux/man-pages/man7/signal.7.html
152 * or run man signal on you OS of choice.
153 */
154 EX_SIG_HUP(129),
155 EX_SIG_INT(130),
156 EX_SIG_QUIT(131),
157 EX_SIG_ILL(132),
158 EX_SIG_TRAP(133),
159 EX_SIG_ABRT(134),
160 EX_SIG_7(135),
161 EX_SIG_FPE(136),
162 EX_SIG_KILL(137),
163 EX_SIG_10(138),
164 EX_SIG_11(139),
165 EX_SIG_12(140),
166 EX_SIG_PIPE(141),
167 EX_SIG_ALRM(142),
168 EX_SIG_TERM(143),
169 EX_SIG_16(144),
170 EX_SIG_17(145),
171 EX_SIG_18(146),
172 EX_SIG_19(147),
173 EX_SIG_20(148),
174 EX_SIG_21(149),
175 EX_SIG_22(150),
176 EX_SIG_23(151),
177 EX_SIG_24(152),
178 EX_SIG_25(153),
179 EX_SIG_26(154),
180 EX_SIG_27(155),
181 EX_SIG_28(156),
182 EX_SIG_29(157),
183 EX_SIG_30(158),
184 EX_SIG_31(159),
185
186 EX_STATUS_OUT_OF_RANGE(255),
187 /**
188 * Any return codes not explicitly defined will be associated with "EX_UNKNOWN"
189 */
190 EX_UNKNOWN(-1);
191
192 private static final TIntObjectMap<SysExits> CODE2ENUM;
193
194 static {
195 SysExits[] values = SysExits.values();
196 TIntObjectMap<SysExits> c2e = new TIntObjectHashMap<>(values.length);
197 for (SysExits e : values) {
198 if (c2e.put(e.exitCode(), e) != null) {
199 throw new ExceptionInInitializerError("Duplicate exit code " + e);
200 }
201 }
202 CODE2ENUM = c2e;
203 }
204
205 private final int exitCode;
206
207 SysExits(final int code) {
208 this.exitCode = code;
209 }
210
211 public int exitCode() {
212 return exitCode;
213 }
214
215 public boolean isOk() {
216 return exitCode == 0;
217 }
218
219 public boolean isError() {
220 return exitCode != 0;
221 }
222
223 /**
224 * @param exitCode
225 * @return corresponding enum.
226 */
227 @Nonnull
228 public static SysExits fromCode(final int exitCode) {
229 SysExits result = CODE2ENUM.get(exitCode);
230 if (result == null) {
231 return SysExits.EX_UNKNOWN;
232 } else {
233 return result;
234 }
235 }
236
237 }