Apache Mesos
status_utils.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __STATUS_UTILS_HPP__
18 #define __STATUS_UTILS_HPP__
19 
20 #include <string>
21 
22 #include <stout/option.hpp>
23 #include <stout/stringify.hpp>
24 
25 // Return whether the wait(2) status was a successful process exit.
26 inline bool WSUCCEEDED(int status)
27 {
28  return WIFEXITED(status) && WEXITSTATUS(status) == 0;
29 }
30 
31 
32 inline std::string WSTRINGIFY(int status)
33 {
34  std::string message;
35 #ifdef __WINDOWS__
36  // NOTE: On Windows, exit codes are not standardized, so we cannot
37  // treat any exit codes differently.
38  message += "exited with status ";
39  message += stringify(status);
40 #else
41  if (WIFEXITED(status)) {
42  message += "exited with status ";
43  message += stringify(WEXITSTATUS(status));
44  } else if (WIFSIGNALED(status)) {
45  message += "terminated with signal ";
46  message += strsignal(WTERMSIG(status));
47  if (WCOREDUMP(status)) {
48  message += " (core dumped)";
49  }
50  } else if (WIFSTOPPED(status)) {
51  message += "stopped on signal ";
52  message += strsignal(WSTOPSIG(status));
53  } else {
54  message += "wait status ";
55  message += stringify(status);
56  }
57 #endif // __WINDOWS__
58  return message;
59 }
60 
61 #endif // __STATUS_UTILS_HPP__
const char * strsignal(int signum)
Definition: windows.hpp:347
Result< ProcessStatus > status(pid_t pid)
Definition: proc.hpp:166
#define WIFSIGNALED(x)
Definition: windows.hpp:376
#define WEXITSTATUS(x)
Definition: windows.hpp:368
#define WIFEXITED(x)
Definition: windows.hpp:362
bool WSUCCEEDED(int status)
Definition: status_utils.hpp:26
std::string WSTRINGIFY(int status)
Definition: status_utils.hpp:32
std::string stringify(int flags)