changeset 3399:fca7cfef837e draft

Add -P, --printpidfile to print the pidfile dhcpcd will use to stdout.
author Roy Marples <roy@marples.name>
date Thu, 07 Jan 2016 19:52:54 +0000
parents 2d56b47adb6b
children 875e04c63371
files common.c dhcpcd.8.in dhcpcd.c if-options.c if-options.h
diffstat 5 files changed, 43 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/common.c	Thu Jan 07 16:47:19 2016 +0000
+++ b/common.c	Thu Jan 07 19:52:54 2016 +0000
@@ -1,6 +1,6 @@
 /*
  * dhcpcd - DHCP client daemon
- * Copyright (c) 2006-2015 Roy Marples <roy@marples.name>
+ * Copyright (c) 2006-2016 Roy Marples <roy@marples.name>
  * All rights reserved
 
  * Redistribution and use in source and binary forms, with or without
@@ -113,6 +113,10 @@
 	char fmt_cpy[1024];
 #endif
 
+	/* If we're printing the pidfile, don't do anything. */
+	if (ctx != NULL && ctx->options & DHCPCD_PRINT_PIDFILE)
+		return;
+
 	serrno = errno;
 	va_start(va, fmt);
 
--- a/dhcpcd.8.in	Thu Jan 07 16:47:19 2016 +0000
+++ b/dhcpcd.8.in	Thu Jan 07 19:52:54 2016 +0000
@@ -1,4 +1,4 @@
-.\" Copyright (c) 2006-2015 Roy Marples
+.\" Copyright (c) 2006-2016 Roy Marples
 .\" All rights reserved
 .\"
 .\" Redistribution and use in source and binary forms, with or without
@@ -22,7 +22,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd December 22, 2015
+.Dd January 7, 2016
 .Dt DHCPCD 8
 .Os
 .Sh NAME
@@ -30,7 +30,7 @@
 .Nd a DHCP client
 .Sh SYNOPSIS
 .Nm
-.Op Fl 46ABbDdEGgHJKLMNpqTV
+.Op Fl 46ABbDdEGgHJKLMNPpqTV
 .Op Fl C , Fl Fl nohook Ar hook
 .Op Fl c , Fl Fl script Ar script
 .Op Fl e , Fl Fl env Ar value
@@ -612,6 +612,11 @@
 Removes the
 .Ar option
 from the DHCP message before processing.
+.It Fl P , Fl FL printpidfile
+Print the
+.Pa pidfile
+.Nm
+will use based on commmand-line arguments to stdout.
 .It Fl Q , Fl Fl require Ar option
 Requires the
 .Ar option
