]>
git.proxmox.com Git - ceph.git/blob - 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
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network
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.
15 #include "common/errno.h"
24 #include "common/SubProcess.h"
27 using std::ostringstream
;
30 std::string
run_cmd(const char *cmd
, ...)
32 std::vector
<const char *> arr
;
38 c
= va_arg(ap
, const char*);
47 oss
<< "run_cmd(" << cmd
<< "): unable to fork(): " << cpp_strerror(err
);
51 // execvp doesn't modify its arguments, so the const-cast here is safe.
55 execvp(cmd
, (char * const*)&arr
[0]);
59 while (waitpid(fret
, &status
, 0) == -1) {
64 oss
<< "run_cmd(" << cmd
<< "): waitpid error: "
68 if (WIFEXITED(status
)) {
69 int wexitstatus
= WEXITSTATUS(status
);
70 if (wexitstatus
!= 0) {
72 oss
<< "run_cmd(" << cmd
<< "): exited with status " << wexitstatus
;
77 else if (WIFSIGNALED(status
)) {
79 oss
<< "run_cmd(" << cmd
<< "): terminated by signal";
83 oss
<< "run_cmd(" << cmd
<< "): terminated by unknown mechanism";
87 std::string
run_cmd(const char *cmd
, ...)
89 SubProcess
p(cmd
, SubProcess::CLOSE
, SubProcess::PIPE
, SubProcess::CLOSE
);
94 c
= va_arg(ap
, const char*);
97 c
= va_arg(ap
, const char*);
101 if (p
.spawn() == 0) {