Remove --writepid file on program exit.
PATCH 1/1 Remove --writepid file on program exit.
For whatever reason, we never removed the pid file on program exit.
Not only this is unclean, but it also makes testing for "I want this test case to FAIL" in t_client.sh more annoying to code for "is the OpenVPN process still around?"...
Do not unlink the file if chroot() is active (might be outside the chroot arena - testing for realpath etc. is left for someone else).
--- src/openvpn/openvpn.c | 6 ++++++ 1 file changed, 6 insertions(+)
src/openvpn/openvpn.c +6 -0
@@ -335,6 +335,12 @@ openvpn_main(int argc, char *argv[])while (c.sig->signal_received ==SIGHUP ); } +/* if we opened a PID file and did not chroot(), unlink() it again */ +if (c.options.writepid && !c.options.chroot_dir) + { +platform_unlink (c.options.writepid); + } +context_gc_free (&c);#ifdef ENABLE_MANAGEMENT
Signature · 1 line
2.27.0
PATCH v2 1/1 Remove --writepid file on program exit.
For whatever reason, we never removed the pid file on program exit.
Not only this is unclean, but it also makes testing for "I want this test case to FAIL" in t_client.sh more annoying to code for "is the OpenVPN process still around?"...
Do not unlink the file if chroot() is active (might be outside the chroot arena - testing for realpath etc. is left for someone else).
--
v2: make this work on M_FATAL exit, by unlinking from openvpn_exit() in error.h - this requires moving write_pid() to init.c so module hierarchy is maintained and introducing a static variable to save the PID file name (otherwise it is no longer available when the top level GC is gone).
--- src/openvpn/error.c | 1 + src/openvpn/init.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/openvpn/init.h | 3 +++ src/openvpn/openvpn.c | 24 +----------------------- 4 files changed, 47 insertions(+), 23 deletions(-)
src/openvpn/error.c +1 -0
@@ -743,6 +743,7 @@ openvpn_exit(const int status)#ifdef _WIN32uninit_win32 ();#endif +remove_pid_file ();close_syslog ();
src/openvpn/init.c +42 -0
54 lines not on this page · raw message
src/openvpn/init.h +3 -0
@@ -143,4 +143,7 @@ void open_plugins(struct context *c, const bool import_options, int init_point);void tun_abort (void ); +void write_pid_file (const char *filename,const char *chroot_dir); +void remove_pid_file (void ); +#endif /* ifndef INIT_H */
src/openvpn/openvpn.c +1 -23
@@ -46,28 +46,6 @@ process_signal_p2p(struct context *c)return process_signal (c); } -/* Write our PID to a file */ -static void -write_pid (const char *filename) -{ -if (filename) - { -unsigned int pid =0 ; -FILE *fp =platform_fopen (filename,"w" ); -if (!fp) - { -msg (M_ERR ,"Open error on pid file %s" , filename); - } - - pid =platform_getpid (); -fprintf (fp,"%u\n" , pid); -if (fclose (fp)) - { -msg (M_ERR ,"Close error on pid file %s" , filename); - } - } -} -/**************************************************************************/ /**
@@ -274,7 +252,7 @@ openvpn_main(int argc, char *argv[])if (c.first_time) { c.did_we_daemonize =possibly_become_daemon (&c.options); -write_pid (c.options.writepid); +write_pid_file (c.options.writepid, c.options.chroot_dir); }#ifdef ENABLE_MANAGEMENT
Signature · 1 line
2.27.0
Hi,
Quoted 64 lines · Gert Doering
For whatever reason, we never removed the pid file on program exit.
Not only this is unclean, but it also makes testing for "I want this test case to FAIL" in
t_client.shmore annoying to code for "is the OpenVPN process still around?"...Do not unlink the file if
chroot()is active (might be outside the chroot arena - testing for realpath etc. is left for someone else).Signed-off-by Gert Doering <[email protected]...>--
v2: make this work onM_FATALexit, by unlinking fromopenvpn_exit()inerror.h- this requires movingwrite_pid()toinit.cso module hierarchy is maintained and introducing a static variable to save the PID file name (otherwise it is no longer available when the top level GC is gone).--- src/openvpn/error.c | 1 + src/openvpn/init.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/openvpn/init.h | 3 +++ src/openvpn/openvpn.c | 24 +----------------------- 4 files changed, 47 insertions(+), 23 deletions(-)src/openvpn/error.c +1
-0@@ -743,6 +743,7 @@ openvpn_exit(const int status)#ifdef _WIN32uninit_win32 ();#endif +remove_pid_file ();close_syslog ();src/openvpn/init.c +14
-0@@ -58,6 +58,7 @@static struct context *static_context;/* GLOBAL */ +static const char *saved_pid_file_name;/* GLOBAL */ /* * Crypto initialization flags @@ -4687,6 +4688,47 @@ close_context(struct context *c, int sig, unsigned int flags) } } +/* Write our PID to a file */ +void +write_pid_file (const char *filename,const char *chroot_dir) +{ +if (filename) + { +unsigned int pid =0 ; +FILE *fp =platform_fopen (filename,"w" ); +if (!fp) + { +msg (M_ERR ,"Open error on pid file %s" , filename); +return ; + }
src/openvpn/init.c close_context(struct context *c, int sig, unsigned int flags) · line 4713
Quoted 10 lines
src/openvpn/init.c +10
-0+ + pid =platform_getpid (); +fprintf (fp,"%u\n" , pid); +if (fclose (fp)) + { +msg (M_ERR ,"Close error on pid file %s" , filename); + } + +/* remember file name so it can be deleted "out of context" later */ +/* (the chroot case is more complex and not handled today) */
This comment is indented using a tab, instead of the proper amount of spaces.
Quoted 68 lines
src/openvpn/init.c +18
-0+if (!chroot_dir) + { + saved_pid_file_name =strdup (filename); + } + } +} + +/* remove PID file on exit, called from openvpn_exit() */ +void +remove_pid_file (void ) +{ +if (saved_pid_file_name) + { +platform_unlink (saved_pid_file_name); + } +} + +/* * Do a loopback test * on the crypto subsystem. src/openvpn/init.h +3
-0@@ -143,4 +143,7 @@ void open_plugins(struct context *c, const bool import_options, int init_point);void tun_abort (void ); +void write_pid_file (const char *filename,const char *chroot_dir); +void remove_pid_file (void ); +#endif /* ifndef INIT_H */ src/openvpn/openvpn.c +0
-22@@ -46,28 +46,6 @@ process_signal_p2p(struct context *c)return process_signal (c); } -/* Write our PID to a file */ -static void -write_pid (const char *filename) -{ -if (filename) - { -unsigned int pid =0 ; -FILE *fp =platform_fopen (filename,"w" ); -if (!fp) - { -msg (M_ERR ,"Open error on pid file %s" , filename); - } - - pid =platform_getpid (); -fprintf (fp,"%u\n" , pid); -if (fclose (fp)) - { -msg (M_ERR ,"Close error on pid file %s" , filename); - } - } -} -/**************************************************************************/ /**
src/openvpn/openvpn.c openvpn_main(int argc, char *argv[]) · line 258
Quoted 8 lines
src/openvpn/openvpn.c +1
-1@@ -274,7 +252,7 @@ openvpn_main(int argc, char *argv[])if (c.first_time) { c.did_we_daemonize =possibly_become_daemon (&c.options); -write_pid (c.options.writepid); +write_pid_file (c.options.writepid, c.options.chroot_dir); }#ifdef ENABLE_MANAGEMENT
Other than the comment nitpick, everything looks good. The patch has been tested and works as expected.
The pidfile is always removed, regardless of the openvpn exist status.
Signature · 1 line
Antonio Quartulli
Patch has been applied to the master branch.
Tab has been expanded to pacify the whitespace dragon.
commit 008ec688d06101c0307e6d17a0239b134355dca4
Author: Gert Doering Date: Tue Jul 7 10:42:20 2020 +0200
Remove --writepid file on program exit.
Signed-off-by: Gert Doering <[email protected]...>
Acked-by: Antonio Quartulli <[email protected]...>
Message-Id: <[email protected]...>
URL: https://www.mail-archive.com/[email protected]/msg20224.html
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering