From 3d23af73c0032ad08766d2375fd9827493a7d658 Mon Sep 17 00:00:00 2001 From: Frederic Culot Date: Sun, 26 Jul 2009 12:47:15 +0000 Subject: More work on implementing calcurse daemon. --- src/dmon.c | 8 +++++++- src/io.c | 47 ++++++++++++++++++++++++++++++----------------- src/io.h | 4 ++-- src/utils.c | 8 ++++++-- src/vars.c | 5 +++-- src/vars.h | 11 +++++++---- 6 files changed, 55 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/dmon.c b/src/dmon.c index 9aae65f..a52d7a5 100644 --- a/src/dmon.c +++ b/src/dmon.c @@ -1,4 +1,4 @@ -/* $calcurse: dmon.c,v 1.2 2009/07/23 18:33:20 culot Exp $ */ +/* $calcurse: dmon.c,v 1.3 2009/07/26 12:47:15 culot Exp $ */ /* * Calcurse - text-based organizer @@ -61,6 +61,9 @@ dmon_sigs_hdlr (int sig) notify_free_app (); (void)io_fprintln (path_dmon_log, _("terminated at %s with signal %d\n"), nowstr (), sig); + + if (unlink (path_dpid) != 0) + EXIT (_("Could not remove daemon lock file: %s\n"), strerror (errno)); exit (EXIT_SUCCESS); } @@ -143,6 +146,9 @@ dmon_start (int parent_exit_status) if (!daemonize (parent_exit_status)) EXIT (_("Cannot daemonize, aborting\n")); + if (!io_dump_pid (path_dpid)) + EXIT (_("Could not set lock file\n")); + io_check_file (path_conf, (int *)0); custom_load_conf (&conf, 0); diff --git a/src/io.c b/src/io.c index ccd432d..3cd765d 100755 --- a/src/io.c +++ b/src/io.c @@ -1,4 +1,4 @@ -/* $calcurse: io.c,v 1.72 2009/07/23 19:16:03 culot Exp $ */ +/* $calcurse: io.c,v 1.73 2009/07/26 12:47:15 culot Exp $ */ /* * Calcurse - text-based organizer @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -726,7 +727,8 @@ io_init (char *cfile, char *datadir) (void)snprintf (path_notes, BUFSIZ, "%s/" NOTES_DIR_NAME, home); (void)snprintf (path_apts, BUFSIZ, "%s/" APTS_PATH_NAME, home); (void)snprintf (path_keys, BUFSIZ, "%s/" KEYS_PATH_NAME, home); - (void)snprintf (path_lock, BUFSIZ, "%s/" LOCK_PATH_NAME, home); + (void)snprintf (path_cpid, BUFSIZ, "%s/" CPID_PATH_NAME, home); + (void)snprintf (path_dpid, BUFSIZ, "%s/" DPID_PATH_NAME, home); (void)snprintf (path_dmon_log, BUFSIZ, "%s/" DLOG_PATH_NAME, home); } else @@ -740,7 +742,8 @@ io_init (char *cfile, char *datadir) (void)snprintf (path_todo, BUFSIZ, "%s/" TODO_PATH, home); (void)snprintf (path_conf, BUFSIZ, "%s/" CONF_PATH, home); (void)snprintf (path_keys, BUFSIZ, "%s/" KEYS_PATH, home); - (void)snprintf (path_lock, BUFSIZ, "%s/" LOCK_PATH, home); + (void)snprintf (path_cpid, BUFSIZ, "%s/" CPID_PATH, home); + (void)snprintf (path_dpid, BUFSIZ, "%s/" DPID_PATH, home); (void)snprintf (path_dmon_log, BUFSIZ, "%s/" DLOG_PATH, home); (void)snprintf (path_notes, BUFSIZ, "%s/" NOTES_DIR, home); if (cfile == NULL) @@ -2934,32 +2937,42 @@ io_set_lock (void) { FILE *lock; - if ((lock = fopen (path_lock, "r")) != NULL) + if ((lock = fopen (path_cpid, "r")) != NULL) { (void)fprintf (stderr, _("\nWARNING: it seems that another calcurse instance is " "already running.\n" "If this is not the case, please remove the following " "lock file: \n\"%s\"\n" - "and restart calcurse.\n"), path_lock); + "and restart calcurse.\n"), path_cpid); exit (EXIT_FAILURE); } else { - if ((lock = fopen (path_lock, "w")) == NULL) - { - (void)fprintf (stderr, _("FATAL ERROR: could not create %s: %s\n"), - path_lock, strerror (errno)); - exit_calcurse (EXIT_FAILURE); - } - (void)fclose (lock); + if (!io_dump_pid (path_cpid)) + EXIT (_("FATAL ERROR: could not create %s: %s\n"), + path_cpid, strerror (errno)); } } -/* Used when calcurse exits to remove the lock file. */ -void -io_unset_lock (void) +/* + * Create a new file and write the process pid inside + * (used to create a simple lock for example). + */ +unsigned +io_dump_pid (char *file) { - if (unlink (path_lock) != 0) - EXIT (_("Could not remove lock file: %s\n"), strerror (errno)); + pid_t pid; + FILE *fp; + + if (!file) + return 0; + + pid = getpid (); + if (!(fp = fopen (file, "w")) + || fprintf (fp, "%ld\n", (long)pid) < 0 + || fclose (fp) != 0) + return 0; + + return 1; } diff --git a/src/io.h b/src/io.h index c71cb13..b910cd4 100755 --- a/src/io.h +++ b/src/io.h @@ -1,4 +1,4 @@ -/* $calcurse: io.h,v 1.23 2009/07/23 18:33:21 culot Exp $ */ +/* $calcurse: io.h,v 1.24 2009/07/26 12:47:16 culot Exp $ */ /* * Calcurse - text-based organizer @@ -87,6 +87,6 @@ void io_log_free (io_file_t *); void io_start_psave_thread (conf_t *); void io_stop_psave_thread (void); void io_set_lock (void); -void io_unset_lock (void); +unsigned io_dump_pid (char *); #endif /* !CALCURSE_IO_H */ diff --git a/src/utils.c b/src/utils.c index 9e4da1d..e825196 100755 --- a/src/utils.c +++ b/src/utils.c @@ -1,4 +1,4 @@ -/* $calcurse: utils.c,v 1.78 2009/07/23 18:33:21 culot Exp $ */ +/* $calcurse: utils.c,v 1.79 2009/07/26 12:47:16 culot Exp $ */ /* * Calcurse - text-based organizer @@ -94,7 +94,11 @@ exit_calcurse (int status) keys_free (); mem_stats (); if (remove_lock) - io_unset_lock (); + { + if (unlink (path_cpid) != 0) + EXIT (_("Could not remove calcurse lock file: %s\n"), + strerror (errno)); + } dmon_start (status); diff --git a/src/vars.c b/src/vars.c index 0dc6a06..8038f9c 100755 --- a/src/vars.c +++ b/src/vars.c @@ -1,4 +1,4 @@ -/* $calcurse: vars.c,v 1.18 2009/07/23 18:33:22 culot Exp $ */ +/* $calcurse: vars.c,v 1.19 2009/07/26 12:47:16 culot Exp $ */ /* * Calcurse - text-based organizer @@ -102,7 +102,8 @@ char path_apts[] = ""; char path_conf[] = ""; char path_notes[] = ""; char path_keys[] = ""; -char path_lock[] = ""; +char path_cpid[] = ""; +char path_dpid[] = ""; char path_dmon_log[] = ""; /* Variable to handle pads. */ diff --git a/src/vars.h b/src/vars.h index 677c15a..3da83a6 100755 --- a/src/vars.h +++ b/src/vars.h @@ -1,4 +1,4 @@ -/* $calcurse: vars.h,v 1.35 2009/07/23 18:33:22 culot Exp $ */ +/* $calcurse: vars.h,v 1.36 2009/07/26 12:47:17 culot Exp $ */ /* * Calcurse - text-based organizer @@ -60,7 +60,8 @@ #define APTS_PATH_NAME "apts" #define CONF_PATH_NAME "conf" #define KEYS_PATH_NAME "keys" -#define LOCK_PATH_NAME ".calcurse.lock" +#define CPID_PATH_NAME ".calcurse.pid" +#define DPID_PATH_NAME ".daemon.pid" #define DLOG_PATH_NAME "daemon.log" #define NOTES_DIR_NAME "notes/" @@ -68,8 +69,9 @@ #define APTS_PATH DIR_NAME APTS_PATH_NAME #define CONF_PATH DIR_NAME CONF_PATH_NAME #define KEYS_PATH DIR_NAME KEYS_PATH_NAME -#define LOCK_PATH DIR_NAME LOCK_PATH_NAME +#define CPID_PATH DIR_NAME CPID_PATH_NAME #define DLOG_PATH DIR_NAME DLOG_PATH_NAME +#define DPID_PATH DIR_NAME DPID_PATH_NAME #define NOTES_DIR DIR_NAME NOTES_DIR_NAME #define ATTR_FALSE 0 @@ -161,7 +163,8 @@ extern char path_apts[BUFSIZ]; extern char path_conf[BUFSIZ]; extern char path_keys[BUFSIZ]; extern char path_notes[BUFSIZ]; -extern char path_lock[BUFSIZ]; +extern char path_cpid[BUFSIZ]; +extern char path_dpid[BUFSIZ]; extern char path_dmon_log[BUFSIZ]; extern struct pad_s apad; -- cgit v1.2.3-54-g00ecf