[prev in list] [next in list] [prev in thread] [next in thread] 

List:       freeipmi-devel
Subject:    Re: [Freeipmi-devel] [PATCH] bmc-watchdog: write pidfile with the	daemon's PID
From:       Albert Chu <chu11 () llnl ! gov>
Date:       2011-09-07 17:49:39
Message-ID: 1315417779.25283.497.camel () auk59 ! llnl ! gov
[Download RAW message or body]

Hey Diego,

Thanks.  I applied the slightly modified attached patch.

Al

On Tue, 2011-09-06 at 23:20 -0700, Diego Elio Pettenò wrote:
> By writing a pidfile, init scripts can rely on that to kill or signal the
> running daemon instance. This is important because relying on the process
> name can be troublesome when using Linux Containers, for instance.
> 
> Signed-off-by: Diego Elio Pettenò <flameeyes@flameeyes.eu>
> ---
>  bmc-watchdog/src/Makefile.am    |    3 ++-
>  bmc-watchdog/src/bmc-watchdog.c |   11 ++++++++++-
>  2 files changed, 12 insertions(+), 2 deletions(-)
> 
> diff --git a/bmc-watchdog/src/Makefile.am b/bmc-watchdog/src/Makefile.am
> index 04c01e2..38d2f7f 100644
> --- a/bmc-watchdog/src/Makefile.am
> +++ b/bmc-watchdog/src/Makefile.am
> @@ -10,7 +10,8 @@ AM_CPPFLAGS = \
>  	-I$(top_srcdir)/common/src/portability \
>  	-I$(top_srcdir)/libfreeipmi/include \
>  	-D_GNU_SOURCE \
> -	-D_REENTRANT
> +	-D_REENTRANT \
> +	-DLOCALSTATEDIR='"$(localstatedir)"'
>  
>  sbin_PROGRAMS = bmc-watchdog
>  
> diff --git a/bmc-watchdog/src/bmc-watchdog.c b/bmc-watchdog/src/bmc-watchdog.c
> index 566ee65..403bc0a 100644
> --- a/bmc-watchdog/src/bmc-watchdog.c
> +++ b/bmc-watchdog/src/bmc-watchdog.c
> @@ -1677,6 +1677,10 @@ _daemon_init ()
>      {
>        unsigned int i;
>        pid_t pid;
> +      FILE *pidfile;
> +
> +      if ( (pidfile = fopen(LOCALSTATEDIR "/run/bmc-watchdog.pid", "w")) == NULL )
> +        _err_exit ("fopen: %s", strerror (errno));
>  
>        if ((pid = fork ()) < 0)
>          _err_exit ("fork: %s", strerror (errno));
> @@ -1690,8 +1694,13 @@ _daemon_init ()
>  
>        if ((pid = fork ()) < 0)
>          _err_exit ("fork: %s", strerror (errno));
> -      if (pid)
> +      if (pid) {
> +        /* write the 2nd child PID to the pidfile */
> +        fprintf(pidfile, "%u\n", pid);
> +        fclose(pidfile);
> +
>          exit (0);                   /* 1st child terminates */
> +      }
>  
>        if (chdir ("/") < 0)
>          _err_exit ("chdir: %s", strerror (errno));
-- 
Albert Chu
chu11@llnl.gov
Computer Scientist
High Performance Systems Division
Lawrence Livermore National Laboratory


["bmc.patch" (bmc.patch)]

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 7815)
+++ ChangeLog	(working copy)
@@ -1,3 +1,10 @@
+2011-09-07  Diego Elio Pettenò <flameeyes@flameeyes.eu>
+
+	* bmc-watchdog/src/bmc-watchdog.c: By writing a pidfile, init
+	scripts can rely on that to kill or signal the running daemon
+	instance. This is important because relying on the process name
+	can be troublesome when using Linux Containers, for instance.
+
 2011-09-06  Diego Elio Pettenò <flameeyes@flameeyes.eu>
 
 	* libfreeipmi/src/Makefile.am: The -module option is used to tell
Index: bmc-watchdog/src/bmc-watchdog.c
===================================================================
--- bmc-watchdog/src/bmc-watchdog.c	(revision 7806)
+++ bmc-watchdog/src/bmc-watchdog.c	(working copy)
@@ -98,6 +98,8 @@
     *(__val) = __temp;                                                  \
   } while (0)
 
+#define BMC_WATCHDOG_PIDFILE BMC_WATCHDOG_LOCALSTATEDIR "/run/bmc-watchdog.pid"
+
 struct bmc_watchdog_arguments cmd_args;
 
 /* program name */
@@ -1677,7 +1679,11 @@
     {
       unsigned int i;
       pid_t pid;
+      FILE *pidfile;
 
+      if ( (pidfile = fopen(BMC_WATCHDOG_PIDFILE, "w")) == NULL )
+        _err_exit ("fopen: %s", strerror (errno));
+
       if ((pid = fork ()) < 0)
         _err_exit ("fork: %s", strerror (errno));
       if (pid)
@@ -1690,8 +1696,13 @@
 
       if ((pid = fork ()) < 0)
         _err_exit ("fork: %s", strerror (errno));
-      if (pid)
+      if (pid) {
+        /* write the 2nd child PID to the pidfile */
+        fprintf(pidfile, "%u\n", pid);
+        fclose(pidfile);
+
         exit (0);                   /* 1st child terminates */
+      }
 
       if (chdir ("/") < 0)
         _err_exit ("chdir: %s", strerror (errno));
Index: bmc-watchdog/src/Makefile.am
===================================================================
--- bmc-watchdog/src/Makefile.am	(revision 7806)
+++ bmc-watchdog/src/Makefile.am	(working copy)
@@ -10,7 +10,8 @@
 	-I$(top_srcdir)/common/src/portability \
 	-I$(top_srcdir)/libfreeipmi/include \
 	-D_GNU_SOURCE \
-	-D_REENTRANT
+	-D_REENTRANT \
+	-DBMC_WATCHDOG_LOCALSTATEDIR='"$(localstatedir)"'
 
 sbin_PROGRAMS = bmc-watchdog
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic