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 fixes the bug reported in trac #563, but also further
simplifies the code.
trac #563
Signed-off-by: Steffan Karger <steffan@karger.me>
Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: <
1434665325-3225-1-git-send-email-steffan@karger.me>
URL: http://article.gmane.org/gmane.network.openvpn.devel/9793
Signed-off-by: Gert Doering <gert@greenie.muc.de>
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);
}
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);
}
}
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);
/* 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 */
*/
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;