]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Add -P, --printpidfile to print the pidfile dhcpcd will use to stdout.
authorRoy Marples <roy@marples.name>
Thu, 7 Jan 2016 19:52:54 +0000 (19:52 +0000)
committerRoy Marples <roy@marples.name>
Thu, 7 Jan 2016 19:52:54 +0000 (19:52 +0000)
common.c
dhcpcd.8.in
dhcpcd.c
if-options.c
if-options.h

index 0f1b013f59cce9e4d1951bf2fa4fdb2a13a55249..c3224b493a081bc055ea9929099078e696affd31 100644 (file)
--- a/common.c
+++ b/common.c
@@ -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);
 
index 5b8ab1604115b58da239eeae215e9cd722191cf8..f5039980fbcccbfccc01dc3e66504619b14576d9 100644 (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
index 9ac56c6ba2f1de44a7938a7aaf2691cca0fc1512..e9e78e4143ef401561a6947eda8a6f2e35619ba3 100644 (file)
--- 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)
index 8bff82a37189e1c4376e84c2474a11a52f746db4..ddf6aaf9b45edac23ec9a7c1343bfba382076bf5 100644 (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)
index 93450f100237d70484b7731dca23ab8a8d11f537..a6a98f8a90a20394f7cc538217a4ea43611d27b9 100644 (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
 #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)