summaryrefslogtreecommitdiffstats
path: root/fastos
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2019-11-20 12:33:56 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2019-11-26 11:41:36 +0000
commit13956571986c12baf76848e1acafc31734fb5970 (patch)
tree4a12c0b955f24fa192d1a46bf04a117ac48d55c8 /fastos
parent4b8a65c48af0dd338bed5506a57bec6e002f58f3 (diff)
GC unused fastos code.
Diffstat (limited to 'fastos')
-rw-r--r--fastos/src/tests/processtest.cpp165
-rw-r--r--fastos/src/vespa/fastos/app.cpp46
-rw-r--r--fastos/src/vespa/fastos/app.h32
-rw-r--r--fastos/src/vespa/fastos/process.cpp3
-rw-r--r--fastos/src/vespa/fastos/process.h86
-rw-r--r--fastos/src/vespa/fastos/unix_app.cpp28
-rw-r--r--fastos/src/vespa/fastos/unix_app.h8
-rw-r--r--fastos/src/vespa/fastos/unix_ipc.cpp94
-rw-r--r--fastos/src/vespa/fastos/unix_ipc.h10
-rw-r--r--fastos/src/vespa/fastos/unix_process.cpp523
-rw-r--r--fastos/src/vespa/fastos/unix_process.h90
11 files changed, 135 insertions, 950 deletions
diff --git a/fastos/src/tests/processtest.cpp b/fastos/src/tests/processtest.cpp
index 846e1fbbc9f..a6729dbb783 100644
--- a/fastos/src/tests/processtest.cpp
+++ b/fastos/src/tests/processtest.cpp
@@ -2,7 +2,6 @@
#include "tests.h"
#include <vespa/fastos/process.h>
#include <vespa/fastos/timestamp.h>
-#include <vespa/fastos/file.h>
class MyListener : public FastOS_ProcessRedirectListener
{
@@ -47,10 +46,6 @@ public:
_receivedBytes += length;
if(data != nullptr)
{
-#if 0
- printf("[%s] received %u bytes of data:\n%s\n",
- _title, length, static_cast<const char *>(data));
-#endif
}
else
delete(this);
@@ -62,43 +57,6 @@ int MyListener::_successCount = 0;
int MyListener::_failCount = 0;
std::mutex *MyListener::_counterLock = nullptr;
-
-class ThreadRunJob : public FastOS_Runnable
-{
-private:
- ThreadRunJob(const ThreadRunJob&);
- ThreadRunJob& operator=(const ThreadRunJob&);
-
- const char *_processCmdLine;
- int _timeSpent;
-public:
- ThreadRunJob (const char *commandLine) :
- _processCmdLine(commandLine),
- _timeSpent(0)
- {
- }
-
- void Run (FastOS_ThreadInterface *, void *) override
- {
-
- fastos::StopWatch timer;
-
- FastOS_Process xproc(_processCmdLine);
- int returnCode = -1;
-
- if (xproc.Create()) {
- xproc.Wait(&returnCode);
- }
-
- _timeSpent = int(timer.elapsed().ms());
- }
-
- int GetTimeSpent ()
- {
- return _timeSpent;
- }
-};
-
class ProcessTest : public BaseTest
{
private:
@@ -123,33 +81,6 @@ public:
{
}
- void OnReceivedIPCMessage (const void *data, size_t length) override
- {
- // printf("Data: [%s]\n", static_cast<const char *>(data));
-
- if(length == 5) {
- const char *dataMatch = "IPCM";
- if(!_isChild)
- dataMatch = "IPCR";
- if(strcmp(static_cast<const char *>(data), dataMatch) != 0)
- Progress(false,
- "Received message did not match \"%s\" (%s)",
- dataMatch, static_cast<const char *>(data));
- }
- else
- Progress(false,
- "Received message was not 5 bytes long (%d)", length);
-
- _gotMessage = true;
-
- // We only have the counter lock if we are the parent process.
- if(_counterLock != nullptr)
- {
- std::lock_guard<std::mutex> guard(*_counterLock);
- _receivedMessages++;
- }
- }
-
void PollWaitTest ()
{
TestHeader("PollWait Test");
@@ -316,8 +247,7 @@ public:
break;
}
- Progress(MyListener::_allocCount == 0, "MyListener alloc count = %d",
- MyListener::_allocCount);
+ Progress(MyListener::_allocCount == 0, "MyListener alloc count = %d", MyListener::_allocCount);
if (!doKill && !waitKill) {
Progress(MyListener::_successCount == (2 * numLoops * numEachTime),
@@ -333,106 +263,13 @@ public:
PrintSeparator();
}
- int DoChildRole () {
- int rc = 124;
- int i;
- for(i=0; i<(20*10); i++) {
- if(_gotMessage)
- break;
-
- FastOS_Thread::Sleep(100);
- }
- if(i < (20*10))
- {
- // Send a message to the parent process.
- const char *messageString = "IPCR";
- SendParentIPCMessage(messageString, strlen(messageString) + 1);
- rc = 123;
- }
- else
- Progress(false, "Child timed out waiting for IPC message");
-
- return rc;
- }
-
- void IPCTest () {
- TestHeader ("IPC Test");
- const char *childProgram = _argv[1];
-
- _counterLock = new std::mutex;
-
- int i;
- for(i=0; i<30; i++)
- {
- FastOS_Process process(childProgram);
- if(process.Create())
- {
- // Send a message to the child process.
- const char *messageString = "IPCM";
- process.SendIPCMessage(messageString,
- strlen(messageString) + 1);
-
- // Wait for the process to end.
- int returnCode;
- if(process.Wait(&returnCode))
- {
- Progress(returnCode == 123,
- "Child exited with code: %d", returnCode);
- }
- else
- Progress(false, "process.Wait() failed");
- }
- else
- Progress(false, "process.Create() (%s) failed", childProgram);
- }
-
- Progress(_receivedMessages == i,
- "Received %d messages", _receivedMessages);
-
- delete _counterLock;
- _counterLock = nullptr;
-
- PrintSeparator();
- }
-
- void NoInheritTest ()
- {
- TestHeader("No Inherit Test");
-
- const char *filename = "process__test.tmp___";
-
- Progress(true, "Opening '%s' for writing...", filename);
- void *inheritData = FastOS_Process::PrefopenNoInherit();
- FILE *fp = fopen(filename, "w");
- int numProc = FastOS_Process::PostfopenNoInherit(inheritData);
- Progress(fp != nullptr, "Open file");
- if(fp != nullptr)
- {
- Progress(numProc > 0, "Number of files processed = %d\n",
- numProc);
- fclose(fp);
- }
- FastOS_File::Delete(filename);
-
- PrintSeparator();
- }
-
int Main () override
{
- // This process is started as either a parent or a child.
- // When the parent role is desired, a child program is supplied
- // as argv[1]
-
- if(_argc != 3)
- return DoChildRole();
-
_isChild = false;
printf("grep for the string '%s' to detect failures.\n\n", failString);
- NoInheritTest();
PollWaitTest();
- IPCTest();
ProcessTests(false, true, false);
ProcessTests(true, true, false);
ProcessTests(true, false, false);
diff --git a/fastos/src/vespa/fastos/app.cpp b/fastos/src/vespa/fastos/app.cpp
index b5c15aea831..3f51a0fb3d1 100644
--- a/fastos/src/vespa/fastos/app.cpp
+++ b/fastos/src/vespa/fastos/app.cpp
@@ -8,17 +8,12 @@
#include "app.h"
#include "file.h"
-
#include "process.h"
#include "thread.h"
#include <cstring>
#include <fcntl.h>
-FastOS_ApplicationInterface *FastOS_ProcessInterface::_app;
-FastOS_ThreadPool *FastOS_ProcessInterface::GetThreadPool ()
-{
- return _app->GetThreadPool();
-}
+FastOS_ApplicationInterface *FastOS_ProcessInterface::_app = nullptr;
FastOS_ThreadPool *FastOS_ApplicationInterface::GetThreadPool ()
{
@@ -29,7 +24,6 @@ FastOS_ApplicationInterface::FastOS_ApplicationInterface() :
_threadPool(nullptr),
_processList(nullptr),
_processListMutex(nullptr),
- _disableLeakReporting(false),
_argc(0),
_argv(nullptr)
{
@@ -48,21 +42,18 @@ FastOS_ApplicationInterface::FastOS_ApplicationInterface() :
#endif
}
-FastOS_ApplicationInterface::~FastOS_ApplicationInterface ()
-{
-}
+FastOS_ApplicationInterface::~FastOS_ApplicationInterface () = default;
bool FastOS_ApplicationInterface::Init ()
{
bool rc=false;
- if(PreThreadInit()) {
- if(FastOS_Thread::InitializeClass()) {
- if(FastOS_File::InitializeClass()) {
-
- _processListMutex = new std::mutex;
- _threadPool = new FastOS_ThreadPool(128 * 1024);
- rc = true;
+ if (PreThreadInit()) {
+ if (FastOS_Thread::InitializeClass()) {
+ if (FastOS_File::InitializeClass()) {
+ _processListMutex = new std::mutex;
+ _threadPool = new FastOS_ThreadPool(128 * 1024);
+ rc = true;
} else
fprintf(stderr, "FastOS_File class initialization failed.\n");
} else
@@ -77,9 +68,7 @@ bool FastOS_ApplicationInterface::Init ()
void FastOS_ApplicationInterface::Cleanup ()
{
if(_threadPool != nullptr) {
- // printf("Closing threadpool...\n");
_threadPool->Close();
- // printf("Deleting threadpool...\n");
delete _threadPool;
_threadPool = nullptr;
}
@@ -100,8 +89,7 @@ int FastOS_ApplicationInterface::Entry (int argc, char **argv)
_argc = argc;
_argv = argv;
- if(Init())
- {
+ if (Init()) {
rc = Main();
}
@@ -110,16 +98,8 @@ int FastOS_ApplicationInterface::Entry (int argc, char **argv)
return rc;
}
-void FastOS_ApplicationInterface::
-OnReceivedIPCMessage (const void *data, size_t length)
-{
- (void)data;
- (void)length;
- // Default dummy handler
-}
-
-void FastOS_ApplicationInterface::
-AddChildProcess (FastOS_ProcessInterface *node)
+void
+FastOS_ApplicationInterface::AddChildProcess (FastOS_ProcessInterface *node)
{
node->_prev = nullptr;
node->_next = _processList;
@@ -130,8 +110,8 @@ AddChildProcess (FastOS_ProcessInterface *node)
_processList = node;
}
-void FastOS_ApplicationInterface::
-RemoveChildProcess (FastOS_ProcessInterface *node)
+void
+FastOS_ApplicationInterface::RemoveChildProcess (FastOS_ProcessInterface *node)
{
if(node->_prev)
node->_prev->_next = node->_next;
diff --git a/fastos/src/vespa/fastos/app.h b/fastos/src/vespa/fastos/app.h
index 01938f93d35..4c07e680755 100644
--- a/fastos/src/vespa/fastos/app.h
+++ b/fastos/src/vespa/fastos/app.h
@@ -145,7 +145,6 @@ protected:
FastOS_ProcessInterface *_processList;
std::mutex *_processListMutex;
- bool _disableLeakReporting;
virtual bool PreThreadInit () { return true; }
public:
@@ -196,43 +195,12 @@ public:
*/
virtual void Cleanup ();
- /**
- * This method is invoked each time an IPC message is received.
- * The default implementation discards the message. Subclass this
- * method to process the data. You should assume that any
- * thread can invoke this method.
- * @param data Pointer to binary message data
- * @param length Length of message in bytes
- */
- virtual void OnReceivedIPCMessage (const void *data, size_t length);
-
- /**
- * Send an IPC message to the parent process. The method fails
- * if the parent process is not a FastOS process.
- * @param data Pointer to binary message data
- * @param length Length of message in bytes
- * @return Boolean success/failure
- */
- virtual bool SendParentIPCMessage (const void *data, size_t length) = 0;
-
void AddChildProcess (FastOS_ProcessInterface *node);
void RemoveChildProcess (FastOS_ProcessInterface *node);
std::unique_lock<std::mutex> getProcessGuard() { return std::unique_lock<std::mutex>(*_processListMutex); }
FastOS_ProcessInterface *GetProcessList () { return _processList; }
FastOS_ThreadPool *GetThreadPool ();
-
- /**
- * Disable reporting of memory- and other resource leaks.
- * If you want to disable leak reporting, call this method
- * before FastOS_Application::Entry() is invoked, as irreversible
- * actions which set up leak reporting can be performed at this point.
- * Leak reporting is either performed during FastOS_Application::Cleanup()
- * or when the application process terminates.
- * Leak reporting is currently only supported with the debug build
- * on Win32.
- */
- void DisableLeakReporting () { _disableLeakReporting = true; }
};
diff --git a/fastos/src/vespa/fastos/process.cpp b/fastos/src/vespa/fastos/process.cpp
index cac491de1e1..d005a73e9cd 100644
--- a/fastos/src/vespa/fastos/process.cpp
+++ b/fastos/src/vespa/fastos/process.cpp
@@ -1,14 +1,13 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "process.h"
-#include <string.h>
+#include <cstring>
FastOS_ProcessInterface::FastOS_ProcessInterface (const char *cmdLine,
bool pipeStdin,
FastOS_ProcessRedirectListener *stdoutListener,
FastOS_ProcessRedirectListener *stderrListener,
int bufferSize) :
- _extradoublehackforalignment(0.0),
_cmdLine(nullptr),
_pipeStdin(pipeStdin),
_stdoutListener(stdoutListener),
diff --git a/fastos/src/vespa/fastos/process.h b/fastos/src/vespa/fastos/process.h
index f0c1fcaa2a9..b564b9d12a9 100644
--- a/fastos/src/vespa/fastos/process.h
+++ b/fastos/src/vespa/fastos/process.h
@@ -51,9 +51,6 @@ private:
FastOS_ProcessInterface &operator=(const FastOS_ProcessInterface &);
protected:
- // Hack to achieve 64-bit alignment of
- // FastOS_ProcessInterface pointers (for Sparc)
- double _extradoublehackforalignment;
char *_cmdLine;
bool _pipeStdin;
@@ -62,40 +59,12 @@ protected:
FastOS_ProcessRedirectListener *_stderrListener;
int _bufferSize;
-
- FastOS_ThreadPool *GetThreadPool ();
-
public:
FastOS_ProcessInterface *_next, *_prev;
static FastOS_ApplicationInterface *_app;
- /**
- * Call this prior to opening files with fopen to avoid having
- * these file handles inherited by child processes. Remember
- * to call PostfopenNoInherit(x) after fopen. x is the return value
- * of PrefopenNoInherit.
- * @return Internal data needed to cancel object inheritance.
- */
- static void *PrefopenNoInherit (void) {return nullptr;}
-
- /**
- * Call this after opening files with fopen to avoid having
- * these files handles inherited by child processes. Remember
- * to call PrefopenNoInherit before fopen, and save its return
- * value to be used as a parameter of this method.
- * @param inheritData Data returned by PrefopenNoInherit
- * @return Number of files processed (should be >= 1)
- */
- static int PostfopenNoInherit (void *inheritData)
- {
- (void) inheritData;
- return 1;
- }
-
enum Constants
{
- NOTFOUND_EXITCODE = 65533, /* Process starter out of sync */
- DETACH_EXITCODE = 65534, /* Process detached */
KILL_EXITCODE = 65535, /* Process killed or failed */
CONSTEND
};
@@ -171,15 +140,6 @@ public:
virtual bool Kill () = 0;
/**
- * Special case kill used in conjunction with wrapper processes
- * that use process groups to kill all descendant processes.
- * On UNIX, this sends SIGTERM.
- * Only use this method with wrapper processes.
- * @return Boolean success / failure
- */
- virtual bool WrapperKill () = 0;
-
- /**
* Wait for the process to finish / terminate. This is called
* automatically by the destructor, but it is recommended that
* it is called as early as possible to free up resources.
@@ -210,58 +170,12 @@ public:
virtual bool PollWait (int *returnCode, bool *stillRunning) = 0;
/**
- * Detach a process, allowing it to exist beyond parent.
- * Only implemented on UNIX platforms.
- *
- * @return Boolean success / failure
- */
- virtual bool Detach(void) { return false; }
-
- /**
- * Check if child is a direct child or a proxied child.
- *
- * @return Boolean true = direct, false = proxied
- */
- virtual bool GetDirectChild(void) const { return true; }
-
- /**
- * Specify that child should be a direct child.
- *
- * @return Boolean success / failure
- */
- virtual bool SetDirectChild(void) { return true; }
-
- /**
- * Check if child inherits open file descriptors if it's a direct child.
- *
- * @return Boolean true = inherit, false = close
- */
- virtual bool GetKeepOpenFilesIfDirectChild(void) const { return false; }
-
- /**
- * Specify that child should inherit open file descriptors from parent
- * if it's a direct child. This reduces the cost of starting direct
- * children.
- *
- * @return Boolean true = inherit, false = close
- */
- virtual bool SetKeepOpenFilesIfDirectChild(void) { return false; }
-
- /**
* Get process identification number.
* @return Process id
*/
virtual unsigned int GetProcessId() = 0;
/**
- * Send IPC message to process.
- * @param data Pointer to data
- * @param length Length of data block in bytes
- * @return Boolean success / failure
- */
- virtual bool SendIPCMessage (const void *data, size_t length) = 0;
-
- /**
* Get command line string.
* @return Command line string
*/
diff --git a/fastos/src/vespa/fastos/unix_app.cpp b/fastos/src/vespa/fastos/unix_app.cpp
index d20b5b424b7..6129417cdb3 100644
--- a/fastos/src/vespa/fastos/unix_app.cpp
+++ b/fastos/src/vespa/fastos/unix_app.cpp
@@ -22,20 +22,13 @@ FastOS_UNIX_Application::FastOS_UNIX_Application ()
{
}
-FastOS_UNIX_Application::~FastOS_UNIX_Application()
-{
-}
+FastOS_UNIX_Application::~FastOS_UNIX_Application() = default;
extern "C"
{
extern char **environ;
};
-unsigned int FastOS_UNIX_Application::GetCurrentProcessId ()
-{
- return static_cast<unsigned int>(getpid());
-}
-
int
FastOS_UNIX_Application::GetOpt (const char *optionsString,
const char* &optionArgument,
@@ -69,25 +62,6 @@ FastOS_UNIX_Application::resetOptIndex(int optionIndex)
optind = optionIndex;
}
-bool FastOS_UNIX_Application::
-SendIPCMessage (FastOS_UNIX_Process *xproc, const void *buffer,
- int length)
-{
- if(_ipcHelper == nullptr)
- return false;
- return _ipcHelper->SendMessage(xproc, buffer, length);
-}
-
-
-bool FastOS_UNIX_Application::
-SendParentIPCMessage (const void *data, size_t length)
-{
- if(_ipcHelper == nullptr)
- return false;
- return _ipcHelper->SendMessage(nullptr, data, length);
-}
-
-
bool FastOS_UNIX_Application::PreThreadInit ()
{
bool rc = true;
diff --git a/fastos/src/vespa/fastos/unix_app.h b/fastos/src/vespa/fastos/unix_app.h
index ef48a7785d2..0693a3d06cc 100644
--- a/fastos/src/vespa/fastos/unix_app.h
+++ b/fastos/src/vespa/fastos/unix_app.h
@@ -26,7 +26,7 @@ private:
FastOS_UNIX_Application& operator=(const FastOS_UNIX_Application&);
FastOS_UNIX_ProcessStarter *_processStarter;
- FastOS_UNIX_IPCHelper *_ipcHelper;
+ FastOS_UNIX_IPCHelper *_ipcHelper;
protected:
bool PreThreadInit () override;
@@ -68,15 +68,11 @@ public:
/**
* Called before calling GetOpt() or GetOptLong() by sub-applications.
*/
- void resetOptIndex(int OptionIndex);
-
- static unsigned int GetCurrentProcessId ();
+ static void resetOptIndex(int OptionIndex);
FastOS_UNIX_ProcessStarter *GetProcessStarter ();
bool Init () override;
void Cleanup () override;
- bool SendParentIPCMessage (const void *data, size_t length) override;
- bool SendIPCMessage (FastOS_UNIX_Process *xproc, const void *buffer, int length);
void AddToIPCComm (FastOS_UNIX_Process *process);
void RemoveFromIPCComm (FastOS_UNIX_Process *process);
};
diff --git a/fastos/src/vespa/fastos/unix_ipc.cpp b/fastos/src/vespa/fastos/unix_ipc.cpp
index d1b7d199782..4c8e3102dbb 100644
--- a/fastos/src/vespa/fastos/unix_ipc.cpp
+++ b/fastos/src/vespa/fastos/unix_ipc.cpp
@@ -150,7 +150,7 @@ BuildPollCheck(bool isRead, int filedes,
void FastOS_UNIX_IPCHelper::
-PerformAsyncIO(void)
+PerformAsyncIO()
{
FastOS_ProcessInterface *node;
for(node = _app->GetProcessList(); node != nullptr; node = node->_next)
@@ -172,7 +172,7 @@ PerformAsyncIO(void)
}
void FastOS_UNIX_IPCHelper::
-PerformAsyncIPCIO(void)
+PerformAsyncIPCIO()
{
FastOS_UNIX_Process::DescriptorHandle &desc = _appParentIPCDescriptor;
if (desc._canRead)
@@ -183,7 +183,7 @@ PerformAsyncIPCIO(void)
void FastOS_UNIX_IPCHelper::
-BuildPollChecks(void)
+BuildPollChecks()
{
FastOS_ProcessInterface *node;
for(node = _app->GetProcessList(); node != nullptr; node = node->_next)
@@ -389,7 +389,7 @@ SavePollArray(pollfd *fds, unsigned int nfds)
void FastOS_UNIX_IPCHelper::
-RemoveClosingProcesses(void)
+RemoveClosingProcesses()
{
// We assume that not updating maxFD isn't harmless.
@@ -478,13 +478,9 @@ Run(FastOS_ThreadInterface *thisThread, void *arg)
for(node = _app->GetProcessList(); node != nullptr; node = node->_next)
{
FastOS_UNIX_Process *xproc = static_cast<FastOS_UNIX_Process *>(node);
- FastOS_UNIX_Process::DescriptorHandle &desc =
- xproc->GetDescriptorHandle(FastOS_UNIX_Process::TYPE_IPC);
- DeliverMessages(desc._readBuffer.get());
PipeData(xproc, FastOS_UNIX_Process::TYPE_STDOUT);
PipeData(xproc, FastOS_UNIX_Process::TYPE_STDERR);
}
- DeliverMessages(_appParentIPCDescriptor._readBuffer.get());
// Setup file descriptor sets for the next select() call
BuildPollChecks();
@@ -567,41 +563,10 @@ Run(FastOS_ThreadInterface *thisThread, void *arg)
free(fds);
delete this;
- // printf("IPCHelper exits\n");
-}
-
-
-bool FastOS_UNIX_IPCHelper::
-SendMessage (FastOS_UNIX_Process *xproc, const void *buffer,
- int length)
-{
- bool rc = false;
-
- FastOS_RingBuffer *ipcBuffer;
-
- FastOS_UNIX_Process::DescriptorHandle &desc =
- xproc != nullptr ?
- xproc->GetDescriptorHandle(FastOS_UNIX_Process::TYPE_IPC) :
- _appParentIPCDescriptor;
- ipcBuffer = desc._writeBuffer.get();
-
- if(ipcBuffer != nullptr) {
- auto ipcBufferGuard = ipcBuffer->getGuard();
-
- if(ipcBuffer->GetWriteSpace() >= int((length + sizeof(int)))) {
- memcpy(ipcBuffer->GetWritePtr(), &length, sizeof(int));
- ipcBuffer->Produce(sizeof(int));
- memcpy(ipcBuffer->GetWritePtr(), buffer, length);
- ipcBuffer->Produce(length);
-
- NotifyProcessListChange();
- rc = true;
- }
- }
- return rc;
}
-void FastOS_UNIX_IPCHelper::NotifyProcessListChange ()
+void
+FastOS_UNIX_IPCHelper::NotifyProcessListChange ()
{
char dummy = 'x';
ssize_t nbwtp = write(_wakeupPipe[1], &dummy, 1);
@@ -610,25 +575,24 @@ void FastOS_UNIX_IPCHelper::NotifyProcessListChange ()
}
}
-void FastOS_UNIX_IPCHelper::Exit ()
+void
+FastOS_UNIX_IPCHelper::Exit ()
{
std::lock_guard<std::mutex> guard(_lock);
_exitFlag = true;
NotifyProcessListChange();
}
-void FastOS_UNIX_IPCHelper::AddProcess (FastOS_UNIX_Process *xproc)
+void
+FastOS_UNIX_IPCHelper::AddProcess (FastOS_UNIX_Process *xproc)
{
bool newStream = false;
for(int type=0; type < int(FastOS_UNIX_Process::TYPE_READCOUNT); type++)
{
- FastOS_UNIX_Process::DescriptorType type_ =
- FastOS_UNIX_Process::DescriptorType(type);
- FastOS_UNIX_Process::DescriptorHandle &desc =
- xproc->GetDescriptorHandle(type_);
+ FastOS_UNIX_Process::DescriptorType type_ = FastOS_UNIX_Process::DescriptorType(type);
+ FastOS_UNIX_Process::DescriptorHandle &desc = xproc->GetDescriptorHandle(type_);
- if (desc._fd != -1)
- {
+ if (desc._fd != -1) {
newStream = true;
SetBlocking(desc._fd, false);
}
@@ -637,7 +601,8 @@ void FastOS_UNIX_IPCHelper::AddProcess (FastOS_UNIX_Process *xproc)
NotifyProcessListChange();
}
-void FastOS_UNIX_IPCHelper::RemoveProcess (FastOS_UNIX_Process *xproc)
+void
+FastOS_UNIX_IPCHelper::RemoveProcess (FastOS_UNIX_Process *xproc)
{
auto closePromise = std::make_unique<std::promise<void>>();
auto closeFuture = closePromise->get_future();
@@ -646,33 +611,8 @@ void FastOS_UNIX_IPCHelper::RemoveProcess (FastOS_UNIX_Process *xproc)
closeFuture.wait();
}
-void FastOS_UNIX_IPCHelper::DeliverMessages (FastOS_RingBuffer *buffer)
-{
- if(buffer == nullptr)
- return;
-
- auto bufferGuard = buffer->getGuard();
-
- unsigned int readSpace;
- while((readSpace = buffer->GetReadSpace()) > sizeof(int))
- {
- FastOS_RingBufferData *bufferData = buffer->GetData();
-
- if((readSpace - sizeof(int)) >= bufferData->_messageSize)
- {
- _app->OnReceivedIPCMessage(&bufferData->_buffer[0] + sizeof(int),
- bufferData->_messageSize);
- buffer->Consume(sizeof(int) + bufferData->_messageSize);
- buffer->RepositionDataAt0();
- }
- else
- break;
- }
-}
-
-void FastOS_UNIX_IPCHelper::
-PipeData (FastOS_UNIX_Process *process,
- FastOS_UNIX_Process::DescriptorType type)
+void
+FastOS_UNIX_IPCHelper::PipeData(FastOS_UNIX_Process *process, FastOS_UNIX_Process::DescriptorType type)
{
FastOS_UNIX_Process::DescriptorHandle &desc = process->GetDescriptorHandle(type);
FastOS_RingBuffer *buffer = desc._readBuffer.get();
diff --git a/fastos/src/vespa/fastos/unix_ipc.h b/fastos/src/vespa/fastos/unix_ipc.h
index 218096e2145..62272856295 100644
--- a/fastos/src/vespa/fastos/unix_ipc.h
+++ b/fastos/src/vespa/fastos/unix_ipc.h
@@ -27,18 +27,16 @@ protected:
void BuildPollCheck (bool isRead, int filedes, FastOS_RingBuffer *buffer, bool *check);
void BuildPollArray(pollfd **fds, unsigned int *nfds, unsigned int *allocnfds);
bool SavePollArray(pollfd *fds, unsigned int nfds);
- void PerformAsyncIO (void);
- void PerformAsyncIPCIO (void);
- void BuildPollChecks(void);
- void DeliverMessages (FastOS_RingBuffer *buffer);
+ void PerformAsyncIO ();
+ void PerformAsyncIPCIO ();
+ void BuildPollChecks();
void PipeData (FastOS_UNIX_Process *process, FastOS_UNIX_Process::DescriptorType type);
- void RemoveClosingProcesses(void);
+ void RemoveClosingProcesses();
public:
FastOS_UNIX_IPCHelper (FastOS_ApplicationInterface *app, int appDescriptor);
~FastOS_UNIX_IPCHelper ();
void Run (FastOS_ThreadInterface *thisThread, void *arg) override;
- bool SendMessage (FastOS_UNIX_Process *xproc, const void *buffer, int length);
void NotifyProcessListChange ();
void AddProcess (FastOS_UNIX_Process *xproc);
void RemoveProcess (FastOS_UNIX_Process *xproc);
diff --git a/fastos/src/vespa/fastos/unix_process.cpp b/fastos/src/vespa/fastos/unix_process.cpp
index 5762d33e08b..86d285059b8 100644
--- a/fastos/src/vespa/fastos/unix_process.cpp
+++ b/fastos/src/vespa/fastos/unix_process.cpp
@@ -40,7 +40,7 @@ extern char **environ;
#endif
-static pid_t safe_fork (void)
+static pid_t safe_fork ()
{
pid_t pid;
int retry = 1;
@@ -96,62 +96,62 @@ private:
void CloseDescriptor(int fd);
void CloseAndResetDescriptor(int *fd);
- void CloseDescriptors(void);
+ void CloseDescriptors();
public:
void SetRunDir(const char * runDir) { _runDir = runDir; }
- int GetIPCDescriptor(void) const { return _ipcSockPair[0]; }
- int GetStdinDescriptor(void) const { return _stdinDes[1]; }
- int GetStdoutDescriptor(void) const { return _stdoutDes[0]; }
- int GetStderrDescriptor(void) const { return _stderrDes[0]; }
- int HandoverIPCDescriptor(void) {
+ int GetIPCDescriptor() const { return _ipcSockPair[0]; }
+ int GetStdinDescriptor() const { return _stdinDes[1]; }
+ int GetStdoutDescriptor() const { return _stdoutDes[0]; }
+ int GetStderrDescriptor() const { return _stderrDes[0]; }
+ int HandoverIPCDescriptor() {
int ret = _ipcSockPair[0];
_ipcSockPair[0] = -1;
return ret;
}
- int HandoverStdinDescriptor(void) {
+ int HandoverStdinDescriptor() {
int ret = _stdinDes[1];
_stdinDes[1] = -1;
return ret;
}
- int HandoverStdoutDescriptor(void) {
+ int HandoverStdoutDescriptor() {
int ret = _stdoutDes[0];
_stdoutDes[0] = -1;
return ret;
}
- int HandoverStderrDescriptor(void) {
+ int HandoverStderrDescriptor() {
int ret = _stderrDes[0];
_stderrDes[0] = -1;
return ret;
}
- void CloseIPCDescriptor(void);
- void CloseStdinDescriptor(void);
- void CloseStdoutDescriptor(void);
- void CloseStderrDescriptor(void);
+ void CloseIPCDescriptor();
+ void CloseStdinDescriptor();
+ void CloseStdoutDescriptor();
+ void CloseStderrDescriptor();
FastOS_UNIX_RealProcess *_prev, *_next;
FastOS_UNIX_RealProcess (int streamMask);
- ~FastOS_UNIX_RealProcess(void);
- pid_t GetProcessID(void) const { return _pid; }
+ ~FastOS_UNIX_RealProcess();
+ pid_t GetProcessID() const { return _pid; }
- bool IsStdinPiped(void) const {
+ bool IsStdinPiped() const {
return (_streamMask & STREAM_STDIN ) != 0;
}
- bool IsStdoutPiped(void) const {
+ bool IsStdoutPiped() const {
return (_streamMask & STREAM_STDOUT) != 0;
}
- bool IsStderrPiped(void) const {
+ bool IsStderrPiped() const {
return (_streamMask & STREAM_STDERR) != 0;
}
- bool IsUsingShell(void) const {
+ bool IsUsingShell() const {
return (_streamMask & EXEC_SHELL) != 0;
}
@@ -190,9 +190,9 @@ public:
FastOS_UNIX_Process *process,
FastOS_UNIX_ProcessStarter *processStarter);
- bool Setup(void);
- pid_t GetProcessId(void) const { return _pid; }
- void SetTerse(void) { _terse = true; }
+ bool Setup();
+ pid_t GetProcessId() const { return _pid; }
+ void SetTerse() { _terse = true; }
ssize_t HandshakeRead(void *buf, size_t len);
void HandshakeWrite(int val);
};
@@ -216,7 +216,7 @@ FastOS_UNIX_RealProcess::CloseAndResetDescriptor(int *fd)
void
-FastOS_UNIX_RealProcess::CloseDescriptors(void)
+FastOS_UNIX_RealProcess::CloseDescriptors()
{
CloseAndResetDescriptor(&_stdinDes[0]);
CloseAndResetDescriptor(&_stdinDes[1]);
@@ -232,28 +232,28 @@ FastOS_UNIX_RealProcess::CloseDescriptors(void)
void
-FastOS_UNIX_RealProcess::CloseIPCDescriptor(void)
+FastOS_UNIX_RealProcess::CloseIPCDescriptor()
{
CloseAndResetDescriptor(&_ipcSockPair[0]);
}
void
-FastOS_UNIX_RealProcess::CloseStdinDescriptor(void)
+FastOS_UNIX_RealProcess::CloseStdinDescriptor()
{
CloseAndResetDescriptor(&_stdinDes[1]);
}
void
-FastOS_UNIX_RealProcess::CloseStdoutDescriptor(void)
+FastOS_UNIX_RealProcess::CloseStdoutDescriptor()
{
CloseAndResetDescriptor(&_stdoutDes[0]);
}
void
-FastOS_UNIX_RealProcess::CloseStderrDescriptor(void)
+FastOS_UNIX_RealProcess::CloseStderrDescriptor()
{
CloseAndResetDescriptor(&_stderrDes[0]);
}
@@ -280,7 +280,7 @@ FastOS_UNIX_RealProcess::FastOS_UNIX_RealProcess(int streamMask)
}
-FastOS_UNIX_RealProcess::~FastOS_UNIX_RealProcess(void)
+FastOS_UNIX_RealProcess::~FastOS_UNIX_RealProcess()
{
CloseDescriptors();
}
@@ -546,19 +546,14 @@ ForkAndExec(const char *command,
CloseDescriptor(_handshakeDes[0]);
_handshakeDes[0] = -1;
if (process != nullptr) {
- if (!process->GetKeepOpenFilesIfDirectChild()) {
- int fdlimit = sysconf(_SC_OPEN_MAX);
- // Close everything else
- // printf("fdlimit = %d\n", fdlimit);
- for(int fd = STDERR_FILENO + 1; fd < fdlimit; fd++)
- {
- if (fd != _ipcSockPair[1] &&
- fd != _handshakeDes[1])
- CloseDescriptor(fd);
- }
- } else {
- // Close only other endpoints of pipes.
- process->CloseDescriptorsDirectChild();
+ int fdlimit = sysconf(_SC_OPEN_MAX);
+ // Close everything else
+ // printf("fdlimit = %d\n", fdlimit);
+ for(int fd = STDERR_FILENO + 1; fd < fdlimit; fd++)
+ {
+ if (fd != _ipcSockPair[1] &&
+ fd != _handshakeDes[1])
+ CloseDescriptor(fd);
}
} else {
processStarter->CloseProxiedChildDescs();
@@ -766,7 +761,7 @@ FastOS_UNIX_RealProcess::HandshakeRead(void *buf, size_t len)
bool
-FastOS_UNIX_RealProcess::Setup(void)
+FastOS_UNIX_RealProcess::Setup()
{
bool rc = true;
@@ -789,8 +784,6 @@ FastOS_UNIX_Process (const char *cmdLine, bool pipeStdin,
stderrListener, bufferSize),
_pid(0),
_died(false),
- _directChild(true),
- _keepOpenFilesIfDirectChild(false),
_returnCode(-1),
_descriptor(),
_runDir(),
@@ -842,27 +835,6 @@ FastOS_UNIX_Process::~FastOS_UNIX_Process ()
CloseListener(TYPE_STDERR);
}
-
-void
-FastOS_UNIX_Process::SetRunDir(const char *runDir)
-{
- _runDir = runDir ? runDir : "";
-}
-
-
-void
-FastOS_UNIX_Process::SetStdoutRedirName(const char *stdoutRedirName)
-{
- _stdoutRedirName = stdoutRedirName ? stdoutRedirName : "";
-}
-
-
-void
-FastOS_UNIX_Process::SetStderrRedirName(const char *stderrRedirName) {
- _stderrRedirName = stderrRedirName ? stderrRedirName : "";
-}
-
-
bool FastOS_UNIX_Process::CreateInternal (bool useShell)
{
return GetProcessStarter()->CreateProcess(this, useShell,
@@ -918,11 +890,6 @@ bool FastOS_UNIX_Process::Kill ()
return Signal(SIGKILL);
}
-bool FastOS_UNIX_Process::WrapperKill ()
-{
- return Signal(SIGTERM);
-}
-
bool FastOS_UNIX_Process::InternalWait (int *returnCode,
int timeOutSeconds,
bool *pollStillRunning)
@@ -949,23 +916,6 @@ bool FastOS_UNIX_Process::PollWait (int *returnCode, bool *stillRunning)
return InternalWait(returnCode, -1, stillRunning);
}
-
-bool FastOS_UNIX_Process::Detach(void)
-{
- return GetProcessStarter()->Detach(this);
-}
-
-
-bool FastOS_UNIX_Process::SendIPCMessage (const void *data,
- size_t length)
-{
- if (_descriptor[TYPE_IPC]._fd != -1) {
- return static_cast<FastOS_UNIX_Application *>
- (_app)->SendIPCMessage(this, data, length);
- }
- return false;
-}
-
int FastOS_UNIX_Process::BuildStreamMask (bool useShell)
{
int streamMask = 0;
@@ -1187,52 +1137,6 @@ void FastOS_UNIX_ProcessStarter::StarterDoWait ()
}
}
-void FastOS_UNIX_ProcessStarter::StarterDoDetachProcess ()
-{
- // printf("DETACH A PROCESS\n");
-
- pid_t dpid;
- pid_t pid;
- int status;
-
- int returnCode;
-
- ReadBytes(_starterSocket, &dpid, sizeof(pid_t));
-
- bool foundProcess = false;
-
- pid = waitpid(dpid, &status, WNOHANG);
-
- FastOS_UNIX_RealProcess *process, *next;
- for(process = FastOS_UNIX_ProcessStarter::_processList;
- process != nullptr; process = next) {
-
- // Need to do this here since we are deleting entries
- next = process->_next;
-
- if (process->GetProcessId() == dpid) {
- foundProcess = true;
- _hasDetachedProcess = true;
- RemoveChildProcess(process);
- delete process;
- break;
- }
- }
-
- if (foundProcess) {
- if (pid == dpid) {
- if (WIFEXITED(status))
- returnCode = WEXITSTATUS(status);
- else
- returnCode = -1;
- } else
- returnCode = FastOS_ProcessInterface::DETACH_EXITCODE;
- } else
- returnCode = FastOS_ProcessInterface::NOTFOUND_EXITCODE;
-
- WriteBytes(_starterSocket, &returnCode, sizeof(int));
-}
-
void FastOS_UNIX_ProcessStarter::StarterDoCreateProcess ()
{
int stringLength = ReadInt(_starterSocket);
@@ -1376,7 +1280,6 @@ void FastOS_UNIX_ProcessStarter::Run ()
switch(command)
{
case CODE_WAIT: StarterDoWait(); break;
- case CODE_DETACH: StarterDoDetachProcess(); break;
case CODE_NEWPROCESS: StarterDoCreateProcess(); break;
case CODE_EXIT: _exit(2);
}
@@ -1474,94 +1377,6 @@ void FastOS_UNIX_ProcessStarter::Stop ()
waitpid(_pid, &result, 0);
}
-int FastOS_UNIX_ProcessStarter::ReadFileDescriptor ()
-{
- struct msghdr msg;
- struct iovec iov;
-
-#ifndef FASTOS_HAVE_ACCRIGHTSLEN
- union
- {
- struct cmsghdr cm;
- char control[CMSG_SPACE(sizeof(int))];
- } control_un;
- struct cmsghdr *cmptr;
-
- msg.msg_control = control_un.control;
- msg.msg_controllen = sizeof(control_un.control);
-#else
- int newfd;
- msg.msg_accrights = static_cast<caddr_t>(&newfd);
- msg.msg_accrightslen = sizeof(int);
-#endif
-
- msg.msg_name = nullptr;
- msg.msg_namelen = 0;
-
- char dummyData = '\0';
- iov.iov_base = &dummyData;
- iov.iov_len = 1;
- msg.msg_iov = &iov;
- msg.msg_iovlen = 1;
-
- int recvmsgrc = recvmsg(_mainSocketDescr, &msg, 0);
-
- if (recvmsgrc <= 0) {
- perror("recvmsg");
- return recvmsgrc;
- }
- // printf("recvmsgrc = %d\n", recvmsgrc);
-
-#ifndef FASTOS_HAVE_ACCRIGHTSLEN
- if ((cmptr = CMSG_FIRSTHDR(&msg)) != nullptr &&
- cmptr->cmsg_len == CMSG_LEN(sizeof(int)))
- {
- if (cmptr->cmsg_level != SOL_SOCKET)
- perror("FastOS_UNIX_ProcessStarter: control level != SOL_SOCKET");
- if (cmptr->cmsg_type != SCM_RIGHTS)
- perror("FastOS_UNIX_ProcessStarter: control type != SCM_RIGHTS");
- int fileDescriptor;
- memcpy(&fileDescriptor, CMSG_DATA(cmptr), sizeof(int));
- return fileDescriptor;
- }
- else
- return -1;
-#else
- if (msg.msg_accrightslen == sizeof(int))
- return newfd;
- else
- return -1;
-#endif
-}
-
-bool FastOS_UNIX_ProcessStarter::
-ReceiveFileDescriptor (bool use, FastOS_UNIX_Process::DescriptorType type,
- FastOS_UNIX_Process *xproc)
-{
- bool rc = false;
- if (!use)
- rc = true;
- else {
- int sendFileDescResult = ReadInt(_mainSocket);
-
- if (sendFileDescResult == CODE_SUCCESS) {
- int fd = ReadFileDescriptor();
-
- // printf("Got file descriptor %d, type %d\n", fd, type);
-
- if (fd > 0) {
- xproc->SetDescriptor(type, fd);
- rc = true;
- WriteInt(_mainSocket, CODE_SUCCESS);
- }
- else
- WriteInt(_mainSocket, CODE_FAILURE);
- }
- }
-
- return rc;
-}
-
char ** FastOS_UNIX_ProcessStarter::ReceiveEnvironmentVariables ()
{
int numEnvVars = ReadInt(_starterSocket);
@@ -1591,42 +1406,8 @@ char ** FastOS_UNIX_ProcessStarter::ReceiveEnvironmentVariables ()
return myEnvironment;
}
-
-void FastOS_UNIX_ProcessStarter::SendEnvironmentVariables ()
-{
- int numEnvVars = 0;
- char **pe = environ;
-
- while(*pe++ != nullptr)
- numEnvVars++;
-
- WriteInt(_mainSocket, numEnvVars);
-
- // In case someone deletes environment variables at the same time
- bool gotNull = false;
-
- pe = environ;
- for(int i=0; i<numEnvVars; i++)
- {
- const char *envString = "";
- if (!gotNull) {
- envString = *pe++;
- if (envString == nullptr) {
- envString = "";
- gotNull = true;
- }
- }
-
- int envBytes = strlen(envString) + 1;
- WriteInt(_mainSocket, envBytes);
- WriteBytes(_mainSocket, envString, envBytes);
- }
-}
-
-
void
-FastOS_UNIX_ProcessStarter::
-CloseProxiedChildDescs(void)
+FastOS_UNIX_ProcessStarter::CloseProxiedChildDescs()
{
if (_starterSocket >= 0) close(_starterSocket);
if (_starterSocketDescr >= 0) close(_starterSocketDescr);
@@ -1634,13 +1415,8 @@ CloseProxiedChildDescs(void)
void
-FastOS_UNIX_ProcessStarter::
-CloseProxyDescs(int stdinPipedDes,
- int stdoutPipedDes,
- int stderrPipedDes,
- int ipcDes,
- int handshakeDes0,
- int handshakeDes1)
+FastOS_UNIX_ProcessStarter::CloseProxyDescs(int stdinPipedDes, int stdoutPipedDes, int stderrPipedDes,
+ int ipcDes, int handshakeDes0, int handshakeDes1)
{
return;
if (_closedProxyProcessFiles)
@@ -1662,8 +1438,7 @@ CloseProxyDescs(int stdinPipedDes,
}
char **
-FastOS_UNIX_ProcessStarter::
-CopyEnvironmentVariables(void)
+FastOS_UNIX_ProcessStarter::CopyEnvironmentVariables()
{
char **env = environ;
while (*env != nullptr)
@@ -1690,8 +1465,7 @@ CopyEnvironmentVariables(void)
void
-FastOS_UNIX_ProcessStarter::
-FreeEnvironmentVariables(char **env)
+FastOS_UNIX_ProcessStarter::FreeEnvironmentVariables(char **env)
{
char **p = env;
while (*p != nullptr) {
@@ -1716,135 +1490,52 @@ CreateProcess (FastOS_UNIX_Process *process,
auto guard = _app->getProcessGuard();
- if (process->GetDirectChild()) {
- _hasDirectChildren = true;
- FastOS_UNIX_RealProcess *rprocess =
- new FastOS_UNIX_RealProcess(process->BuildStreamMask(useShell));
- const char *runDir = process->GetRunDir();
- if (runDir != nullptr) {
- rprocess->SetRunDir(runDir); // Handover
- }
- const char *stdoutRedirName = process->GetStdoutRedirName();
- if (stdoutRedirName != nullptr) {
- rprocess->SetStdoutRedirName(stdoutRedirName);
- }
- const char *stderrRedirName = process->GetStderrRedirName();
- if (stderrRedirName != nullptr) {
- rprocess->SetStderrRedirName(stderrRedirName);
- }
- char **env = CopyEnvironmentVariables();
- rprocess->SetTerse();
- rprocess->Setup();
- if (!useShell)
- process->SetDescriptor(FastOS_UNIX_Process::TYPE_IPC,
- rprocess->HandoverIPCDescriptor());
- if (pipeStdin)
- process->SetDescriptor(FastOS_UNIX_Process::TYPE_STDIN,
- rprocess->HandoverStdinDescriptor());
- if (pipeStdout)
- process->SetDescriptor(FastOS_UNIX_Process::TYPE_STDOUT,
- rprocess->HandoverStdoutDescriptor());
- if (pipeStderr)
- process->SetDescriptor(FastOS_UNIX_Process::TYPE_STDERR,
- rprocess->HandoverStderrDescriptor());
- pid_t processId = -1;
- if (rprocess->ForkAndExec(cmdLine, env, process, this)) {
- processId = rprocess->GetProcessID();
- }
- if (processId != -1) {
- process->SetProcessId(static_cast<unsigned int>(processId));
- if (!useShell || pipeStdout || pipeStderr)
- static_cast<FastOS_UNIX_Application *>
- (_app)->AddToIPCComm(process);
- rc = true;
- } else {
- fprintf(stderr,
- "Forkandexec %s failed\n",
- cmdLine);
- }
- guard.unlock();
- delete rprocess;
- FreeEnvironmentVariables(env);
- return rc;
- }
-
- _hasProxiedChildren = true;
- int cmdLineLength = strlen(cmdLine) + 1;
- WriteInt(_mainSocket, FastOS_UNIX_ProcessStarter::CODE_NEWPROCESS);
- WriteInt(_mainSocket, cmdLineLength);
- WriteBytes(_mainSocket, cmdLine, cmdLineLength);
- WriteInt(_mainSocket, process->BuildStreamMask(useShell));
-
- SendEnvironmentVariables();
-
+ _hasDirectChildren = true;
+ FastOS_UNIX_RealProcess *rprocess = new FastOS_UNIX_RealProcess(process->BuildStreamMask(useShell));
const char *runDir = process->GetRunDir();
if (runDir != nullptr) {
- int runDirLength = strlen(runDir) + 1;
- WriteInt(_mainSocket, runDirLength);
- WriteBytes(_mainSocket, runDir, runDirLength);
- } else
- WriteInt(_mainSocket, 0);
-
+ rprocess->SetRunDir(runDir); // Handover
+ }
const char *stdoutRedirName = process->GetStdoutRedirName();
if (stdoutRedirName != nullptr) {
- int stdoutRedirNameLength = strlen(stdoutRedirName) + 1;
- WriteInt(_mainSocket, stdoutRedirNameLength);
- WriteBytes(_mainSocket, stdoutRedirName, stdoutRedirNameLength);
- } else
- WriteInt(_mainSocket, 0);
-
+ rprocess->SetStdoutRedirName(stdoutRedirName);
+ }
const char *stderrRedirName = process->GetStderrRedirName();
if (stderrRedirName != nullptr) {
- int stderrRedirNameLength = strlen(stderrRedirName) + 1;
- WriteInt(_mainSocket, stderrRedirNameLength);
- WriteBytes(_mainSocket, stderrRedirName, stderrRedirNameLength);
- } else
- WriteInt(_mainSocket, 0);
-
-
- // Check return code of process Setup
- if (ReadInt(_mainSocket) == CODE_SUCCESS) {
- if (ReceiveFileDescriptor(!useShell,
- FastOS_UNIX_Process::TYPE_IPC,
- process))
- {
- if (ReceiveFileDescriptor(pipeStdin,
- FastOS_UNIX_Process::TYPE_STDIN,
- process))
- {
- if (ReceiveFileDescriptor(pipeStdout,
- FastOS_UNIX_Process::TYPE_STDOUT,
- process))
- {
- if (ReceiveFileDescriptor(pipeStderr,
- FastOS_UNIX_Process::TYPE_STDERR,
- process))
- {
- pid_t processId;
- ReadBytes(_mainSocket, &processId, sizeof(pid_t));
- // printf("Received pid = %d\n", int(processId));
-
- if (processId != -1) {
- process->
- SetProcessId(static_cast<unsigned int>(processId));
-
- if (!useShell || pipeStdout || pipeStderr)
- static_cast<FastOS_UNIX_Application *>
- (_app)->AddToIPCComm(process);
-
- rc = true;
- }
- }
- }
- }
- }
+ rprocess->SetStderrRedirName(stderrRedirName);
+ }
+ char **env = CopyEnvironmentVariables();
+ rprocess->SetTerse();
+ rprocess->Setup();
+ if (!useShell)
+ process->SetDescriptor(FastOS_UNIX_Process::TYPE_IPC, rprocess->HandoverIPCDescriptor());
+ if (pipeStdin)
+ process->SetDescriptor(FastOS_UNIX_Process::TYPE_STDIN, rprocess->HandoverStdinDescriptor());
+ if (pipeStdout)
+ process->SetDescriptor(FastOS_UNIX_Process::TYPE_STDOUT, rprocess->HandoverStdoutDescriptor());
+ if (pipeStderr)
+ process->SetDescriptor(FastOS_UNIX_Process::TYPE_STDERR, rprocess->HandoverStderrDescriptor());
+ pid_t processId = -1;
+ if (rprocess->ForkAndExec(cmdLine, env, process, this)) {
+ processId = rprocess->GetProcessID();
+ }
+ if (processId != -1) {
+ process->SetProcessId(static_cast<unsigned int>(processId));
+ if (!useShell || pipeStdout || pipeStderr)
+ static_cast<FastOS_UNIX_Application *>(_app)->AddToIPCComm(process);
+ rc = true;
+ } else {
+ fprintf(stderr, "Forkandexec %s failed\n", cmdLine);
}
+ guard.unlock();
+ delete rprocess;
+ FreeEnvironmentVariables(env);
return rc;
}
void
-FastOS_UNIX_ProcessStarter::PollReapDirectChildren(void)
+FastOS_UNIX_ProcessStarter::PollReapDirectChildren()
{
int status;
pid_t pid;
@@ -1858,8 +1549,7 @@ FastOS_UNIX_ProcessStarter::PollReapDirectChildren(void)
for(node = _app->GetProcessList();
node != nullptr; node = node->_next)
{
- FastOS_UNIX_Process *xproc =
- static_cast<FastOS_UNIX_Process *>(node);
+ FastOS_UNIX_Process *xproc = static_cast<FastOS_UNIX_Process *>(node);
if (xproc->GetProcessId() == static_cast<unsigned int>(pid))
xproc->DeathNotification(normalizedWaitStatus(status));
@@ -1869,7 +1559,7 @@ FastOS_UNIX_ProcessStarter::PollReapDirectChildren(void)
void
-FastOS_UNIX_ProcessStarter::PollReapProxiedChildren(void)
+FastOS_UNIX_ProcessStarter::PollReapProxiedChildren()
{
// Ask our process starter to report dead processes
WriteInt(_mainSocket, FastOS_UNIX_ProcessStarter::CODE_WAIT);
@@ -1888,8 +1578,7 @@ FastOS_UNIX_ProcessStarter::PollReapProxiedChildren(void)
FastOS_ProcessInterface *node;
for(node = _app->GetProcessList(); node != nullptr; node = node->_next)
{
- FastOS_UNIX_Process *xproc =
- static_cast<FastOS_UNIX_Process *>(node);
+ FastOS_UNIX_Process *xproc = static_cast<FastOS_UNIX_Process *>(node);
if (xproc->GetProcessId() == static_cast<unsigned int>(deadProcess))
{
@@ -1929,9 +1618,6 @@ FastOS_UNIX_ProcessStarter::Wait(FastOS_UNIX_Process *process,
break;
}
- // printf("wasn't dead yet (%d), sleeping\n",
- // process->GetProcessId());
-
if (pollStillRunning != nullptr)
break;
@@ -1949,45 +1635,6 @@ FastOS_UNIX_ProcessStarter::Wait(FastOS_UNIX_Process *process,
return rc;
}
-bool FastOS_UNIX_ProcessStarter::Detach(FastOS_UNIX_Process *process)
-{
- bool rc = true;
- pid_t pid;
-
- auto guard = process->_app->getProcessGuard();
-
- pid = process->GetProcessId();
-
- if (pid == 0) {
- return false; // Cannot detach nonstarted process.
- }
- if (process->GetDeathFlag()) {
- return true;
- }
-
- if (process->GetDirectChild()) {
- int status = 0;
- _hasDetachedProcess = true;
- pid_t rpid = waitpid(pid, &status, WNOHANG);
- if (rpid > 0) {
- status = normalizedWaitStatus(status);
- } else if (rpid < 0)
- status = FastOS_ProcessInterface::NOTFOUND_EXITCODE;
- else
- status = FastOS_ProcessInterface::DETACH_EXITCODE;
- process->DeathNotification(status);
- } else {
- // Ask our process starter to detach process
- WriteInt(_mainSocket, FastOS_UNIX_ProcessStarter::CODE_DETACH);
- WriteBytes(_mainSocket, &pid, sizeof(pid_t));
-
- int returnCode;
- ReadBytes(_mainSocket, &returnCode, sizeof(int));
- process->DeathNotification(returnCode);
- }
- return rc;
-}
-
FastOS_UNIX_Process::DescriptorHandle::DescriptorHandle()
: _fd(-1),
_wantRead(false),
@@ -1999,7 +1646,7 @@ FastOS_UNIX_Process::DescriptorHandle::DescriptorHandle()
_writeBuffer()
{
}
-FastOS_UNIX_Process::DescriptorHandle::~DescriptorHandle() { }
+FastOS_UNIX_Process::DescriptorHandle::~DescriptorHandle() = default;
void
FastOS_UNIX_Process::DescriptorHandle::CloseHandle()
{
@@ -2017,11 +1664,3 @@ FastOS_UNIX_Process::DescriptorHandle::CloseHandle()
if (_writeBuffer.get() != nullptr)
_writeBuffer->Close();
}
-void
-FastOS_UNIX_Process::DescriptorHandle::CloseHandleDirectChild()
-{
- if (_fd != -1) {
- close(_fd);
- _fd = -1;
- }
-}
diff --git a/fastos/src/vespa/fastos/unix_process.h b/fastos/src/vespa/fastos/unix_process.h
index bff5a1d276e..0f641706bb5 100644
--- a/fastos/src/vespa/fastos/unix_process.h
+++ b/fastos/src/vespa/fastos/unix_process.h
@@ -25,8 +25,6 @@ private:
unsigned int _pid;
bool _died;
- bool _directChild;
- bool _keepOpenFilesIfDirectChild;
int _returnCode;
public:
class DescriptorHandle
@@ -47,7 +45,6 @@ public:
DescriptorHandle();
~DescriptorHandle();
void CloseHandle();
- void CloseHandleDirectChild();
};
private:
DescriptorHandle _descriptor[4];
@@ -57,10 +54,8 @@ private:
std::string _stderrRedirName;
bool _killed;
- FastOS_UNIX_ProcessStarter *GetProcessStarter ()
- {
- return static_cast<FastOS_UNIX_Application *>(_app)->
- GetProcessStarter();
+ FastOS_UNIX_ProcessStarter *GetProcessStarter () {
+ return static_cast<FastOS_UNIX_Application *>(_app)->GetProcessStarter();
}
bool InternalWait (int *returnCode, int timeOutSeconds, bool *pollStillRunning);
@@ -120,15 +115,11 @@ public:
bool WriteStdin (const void *data, size_t length) override;
bool Signal(int sig);
bool Kill () override;
- bool WrapperKill () override;
bool Wait (int *returnCode, int timeOutSeconds = -1) override;
bool PollWait (int *returnCode, bool *stillRunning) override;
- bool Detach() override;
void SetProcessId (unsigned int pid) { _pid = pid; }
unsigned int GetProcessId() override { return _pid; }
- bool SendIPCMessage (const void *data, size_t length) override;
- void DeathNotification (int returnCode)
- {
+ void DeathNotification (int returnCode) {
_returnCode = returnCode;
_died = true;
}
@@ -140,21 +131,7 @@ public:
_descriptor[type].CloseHandle();
}
- void CloseDescriptorDirectChild(DescriptorType type)
- {
- _descriptor[type].CloseHandleDirectChild();
- }
-
- void CloseDescriptorsDirectChild(void)
- {
- CloseDescriptorDirectChild(TYPE_STDOUT);
- CloseDescriptorDirectChild(TYPE_STDERR);
- CloseDescriptorDirectChild(TYPE_IPC);
- CloseDescriptorDirectChild(TYPE_STDIN);
- }
-
- void SetDescriptor (DescriptorType type,
- int descriptor)
+ void SetDescriptor (DescriptorType type, int descriptor)
{
_descriptor[type]._fd = descriptor;
}
@@ -164,35 +141,11 @@ public:
return _descriptor[type];
}
- FastOS_ProcessRedirectListener *GetStdoutListener ()
- {
- return _stdoutListener;
- }
- FastOS_ProcessRedirectListener *GetStderrListener ()
- {
- return _stderrListener;
- }
bool GetKillFlag () {return _killed; }
- bool GetDirectChild() const override { return _directChild; }
-
- bool SetDirectChild() override {
- _directChild = true;
- return true;
- }
-
- bool GetKeepOpenFilesIfDirectChild() const override { return _keepOpenFilesIfDirectChild; }
-
- bool SetKeepOpenFilesIfDirectChild() override {
- _keepOpenFilesIfDirectChild = true;
- return true;
- }
- void SetRunDir(const char *runDir);
- void SetStdoutRedirName(const char *stdoutRedirName);
- void SetStderrRedirName(const char *stderrRedirName);
- const char *GetRunDir(void) const { return _runDir.c_str(); }
- const char *GetStdoutRedirName(void) const { return _stdoutRedirName.c_str(); }
- const char *GetStderrRedirName(void) const { return _stderrRedirName.c_str(); }
+ const char *GetRunDir() const { return _runDir.c_str(); }
+ const char *GetStdoutRedirName() const { return _stdoutRedirName.c_str(); }
+ const char *GetStderrRedirName() const { return _stderrRedirName.c_str(); }
};
@@ -210,7 +163,6 @@ public:
CODE_EXIT,
CODE_NEWPROCESS,
CODE_WAIT,
- CODE_DETACH,
CODE_SUCCESS,
CODE_FAILURE,
@@ -241,14 +193,11 @@ protected:
bool _hasDirectChildren;
void StarterDoWait ();
- void StarterDoDetachProcess ();
void StarterDoCreateProcess ();
bool SendFileDescriptor (int fd);
- int ReadFileDescriptor ();
char **ReceiveEnvironmentVariables ();
- void SendEnvironmentVariables ();
bool CreateSocketPairs ();
void Run ();
@@ -256,13 +205,10 @@ protected:
void AddChildProcess (FastOS_UNIX_RealProcess *node);
void RemoveChildProcess (FastOS_UNIX_RealProcess *node);
- bool ReceiveFileDescriptor (bool use,
- FastOS_UNIX_Process::DescriptorType type,
- FastOS_UNIX_Process *xproc);
- void PollReapProxiedChildren(void);
- char **CopyEnvironmentVariables(void);
- void FreeEnvironmentVariables(char **env);
- void PollReapDirectChildren(void);
+ void PollReapProxiedChildren();
+ char **CopyEnvironmentVariables();
+ static void FreeEnvironmentVariables(char **env);
+ void PollReapDirectChildren();
public:
FastOS_UNIX_ProcessStarter (FastOS_ApplicationInterface *app);
@@ -270,19 +216,13 @@ public:
bool Start ();
void Stop ();
- void CloseProxiedChildDescs(void);
- void CloseProxyDescs(int stdinPipedDes,
- int stdoutPipedDes,
- int stderrPipedDes,
- int ipcDes,
- int handshakeDes0,
- int handshakeDes1);
+ void CloseProxiedChildDescs();
+ void CloseProxyDescs(int stdinPipedDes, int stdoutPipedDes, int stderrPipedDes,
+ int ipcDes, int handshakeDes0, int handshakeDes1);
bool CreateProcess (FastOS_UNIX_Process *process, bool useShell,
bool pipeStdin, bool pipeStdout, bool pipeStderr);
- bool Wait (FastOS_UNIX_Process *process, int timeOutSeconds,
- bool *pollStillRunning);
- bool Detach(FastOS_UNIX_Process *process);
+ bool Wait (FastOS_UNIX_Process *process, int timeOutSeconds, bool *pollStillRunning);
};