[6290] in Moira
Re: PATCH: Make update_server write out a PID file
daemon@ATHENA.MIT.EDU (Jonathan Reed)
Mon Apr 8 16:33:06 2013
Date: Mon, 8 Apr 2013 16:33:04 -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: <7A06C2897958944188C59D5BE070539426A3233B@OC11EXPO24.exchange.mit.edu>
Message-ID: <alpine.DEB.2.02.1304081632430.32427@infinite-loop.mit.edu>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed
Right, yeah, exiting would make sense. Looks great, thanks.
-Jon
On Mon, 8 Apr 2013, Garry P Zacheiss wrote:
> 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
>
>