[6289] in Moira
Re: PATCH: Make update_server write out a PID file
daemon@ATHENA.MIT.EDU (Garry P Zacheiss)
Mon Apr 8 16:20:30 2013
From: Garry P Zacheiss <zacheiss@MIT.EDU>
To: Jonathan D Reed <jdreed@MIT.EDU>
CC: "<moiradev@mit.edu>" <moiradev@MIT.EDU>
Date: Mon, 8 Apr 2013 20:20:23 +0000
Message-ID: <7A06C2897958944188C59D5BE070539426A3233B@OC11EXPO24.exchange.mit.edu>
In-Reply-To: <alpine.DEB.2.02.1304081550250.30997@infinite-loop.mit.edu>
Content-Language: en-US
Content-Type: text/plain; charset="us-ascii"
Content-ID: <BCC0D1AF16B705499BBF7AC98634BFDE@exchange.mit.edu>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
I made a couple of tweaks to it. Does this work for you?
Garry
Index: update_server.c
===================================================================
--- update_server.c (revision 4094)
+++ 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)
@@ -128,6 +130,28 @@
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, "Unable to write PID file %s", pid_path);
+ exit(1);
+ }
+ free(pid_path);
+ }
+ else
+ {
+ com_err(whoami, errno, "Could not allocate memory for pidfile path");
+ exit(1);
+ }
+
/* now loop waiting for connections */
while (1)
{
Index: update_server.h
===================================================================
--- update_server.h (revision 4094)
+++ 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
On Apr 8, 2013, at 3:54 PM, Jonathan Reed <jdreed@MIT.EDU>
wrote:
>
> 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