From 6ff95238766656c5ea9d5c65c35d3aef93499f60 Mon Sep 17 00:00:00 2001 From: Erik Saule Date: Sun, 20 Mar 2011 17:19:40 -0400 Subject: ignore the lock file if the pointed process is dead. Lukas: Small formatting and logic changes. Signed-off-by: Lukas Fleischer --- src/io.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/io.c b/src/io.c index 699592c..0fe930e 100644 --- a/src/io.c +++ b/src/io.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -2948,19 +2949,34 @@ io_stop_psave_thread (void) /* * This sets a lock file to prevent from having two different instances of * calcurse running. - * If the lock cannot be obtained, then warn the user and exit calcurse. - * Else, create a .calcurse.lock file in the user defined directory, which - * will be removed when calcurse exits. * - * Note: when creating the lock file, the interactive mode is not initialized + * If the lock cannot be obtained, then warn the user and exit calcurse. Else, + * create a .calcurse.pid file in the user defined directory, which will be + * removed when calcurse exits. + * + * Note: When creating the lock file, the interactive mode is not initialized * yet. */ void io_set_lock (void) { - FILE *lock; + FILE *lock = fopen (path_cpid, "r"); + int pid; + + if (lock != NULL) + { + /* If there is a lock file, check whether the process exists. */ + if (fscanf(lock, "%d", &pid) == 1) + { + fclose(lock); + if (kill(pid, 0) != 0 && errno == ESRCH) + lock = NULL; + } + else + fclose(lock); + } - if ((lock = fopen (path_cpid, "r")) != NULL) + if (lock != NULL) { (void)fprintf (stderr, _("\nWARNING: it seems that another calcurse instance is " @@ -2979,8 +2995,8 @@ io_set_lock (void) } /* - * Create a new file and write the process pid inside - * (used to create a simple lock for example). + * Create a new file and write the process pid inside (used to create a simple + * lock for example). Overwrite already existing files. */ unsigned io_dump_pid (char *file) -- cgit v1.2.3-70-g09d2