diff options
author | des <des@FreeBSD.org> | 2011-10-13 17:20:45 +0000 |
---|---|---|
committer | des <des@FreeBSD.org> | 2011-10-13 17:20:45 +0000 |
commit | ad844b42a2ded6583ac9176413a9ef594c5e677b (patch) | |
tree | 572fea409097b2acc4b4642288642b8048c9efca /sbin/dhclient/dhclient.c | |
parent | c9a3d28e97b357af334a68382823f39076347e40 (diff) | |
download | FreeBSD-src-ad844b42a2ded6583ac9176413a9ef594c5e677b.zip FreeBSD-src-ad844b42a2ded6583ac9176413a9ef594c5e677b.tar.gz |
Make dhclient use a pid file. Modify the rc script accordingly; while
there, clean it up and add some error checks.
Glanced at by: brooks@
MFC after: 3 weeks
Diffstat (limited to 'sbin/dhclient/dhclient.c')
-rw-r--r-- | sbin/dhclient/dhclient.c | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c index afb6d63..0521730 100644 --- a/sbin/dhclient/dhclient.c +++ b/sbin/dhclient/dhclient.c @@ -95,6 +95,9 @@ struct iaddr iaddr_broadcast = { 4, { 255, 255, 255, 255 } }; struct in_addr inaddr_any; struct sockaddr_in sockaddr_broadcast; +char *path_dhclient_pidfile; +struct pidfh *pidfile; + /* * ASSERT_STATE() does nothing now; it used to be * assert (state_is == state_shouldbe). @@ -316,6 +319,8 @@ die: if (ifi->client->alias) script_write_params("alias_", ifi->client->alias); script_go(); + if (pidfile != NULL) + pidfile_remove(pidfile); exit(1); } @@ -327,12 +332,13 @@ main(int argc, char *argv[]) int pipe_fd[2]; int immediate_daemon = 0; struct passwd *pw; + pid_t otherpid; /* Initially, log errors to stderr as well as to syslogd. */ openlog(__progname, LOG_PID | LOG_NDELAY, DHCPD_LOG_FACILITY); setlogmask(LOG_UPTO(LOG_DEBUG)); - while ((ch = getopt(argc, argv, "bc:dl:qu")) != -1) + while ((ch = getopt(argc, argv, "bc:dl:p:qu")) != -1) switch (ch) { case 'b': immediate_daemon = 1; @@ -346,6 +352,9 @@ main(int argc, char *argv[]) case 'l': path_dhclient_db = optarg; break; + case 'p': + path_dhclient_pidfile = optarg; + break; case 'q': quiet = 1; break; @@ -362,6 +371,21 @@ main(int argc, char *argv[]) if (argc != 1) usage(); + if (path_dhclient_pidfile == NULL) { + asprintf(&path_dhclient_pidfile, + "%sdhclient.%s.pid", _PATH_VARRUN, *argv); + if (path_dhclient_pidfile == NULL) + error("asprintf"); + } + pidfile = pidfile_open(path_dhclient_pidfile, 0600, &otherpid); + if (pidfile == NULL) { + if (errno == EEXIST) + error("dhclient already running, pid: %d.", otherpid); + if (errno == EAGAIN) + error("dhclient already running."); + warning("Cannot open or create pidfile: %m"); + } + if ((ifi = calloc(1, sizeof(struct interface_info))) == NULL) error("calloc"); if (strlcpy(ifi->name, argv[0], IFNAMSIZ) >= IFNAMSIZ) @@ -385,6 +409,12 @@ main(int argc, char *argv[]) read_client_conf(); + /* The next bit is potentially very time-consuming, so write out + the pidfile right away. We will write it out again with the + correct pid after daemonizing. */ + if (pidfile != NULL) + pidfile_write(pidfile); + if (!interface_link_status(ifi->name)) { fprintf(stderr, "%s: no link ...", ifi->name); fflush(stderr); @@ -2298,6 +2328,9 @@ go_daemon(void) if (daemon(1, 0) == -1) error("daemon"); + if (pidfile != NULL) + pidfile_write(pidfile); + /* we are chrooted, daemon(3) fails to open /dev/null */ if (nullfd != -1) { dup2(nullfd, STDIN_FILENO); |