main: write pid file even when not daemonizing

This commit moves the pid file writing code to main
from ply_create_daemon, so that it gets run even when we
plymouthd isn't daemonized.

This is more symmetrical, anyway, since unlinking of the pid
file is handled in main.
This commit is contained in:
cee1 2011-04-21 15:18:13 -04:00 committed by Ray Strode
parent aefa84dc9e
commit b1e9022f11
3 changed files with 23 additions and 19 deletions

View file

@ -836,7 +836,7 @@ ply_show_new_kernel_messages (bool should_show)
}
ply_daemon_handle_t *
ply_create_daemon (const char *pid_file)
ply_create_daemon (void)
{
pid_t pid;
int sender_fd, receiver_fd;
@ -875,22 +875,6 @@ ply_create_daemon (const char *pid_file)
_exit (1);
}
if ((byte == 0) && (pid_file != NULL))
{
FILE *pidf;
pidf = fopen (pid_file, "w");
if (!pidf)
{
ply_error ("could not write pid file %s: %m", pid_file);
}
else
{
fprintf (pidf, "%d\n", (int)pid);
fclose (pidf);
}
}
_exit ((int) byte);
}
close (receiver_fd);

View file

@ -108,7 +108,7 @@ bool ply_create_file_link (const char *source,
const char *destination);
void ply_show_new_kernel_messages (bool should_show);
ply_daemon_handle_t *ply_create_daemon (const char *pid_file);
ply_daemon_handle_t *ply_create_daemon (void);
bool ply_detach_daemon (ply_daemon_handle_t *handle,
int exit_code);

View file

@ -2034,6 +2034,23 @@ on_crash (int signum)
raise(signum);
}
static void
write_pid_file (const char *filename)
{
FILE *fp;
fp = fopen (filename, "w");
if (fp == NULL)
{
ply_error ("could not write pid file %s: %m", filename);
}
else
{
fprintf (fp, "%d\n", (int) getpid ());
fclose (fp);
}
}
int
main (int argc,
char **argv)
@ -2140,7 +2157,7 @@ main (int argc,
if (! no_daemon)
{
daemon_handle = ply_create_daemon (pid_file);
daemon_handle = ply_create_daemon ();
if (daemon_handle == NULL)
{
@ -2205,6 +2222,9 @@ main (int argc,
return EX_UNAVAILABLE;
}
if (pid_file != NULL)
write_pid_file (pid_file);
if (daemon_handle != NULL
&& !ply_detach_daemon (daemon_handle, 0))
{