0
0
mirror of https://github.com/OpenVPN/openvpn.git synced 2024-09-20 03:52:28 +02:00

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).

Signed-off-by: Gert Doering <gert@greenie.muc.de>
Acked-by: Antonio Quartulli <antonio@openvpn.net>
Message-Id: <20200707084220.45753-1-gert@greenie.muc.de>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg20224.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
This commit is contained in:
Gert Doering 2020-07-07 10:42:20 +02:00
parent a5e6f2d217
commit 008ec688d0
4 changed files with 47 additions and 23 deletions

View File

@ -743,6 +743,7 @@ openvpn_exit(const int status)
#ifdef _WIN32
uninit_win32();
#endif
remove_pid_file();
close_syslog();

View File

@ -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;
}
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) */
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.

View File

@ -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 */

View File

@ -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