]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
f67539c2 | 17 | #ifndef _WIN32 |
7c673cae FG |
18 | #include <sstream> |
19 | #include <stdarg.h> | |
7c673cae FG |
20 | #include <sys/wait.h> |
21 | #include <unistd.h> | |
22 | #include <vector> | |
f67539c2 TL |
23 | #else |
24 | #include "common/SubProcess.h" | |
25 | #endif /* _WIN32 */ | |
7c673cae FG |
26 | |
27 | using std::ostringstream; | |
28 | ||
f67539c2 | 29 | #ifndef _WIN32 |
7c673cae FG |
30 | std::string run_cmd(const char *cmd, ...) |
31 | { | |
32 | std::vector <const char *> arr; | |
33 | va_list ap; | |
34 | va_start(ap, cmd); | |
35 | const char *c = cmd; | |
36 | do { | |
37 | arr.push_back(c); | |
38 | c = va_arg(ap, const char*); | |
39 | } while (c != NULL); | |
40 | va_end(ap); | |
41 | arr.push_back(NULL); | |
42 | ||
43 | int fret = fork(); | |
44 | if (fret == -1) { | |
45 | int err = errno; | |
46 | ostringstream oss; | |
47 | oss << "run_cmd(" << cmd << "): unable to fork(): " << cpp_strerror(err); | |
48 | return oss.str(); | |
49 | } | |
50 | else if (fret == 0) { | |
51 | // execvp doesn't modify its arguments, so the const-cast here is safe. | |
52 | close(STDIN_FILENO); | |
53 | close(STDOUT_FILENO); | |
54 | close(STDERR_FILENO); | |
55 | execvp(cmd, (char * const*)&arr[0]); | |
56 | _exit(127); | |
57 | } | |
58 | int status; | |
59 | while (waitpid(fret, &status, 0) == -1) { | |
60 | int err = errno; | |
61 | if (err == EINTR) | |
62 | continue; | |
63 | ostringstream oss; | |
64 | oss << "run_cmd(" << cmd << "): waitpid error: " | |
65 | << cpp_strerror(err); | |
66 | return oss.str(); | |
67 | } | |
68 | if (WIFEXITED(status)) { | |
69 | int wexitstatus = WEXITSTATUS(status); | |
70 | if (wexitstatus != 0) { | |
71 | ostringstream oss; | |
72 | oss << "run_cmd(" << cmd << "): exited with status " << wexitstatus; | |
73 | return oss.str(); | |
74 | } | |
75 | return ""; | |
76 | } | |
77 | else if (WIFSIGNALED(status)) { | |
78 | ostringstream oss; | |
79 | oss << "run_cmd(" << cmd << "): terminated by signal"; | |
80 | return oss.str(); | |
81 | } | |
82 | ostringstream oss; | |
83 | oss << "run_cmd(" << cmd << "): terminated by unknown mechanism"; | |
84 | return oss.str(); | |
85 | } | |
f67539c2 TL |
86 | #else |
87 | std::string run_cmd(const char *cmd, ...) | |
88 | { | |
89 | SubProcess p(cmd, SubProcess::CLOSE, SubProcess::PIPE, SubProcess::CLOSE); | |
90 | ||
91 | va_list ap; | |
92 | va_start(ap, cmd); | |
93 | const char *c = cmd; | |
94 | c = va_arg(ap, const char*); | |
95 | while (c != NULL) { | |
96 | p.add_cmd_arg(c); | |
97 | c = va_arg(ap, const char*); | |
98 | } | |
99 | va_end(ap); | |
100 | ||
101 | if (p.spawn() == 0) { | |
102 | p.join(); | |
103 | } | |
104 | ||
105 | return p.err(); | |
106 | } | |
107 | #endif /* _WIN32 */ |