static int verbose = PRG_INFO;
static int timestamp;
-int background;
+static int background;
+static FILE *pid_fp = NULL;
+static char *pidfile = NULL;
static int do_passphrase_from_fsid;
static int non_inter;
static int cookieonly;
dtls_state);
}
+static FILE *background_self(struct openconnect_info *vpninfo, char *pidfile) {
+ FILE *fp = NULL;
+ 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);
+ fclose(fp);
+ }
+ vpn_progress(vpninfo, PRG_INFO,
+ _("Continuing in background; pid %d\n"),
+ pid);
+ openconnect_vpninfo_free(vpninfo);
+ exit(0);
+ }
+ if (fp)
+ fclose(fp);
+ return fp;
+}
+
+static void fully_up_cb(void *_vpninfo) {
+ struct openconnect_info *vpninfo = _vpninfo;
+
+ print_connection_info(vpninfo);
+#ifndef _WIN32
+ if (background)
+ pid_fp = background_self(vpninfo, pidfile);
+#endif
+}
+
int main(int argc, char **argv)
{
struct openconnect_info *vpninfo;
char *vpnc_script = NULL;
int autoproxy = 0;
int opt;
- char *pidfile = NULL;
- FILE *fp = NULL;
char *config_arg;
char *config_filename;
char *token_str = NULL;
fprintf(stderr, _("Set up UDP failed; using SSL instead\n"));
}
- print_connection_info(vpninfo);
if (!vpninfo->vpnc_script) {
vpn_progress(vpninfo, PRG_INFO,
_("See http://www.infradead.org/openconnect/vpnc-script.html\n"));
}
-#ifndef _WIN32
- 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);
- fclose(fp);
- }
- vpn_progress(vpninfo, PRG_INFO,
- _("Continuing in background; pid %d\n"),
- pid);
- openconnect_vpninfo_free(vpninfo);
- exit(0);
- }
- if (fp)
- fclose(fp);
- }
-#endif
openconnect_set_loglevel(vpninfo, verbose);
+ openconnect_set_setup_tun_handler(vpninfo, fully_up_cb);
while (1) {
ret = openconnect_mainloop(vpninfo, reconnect_timeout, RECONNECT_INTERVAL_MIN);
vpn_progress(vpninfo, PRG_INFO, _("User requested reconnect\n"));
}
- if (fp)
+ if (pid_fp)
unlink(pidfile);
switch (ret) {