From f99fc16e14316e699b830cea95966798c3aab76c Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Tue, 6 Dec 2016 07:28:50 -0500 Subject: [PATCH] util: Removed execute --- src/util.cpp | 149 --------------------------------------------------- src/util.h | 2 - 2 files changed, 151 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index e6639ee2e..2ba0ffd27 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -197,155 +197,6 @@ const std::string uuid () } #endif -//////////////////////////////////////////////////////////////////////////////// -// Run a binary with args, capturing output. -int execute ( - const std::string& executable, - const std::vector & args, - const std::string& input, - std::string& output) -{ - pid_t pid; - int pin[2], pout[2]; - fd_set rfds, wfds; - struct timeval tv; - int select_retval, read_retval, write_retval; - char buf[16384]; - unsigned int written; - const char* input_cstr = input.c_str (); - - if (signal (SIGPIPE, SIG_IGN) == SIG_ERR) // Handled locally with EPIPE. - throw std::string (std::strerror (errno)); - - if (pipe (pin) == -1) - throw std::string (std::strerror (errno)); - - if (pipe (pout) == -1) - throw std::string (std::strerror (errno)); - - if ((pid = fork ()) == -1) - throw std::string (std::strerror (errno)); - - if (pid == 0) - { - // This is only reached in the child - close (pin[1]); // Close the write end of the input pipe. - close (pout[0]); // Close the read end of the output pipe. - - // Parent writes to pin[1]. Set read end pin[0] as STDIN for child. - if (dup2 (pin[0], STDIN_FILENO) == -1) - throw std::string (std::strerror (errno)); - close (pin[0]); - - // Parent reads from pout[0]. Set write end pout[1] as STDOUT for child. - if (dup2 (pout[1], STDOUT_FILENO) == -1) - throw std::string (std::strerror (errno)); - close (pout[1]); - - // Add executable as argv[0] and NULL-terminate the array for execvp(). - char** argv = new char* [args.size () + 2]; - argv[0] = (char*) executable.c_str (); - for (unsigned int i = 0; i < args.size (); ++i) - argv[i+1] = (char*) args[i].c_str (); - - argv[args.size () + 1] = NULL; - _exit (execvp (executable.c_str (), argv)); - } - - // This is only reached in the parent - close (pin[0]); // Close the read end of the input pipe. - close (pout[1]); // Close the write end of the output pipe. - - if (input.size () == 0) - { - // Nothing to send to the child, close the pipe early. - close (pin[1]); - } - - output = ""; - read_retval = -1; - written = 0; - while (read_retval != 0 || input.size () != written) - { - FD_ZERO (&rfds); - if (read_retval != 0) - FD_SET (pout[0], &rfds); - - FD_ZERO (&wfds); - if (input.size () != written) - FD_SET (pin[1], &wfds); - - // On Linux, tv may be overwritten by select(). Reset it each time. - // NOTE: Timeout chosen arbitrarily - we don't time out execute() calls. - // select() is run over and over again unless the child exits or closes - // its pipes. - tv.tv_sec = 5; - tv.tv_usec = 0; - - select_retval = select (std::max (pout[0], pin[1]) + 1, &rfds, &wfds, NULL, &tv); - - if (select_retval == -1) - throw std::string (std::strerror (errno)); - - // Write data to child's STDIN - if (FD_ISSET (pin[1], &wfds)) - { - write_retval = write (pin[1], input_cstr + written, input.size () - written); - if (write_retval == -1) - { - if (errno == EPIPE) - { - // Child died (or closed the pipe) before reading all input. - // We don't really care; pretend we wrote it all. - write_retval = input.size () - written; - } - else - { - throw std::string (std::strerror (errno)); - } - } - written += write_retval; - - if (written == input.size ()) - { - // Let the child know that no more input is coming by closing the pipe. - close (pin[1]); - } - } - - // Read data from child's STDOUT - if (FD_ISSET (pout[0], &rfds)) - { - read_retval = read (pout[0], &buf, sizeof (buf) - 1); - if (read_retval == -1) - throw std::string (std::strerror (errno)); - - buf[read_retval] = '\0'; - output += buf; - } - } - - close (pout[0]); // Close the read end of the output pipe. - - int status = -1; - if (wait (&status) == -1) - throw std::string (std::strerror (errno)); - - if (WIFEXITED (status)) - { - status = WEXITSTATUS (status); - } - else - { - throw std::string ("Error: Could not get Hook exit status!"); - } - - if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) // We're done, return to default. - throw std::string (std::strerror (errno)); - - return status; -} - // Collides with std::numeric_limits methods #undef max diff --git a/src/util.h b/src/util.h index d3fb12047..cc500723c 100644 --- a/src/util.h +++ b/src/util.h @@ -48,8 +48,6 @@ void uuid_unparse_lower (uuid_t uu, char *out); #endif const std::string uuid (); -int execute (const std::string&, const std::vector &, const std::string&, std::string&); - const std::string indentProject ( const std::string&, const std::string& whitespace = " ",