[PATCH] Add pid-file support

Jason openconnect at lakedaemon.net
Fri Aug 5 15:57:46 EDT 2011


Hi Steven,

On Fri, Aug 05, 2011 at 12:29:31PM -0700, Steven Allen wrote:
> 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) {

s/pidfile/fp/ ?  You already checked it three lines above.

> +					fprintf(fp, "%d\n", pid);
> +					fclose(fp);
> +				} else {
> +					fprintf(stderr, "Failed to open '%s' for writing\n", pidfile);

I would exit fail here.  If the user or a startup script expect a pid
file to be created, then failing out is appropriate.

> +				}
> +			}
>  			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

hth,

Jason.



More information about the openconnect-devel mailing list