| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- //
- // Process_UNIX.cpp
- //
- // $Id: //poco/1.4/Foundation/src/Process_UNIX.cpp#3 $
- //
- // Library: Foundation
- // Package: Processes
- // Module: Process
- //
- // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
- // and Contributors.
- //
- // Permission is hereby granted, free of charge, to any person or organization
- // obtaining a copy of the software and accompanying documentation covered by
- // this license (the "Software") to use, reproduce, display, distribute,
- // execute, and transmit the Software, and to prepare derivative works of the
- // Software, and to permit third-parties to whom the Software is furnished to
- // do so, all subject to the following:
- //
- // The copyright notices in the Software and this entire statement, including
- // the above license grant, this restriction and the following disclaimer,
- // must be included in all copies of the Software, in whole or in part, and
- // all derivative works of the Software, unless such copies or derivative
- // works are solely in the form of machine-executable object code generated by
- // a source language processor.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
- // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
- // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
- // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- // DEALINGS IN THE SOFTWARE.
- //
- #include "Poco/Process_UNIX.h"
- #include "Poco/Exception.h"
- #include "Poco/NumberFormatter.h"
- #include "Poco/Pipe.h"
- #include <errno.h>
- #include <signal.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <sys/resource.h>
- #include <sys/wait.h>
- #if defined(__QNX__)
- #include <process.h>
- #include <spawn.h>
- #include <cstring>
- #endif
- namespace Poco {
- //
- // ProcessHandleImpl
- //
- ProcessHandleImpl::ProcessHandleImpl(pid_t pid):
- _pid(pid)
- {
- }
- ProcessHandleImpl::~ProcessHandleImpl()
- {
- }
- pid_t ProcessHandleImpl::id() const
- {
- return _pid;
- }
- int ProcessHandleImpl::wait() const
- {
- int status;
- int rc;
- do
- {
- rc = waitpid(_pid, &status, 0);
- }
- while (rc < 0 && errno == EINTR);
- if (rc != _pid)
- throw SystemException("Cannot wait for process", NumberFormatter::format(_pid));
- return WEXITSTATUS(status);
- }
- //
- // ProcessImpl
- //
- ProcessImpl::PIDImpl ProcessImpl::idImpl()
- {
- return getpid();
- }
- void ProcessImpl::timesImpl(long& userTime, long& kernelTime)
- {
- struct rusage usage;
- getrusage(RUSAGE_SELF, &usage);
- userTime = usage.ru_utime.tv_sec;
- kernelTime = usage.ru_stime.tv_sec;
- }
- ProcessHandleImpl* ProcessImpl::launchImpl(const std::string& command, const ArgsImpl& args, const std::string& initialDirectory, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe, const EnvImpl& env)
- {
- #if defined(__QNX__)
- if (initialDirectory.empty())
- {
- /// use QNX's spawn system call which is more efficient than fork/exec.
- char** argv = new char*[args.size() + 2];
- int i = 0;
- argv[i++] = const_cast<char*>(command.c_str());
- for (ArgsImpl::const_iterator it = args.begin(); it != args.end(); ++it)
- argv[i++] = const_cast<char*>(it->c_str());
- argv[i] = NULL;
- struct inheritance inherit;
- std::memset(&inherit, 0, sizeof(inherit));
- inherit.flags = SPAWN_ALIGN_DEFAULT | SPAWN_CHECK_SCRIPT | SPAWN_SEARCH_PATH;
- int fdmap[3];
- fdmap[0] = inPipe ? inPipe->readHandle() : 0;
- fdmap[1] = outPipe ? outPipe->writeHandle() : 1;
- fdmap[2] = errPipe ? errPipe->writeHandle() : 2;
-
- char** envPtr = 0;
- std::vector<char> envChars;
- std::vector<char*> envPtrs;
- if (!env.empty())
- {
- envChars = getEnvironmentVariablesBuffer(env);
- envPtrs.reserve(env.size() + 1);
- char* p = &envChars[0];
- while (*p)
- {
- envPtrs.push_back(p);
- while (*p) ++p;
- ++p;
- }
- envPtrs.push_back(0);
- envPtr = &envPtrs[0];
- }
-
- int pid = spawn(command.c_str(), 3, fdmap, &inherit, argv, envPtr);
- delete [] argv;
- if (pid == -1)
- throw SystemException("cannot spawn", command);
- if (inPipe) inPipe->close(Pipe::CLOSE_READ);
- if (outPipe) outPipe->close(Pipe::CLOSE_WRITE);
- if (errPipe) errPipe->close(Pipe::CLOSE_WRITE);
- return new ProcessHandleImpl(pid);
- }
- else
- {
- return launchByForkExecImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env);
- }
- #else
- return launchByForkExecImpl(command, args, initialDirectory, inPipe, outPipe, errPipe, env);
- #endif
- }
- ProcessHandleImpl* ProcessImpl::launchByForkExecImpl(const std::string& command, const ArgsImpl& args, const std::string& initialDirectory, Pipe* inPipe, Pipe* outPipe, Pipe* errPipe, const EnvImpl& env)
- {
- int pid = fork();
- if (pid < 0)
- {
- throw SystemException("Cannot fork process for", command);
- }
- else if (pid == 0)
- {
- if (!initialDirectory.empty())
- {
- if (chdir(initialDirectory.c_str()) != 0)
- {
- _exit(72);
- }
- }
- setEnvironmentVariables(env);
- // setup redirection
- if (inPipe)
- {
- dup2(inPipe->readHandle(), STDIN_FILENO);
- inPipe->close(Pipe::CLOSE_BOTH);
- }
- // outPipe and errPipe may be the same, so we dup first and close later
- if (outPipe) dup2(outPipe->writeHandle(), STDOUT_FILENO);
- if (errPipe) dup2(errPipe->writeHandle(), STDERR_FILENO);
- if (outPipe) outPipe->close(Pipe::CLOSE_BOTH);
- if (errPipe) errPipe->close(Pipe::CLOSE_BOTH);
- // close all open file descriptors other than stdin, stdout, stderr
- for (int i = 3; i < getdtablesize(); ++i)
- close(i);
- char** argv = new char*[args.size() + 2];
- int i = 0;
- argv[i++] = const_cast<char*>(command.c_str());
- for (ArgsImpl::const_iterator it = args.begin(); it != args.end(); ++it)
- argv[i++] = const_cast<char*>(it->c_str());
- argv[i] = NULL;
- execvp(command.c_str(), argv);
- _exit(72);
- }
- if (inPipe) inPipe->close(Pipe::CLOSE_READ);
- if (outPipe) outPipe->close(Pipe::CLOSE_WRITE);
- if (errPipe) errPipe->close(Pipe::CLOSE_WRITE);
- return new ProcessHandleImpl(pid);
- }
- void ProcessImpl::killImpl(const ProcessHandleImpl& handle)
- {
- killImpl(handle.id());
- }
- void ProcessImpl::killImpl(PIDImpl pid)
- {
- if (kill(pid, SIGKILL) != 0)
- {
- switch (errno)
- {
- case ESRCH:
- throw NotFoundException("cannot kill process");
- case EPERM:
- throw NoPermissionException("cannot kill process");
- default:
- throw SystemException("cannot kill process");
- }
- }
- }
- void ProcessImpl::requestTerminationImpl(PIDImpl pid)
- {
- if (kill(pid, SIGINT) != 0)
- {
- switch (errno)
- {
- case ESRCH:
- throw NotFoundException("cannot terminate process");
- case EPERM:
- throw NoPermissionException("cannot terminate process");
- default:
- throw SystemException("cannot terminate process");
- }
- }
- }
- } // namespace Poco
|