summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrederic Culot <calcurse@culot.org>2009-07-26 12:47:15 +0000
committerFrederic Culot <calcurse@culot.org>2009-07-26 12:47:15 +0000
commit3d23af73c0032ad08766d2375fd9827493a7d658 (patch)
tree695d9d0890fba0d1b4b9763289b4d20345537aa3
parentaef6f012fe2904dcedf47b3bf9a36366841e49a2 (diff)
downloadcalcurse-3d23af73c0032ad08766d2375fd9827493a7d658.tar.gz
calcurse-3d23af73c0032ad08766d2375fd9827493a7d658.zip
More work on implementing calcurse daemon.
-rwxr-xr-xChangeLog2
-rw-r--r--src/dmon.c8
-rwxr-xr-xsrc/io.c47
-rwxr-xr-xsrc/io.h4
-rwxr-xr-xsrc/utils.c8
-rwxr-xr-xsrc/vars.c5
-rwxr-xr-xsrc/vars.h11
7 files changed, 57 insertions, 28 deletions
diff --git a/ChangeLog b/ChangeLog
index 5568173..0b59f65 100755
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,8 @@
* src/args.c (parse_args): check for file presence added (thanks
again Chris for reporting it)
+
+ * src/io.c (io_unset_lock): function removed
2009-07-23 Frederic Culot <frederic@culot.org>
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 <stdlib.h>
#include <string.h>
#include <sys/stat.h>
+#include <sys/types.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
@@ -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;