[PATCH] Add pid-file support

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Devs,

Here is a simple patch to add support for creating a pid-file when
backgrounding. This is obviously not a high priority feature but it
makes it easier to track openconnect sessions after they have been
backgrounded.  While I do not believe that my patch introduces any
bugs, my experience with C is very limited.

Steven Allen

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

diff --git a/main.c b/main.c
index bee6502..0fef578 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;
@@ -571,6 +578,16 @@ int main(int argc, char **argv)
 	if (background) {
 		int pid;
 		if ((pid = fork())) {
+		 	if (pidfile != NULL) {
+				FILE *fp;
+				fp = fopen(pidfile, "w");
+				if (pidfile != NULL) {
+					fprintf(fp, "%d\n", pid);
+					fclose(fp);
+				} else {
+					fprintf(stderr, "Failed to open '%s' for writing\n", pidfile);
+				}
+			}
 			vpn_progress(vpninfo, PRG_INFO,
 					  "Continuing in background; pid %d\n",
 					  pid);
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/8bd4c009/attachment.sig>


[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux