git » nmdb » commit dfed7a8

nmdb: Add an option to save the pidfile

author Alberto Bertogli
2010-05-03 07:33:52 UTC
committer Alberto Bertogli
2010-05-03 07:33:52 UTC
parent 12ad5b5a7c8f6e292c095623eccb7d28e437bca8

nmdb: Add an option to save the pidfile

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

nmdb/common.h +1 -0
nmdb/log.c +18 -0
nmdb/log.h +3 -0
nmdb/main.c +12 -1
tests/coverage/coverage +2 -0

diff --git a/nmdb/common.h b/nmdb/common.h
index 3c66fc0..06f0789 100644
--- a/nmdb/common.h
+++ b/nmdb/common.h
@@ -30,6 +30,7 @@ struct settings {
 	char *dbname;
 	char *logfname;
 	enum backend_type backend;
+	char *pidfile;
 };
 extern struct settings settings;
 
diff --git a/nmdb/log.c b/nmdb/log.c
index 0a485b9..3026350 100644
--- a/nmdb/log.c
+++ b/nmdb/log.c
@@ -72,3 +72,21 @@ void errlog(const char *s)
 	wlog("%s: %s\n", s, strerror(errno));
 }
 
+void write_pid()
+{
+	FILE *f;
+
+	if (settings.pidfile == NULL)
+		return;
+
+	f = fopen(settings.pidfile, "w");
+	if (f == NULL) {
+		errlog("Can't open pidfile for writing");
+		return;
+	}
+
+	fprintf(f, "%d\n", getpid());
+
+	fclose(f);
+}
+
diff --git a/nmdb/log.h b/nmdb/log.h
index 3e5672b..2a52e5f 100644
--- a/nmdb/log.h
+++ b/nmdb/log.h
@@ -14,6 +14,9 @@ void wlog(const char *fmt, ...);
 /* Errno logging, perror()-alike */
 void errlog(const char *s);
 
+/* PID file */
+void write_pid();
+
 #endif
 
 
diff --git a/nmdb/main.c b/nmdb/main.c
index abb8d52..e784145 100644
--- a/nmdb/main.c
+++ b/nmdb/main.c
@@ -41,6 +41,7 @@ static void help(void) {
 	  "  -S addr	SCTP listening address (all local addresses)\n"
 	  "  -c nobj	max. number of objects to be cached, in thousands (128)\n"
 	  "  -o fname	log to the given file (stdout).\n"
+	  "  -i pidfile file to write the PID to (none).\n"
 	  "  -f		don't fork and stay in the foreground\n"
 	  "  -p		enable passive mode, for redundancy purposes (read docs.)\n"
 	  "  -r		read-only mode\n"
@@ -71,13 +72,14 @@ static int load_settings(int argc, char **argv)
 	settings.passive = 0;
 	settings.read_only = 0;
 	settings.logfname = "-";
+	settings.pidfile = NULL;
 	settings.backend = DEFAULT_BE;
 
 	settings.dbname = malloc(strlen(DEFDBNAME) + 1);
 	strcpy(settings.dbname, DEFDBNAME);
 
 	while ((c = getopt(argc, argv,
-				"b:d:l:L:t:T:u:U:s:S:c:o:fprh?")) != -1) {
+				"b:d:l:L:t:T:u:U:s:S:c:o:i:fprh?")) != -1) {
 		switch(c) {
 		case 'b':
 			settings.backend = be_type_from_str(optarg);
@@ -125,6 +127,11 @@ static int load_settings(int argc, char **argv)
 			strcpy(settings.logfname, optarg);
 			break;
 
+		case 'i':
+			settings.pidfile = malloc(strlen(optarg) + 1);
+			strcpy(settings.pidfile, optarg);
+			break;
+
 		case 'f':
 			settings.foreground = 1;
 			break;
@@ -232,6 +239,8 @@ int main(int argc, char **argv)
 
 	wlog("Starting nmdb\n");
 
+	write_pid();
+
 	dbthread = db_loop_start(db);
 
 	net_loop();
@@ -244,6 +253,8 @@ int main(int argc, char **argv)
 
 	cache_free(cd);
 
+	unlink(settings.pidfile);
+
 	return 0;
 }
 
diff --git a/tests/coverage/coverage b/tests/coverage/coverage
index bcdfb93..56c8405 100755
--- a/tests/coverage/coverage
+++ b/tests/coverage/coverage
@@ -89,6 +89,8 @@ nmdb_and_kill -o /dev/null
 nmdb_and_kill -Z		# supposed to fail
 nmdb_and_kill -o /root/nmdb-coverage-fail	# supposed to fail
 nmdb_and_kill -d /dev/null	# supposed to fail
+nmdb_and_kill -i /tmp/nmdb-pid-file.pid
+nmdb_and_kill -i /root/nmdb-pid-file.pid	# supposed to fail
 
 
 out "+ signals"