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"
23 using std::ostringstream;
25 std::string run_cmd(const char *cmd, ...)
27 std::vector <const char *> arr;
33 c = va_arg(ap, const char*);
42 oss << "run_cmd(" << cmd << "): unable to fork(): " << cpp_strerror(err);
46 // execvp doesn't modify its arguments, so the const-cast here is safe.
50 execvp(cmd, (char * const*)&arr[0]);
54 while (waitpid(fret, &status, 0) == -1) {
59 oss << "run_cmd(" << cmd << "): waitpid error: "
63 if (WIFEXITED(status)) {
64 int wexitstatus = WEXITSTATUS(status);
65 if (wexitstatus != 0) {
67 oss << "run_cmd(" << cmd << "): exited with status " << wexitstatus;
72 else if (WIFSIGNALED(status)) {
74 oss << "run_cmd(" << cmd << "): terminated by signal";
78 oss << "run_cmd(" << cmd << "): terminated by unknown mechanism";