From: Roy Marples Date: Fri, 20 Feb 2015 08:28:04 +0000 (+0000) Subject: Move the pidfile into the dhcpcd context. X-Git-Tag: v6.8.0~88 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ff4df8e09af1ee3d120f583bc5cd08dcdd978a72;p=thirdparty%2Fdhcpcd.git Move the pidfile into the dhcpcd context. When dumping a lease by filename, store the filename in the pidfile. Fixes an issue where the filename overflows the interface name size. --- diff --git a/dhcp-common.c b/dhcp-common.c index 7a14f3a7..d5ccbeef 100644 --- a/dhcp-common.c +++ b/dhcp-common.c @@ -737,6 +737,11 @@ dhcp_set_leasefile(char *leasefile, size_t len, int family, { char ssid[len]; + if (ifp->name[0] == '\0') { + strlcpy(leasefile, ifp->ctx->pidfile, len); + return 0; + } + switch (family) { case AF_INET: case AF_INET6: diff --git a/dhcp.c b/dhcp.c index d6d8b40e..e207f28c 100644 --- a/dhcp.c +++ b/dhcp.c @@ -2919,10 +2919,6 @@ dhcp_dump(struct interface *ifp) dhcp_set_leasefile(state->leasefile, sizeof(state->leasefile), AF_INET, ifp, ""); state->new = read_lease(ifp); - if (state->new == NULL && errno == ENOENT) { - strlcpy(state->leasefile, ifp->name, sizeof(state->leasefile)); - state->new = read_lease(ifp); - } if (state->new == NULL) { if (errno == ENOENT) syslog(LOG_ERR, "%s: no lease to dump", ifp->name); diff --git a/dhcp6.c b/dhcp6.c index 8f232cfd..cbd7b8a9 100644 --- a/dhcp6.c +++ b/dhcp6.c @@ -1966,13 +1966,15 @@ dhcp6_findia(struct interface *ifp, const struct dhcp6_message *m, size_t l, if (memcmp(&ifo->ia[j].iaid, iaid, sizeof(iaid)) == 0) break; } - if (j == ifo->ia_len) { + if (j == ifo->ia_len && + !(ifo->ia_len == 0 && ifp->ctx->options & DHCPCD_DUMPLEASE)) + { syslog(LOG_DEBUG, "%s: ignoring unrequested IAID %s", ifp->name, hwaddr_ntoa(iaid, sizeof(iaid), buf, sizeof(buf))); continue; } - if (ifo->ia[j].ia_type != code) { + if ( j < ifo->ia_len && ifo->ia[j].ia_type != code) { syslog(LOG_ERR, "%s: IAID %s: option type mismatch", ifp->name, hwaddr_ntoa(iaid, sizeof(iaid), buf, sizeof(buf))); @@ -3530,17 +3532,15 @@ dhcp6_dump(struct interface *ifp) int r; ifp->if_data[IF_DATA_DHCP6] = state = calloc(1, sizeof(*state)); - if (state == NULL) - goto eexit; + if (state == NULL) { + syslog(LOG_ERR, "%s: %m", __func__); + return -1; + } TAILQ_INIT(&state->addrs); dhcp_set_leasefile(state->leasefile, sizeof(state->leasefile), AF_INET6, ifp, ifp->options->options & DHCPCD_PFXDLGONLY ? ".pd" : ""); r = dhcp6_readlease(ifp); - if (r == -1 && errno == ENOENT) { - strlcpy(state->leasefile, ifp->name, sizeof(state->leasefile)); - r = dhcp6_readlease(ifp); - } if (r == -1) { if (errno == ENOENT) syslog(LOG_ERR, "%s: no lease to dump", ifp->name); @@ -3550,8 +3550,4 @@ dhcp6_dump(struct interface *ifp) } state->reason = "DUMP6"; return script_runreason(ifp, state->reason); - -eexit: - syslog(LOG_ERR, "%s: %m", __func__); - return -1; } diff --git a/dhcpcd.c b/dhcpcd.c index 00e58ae7..e47bdbe1 100644 --- a/dhcpcd.c +++ b/dhcpcd.c @@ -468,7 +468,9 @@ configure_interface1(struct interface *ifp) } #ifdef INET6 - if (ifo->ia_len == 0 && ifo->options & DHCPCD_IPV6) { + if (ifo->ia_len == 0 && ifo->options & DHCPCD_IPV6 && + ifp->name[0] != '\0') + { ifo->ia = malloc(sizeof(*ifo->ia)); if (ifo->ia == NULL) syslog(LOG_ERR, "%s: %m", __func__); @@ -1284,7 +1286,6 @@ int main(int argc, char **argv) { struct dhcpcd_ctx ctx; - char pidfile[sizeof(PIDFILE) + IF_NAMESIZE + 1]; struct if_options *ifo; struct interface *ifp; uint16_t family = 0; @@ -1491,10 +1492,11 @@ main(int argc, char **argv) default: per = ""; } - snprintf(pidfile, sizeof(pidfile), + snprintf(ctx.pidfile, sizeof(ctx.pidfile), PIDFILE, "-", argv[optind], per); } else { - snprintf(pidfile, sizeof(pidfile), PIDFILE, "", "", ""); + snprintf(ctx.pidfile, sizeof(ctx.pidfile), + PIDFILE, "", "", ""); ctx.options |= DHCPCD_MASTER; } } @@ -1530,9 +1532,15 @@ main(int argc, char **argv) syslog(LOG_ERR, "%s: %m", __func__); goto exit_failure; } - strlcpy(ifp->name, argv[optind], sizeof(ifp->name)); + strlcpy(ctx.pidfile, argv[optind], sizeof(ctx.pidfile)); ifp->ctx = &ctx; TAILQ_INSERT_HEAD(ctx.ifaces, ifp, next); + if (family == 0) { + if (ctx.pidfile[strlen(ctx.pidfile) - 1] == '6') + family = AF_INET6; + else + family = AF_INET; + } } configure_interface(ifp, ctx.argc, ctx.argv); if (ctx.options & DHCPCD_PFXDLGONLY) @@ -1587,7 +1595,7 @@ main(int argc, char **argv) #ifdef USE_SIGNALS if (sig != 0) { - pid = read_pid(pidfile); + pid = read_pid(ctx.pidfile); if (pid != 0) syslog(LOG_INFO, "sending signal %s to pid %d", siga, pid); @@ -1598,7 +1606,7 @@ main(int argc, char **argv) syslog(LOG_ERR, "kill: %m"); goto exit_failure; } - unlink(pidfile); + unlink(ctx.pidfile); if (sig != SIGHUP) goto exit_failure; } else { @@ -1610,7 +1618,7 @@ main(int argc, char **argv) ts.tv_nsec = 100000000; /* 10th of a second */ for(i = 0; i < 100; i++) { nanosleep(&ts, NULL); - if (read_pid(pidfile) == 0) + if (read_pid(ctx.pidfile) == 0) goto exit_success; } syslog(LOG_ERR, "pid %d failed to exit", pid); @@ -1619,12 +1627,12 @@ main(int argc, char **argv) } if (!(ctx.options & DHCPCD_TEST)) { - if ((pid = read_pid(pidfile)) > 0 && + if ((pid = read_pid(ctx.pidfile)) > 0 && kill(pid, 0) == 0) { syslog(LOG_ERR, ""PACKAGE " already running on pid %d (%s)", - pid, pidfile); + pid, ctx.pidfile); goto exit_failure; } @@ -1638,15 +1646,15 @@ main(int argc, char **argv) #ifdef O_CLOEXEC opt |= O_CLOEXEC; #endif - ctx.pid_fd = open(pidfile, opt, 0664); + ctx.pid_fd = open(ctx.pidfile, opt, 0664); if (ctx.pid_fd == -1) - syslog(LOG_ERR, "open `%s': %m", pidfile); + syslog(LOG_ERR, "open `%s': %m", ctx.pidfile); else { #ifdef LOCK_EX /* Lock the file so that only one instance of dhcpcd * runs on an interface */ if (flock(ctx.pid_fd, LOCK_EX | LOCK_NB) == -1) { - syslog(LOG_ERR, "flock `%s': %m", pidfile); + syslog(LOG_ERR, "flock `%s': %m", ctx.pidfile); close(ctx.pid_fd); ctx.pid_fd = -1; goto exit_failure; @@ -1814,7 +1822,7 @@ exit1: syslog(LOG_ERR, "control_stop: %m:"); if (ctx.pid_fd != -1) { close(ctx.pid_fd); - unlink(pidfile); + unlink(ctx.pidfile); } eloop_free(ctx.eloop); diff --git a/dhcpcd.h b/dhcpcd.h index 231225cd..f97a94a7 100644 --- a/dhcpcd.h +++ b/dhcpcd.h @@ -84,9 +84,8 @@ struct interface { TAILQ_HEAD(if_head, interface); struct dhcpcd_ctx { -#ifdef USE_SIGNALS - sigset_t sigset; -#endif + int pid_fd; + char pidfile[sizeof(PIDFILE) + IF_NAMESIZE + 1]; const char *cffile; unsigned long long options; int argc; @@ -101,10 +100,12 @@ struct dhcpcd_ctx { char **ifcv; /* configured interfaces */ unsigned char *duid; size_t duid_len; - int pid_fd; int link_fd; struct if_head *ifaces; +#ifdef USE_SIGNALS + sigset_t sigset; +#endif struct eloop_ctx *eloop; int control_fd;