Skip to content

Commit

Permalink
tun, cstp: Don't exit() on failure
Browse files Browse the repository at this point in the history
These files are part of a library now so they should report errors back
to the caller.

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
  • Loading branch information
cernekee committed Jan 15, 2014
1 parent d50e470 commit 0f484d6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
14 changes: 10 additions & 4 deletions cstp.c
Expand Up @@ -196,8 +196,9 @@ static int start_cstp_connection(struct openconnect_info *vpninfo)
if (vpninfo->dtls_times.last_rekey + vpninfo->dtls_times.rekey <
time(NULL) + 300 &&
openconnect_random(vpninfo->dtls_secret, sizeof(vpninfo->dtls_secret))) {
fprintf(stderr, _("Failed to initialise DTLS secret\n"));
exit(1);
vpn_progress(vpninfo, PRG_ERR,
_("Failed to initialise DTLS secret\n"));
return -EIO;
}

retry:
Expand Down Expand Up @@ -238,7 +239,7 @@ static int start_cstp_connection(struct openconnect_info *vpninfo)
vpn_progress(vpninfo, PRG_ERR,
_("Failed to open HTTPS connection to %s\n"),
vpninfo->hostname);
exit(1);
return -EIO;
}
goto retry;
}
Expand All @@ -264,7 +265,7 @@ static int start_cstp_connection(struct openconnect_info *vpninfo)
_("Got inappropriate HTTP CONNECT response: %s\n"),
buf);
if (!strncmp(buf, "HTTP/1.1 401 ", 13))
exit(2);
return -EPERM;
return -EINVAL;
}

Expand Down Expand Up @@ -566,6 +567,11 @@ int cstp_reconnect(struct openconnect_info *vpninfo)
while ((ret = openconnect_make_cstp_connection(vpninfo))) {
if (timeout <= 0)
return ret;
if (ret == -EPERM) {
vpn_progress(vpninfo, PRG_ERR,
_("Cookie is no longer valid, ending session\n"));
return ret;
}
vpn_progress(vpninfo, PRG_INFO,
_("sleep %ds, remaining timeout %ds\n"),
interval, timeout);
Expand Down
16 changes: 11 additions & 5 deletions main.c
Expand Up @@ -520,6 +520,7 @@ int main(int argc, char **argv)
char *token_str = NULL;
oc_token_mode_t token_mode = OC_TOKEN_MODE_NONE;
int reconnect_timeout = 300;
int ret;

#ifdef ENABLE_NLS
bindtextdomain("openconnect", LOCALEDIR);
Expand Down Expand Up @@ -972,14 +973,19 @@ int main(int argc, char **argv)
if (fp)
fclose(fp);
}
openconnect_mainloop(vpninfo, reconnect_timeout, RECONNECT_INTERVAL_MIN);
ret = openconnect_mainloop(vpninfo, reconnect_timeout, RECONNECT_INTERVAL_MIN);
if (fp)
unlink(pidfile);

if (sig_caught)
if (sig_caught) {
vpn_progress(vpninfo, PRG_INFO, _("Caught signal: %s\n"), strsignal(sig_caught));
ret = 0;
} else if (ret == -EPERM)
ret = 2;
else
ret = 1;

if (fp)
unlink(pidfile);
exit(1);
exit(ret);
}

static int write_new_config(void *_vpninfo, char *buf, int buflen)
Expand Down
20 changes: 11 additions & 9 deletions tun.c
Expand Up @@ -508,7 +508,7 @@ static int os_setup_tun(struct openconnect_info *vpninfo)
vpn_progress(vpninfo, PRG_ERR,
_("Failed to open tun device: %s\n"),
strerror(tunerr));
exit(1);
return -EIO;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
Expand All @@ -519,7 +519,7 @@ static int os_setup_tun(struct openconnect_info *vpninfo)
vpn_progress(vpninfo, PRG_ERR,
_("TUNSETIFF failed: %s\n"),
strerror(errno));
exit(1);
return -EIO;
}
if (!vpninfo->ifname)
vpninfo->ifname = strdup(ifr.ifr_name);
Expand Down Expand Up @@ -615,16 +615,18 @@ static int os_setup_tun(struct openconnect_info *vpninfo)
break;
}
if (tun_fd < 0) {
perror(_("open tun"));
exit(1);
vpn_progress(vpninfo, PRG_ERR,
_("Failed to open tun device: %s\n"),
strerror(errno));
return -EIO;
}
vpninfo->ifname = strdup(tun_name + 5);
}
#ifdef TUNSIFHEAD
i = 1;
if (ioctl(tun_fd, TUNSIFHEAD, &i) < 0) {
perror(_("TUNSIFHEAD"));
exit(1);
return -EIO;
}
#endif
#endif
Expand Down Expand Up @@ -659,13 +661,13 @@ int openconnect_setup_tun_script(struct openconnect_info *vpninfo, char *tun_scr

set_script_env(vpninfo);
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
perror(_("socketpair"));
exit(1);
vpn_progress(vpninfo, PRG_ERR, _("socketpair failed: %s\n"), strerror(errno));
return -EIO;
}
child = fork();
if (child < 0) {
perror(_("fork"));
exit(1);
vpn_progress(vpninfo, PRG_ERR, _("fork failed: %s\n"), strerror(errno));
return -EIO;
} else if (!child) {
if (setpgid(0, getpid()) < 0)
perror(_("setpgid"));
Expand Down

0 comments on commit 0f484d6

Please sign in to comment.