[6288] in Moira

home help back first fref pref prev next nref lref last post

Re: PATCH: Make update_server write out a PID file

daemon@ATHENA.MIT.EDU (Jonathan Reed)
Mon Apr 8 15:54:17 2013

Date: Mon, 8 Apr 2013 15:54:14 -0400 (EDT)
From: Jonathan Reed <jdreed@MIT.EDU>
To: Garry P Zacheiss <zacheiss@MIT.EDU>
cc: "<moiradev@mit.edu>" <moiradev@MIT.EDU>
In-Reply-To: <7A06C2897958944188C59D5BE070539426A3122F@OC11EXPO24.exchange.mit.edu>
Message-ID: <alpine.DEB.2.02.1304081550250.30997@infinite-loop.mit.edu>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII


On Mon, 8 Apr 2013, Garry P Zacheiss wrote:

> I'd take something like it, although I'd rather we make use of the 
> 'whoami' variable in the filename rather than hardcoding "update_server" 
> in the filename.  The PIDFILEPATH definition would also be better moved 
> to update_server.h rather than the .c file.
>
> Garry
>

OK, thanks.  Here's a second attempt.  I think I got the string handling 
right.  I also think I don't have to check for a trailing slash in 
PIDFILEPATH, right?


Index: moira/update/update_server.c
===================================================================
--- moira/update/update_server.c	(revision 4103)
+++ moira/update/update_server.c	(working copy)
@@ -65,6 +65,8 @@
    struct utsname name;
    int s, conn;
    struct sigaction sa;
+  FILE *pid_file;
+  char *pid_path = NULL;

    whoami = strrchr(argv[0], '/');
    if (whoami)
@@ -127,6 +129,26 @@

    set_com_err_hook(syslog_com_err_proc);
    openlog(whoami, LOG_PID, LOG_DAEMON);
+ 
+  if ((pid_path = malloc(strlen(PIDFILEPATH) + strlen(whoami) + 6)) != NULL) 
+    {
+      sprintf(pid_path, "%s/%s.pid", PIDFILEPATH, whoami);
+      pid_file = fopen(pid_path, "w");
+      if (pid_file)
+	{
+	  fprintf(pid_file, "%d\n", getpid ());
+	  fclose(pid_file);
+	}
+      else
+	{
+	  com_err(whoami, errno, "Could not write PID file");
+	}
+      free(pid_path);
+    }
+  else 
+    {
+      com_err(whoami, errno, "Could not allocate memory for pidfile path");
+    }

    /* now loop waiting for connections */
    while (1)
Index: moira/update/update_server.h
===================================================================
--- moira/update/update_server.h	(revision 4103)
+++ moira/update/update_server.h	(working copy)
@@ -33,3 +33,7 @@

  extern char *whoami, *hostname;
  extern int have_authorization, uid;
+
+#ifndef PIDFILEPATH
+#define PIDFILEPATH "/var/run"
+#endif

home help back first fref pref prev next nref lref last post