Skip to contentSkip to conversation

write pid file immediately after daemonizing

PATCH In review Steffan Karger 2 participants 2 messages Jun 18, 2015 – Jun 21, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 write pid file immediately after daemonizing

Since we split daemonizing from changing directory in commit da9b292 (f025de005d719201a69ad0313d545a1ddd244752 in release/2.3), we can now simply write the pid file immediately after daemonizing. This not only hould fix the bug reported in trac #563, but also furhter simplifies the code.

---
 src/openvpn/init.c    |  6 ------
 src/openvpn/misc.c    | 27 +++++++++------------------
 src/openvpn/misc.h    |  9 +--------
 src/openvpn/openvpn.c |  5 ++++-
 src/openvpn/openvpn.h |  3 ---
 5 files changed, 14 insertions(+), 36 deletions(-)
src/openvpn/init.c +0 -6
index c99e775..7a2c69b 100644
@@ -2775,16 +2775,10 @@ do_init_first_time (struct context *c)
 	platform_group_get (c->options.groupname, &c0->platform_state_group) |
 	platform_user_get (c->options.username, &c0->platform_state_user);
 
-      /* get --writepid file descriptor */
-      get_pid_file (c->options.writepid, &c0->pid_state);
-
       /* perform postponed chdir if --daemon */
       if (c->did_we_daemonize && c->options.cd_dir == NULL)
 	platform_chdir("/");
 
-      /* save process ID in a file */
-      write_pid (&c0->pid_state);
-
       /* should we change scheduling priority? */
       platform_nice (c->options.nice);
     }
src/openvpn/misc.c +9 -18
index 8408438..8e78117 100644
@@ -127,30 +127,21 @@ run_up_down (const char *command,
   gc_free (&gc);
 }
 
-/* Get the file we will later write our process ID to */
+/* Write our PID to a file */
 void
-get_pid_file (const char* filename, struct pid_state *state)
+write_pid (const char *filename)
 {
-  CLEAR (*state);
   if (filename)
     {
-      state->fp = platform_fopen (filename, "w");
-      if (!state->fp)
+      unsigned int pid = 0;
+      FILE *fp = platform_fopen (filename, "w");
+      if (!fp)
 	msg (M_ERR, "Open error on pid file %s", filename);
-      state->filename = filename;
-    }
-}
 
-/* Write our PID to a file */
-void
-write_pid (const struct pid_state *state)
-{
-  if (state->filename && state->fp)
-    {
-      unsigned int pid = platform_getpid (); 
-      fprintf(state->fp, "%u\n", pid);
-      if (fclose (state->fp))
-	msg (M_ERR, "Close error on pid file %s", state->filename);
+      pid = platform_getpid ();
+      fprintf(fp, "%u\n", pid);
+      if (fclose (fp))
+	msg (M_ERR, "Close error on pid file %s", filename);
     }
 }
 
src/openvpn/misc.h +1 -8
index 183898e..e67b5e4 100644
@@ -73,14 +73,7 @@ void run_up_down (const char *command,
 		  const char *script_type,
 		  struct env_set *es);
 
-/* workspace for get_pid_file/write_pid */
-struct pid_state {
-  FILE *fp;
-  const char *filename;
-};
-
-void get_pid_file (const char* filename, struct pid_state *state);
-void write_pid (const struct pid_state *state);
+void write_pid (const char *filename);
 
 /* check file protections */
 void warn_if_group_others_accessible(const char* filename);
src/openvpn/openvpn.c +4 -1
index 2f327f3..00bd570 100644
@@ -231,7 +231,10 @@ openvpn_main (int argc, char *argv[])
 	  
 	  /* become a daemon if --daemon */
 	  if (c.first_time)
-	    c.did_we_daemonize = possibly_become_daemon (&c.options);
+	    {
+	      c.did_we_daemonize = possibly_become_daemon (&c.options);
+	      write_pid (c.options.writepid);
+	    }
 
 #ifdef ENABLE_MANAGEMENT
 	  /* open management subsystem */
src/openvpn/openvpn.h +0 -3
index bdfa685..10ec859 100644
@@ -137,9 +137,6 @@ struct context_persist
  */
 struct context_0
 {
-  /* workspace for get_pid_file/write_pid */
-  struct pid_state pid_state;
-
   /* workspace for --user/--group */
   bool uid_gid_specified;
   bool uid_gid_set;
Signature · 1 line
2.1.4
#1 lore raw

ACK. Your patch has been applied to the master and release/2.3 branch.

commit 659eae7b79e5565bb0c93f6d6d04e2163fea1141 (master) commit bce656d27fe06ed364a4acebd3c3d6d996750613 (release/2.3)

Author: Steffan Karger
Date:   Fri Jun 19 00:08:45 2015 +0200
     write pid file immediately after daemonizing

     Signed-off-by: Steffan Karger <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: http://article.gmane.org/gmane.network.openvpn.devel/9793
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering