Revise processing for pid_file

This commit is contained in:
Mr-Dave
2022-12-11 15:47:00 -07:00
parent 3f83a7646e
commit dc107bb16a

View File

@@ -479,7 +479,7 @@ static void setup_signals(void)
*/
void motion_remove_pid(void)
{
if ((cnt_list[0]->daemon) && (cnt_list[0]->conf.pid_file) && (restart == 0)) {
if ((cnt_list[0]->conf.pid_file) && (restart == 0)) {
if (!unlink(cnt_list[0]->conf.pid_file)) {
MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO, _("Removed process id file (pid file)."));
} else {
@@ -2876,7 +2876,6 @@ static void *motion_loop(void *arg)
static void become_daemon(void)
{
int i;
FILE *pidf = NULL;
struct sigaction sig_ign_action;
/* Setup sig_ign_action */
@@ -2894,29 +2893,6 @@ static void become_daemon(void)
exit(0);
}
/*
* Create the pid file if defined, if failed exit
* If we fail we report it. If we succeed we postpone the log entry till
* later when we have closed stdout. Otherwise Motion hangs in the terminal waiting
* for an enter.
*/
if (cnt_list[0]->conf.pid_file) {
pidf = myfopen(cnt_list[0]->conf.pid_file, "w+e");
if (pidf) {
(void)fprintf(pidf, "%d\n", getpid());
myfclose(pidf);
} else {
MOTION_LOG(EMG, TYPE_ALL, SHOW_ERRNO
,_("Exit motion, cannot create process"
" id file (pid file) %s"), cnt_list[0]->conf.pid_file);
if (ptr_logfile) {
myfclose(ptr_logfile);
}
exit(0);
}
}
/*
* Changing dir to root enables people to unmount a disk
* without having to stop Motion
@@ -2925,7 +2901,6 @@ static void become_daemon(void)
MOTION_LOG(ERR, TYPE_ALL, SHOW_ERRNO, _("Could not change directory"));
}
#if (defined(BSD) && !defined(__APPLE__))
setpgrp(0, getpid());
#else
@@ -2954,18 +2929,32 @@ static void become_daemon(void)
close(i);
}
/* Now it is safe to add the PID creation to the logs */
if (pidf) {
MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO
,_("Created process id file %s. Process ID is %d")
,cnt_list[0]->conf.pid_file, getpid());
}
sigaction(SIGTTOU, &sig_ign_action, NULL);
sigaction(SIGTTIN, &sig_ign_action, NULL);
sigaction(SIGTSTP, &sig_ign_action, NULL);
}
static void pid_write(void)
{
FILE *pidf = NULL;
if (cnt_list[0]->conf.pid_file) {
pidf = myfopen(cnt_list[0]->conf.pid_file, "w+e");
if (pidf) {
(void)fprintf(pidf, "%d\n", getpid());
myfclose(pidf);
MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO
,_("Created process id file %s. Process ID is %d")
,cnt_list[0]->conf.pid_file, getpid());
} else {
MOTION_LOG(ERR, TYPE_ALL, SHOW_ERRNO
,_("Cannot create process id file (pid file) %s")
, cnt_list[0]->conf.pid_file);
}
}
}
static void cntlist_create(int argc, char *argv[])
{
/*
@@ -3213,6 +3202,8 @@ static void motion_startup(int daemonize, int argc, char *argv[])
}
}
pid_write();
if (cnt_list[0]->conf.setup_mode) {
MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO,_("Motion running in setup mode."));
}