launch_daemon: Replace fork+exec with posix_spawn.
This commit is contained in:
@@ -7,10 +7,12 @@
|
|||||||
#include "BaseJob.h"
|
#include "BaseJob.h"
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <spawn.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <AutoDeleter.h>
|
||||||
#include <Message.h>
|
#include <Message.h>
|
||||||
|
|
||||||
#include "Conditions.h"
|
#include "Conditions.h"
|
||||||
@@ -201,25 +203,38 @@ BaseJob::_GetSourceFileEnvironment(const char* script, BStringList& environment)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pid_t child = fork();
|
posix_spawn_file_actions_t fileActions;
|
||||||
if (child < 0) {
|
int status = posix_spawn_file_actions_init(&fileActions);
|
||||||
|
if (status != 0) {
|
||||||
// TODO: log error
|
// TODO: log error
|
||||||
debug_printf("could not fork: %s\n", strerror(errno));
|
return;
|
||||||
} else if (child == 0) {
|
}
|
||||||
// We're the child, redirect stdout
|
CObjectDeleter<posix_spawn_file_actions_t, int, posix_spawn_file_actions_destroy>
|
||||||
close(STDOUT_FILENO);
|
actionsDeleter(&fileActions);
|
||||||
close(STDERR_FILENO);
|
|
||||||
dup2(pipes[1], STDOUT_FILENO);
|
// redirect stdout in the child
|
||||||
dup2(pipes[1], STDERR_FILENO);
|
posix_spawn_file_actions_addclose(&fileActions, STDOUT_FILENO);
|
||||||
|
posix_spawn_file_actions_addclose(&fileActions, STDERR_FILENO);
|
||||||
|
posix_spawn_file_actions_adddup2(&fileActions, pipes[1], STDOUT_FILENO);
|
||||||
|
posix_spawn_file_actions_adddup2(&fileActions, pipes[1], STDERR_FILENO);
|
||||||
|
|
||||||
for (int32 i = 0; i < 2; i++)
|
for (int32 i = 0; i < 2; i++)
|
||||||
close(pipes[i]);
|
posix_spawn_file_actions_addclose(&fileActions, pipes[i]);
|
||||||
|
|
||||||
BString command;
|
BString command;
|
||||||
command.SetToFormat(". \"%s\"; export -p", script);
|
command.SetToFormat(". \"%s\"; export -p", script);
|
||||||
execl("/bin/sh", "/bin/sh", "-c", command.String(), NULL);
|
|
||||||
exit(1);
|
const char* argv[] = {"/bin/sh", "-c", command.String(), NULL};
|
||||||
} else {
|
|
||||||
|
pid_t child;
|
||||||
|
status = posix_spawn(&child, argv[0], &fileActions, NULL, (char**)argv, NULL);
|
||||||
|
|
||||||
|
if (status != 0) {
|
||||||
|
// TODO: log error
|
||||||
|
debug_printf("could not fork: %s\n", strerror(errno));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Retrieve environment from child
|
// Retrieve environment from child
|
||||||
|
|
||||||
close(pipes[1]);
|
close(pipes[1]);
|
||||||
@@ -252,7 +267,6 @@ BaseJob::_GetSourceFileEnvironment(const char* script, BStringList& environment)
|
|||||||
_ParseExportVariable(environment, line);
|
_ParseExportVariable(environment, line);
|
||||||
|
|
||||||
close(pipes[0]);
|
close(pipes[0]);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user