Make it possible track the PID of openconnect via a pid-file even when some supervisor daemon keeps it in the foreground. Signed-off-by: Robin H. Johnson <robbat2 at gentoo.org> --- main.c | 31 ++++++++++++++++++------------- openconnect.8.in | 2 +- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/main.c b/main.c index 283db78..2450eba 100644 --- a/main.c +++ b/main.c @@ -1632,21 +1632,21 @@ int main(int argc, char **argv) } #ifndef _WIN32 + /* Open the pidfile before forking, so we can report errors + more sanely. It's *possible* that we'll fail to write to + it, but very unlikely. In the event that we do NOT fork, + still write the pid for management tools to use. */ + if (pidfile != NULL) { + fp = openconnect_fopen_utf8(vpninfo, pidfile, "w"); + if (!fp) { + fprintf(stderr, _("Failed to open '%s' for write: %s\n"), + pidfile, strerror(errno)); + openconnect_vpninfo_free(vpninfo); + exit(1); + } + } if (background) { int pid; - - /* Open the pidfile before forking, so we can report errors - more sanely. It's *possible* that we'll fail to write to - it, but very unlikely. */ - if (pidfile != NULL) { - fp = openconnect_fopen_utf8(vpninfo, pidfile, "w"); - if (!fp) { - fprintf(stderr, _("Failed to open '%s' for write: %s\n"), - pidfile, strerror(errno)); - openconnect_vpninfo_free(vpninfo); - exit(1); - } - } if ((pid = fork())) { if (fp) { fprintf(fp, "%d\n", pid); @@ -1660,6 +1660,11 @@ int main(int argc, char **argv) } if (fp) fclose(fp); + } else { + if (fp) { + fprintf(fp, "%d\n", getpid()); + fclose(fp); + } } #endif diff --git a/openconnect.8.in b/openconnect.8.in index c190680..f7ea321 100644 --- a/openconnect.8.in +++ b/openconnect.8.in @@ -119,7 +119,7 @@ Continue in background after startup .B \-\-pid\-file=PIDFILE Save the pid to .I PIDFILE -when backgrounding +right before backgrounding or main reconnect loop. .TP .B \-c,\-\-certificate=CERT Use SSL client certificate -- 2.14.1