[PATCH] Add pid-file support

Steven Allen steven at stebalien.com
Fri Aug 5 17:37:18 EDT 2011


Jason,

The patch now correctly checks fp instead of pid-file. Also, the
pid-file is now created before forking so that openconnect can be
aborted on failure.

Steven Allen

---
 main.c        |   22 ++++++++++++++++++++++
 openconnect.8 |    9 +++++++++
 2 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/main.c b/main.c
index bee6502..a5c6c47 100644
--- a/main.c
+++ b/main.c
@@ -81,6 +81,7 @@ enum {
 	OPT_NO_HTTP_KEEPALIVE,
 	OPT_NO_PASSWD,
 	OPT_NO_PROXY,
+	OPT_PIDFILE,
 	OPT_PASSWORD_ON_STDIN,
 	OPT_PRINTCOOKIE,
 	OPT_RECONNECT_TIMEOUT,
@@ -91,6 +92,7 @@ enum {
 
 static struct option long_options[] = {
 	{"background", 0, 0, 'b'},
+	{"pid-file", 1, 0, OPT_PIDFILE},
 	{"certificate", 1, 0, 'c'},
 	{"sslkey", 1, 0, 'k'},
 	{"cookie", 1, 0, 'C'},
@@ -143,6 +145,7 @@ void usage(void)
 	printf("Usage:  openconnect [options] <server>\n");
 	printf("Open client for Cisco AnyConnect VPN, version %s\n\n", openconnect_version);
 	printf("  -b, --background                Continue in background after startup\n");
+	printf("      --pid-file=PIDFILE          Write the daemons pid to this file\n");
 	printf("  -c, --certificate=CERT          Use SSL client certificate CERT\n");
 	printf("  -k, --sslkey=KEY                Use SSL private key file KEY\n");
 	printf("  -K, --key-type=TYPE             Private key type (PKCS#12 / TPM / PEM)\n");
@@ -230,6 +233,7 @@ int main(int argc, char **argv)
 	int autoproxy = 0;
 	uid_t uid = getuid();
 	int opt;
+	char *pidfile = NULL;
 
 	openconnect_init_openssl();
 
@@ -268,6 +272,9 @@ int main(int argc, char **argv)
 		case OPT_CAFILE:
 			vpninfo->cafile = optarg;
 			break;
+		case OPT_PIDFILE:
+			pidfile = optarg;
+			break;
 		case OPT_SERVERCERT:
 			vpninfo->servercert = optarg;
 			break;
@@ -570,12 +577,27 @@ int main(int argc, char **argv)
 
 	if (background) {
 		int pid;
+		FILE *fp = NULL;
+		if (pidfile != NULL) {
+			fp = fopen(pidfile, "w");
+			if (fp == NULL) {
+				fprintf(stderr, "Failed to open '%s' for write: %s\n",
+						pidfile, strerror(errno));
+				exit(1);
+			}
+		}
 		if ((pid = fork())) {
+		 	if (fp != NULL) {
+				fprintf(fp, "%d\n", pid);
+				fclose(fp);
+			}
 			vpn_progress(vpninfo, PRG_INFO,
 					  "Continuing in background; pid %d\n",
 					  pid);
 			exit(0);
 		}
+		if (fp != NULL)
+			fclose(fp);
 	}
 	vpn_mainloop(vpninfo);
 	exit(1);
diff --git a/openconnect.8 b/openconnect.8
index 352fa89..ab71a64 100644
--- a/openconnect.8
+++ b/openconnect.8
@@ -7,6 +7,10 @@ openconnect \- Connect to Cisco AnyConnect VPN
 .B -b,--background
 ]
 [
+.B --pid-file
+.I PIDFILE
+]
+[
 .B -c,--certificate
 .I CERT
 ]
@@ -188,6 +192,11 @@ exchanged, which allows data transport over UDP to occur.
 .B -b,--background
 Continue in background after startup
 .TP
+.B --pid-file=PIDFILE
+Save the pid to
+.I PIDFILE
+when backgrounding
+.TP
 .B -c,--certificate=CERT
 Use SSL client certificate
 .I CERT
-- 
1.7.6

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/openconnect-devel/attachments/20110805/5ff53d2f/attachment.sig>


More information about the openconnect-devel mailing list