wmix: create the PID file only after successful initialisation

The PID file was created at the early beginning, but wmix can abort later
if something is not ok. It is better to create it at the end, so it will
be created only if wmix will actually be running.

Took the opportunity to move the code to a dedicated function to keep main
simple, and added a check to fix possible crash on startup if environment
variable $HOME does not exist.

Signed-off-by: Christophe CURIS <christophe.curis@free.fr>
This commit is contained in:
Christophe CURIS 2014-06-07 21:21:44 +02:00 committed by Carlos R. Mafra
parent 8eae1880b9
commit e2dc01bdc0
3 changed files with 25 additions and 12 deletions

View file

@ -24,3 +24,4 @@ double get_current_time(void);
void add_region (int index, int x, int y, int width, int height);
int check_region (int x, int y);
void config_read (void);
void create_pid_file (void);

View file

@ -22,10 +22,12 @@
#include "config.h"
#endif
#include <sys/types.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include "include/common.h"
@ -161,3 +163,24 @@ void config_read(void)
}
fclose(fp);
}
/* handle writing PID file, silently ignore if we can't do it */
void create_pid_file(void)
{
char *home;
char *pid;
FILE *fp;
home = getenv("HOME");
if (home == NULL)
return;
pid = calloc(1, strlen(home) + 10);
sprintf(pid, "%s/.wmix.pid", home);
fp = fopen(pid, "w");
if (fp) {
fprintf(fp, "%d\n", getpid());
fclose(fp);
}
free(pid);
}

View file

@ -121,8 +121,6 @@ int main(int argc, char **argv)
{
XEvent event;
char *home;
char *pid;
FILE *fp;
memset(&config, 0, sizeof(config));
@ -133,16 +131,6 @@ int main(int argc, char **argv)
sprintf(config.file, "%s/.wmixrc", home);
}
/* handle writing PID file, silently ignore if we can't do it */
pid = calloc(1, strlen(home) + 10);
sprintf(pid, "%s/.wmix.pid", home);
fp = fopen(pid, "w");
if (fp) {
fprintf(fp, "%d\n", getpid());
fclose(fp);
}
free(pid);
/* default values */
config.mousewheel = 1;
config.scrolltext = 1;
@ -197,6 +185,7 @@ int main(int argc, char **argv)
add_region(10, 3, 4, 56, 7); /* re-scroll current channel name */
/* setup up/down signal handler */
create_pid_file();
signal(SIGUSR1, (void *) signal_catch);
signal(SIGUSR2, (void *) signal_catch);