Add -P, --printpidfile to print the pidfile dhcpcd will use to stdout.

This commit is contained in:
Roy Marples 2016-01-07 19:52:54 +00:00
parent 64cd83651d
commit c31b17193e
5 changed files with 43 additions and 20 deletions

View File

@ -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 @@ logger(struct dhcpcd_ctx *ctx, int pri, const char *fmt, ...)
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);

View File

@ -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 @@ Don't use IPv4LL (aka APIPA, aka Bonjour, aka ZeroConf).
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

View File

@ -109,7 +109,7 @@ static void
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 @@ main(int argc, char **argv)
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 @@ main(int argc, char **argv)
siga = "USR1";
break;
#endif
case 'P':
ctx.options |= DHCPCD_PRINT_PIDFILE;
break;
case 'T':
i = 1;
break;
@ -1528,6 +1533,8 @@ main(int argc, char **argv)
i = 2;
break;
case '?':
if (ctx.options & DHCPCD_PRINT_PIDFILE)
continue;
usage();
goto exit_failure;
}
@ -1539,10 +1546,15 @@ main(int argc, char **argv)
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 @@ main(int argc, char **argv)
#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 @@ main(int argc, char **argv)
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 @@ main(int argc, char **argv)
PIDFILE, "", "", "");
ctx.options |= DHCPCD_MASTER;
}
if (ctx.options & DHCPCD_PRINT_PIDFILE) {
printf("%s\n", ctx.pidfile);
goto exit_success;
}
}
if (chdir("/") == -1)

View File

@ -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 @@ const struct option cf_options[] = {
{"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 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo,
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 @@ add_options(struct dhcpcd_ctx *ctx, const char *ifname,
* 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)

View File

@ -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)