From: Roy Marples Date: Thu, 7 Jan 2016 19:52:54 +0000 (+0000) Subject: Add -P, --printpidfile to print the pidfile dhcpcd will use to stdout. X-Git-Tag: v6.10.1~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c31b17193ec9949d62a675291f9e5b950ad4a135;p=thirdparty%2Fdhcpcd.git Add -P, --printpidfile to print the pidfile dhcpcd will use to stdout. --- diff --git a/common.c b/common.c index 0f1b013f..c3224b49 100644 --- a/common.c +++ b/common.c @@ -1,6 +1,6 @@ /* * dhcpcd - DHCP client daemon - * 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 @@ -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); diff --git a/dhcpcd.8.in b/dhcpcd.8.in index 5b8ab160..f5039980 100644 --- a/dhcpcd.8.in +++ b/dhcpcd.8.in @@ -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 diff --git a/dhcpcd.c b/dhcpcd.c index 9ac56c6b..e9e78e41 100644 --- a/dhcpcd.c +++ b/dhcpcd.c @@ -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) diff --git a/if-options.c b/if-options.c index 8bff82a3..ddf6aaf9 100644 --- a/if-options.c +++ b/if-options.c @@ -1,6 +1,6 @@ /* * dhcpcd - DHCP client daemon - * 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 @@ -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) diff --git a/if-options.h b/if-options.h index 93450f10..a6a98f8a 100644 --- a/if-options.h +++ b/if-options.h @@ -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)