diff options
author | Frederic Culot <calcurse@culot.org> | 2009-06-21 18:16:21 +0000 |
---|---|---|
committer | Frederic Culot <calcurse@culot.org> | 2009-06-21 18:16:21 +0000 |
commit | 627fd8a8aa380c3343800012c58ff0431c566614 (patch) | |
tree | 3209af506b43c59ef558eda23680ec6c32fdb017 | |
parent | bff0d973a426a5c9b55e824ab2a11560a2245938 (diff) | |
download | calcurse-627fd8a8aa380c3343800012c58ff0431c566614.zip |
Basic lock mechanism implemented to avoid having two calcurse instances running at the same time.
-rwxr-xr-x | ChangeLog | 4 | ||||
-rwxr-xr-x | src/calcurse.c | 6 | ||||
-rwxr-xr-x | src/io.c | 53 | ||||
-rwxr-xr-x | src/io.h | 4 | ||||
-rwxr-xr-x | src/utils.c | 3 | ||||
-rwxr-xr-x | src/vars.c | 3 | ||||
-rwxr-xr-x | src/vars.h | 6 |
7 files changed, 70 insertions, 9 deletions
@@ -7,6 +7,10 @@ * src/utils.c: fixed a memory leak caused by a wrong usage of the notify_app structure + + * src/io.c (io_set_lock, io_unset_lock): new functions to + implement a basic locking mechanism in order to avoid having two + calcurse instances running at the same time 2009-06-20 Frederic Culot <frederic@culot.org> diff --git a/src/calcurse.c b/src/calcurse.c index 552cfb4..30beffb 100755 --- a/src/calcurse.c +++ b/src/calcurse.c @@ -1,4 +1,4 @@ -/* $calcurse: calcurse.c,v 1.78 2009/01/24 18:45:35 culot Exp $ */ +/* $calcurse: calcurse.c,v 1.79 2009/06/21 18:16:22 culot Exp $ */ /* * Calcurse - text-based organizer @@ -88,7 +88,9 @@ main (int argc, char **argv) non_interactive = parse_args (argc, argv, &conf); if (non_interactive) exit_calcurse (EXIT_SUCCESS); - + else + io_set_lock (); + /* Begin of interactive mode with ncurses interface. */ sigs_init (&sigact); /* signal handling init */ initscr (); /* start the curses mode */ @@ -1,4 +1,4 @@ -/* $calcurse: io.c,v 1.59 2009/06/01 08:04:04 culot Exp $ */ +/* $calcurse: io.c,v 1.60 2009/06/21 18:16:22 culot Exp $ */ /* * Calcurse - text-based organizer @@ -679,7 +679,8 @@ io_init (char *cfile, char *datadir) (void)snprintf (path_conf, BUFSIZ, "%s/" CONF_PATH_NAME, home); (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_keys, BUFSIZ, "%s/" KEYS_PATH_NAME, home); + (void)snprintf (path_lock, BUFSIZ, "%s/" LOCK_PATH_NAME, home); } else { @@ -691,7 +692,8 @@ io_init (char *cfile, char *datadir) (void)snprintf (path_dir, BUFSIZ, "%s/" DIR_NAME, home); (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_keys, BUFSIZ, "%s/" KEYS_PATH, home); + (void)snprintf (path_lock, BUFSIZ, "%s/" LOCK_PATH, home); (void)snprintf (path_notes, BUFSIZ, "%s/" NOTES_DIR, home); if (cfile == NULL) { @@ -2820,3 +2822,48 @@ io_stop_psave_thread (void) if (io_t_psave) pthread_cancel (io_t_psave); } + +/* + * 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 + * yet. + */ +void +io_set_lock (void) +{ + FILE *lock; + + if ((lock = fopen (path_lock, "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); + exit_calcurse (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); + } +} + +/* Used when calcurse exits to remove the lock file. */ +void +io_unset_lock (void) +{ + if (unlink (path_lock) != 0) + EXIT (_("Could not remove lock file: %s\n"), strerror (errno)); +} @@ -1,4 +1,4 @@ -/* $calcurse: io.h,v 1.18 2008/12/28 19:41:45 culot Exp $ */ +/* $calcurse: io.h,v 1.19 2009/06/21 18:16:22 culot Exp $ */ /* * Calcurse - text-based organizer @@ -68,5 +68,7 @@ void io_log_display (io_file_t *, char *, char *); 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); #endif /* !CALCURSE_IO_H */ diff --git a/src/utils.c b/src/utils.c index 80101b9..42c4170 100755 --- a/src/utils.c +++ b/src/utils.c @@ -1,4 +1,4 @@ -/* $calcurse: utils.c,v 1.69 2009/06/21 14:42:50 culot Exp $ */ +/* $calcurse: utils.c,v 1.70 2009/06/21 18:16:23 culot Exp $ */ /* * Calcurse - text-based organizer @@ -74,6 +74,7 @@ exit_calcurse (int status) notify_free_app (); keys_free (); mem_stats (); + io_unset_lock (); exit (status); } @@ -1,4 +1,4 @@ -/* $calcurse: vars.c,v 1.14 2009/01/02 22:28:54 culot Exp $ */ +/* $calcurse: vars.c,v 1.15 2009/06/21 18:16:23 culot Exp $ */ /* * Calcurse - text-based organizer @@ -90,6 +90,7 @@ char path_apts[] = ""; char path_conf[] = ""; char path_notes[] = ""; char path_keys[] = ""; +char path_lock[] = ""; /* Variable to handle pads. */ struct pad_s apad; @@ -1,4 +1,4 @@ -/* $calcurse: vars.h,v 1.31 2009/01/23 21:09:21 culot Exp $ */ +/* $calcurse: vars.h,v 1.32 2009/06/21 18:16:23 culot Exp $ */ /* * Calcurse - text-based organizer @@ -49,11 +49,14 @@ #define APTS_PATH_NAME "apts" #define CONF_PATH_NAME "conf" #define KEYS_PATH_NAME "keys" +#define LOCK_PATH_NAME ".calcurse.lock" #define NOTES_DIR_NAME "notes/" + #define TODO_PATH DIR_NAME TODO_PATH_NAME #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 NOTES_DIR DIR_NAME NOTES_DIR_NAME #define ATTR_FALSE 0 @@ -145,6 +148,7 @@ 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 struct pad_s apad; extern struct nbar_s nbar; |