Move pidpath handling outside of daemonize, as it can be used independently

This commit is contained in:
Paul Kulchenko 2022-07-22 22:50:47 -07:00
parent 3894788097
commit 20d9d652e5

View file

@ -1045,17 +1045,10 @@ static void ChangeUser(void) {
} }
static void Daemonize(void) { static void Daemonize(void) {
char ibuf[21];
int fd;
if (fork() > 0) exit(0); if (fork() > 0) exit(0);
setsid(); setsid();
if (fork() > 0) _exit(0); if (fork() > 0) _exit(0);
umask(0); umask(0);
if (pidpath) {
fd = open(pidpath, O_CREAT | O_WRONLY, 0644);
WRITE(fd, ibuf, FormatInt32(ibuf, getpid()) - ibuf);
close(fd);
}
} }
static void LogLuaError(char *hook, char *err) { static void LogLuaError(char *hook, char *err) {
@ -7287,6 +7280,8 @@ static void GetOpts(int argc, char *argv[]) {
} }
void RedBean(int argc, char *argv[]) { void RedBean(int argc, char *argv[]) {
char ibuf[21];
int fd;
if (IsLinux()) { if (IsLinux()) {
// disable sneak privilege since we don't use them // disable sneak privilege since we don't use them
// seccomp will fail later if this fails // seccomp will fail later if this fails
@ -7340,6 +7335,11 @@ void RedBean(int argc, char *argv[]) {
if (daemonize) { if (daemonize) {
Daemonize(); Daemonize();
} }
if (pidpath) {
fd = open(pidpath, O_CREAT | O_WRONLY, 0644);
WRITE(fd, ibuf, FormatInt32(ibuf, getpid()) - ibuf);
close(fd);
}
ChangeUser(); ChangeUser();
UpdateCurrentDate(nowl()); UpdateCurrentDate(nowl());
CollectGarbage(); CollectGarbage();