Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / run_cmd.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2011 New Dream Network
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  *
13  */
14
15 #include "common/errno.h"
16
17 #include <sstream>
18 #include <stdarg.h>
19 #include <sys/wait.h>
20 #include <unistd.h>
21 #include <vector>
22
23 using std::ostringstream;
24
25 std::string run_cmd(const char *cmd, ...)
26 {
27   std::vector <const char *> arr;
28   va_list ap;
29   va_start(ap, cmd);
30   const char *c = cmd;
31   do {
32     arr.push_back(c);
33     c = va_arg(ap, const char*);
34   } while (c != NULL);
35   va_end(ap);
36   arr.push_back(NULL);
37
38   int fret = fork();
39   if (fret == -1) {
40     int err = errno;
41     ostringstream oss;
42     oss << "run_cmd(" << cmd << "): unable to fork(): " << cpp_strerror(err);
43     return oss.str();
44   }
45   else if (fret == 0) {
46     // execvp doesn't modify its arguments, so the const-cast here is safe.
47     close(STDIN_FILENO);
48     close(STDOUT_FILENO);
49     close(STDERR_FILENO);
50     execvp(cmd, (char * const*)&arr[0]);
51     _exit(127);
52   }
53   int status;
54   while (waitpid(fret, &status, 0) == -1) {
55     int err = errno;
56     if (err == EINTR)
57       continue;
58     ostringstream oss;
59     oss << "run_cmd(" << cmd << "): waitpid error: "
60          << cpp_strerror(err);
61     return oss.str();
62   }
63   if (WIFEXITED(status)) {
64     int wexitstatus = WEXITSTATUS(status);
65     if (wexitstatus != 0) {
66       ostringstream oss;
67       oss << "run_cmd(" << cmd << "): exited with status " << wexitstatus;
68       return oss.str();
69     }
70     return "";
71   }
72   else if (WIFSIGNALED(status)) {
73     ostringstream oss;
74     oss << "run_cmd(" << cmd << "): terminated by signal";
75     return oss.str();
76   }
77   ostringstream oss;
78   oss << "run_cmd(" << cmd << "): terminated by unknown mechanism";
79   return oss.str();
80 }