Fix compile on Linux and fix some memory errors.
[dhcpcd-ui] / src / dhcpcd-online / dhcpcd-online.c
1 /*
2  * dhcpcd-online
3  * Copyright 2014-2015 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <errno.h>
28 #include <limits.h>
29 #include <poll.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <syslog.h>
35 #include <time.h>
36 #include <unistd.h>
37
38 #include "dhcpcd.h"
39
40 #if __GNUC__ > 2 || defined(__INTEL_COMPILER)
41 # ifndef __dead
42 #  define __dead __attribute__((__noreturn__))
43 # endif
44 # ifndef __unused
45 #  define __unused   __attribute__((__unused__))
46 # endif
47 #else
48 # ifndef __dead
49 #  define __dead
50 # endif
51 # ifndef __unused
52 #  define __unused
53 # endif
54 #endif
55
56 #ifndef timespeccmp
57 #define timespeccmp(tsp, usp, cmp)                                      \
58         (((tsp)->tv_sec == (usp)->tv_sec) ?                             \
59             ((tsp)->tv_nsec cmp (usp)->tv_nsec) :                       \
60             ((tsp)->tv_sec cmp (usp)->tv_sec))
61 #endif
62 #ifndef timespecsub
63 #define timespecsub(tsp, usp, vsp)                                      \
64         do {                                                            \
65                 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;          \
66                 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;       \
67                 if ((vsp)->tv_nsec < 0) {                               \
68                         (vsp)->tv_sec--;                                \
69                         (vsp)->tv_nsec += 1000000000L;                  \
70                 }                                                       \
71         } while (/* CONSTCOND */ 0)
72 #endif
73
74 static void __dead
75 do_exit(DHCPCD_CONNECTION *con, int code)
76 {
77
78         /* Unregister the status callback so that close doesn't spam. */
79         dhcpcd_set_status_callback(con, NULL, NULL);
80
81         dhcpcd_close(con);
82         dhcpcd_free(con);
83         exit(code);
84 }
85
86 static void
87 do_status_cb(DHCPCD_CONNECTION *con, const char *status, void *arg)
88 {
89         struct pollfd *pfd;
90
91         syslog(LOG_INFO, "%s", status);
92         if (strcmp(status, "connected") == 0)
93                 do_exit(con, EXIT_SUCCESS);
94         if (strcmp(status, "down") == 0) {
95                 pfd = arg;
96                 pfd->fd = -1;
97         }
98 }
99
100 int
101 main(int argc, char **argv)
102 {
103         DHCPCD_CONNECTION *con;
104         bool xflag;
105         struct timespec now, end, t;
106         struct pollfd pfd;
107         int timeout, n, lerrno;
108         long lnum;
109         char *lend;
110
111         /* Defaults */
112         timeout = 30;
113
114         xflag = false;
115
116         openlog("dhcpcd-online", LOG_PERROR, 0);
117         setlogmask(LOG_UPTO(LOG_INFO));
118
119         while ((n = getopt(argc, argv, "qt:x")) != -1) {
120                 switch (n) {
121                 case 'q':
122                         closelog();
123                         openlog("dhcpcd-online", 0, 0);
124                         break;
125                 case 't':
126                         lnum = strtol(optarg, &lend, 0);
127                         if (lend == NULL || *lend != '\0' ||
128                             lnum < 0 || lnum > INT_MAX)
129                         {
130                                 syslog(LOG_ERR, "-t %s: invalid timeout",
131                                     optarg);
132                                 exit(EXIT_FAILURE);
133                         }
134                         timeout = (int)lnum;
135                         break;
136                 case 'x':
137                         xflag = true;
138                         break;
139                 case '?':
140                         fprintf(stderr, "usage: dhcpcd-online "
141                             "[-q] [-t timeout]\n");
142                         exit(EXIT_FAILURE);
143                 }
144         }
145
146         if ((con = dhcpcd_new()) == NULL) {
147                 syslog(LOG_ERR, "dhcpcd_new: %m");
148                 return EXIT_FAILURE;
149         }
150
151         dhcpcd_set_status_callback(con, do_status_cb, &pfd);
152
153         if ((pfd.fd = dhcpcd_open(con, false)) == -1) {
154                 lerrno = errno;
155                 syslog(LOG_WARNING, "dhcpcd_open: %m");
156                 if (xflag)
157                         do_exit(con, EXIT_FAILURE);
158         } else
159                 lerrno = 0;
160         pfd.events = POLLIN;
161         pfd.revents = 0;
162
163         /* Work out our timeout time */
164         if (clock_gettime(CLOCK_MONOTONIC, &end) == -1) {
165                 syslog(LOG_ERR, "clock_gettime: %m");
166                 do_exit(con, EXIT_FAILURE);
167         }
168         end.tv_sec += timeout;
169
170         for (;;) {
171                 if (clock_gettime(CLOCK_MONOTONIC, &now) == -1) {
172                         syslog(LOG_ERR, "clock_gettime: %m");
173                         do_exit(con, EXIT_FAILURE);
174                 }
175                 if (timespeccmp(&now, &end, >)) {
176                         syslog(LOG_ERR, "timed out");
177                         do_exit(con, EXIT_FAILURE);
178                 }
179                 if (pfd.fd == -1) {
180                         n = poll(NULL, 0, DHCPCD_RETRYOPEN);
181                 } else {
182                         /* poll(2) should really take a timespec */
183                         timespecsub(&end, &now, &t);
184                         if (t.tv_sec > INT_MAX / 1000 ||
185                             (t.tv_sec == INT_MAX / 1000 &&
186                             (t.tv_nsec + 999999) / 1000000 > INT_MAX % 1000000))
187                                 timeout = INT_MAX;
188                         else
189                                 timeout = (int)(t.tv_sec * 1000 +
190                                     (t.tv_nsec + 999999) / 1000000);
191                         n = poll(&pfd, 1, timeout);
192                 }
193                 if (n == -1) {
194                         syslog(LOG_ERR, "poll: %m");
195                         do_exit(con, EXIT_FAILURE);
196                 }
197                 if (pfd.fd == -1) {
198                         if ((pfd.fd = dhcpcd_open(con, false)) == -1) {
199                                 if (lerrno != errno) {
200                                         lerrno = errno;
201                                         syslog(LOG_WARNING, "dhcpcd_open: %m");
202                                 }
203                         }
204                 } else {
205                         if (n > 0 && pfd.revents)
206                                 dhcpcd_dispatch(con);
207                 }
208         }
209
210         /* Impossible to reach here */
211         do_exit(con, EXIT_FAILURE);
212 }