--- a/dhcpcd.c	Thu Jan 07 16:47:19 2016 +0000
+++ b/dhcpcd.c	Thu Jan 07 19:52:54 2016 +0000
@@ -109,7 +109,7 @@
 usage(void)
 {
 
-printf("usage: "PACKAGE"\t[-46ABbDdEGgHJKkLnpqTVw]\n"
+printf("usage: "PACKAGE"\t[-46ABbDdEGgHJKkLnPpqTVw]\n"
 	"\t\t[-C, --nohook hook] [-c, --script script]\n"
 	"\t\t[-e, --env value] [-F, --fqdn FQDN] [-f, --config file]\n"
 	"\t\t[-h, --hostname hostname] [-I, --clientid clientid]\n"
@@ -1477,7 +1477,9 @@
 	ctx.udp_fd = -1;
 #endif
 	i = 0;
-	while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1)
+	while ((opt = getopt_long(argc, argv,
+	    ctx.options & DHCPCD_PRINT_PIDFILE ? NOERR_IF_OPTS : IF_OPTS,
+	    cf_options, &oi)) != -1)
 	{
 		switch (opt) {
 		case '4':
@@ -1518,6 +1520,9 @@
 			siga = "USR1";
 			break;
 #endif
+		case 'P':
+			ctx.options |= DHCPCD_PRINT_PIDFILE;
+			break;
 		case 'T':
 			i = 1;
 			break;
@@ -1528,6 +1533,8 @@
 			i = 2;
 			break;
 		case '?':
+			if (ctx.options & DHCPCD_PRINT_PIDFILE)
+				continue;
 			usage();
 			goto exit_failure;
 		}
@@ -1539,10 +1546,15 @@
 	ctx.ifv = argv + optind;
 
 	ifo = read_config(&ctx, NULL, NULL, NULL);
-	if (ifo == NULL)
+	if (ifo == NULL) {
+		if (ctx.options & DHCPCD_PRINT_PIDFILE)
+			goto printpidfile;
 		goto exit_failure;
+	}
 	opt = add_options(&ctx, NULL, ifo, argc, argv);
 	if (opt != 1) {
+		if (ctx.options & DHCPCD_PRINT_PIDFILE)
+			goto printpidfile;
 		if (opt == 0)
 			usage();
 		goto exit_failure;
@@ -1576,7 +1588,7 @@
 #endif
 		goto exit_success;
 	}
-	ctx.options = ifo->options;
+	ctx.options |= ifo->options;
 	if (i == 1 || i == 3) {
 		if (i == 1)
 			ctx.options |= DHCPCD_TEST;
@@ -1592,17 +1604,9 @@
 
 	if (ctx.options & DHCPCD_DEBUG)
 		logger_mask(&ctx, LOG_UPTO(LOG_DEBUG));
-	if (ctx.options & DHCPCD_QUIET) {
-		i = open(_PATH_DEVNULL, O_RDWR);
-		if (i == -1)
-			logger(&ctx, LOG_ERR, "%s: open: %m", __func__);
-		else {
-			dup2(i, STDERR_FILENO);
-			close(i);
-		}
-	}
 
 	if (!(ctx.options & (DHCPCD_TEST | DHCPCD_DUMPLEASE))) {
+printpidfile:
 		/* If we have any other args, we should run as a single dhcpcd
 		 *  instance for that interface. */
 		if (optind == argc - 1 && !(ctx.options & DHCPCD_MASTER)) {
@@ -1632,6 +1636,10 @@
 			    PIDFILE, "", "", "");
 			ctx.options |= DHCPCD_MASTER;
 		}
+		if (ctx.options & DHCPCD_PRINT_PIDFILE) {
+			printf("%s\n", ctx.pidfile);
+			goto exit_success;
+		}
 	}
 
 	if (chdir("/") == -1)
--- a/if-options.c	Thu Jan 07 16:47:19 2016 +0000
+++ b/if-options.c	Thu Jan 07 19:52:54 2016 +0000
@@ -1,6 +1,6 @@
 /*
  * dhcpcd - DHCP client daemon
- * Copyright (c) 2006-2015 Roy Marples <roy@marples.name>
+ * Copyright (c) 2006-2016 Roy Marples <roy@marples.name>
  * All rights reserved
 
  * Redistribution and use in source and binary forms, with or without
@@ -144,6 +144,7 @@
 	{"master",          no_argument,       NULL, 'M'},
 	{"renew",           no_argument,       NULL, 'N'},
 	{"nooption",        required_argument, NULL, 'O'},
+	{"printpidfile",    no_argument,       NULL, 'P'},
 	{"require",         required_argument, NULL, 'Q'},
 	{"static",          required_argument, NULL, 'S'},
 	{"test",            no_argument,       NULL, 'T'},
@@ -684,6 +685,7 @@
 	case 'n': /* FALLTHROUGH */
 	case 'x': /* FALLTHROUGH */
 	case 'N': /* FALLTHROUGH */
+	case 'P': /* FALLTHROUGH */
 	case 'T': /* FALLTHROUGH */
 	case 'U': /* FALLTHROUGH */
 	case 'V': /* We need to handle non interface options */
@@ -2400,7 +2402,9 @@
 	 * only use the dhcpcd.conf entry for that. */
 	if (ifname != NULL)
 		wait_opts = ifo->options & DHCPCD_WAITOPTS;
-	while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1)
+	while ((opt = getopt_long(argc, argv,
+	    ctx->options & DHCPCD_PRINT_PIDFILE ? NOERR_IF_OPTS : IF_OPTS,
+	    cf_options, &oi)) != -1)
 	{
 		r = parse_option(ctx, ifname, ifo, opt, optarg, NULL, NULL);
 		if (r != 1)
--- a/if-options.h	Thu Jan 07 16:47:19 2016 +0000
+++ b/if-options.h	Thu Jan 07 19:52:54 2016 +0000
@@ -42,7 +42,8 @@
 /* Don't set any optional arguments here so we retain POSIX
  * compatibility with getopt */
 #define IF_OPTS "46bc:de:f:gh:i:j:kl:m:no:pqr:s:t:u:v:wxy:z:" \
-		"ABC:DEF:GHI:JKLMNO:Q:S:TUVW:X:Z:"
+		"ABC:DEF:GHI:JKLMNO:PQ:S:TUVW:X:Z:"
+#define NOERR_IF_OPTS		":" IF_OPTS
 
 #define DEFAULT_TIMEOUT		30
 #define DEFAULT_REBOOT		5
@@ -112,6 +113,7 @@
 #define DHCPCD_IPV6RA_ACCEPT_NOPUBLIC	(1ULL << 56)
 #define DHCPCD_BOOTP			(1ULL << 57)
 #define DHCPCD_INITIAL_DELAY		(1ULL << 58)
+#define DHCPCD_PRINT_PIDFILE		(1ULL << 59)
 
 #define DHCPCD_NODROP	(DHCPCD_EXITING | DHCPCD_PERSISTENT)