Repository: mesos Updated Branches: refs/heads/master 65a9674a1 -> c03c1a5cb
Factored os::shell into it's own header. Review: https://reviews.apache.org/r/18862 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/c03c1a5c Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/c03c1a5c Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/c03c1a5c Branch: refs/heads/master Commit: c03c1a5cb8cf1d668e7f641de09e5283eaa0552b Parents: 65a9674 Author: Benjamin Hindman <[email protected]> Authored: Tue Mar 4 14:49:36 2014 -0800 Committer: Benjamin Hindman <[email protected]> Committed: Thu Mar 6 14:33:49 2014 -0800 ---------------------------------------------------------------------- 3rdparty/libprocess/3rdparty/stout/Makefile.am | 1 + .../3rdparty/stout/include/stout/os.hpp | 48 +------------ .../3rdparty/stout/include/stout/os/shell.hpp | 75 ++++++++++++++++++++ 3 files changed, 77 insertions(+), 47 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/c03c1a5c/3rdparty/libprocess/3rdparty/stout/Makefile.am ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/Makefile.am b/3rdparty/libprocess/3rdparty/stout/Makefile.am index b53a3f1..1075b46 100644 --- a/3rdparty/libprocess/3rdparty/stout/Makefile.am +++ b/3rdparty/libprocess/3rdparty/stout/Makefile.am @@ -49,6 +49,7 @@ EXTRA_DIST = \ include/stout/os/process.hpp \ include/stout/os/read.hpp \ include/stout/os/sendfile.hpp \ + include/stout/os/shell.hpp \ include/stout/os/signals.hpp \ include/stout/os/pstree.hpp \ include/stout/os/sysctl.hpp \ http://git-wip-us.apache.org/repos/asf/mesos/blob/c03c1a5c/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp index 20d028f..3f475a4 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp @@ -49,7 +49,6 @@ #include <list> #include <set> -#include <sstream> #include <string> #include <stout/bytes.hpp> @@ -77,6 +76,7 @@ #include <stout/os/pstree.hpp> #include <stout/os/read.hpp> #include <stout/os/sendfile.hpp> +#include <stout/os/shell.hpp> #include <stout/os/signals.hpp> #ifdef __APPLE__ #include <stout/os/sysctl.hpp> @@ -721,52 +721,6 @@ inline Try<std::string> hostname() } -// Runs a shell command formatted with varargs and return the return value -// of the command. Optionally, the output is returned via an argument. -// TODO(vinod): Pass an istream object that can provide input to the command. -inline Try<int> shell(std::ostream* os, const std::string fmt, ...) -{ - va_list args; - va_start(args, fmt); - - const Try<std::string>& cmdline = strings::internal::format(fmt, args); - - va_end(args); - - if (cmdline.isError()) { - return Error(cmdline.error()); - } - - FILE* file; - - if ((file = popen(cmdline.get().c_str(), "r")) == NULL) { - return Error("Failed to run '" + cmdline.get() + "'"); - } - - char line[1024]; - // NOTE(vinod): Ideally the if and while loops should be interchanged. But - // we get a broken pipe error if we don't read the output and simply close. - while (fgets(line, sizeof(line), file) != NULL) { - if (os != NULL) { - *os << line ; - } - } - - if (ferror(file) != 0) { - ErrnoError error("Error reading output of '" + cmdline.get() + "'"); - pclose(file); // Ignoring result since we already have an error. - return error; - } - - int status; - if ((status = pclose(file)) == -1) { - return Error("Failed to get status of '" + cmdline.get() + "'"); - } - - return status; -} - - // Suspends execution for the given duration. inline Try<Nothing> sleep(const Duration& duration) { http://git-wip-us.apache.org/repos/asf/mesos/blob/c03c1a5c/3rdparty/libprocess/3rdparty/stout/include/stout/os/shell.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os/shell.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os/shell.hpp new file mode 100644 index 0000000..6728ad8 --- /dev/null +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os/shell.hpp @@ -0,0 +1,75 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __STOUT_OS_SHELL_HPP__ +#define __STOUT_OS_SHELL_HPP__ + +#include <stdarg.h> // For va_list, va_start, etc. +#include <stdio.h> // For ferror, fgets, FILE, pclose, popen. + +#include <ostream> +#include <string> + +#include <stout/error.hpp> +#include <stout/format.hpp> +#include <stout/try.hpp> + +namespace os { + +// Runs a shell command formatted with varargs and return the return value +// of the command. Optionally, the output is returned via an argument. +// TODO(vinod): Pass an istream object that can provide input to the command. +inline Try<int> shell(std::ostream* os, const std::string fmt, ...) +{ + va_list args; + va_start(args, fmt); + + const Try<std::string>& command = strings::internal::format(fmt, args); + + va_end(args); + + if (command.isError()) { + return Error(command.error()); + } + + FILE* file; + + if ((file = popen(command.get().c_str(), "r")) == NULL) { + return Error("Failed to run '" + command.get() + "'"); + } + + char line[1024]; + // NOTE(vinod): Ideally the if and while loops should be interchanged. But + // we get a broken pipe error if we don't read the output and simply close. + while (fgets(line, sizeof(line), file) != NULL) { + if (os != NULL) { + *os << line; + } + } + + if (ferror(file) != 0) { + pclose(file); // Ignoring result since we already have an error. + return Error("Error reading output of '" + command.get() + "'"); + } + + int status; + if ((status = pclose(file)) == -1) { + return Error("Failed to get status of '" + command.get() + "'"); + } + + return status; +} + +} // namespace os { + +#endif // __STOUT_OS_SHELL_HPP__
