3 * Copyright 2014 Roy Marples <roy@marples.name>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #if __GNUC__ > 2 || defined(__INTEL_COMPILER)
42 # define __dead __attribute__((__noreturn__))
45 # define __unused __attribute__((__unused__))
57 do_exit(DHCPCD_CONNECTION *con, int code)
66 do_status_cb(DHCPCD_CONNECTION *con, const char *status, void __unused *arg)
69 syslog(LOG_INFO, "%s", status);
70 if (strcmp(status, "connected") == 0)
71 do_exit(con, EXIT_SUCCESS);
75 main(int argc, char **argv)
77 DHCPCD_CONNECTION *con;
79 struct timespec now, end, t;
81 int timeout, n, lerrno;
90 openlog("dhcpcd-online", LOG_PERROR, 0);
91 setlogmask(LOG_UPTO(LOG_INFO));
93 while ((n = getopt(argc, argv, "qt:x")) != -1) {
97 openlog("dhcpcd-online", 0, 0);
100 lnum = strtol(optarg, &lend, 0);
101 if (lend == NULL || *lend != '\0' ||
102 lnum < 0 || lnum > INT_MAX)
104 syslog(LOG_ERR, "-t %s: invalid timeout",
114 fprintf(stderr, "usage: dhcpcd-online "
115 "[-q] [-t timeout]\n");
120 if ((con = dhcpcd_new()) == NULL) {
121 syslog(LOG_ERR, "dhcpcd_new: %m");
125 dhcpcd_set_status_callback(con, do_status_cb, NULL);
127 if ((pfd.fd = dhcpcd_open(con)) == -1) {
129 syslog(LOG_WARNING, "dhcpcd_open: %m");
131 do_exit(con, EXIT_FAILURE);
137 /* Work out our timeout time */
138 if (clock_gettime(CLOCK_MONOTONIC, &end) == -1) {
139 syslog(LOG_ERR, "clock_gettime: %m");
140 do_exit(con, EXIT_FAILURE);
142 end.tv_sec += timeout;
145 if (clock_gettime(CLOCK_MONOTONIC, &now) == -1) {
146 syslog(LOG_ERR, "clock_gettime: %m");
147 do_exit(con, EXIT_FAILURE);
149 if (timespeccmp(&now, &end, >)) {
150 syslog(LOG_ERR, "timed out");
151 do_exit(con, EXIT_FAILURE);
154 n = poll(NULL, 0, DHCPCD_RETRYOPEN);
156 /* poll(2) should really take a timespec */
157 timespecsub(&end, &now, &t);
158 if (t.tv_sec > INT_MAX / 1000 ||
159 (t.tv_sec == INT_MAX / 1000 &&
160 (t.tv_nsec + 999999) / 1000000 > INT_MAX % 1000000))
163 timeout = (int)t.tv_sec * 1000 +
164 (t.tv_nsec + 999999) / 1000000;
165 n = poll(&pfd, 1, timeout);
168 syslog(LOG_ERR, "poll: %m");
169 do_exit(con, EXIT_FAILURE);
172 if ((pfd.fd = dhcpcd_open(con)) == -1) {
173 if (lerrno != errno) {
175 syslog(LOG_WARNING, "dhcpcd_open: %m");
179 if (n > 0 && pfd.revents)
180 dhcpcd_dispatch(con);
184 /* Impossible to reach here */
185 do_exit(con, EXIT_FAILURE);