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) |
2775 | 2775 | platform_group_get (c->options.groupname, &c0->platform_state_group) | |
2776 | 2776 | platform_user_get (c->options.username, &c0->platform_state_user); |
2777 | 2777 | |
2778 | | /* get --writepid file descriptor */ |
2779 | | get_pid_file (c->options.writepid, &c0->pid_state); |
2780 | | |
2781 | 2778 | /* perform postponed chdir if --daemon */ |
2782 | 2779 | if (c->did_we_daemonize && c->options.cd_dir == NULL) |
2783 | 2780 | platform_chdir("/"); |
2784 | 2781 | |
2785 | | /* save process ID in a file */ |
2786 | | write_pid (&c0->pid_state); |
2787 | | |
2788 | 2782 | /* should we change scheduling priority? */ |
2789 | 2783 | platform_nice (c->options.nice); |
2790 | 2784 | } |
diff --git a/src/openvpn/misc.c b/src/openvpn/misc.c
index 8408438..8e78117 100644
a
|
b
|
run_up_down (const char *command, |
127 | 127 | gc_free (&gc); |
128 | 128 | } |
129 | 129 | |
130 | | /* Get the file we will later write our process ID to */ |
| 130 | /* Write our PID to a file */ |
131 | 131 | void |
132 | | get_pid_file (const char* filename, struct pid_state *state) |
| 132 | write_pid (const char *filename) |
133 | 133 | { |
134 | | CLEAR (*state); |
135 | 134 | if (filename) |
136 | 135 | { |
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) |
139 | 139 | msg (M_ERR, "Open error on pid file %s", filename); |
140 | | state->filename = filename; |
141 | | } |
142 | | } |
143 | 140 | |
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); |
154 | 145 | } |
155 | 146 | } |
156 | 147 | |
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, |
73 | 73 | const char *script_type, |
74 | 74 | struct env_set *es); |
75 | 75 | |
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); |
| 76 | void write_pid (const char *filename); |
84 | 77 | |
85 | 78 | /* check file protections */ |
86 | 79 | void warn_if_group_others_accessible(const char* filename); |
diff --git a/src/openvpn/openvpn.c b/src/openvpn/openvpn.c
index 2f327f3..00bd570 100644
a
|
b
|
openvpn_main (int argc, char *argv[]) |
231 | 231 | |
232 | 232 | /* become a daemon if --daemon */ |
233 | 233 | 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 | } |
235 | 238 | |
236 | 239 | #ifdef ENABLE_MANAGEMENT |
237 | 240 | /* open management subsystem */ |
diff --git a/src/openvpn/openvpn.h b/src/openvpn/openvpn.h
index bdfa685..10ec859 100644
a
|
b
|
struct context_persist |
137 | 137 | */ |
138 | 138 | struct context_0 |
139 | 139 | { |
140 | | /* workspace for get_pid_file/write_pid */ |
141 | | struct pid_state pid_state; |
142 | | |
143 | 140 | /* workspace for --user/--group */ |
144 | 141 | bool uid_gid_specified; |
145 | 142 | bool uid_gid_set; |