Ticket #563: 0001-write-pid-file-immediately-after-daemonizing.patch

File 0001-write-pid-file-immediately-after-daemonizing.patch, 4.2 KB (added by Steffan Karger, 7 years ago)
  • src/openvpn/init.c

    From 61a556cb50265e0b2073ebed954f79f740438280 Mon Sep 17 00:00:00 2001
    From: Steffan Karger <steffan@karger.me>
    Date: Wed, 17 Jun 2015 22:38:13 +0200
    Subject: [PATCH] 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.
    
    Signed-off-by: Steffan Karger <steffan@karger.me>
    ---
     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(-)
    
    diff --git a/src/openvpn/init.c b/src/openvpn/init.c
    index c99e775..7a2c69b 100644
    a b do_init_first_time (struct context *c) 
    27752775        platform_group_get (c->options.groupname, &c0->platform_state_group) |
    27762776        platform_user_get (c->options.username, &c0->platform_state_user);
    27772777
    2778       /* get --writepid file descriptor */
    2779       get_pid_file (c->options.writepid, &c0->pid_state);
    2780 
    27812778      /* perform postponed chdir if --daemon */
    27822779      if (c->did_we_daemonize && c->options.cd_dir == NULL)
    27832780        platform_chdir("/");
    27842781
    2785       /* save process ID in a file */
    2786       write_pid (&c0->pid_state);
    2787 
    27882782      /* should we change scheduling priority? */
    27892783      platform_nice (c->options.nice);
    27902784    }
  • src/openvpn/misc.c

    diff --git a/src/openvpn/misc.c b/src/openvpn/misc.c
    index 8408438..8e78117 100644
    a b run_up_down (const char *command, 
    127127  gc_free (&gc);
    128128}
    129129
    130 /* Get the file we will later write our process ID to */
     130/* Write our PID to a file */
    131131void
    132 get_pid_file (const char* filename, struct pid_state *state)
     132write_pid (const char *filename)
    133133{
    134   CLEAR (*state);
    135134  if (filename)
    136135    {
    137       state->fp = platform_fopen (filename, "w");
    138       if (!state->fp)
     136      unsigned int pid = 0;
     137      FILE *fp = platform_fopen (filename, "w");
     138      if (!fp)
    139139        msg (M_ERR, "Open error on pid file %s", filename);
    140       state->filename = filename;
    141     }
    142 }
    143140
    144 /* Write our PID to a file */
    145 void
    146 write_pid (const struct pid_state *state)
    147 {
    148   if (state->filename && state->fp)
    149     {
    150       unsigned int pid = platform_getpid ();
    151       fprintf(state->fp, "%u\n", pid);
    152       if (fclose (state->fp))
    153         msg (M_ERR, "Close error on pid file %s", state->filename);
     141      pid = platform_getpid ();
     142      fprintf(fp, "%u\n", pid);
     143      if (fclose (fp))
     144        msg (M_ERR, "Close error on pid file %s", filename);
    154145    }
    155146}
    156147
  • src/openvpn/misc.h

    diff --git a/src/openvpn/misc.h b/src/openvpn/misc.h
    index 183898e..e67b5e4 100644
    a b void run_up_down (const char *command, 
    7373                  const char *script_type,
    7474                  struct env_set *es);
    7575
    76 /* workspace for get_pid_file/write_pid */
    77 struct pid_state {
    78   FILE *fp;
    79   const char *filename;
    80 };
    81 
    82 void get_pid_file (const char* filename, struct pid_state *state);
    83 void write_pid (const struct pid_state *state);
     76void write_pid (const char *filename);
    8477
    8578/* check file protections */
    8679void warn_if_group_others_accessible(const char* filename);
  • src/openvpn/openvpn.c

    diff --git a/src/openvpn/openvpn.c b/src/openvpn/openvpn.c
    index 2f327f3..00bd570 100644
    a b openvpn_main (int argc, char *argv[]) 
    231231         
    232232          /* become a daemon if --daemon */
    233233          if (c.first_time)
    234             c.did_we_daemonize = possibly_become_daemon (&c.options);
     234            {
     235              c.did_we_daemonize = possibly_become_daemon (&c.options);
     236              write_pid (c.options.writepid);
     237            }
    235238
    236239#ifdef ENABLE_MANAGEMENT
    237240          /* open management subsystem */
  • src/openvpn/openvpn.h

    diff --git a/src/openvpn/openvpn.h b/src/openvpn/openvpn.h
    index bdfa685..10ec859 100644
    a b struct context_persist 
    137137 */
    138138struct context_0
    139139{
    140   /* workspace for get_pid_file/write_pid */
    141   struct pid_state pid_state;
    142 
    143140  /* workspace for --user/--group */
    144141  bool uid_gid_specified;
    145142  bool uid_gid_set;