From: Christophe CURIS <[email protected]>

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 <[email protected]>
---
 wmix/include/misc.h |  1 +
 wmix/misc.c         | 23 +++++++++++++++++++++++
 wmix/wmix.c         | 13 +------------
 3 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/wmix/include/misc.h b/wmix/include/misc.h
index 5a70664..7794111 100644
--- a/wmix/include/misc.h
+++ b/wmix/include/misc.h
@@ -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);
diff --git a/wmix/misc.c b/wmix/misc.c
index 3c7e854..544a6e9 100644
--- a/wmix/misc.c
+++ b/wmix/misc.c
@@ -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);
+}
diff --git a/wmix/wmix.c b/wmix/wmix.c
index eed28b6..84b1e42 100644
--- a/wmix/wmix.c
+++ b/wmix/wmix.c
@@ -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);
 
-- 
1.9.2


-- 
To unsubscribe, send mail to [email protected].

Reply via email to