Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
main: Refactor signal handling
Eliminate the SIGUSR1/SIGUSR2 debug logging behavior and introduce new
behavior for the following signals:

    SIGINT: Disconnect and logoff, run vpnc-script accordingly
    SIGHUP: Disconnect and run vpnc-script (cookie can be re-used)
    SIGUSR2: Reconnect to server immediately as if DPD triggered.
    SIGTERM: Just die. Disconnect without logoff, no vpnc-script

Signed-off-by: Kevin Cernekee <cernekee@gmail.com>
  • Loading branch information
cernekee committed Jun 13, 2014
1 parent b33815b commit 56c5acb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 27 deletions.
75 changes: 48 additions & 27 deletions main.c
Expand Up @@ -443,26 +443,31 @@ static void read_stdin(char **string, int hidden)
}

static int sig_cmd_fd;

#ifndef _WIN32
static int sig_caught;

static void handle_sigint(int sig)
static void handle_signal(int sig)
{
char x = OC_CMD_CANCEL;
char cmd;

switch (sig) {
case SIGINT:
cmd = OC_CMD_CANCEL;
break;
case SIGHUP:
cmd = OC_CMD_DETACH;
break;
case SIGUSR2:
default:
cmd = OC_CMD_PAUSE;
break;
}

sig_caught = sig;
if (write(sig_cmd_fd, &x, 1) < 0) {
if (write(sig_cmd_fd, &cmd, 1) < 0) {
/* suppress warn_unused_result */
}
}

static void handle_sigusr(int sig)
{
if (sig == SIGUSR1)
verbose = PRG_TRACE;
else if (sig == SIGUSR2)
verbose = PRG_INFO;
}
#endif

static FILE *config_file = NULL;
Expand Down Expand Up @@ -965,14 +970,10 @@ int main(int argc, char **argv)
#ifndef _WIN32
memset(&sa, 0, sizeof(sa));

sa.sa_handler = handle_sigusr;
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);

sa.sa_handler = handle_sigint;
sigaction(SIGTERM, &sa, NULL);
sa.sa_handler = handle_signal;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
#endif /* !_WIN32 */

if (vpninfo->sslkey && do_passphrase_from_fsid)
Expand Down Expand Up @@ -1116,20 +1117,40 @@ int main(int argc, char **argv)
fclose(fp);
}
#endif
ret = openconnect_mainloop(vpninfo, reconnect_timeout, RECONNECT_INTERVAL_MIN);

while (1) {
ret = openconnect_mainloop(vpninfo, reconnect_timeout, RECONNECT_INTERVAL_MIN);
if (ret)
break;

vpn_progress(vpninfo, PRG_INFO, _("User requested reconnect\n"));
}

if (fp)
unlink(pidfile);

#ifndef _WIN32
if (sig_caught) {
vpn_progress(vpninfo, PRG_INFO, _("Caught signal: %s\n"), strsignal(sig_caught));
ret = 0;
} else
#endif
if (ret == -EPERM)
switch (ret) {
case -EPERM:
vpn_progress(vpninfo, PRG_ERR, _("Cookie was rejected on reconnection; exiting.\n"));
ret = 2;
else
break;
case -EPIPE:
vpn_progress(vpninfo, PRG_ERR, _("Session terminated by server; exiting.\n"));
ret = 1;
break;
case -EINTR:
vpn_progress(vpninfo, PRG_INFO, _("User canceled (SIGINT); exiting.\n"));
ret = 0;
break;
case -ECONNABORTED:
vpn_progress(vpninfo, PRG_INFO, _("User detached from session (SIGHUP); exiting.\n"));
ret = 0;
break;
default:
vpn_progress(vpninfo, PRG_ERR, _("Unknown error; exiting.\n"));
ret = 1;
break;
}

openconnect_vpninfo_free(vpninfo);
exit(ret);
Expand Down
18 changes: 18 additions & 0 deletions openconnect.8.in
Expand Up @@ -420,6 +420,24 @@ applied to the VPN session. If the gateway requires CSD, it will also cause
the corresponding CSD trojan binary to be downloaded, so you may need to use
.B \-\-csd\-wrapper
if this code is not executable on the local machine.
.SH SIGNALS
In the data phase of the connection, the following signals are handled:
.TP
.B SIGINT
performs a clean shutdown by logging the session off, disconnecting from the
gateway, and running the vpnc\-script to restore the network configuration.
.TP
.B SIGHUP
disconnects from the gateway and runs the vpnc\-script, but does not log the
session off; this allows for reconnection later using
.BR \-\-cookie .
.TP
.B SIGUSR2
forces an immediate disconnection and reconnection; this can be used to
quickly recover from LAN IP address changes.
.TP
.B SIGTERM
exits immediately without logging off or running vpnc\-script.
.SH LIMITATIONS
Note that although IPv6 has been tested on all platforms on which
.B openconnect
Expand Down

0 comments on commit 56c5acb

Please sign in to comment.