Disconnect when same SSID selected (#1)
[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 /* Incase we need to pass anything else in context to status cb */
75 struct ctx {
76         struct pollfd pollfd;
77 };
78
79 static void __dead
80 do_exit(DHCPCD_CONNECTION *con, int code)
81 {
82
83         /* Unregister the status callback so that close doesn't spam. */
84         dhcpcd_set_status_callback(con, NULL, NULL);
85
86         dhcpcd_close(con);
87         dhcpcd_free(con);
88         exit(code);
89 }
90
91 static void
92 status_cb(DHCPCD_CONNECTION *con,
93     unsigned int status, const char *status_msg, void *arg)
94 {
95         struct ctx *ctx;
96
97         syslog(LOG_INFO, "%s", status_msg);
98         switch (status) {
99         case DHC_CONNECTED:
100                 do_exit(con, EXIT_SUCCESS);
101                 /* NOT REACHED */
102         case DHC_DOWN:
103                 ctx = arg;
104                 ctx->pollfd.fd = -1;
105                 break;
106         }
107 }
108
109 int
110 main(int argc, char **argv)
111 {
112         DHCPCD_CONNECTION *con;
113         bool xflag;
114         struct timespec now, end, t;
115         struct ctx ctx;
116         int timeout, n, lerrno;
117         long lnum;
118         char *lend;
119
120         /* Defaults */
121         timeout = 30;
122
123         xflag = false;
124         ctx.pollfd.fd = -1;
125         ctx.pollfd.events = POLLIN;
126         ctx.pollfd.revents = 0;
127
128         openlog("dhcpcd-online", LOG_PERROR, 0);
129         setlogmask(LOG_UPTO(LOG_INFO));
130
131         while ((n = getopt(argc, argv, "qt:x")) != -1) {
132                 switch (n) {
133                 case 'q':
134                         closelog();
135                         openlog("dhcpcd-online", 0, 0);
136                         break;
137                 case 't':
138                         lnum = strtol(optarg, &lend, 0);
139                         if (lend == NULL || *lend != '\0' ||
140                             lnum < 0 || lnum > INT_MAX)
141                         {
142                                 syslog(LOG_ERR, "-t %s: invalid timeout",
143                                     optarg);
144                                 exit(EXIT_FAILURE);
145                         }
146                         timeout = (int)lnum;
147                         break;
148                 case 'x':
149                         xflag = true;
150                         break;
151                 case '?':
152                         fprintf(stderr, "usage: dhcpcd-online "
153                             "[-q] [-t timeout]\n");
154                         exit(EXIT_FAILURE);
155                 }
156         }
157
158         if ((con = dhcpcd_new()) == NULL) {
159                 syslog(LOG_ERR, "dhcpcd_new: %m");
160                 return EXIT_FAILURE;
161         }
162         dhcpcd_set_status_callback(con, status_cb, &ctx);
163
164         if ((ctx.pollfd.fd = dhcpcd_open(con, false)) == -1) {
165                 lerrno = errno;
166                 syslog(LOG_WARNING, "dhcpcd_open: %m");
167                 if (xflag)
168                         do_exit(con, EXIT_FAILURE);
169         } else
170                 lerrno = 0;
171
172         /* Work out our timeout time */
173         if (clock_gettime(CLOCK_MONOTONIC, &end) == -1) {
174                 syslog(LOG_ERR, "clock_gettime: %m");
175                 do_exit(con, EXIT_FAILURE);
176         }
177         end.tv_sec += timeout;
178
179         for (;;) {
180                 if (clock_gettime(CLOCK_MONOTONIC, &now) == -1) {
181                         syslog(LOG_ERR, "clock_gettime: %m");
182                         do_exit(con, EXIT_FAILURE);
183                 }
184                 if (timespeccmp(&now, &end, >)) {
185                         syslog(LOG_ERR, "timed out");
186                         do_exit(con, EXIT_FAILURE);
187                 }
188                 if (ctx.pollfd.fd == -1) {
189                         n = poll(NULL, 0, DHCPCD_RETRYOPEN);
190                 } else {
191                         /* poll(2) should really take a timespec */
192                         timespecsub(&end, &now, &t);
193                         if (t.tv_sec > INT_MAX / 1000 ||
194                             (t.tv_sec == INT_MAX / 1000 &&
195                             (t.tv_nsec + 999999) / 1000000 > INT_MAX % 1000000))
196                                 timeout = INT_MAX;
197                         else
198                                 timeout = (int)(t.tv_sec * 1000 +
199                                     (t.tv_nsec + 999999) / 1000000);
200                         n = poll(&ctx.pollfd, 1, timeout);
201                 }
202                 if (n == -1) {
203                         syslog(LOG_ERR, "poll: %m");
204                         do_exit(con, EXIT_FAILURE);
205                 }
206                 if (ctx.pollfd.fd == -1) {
207                         if ((ctx.pollfd.fd = dhcpcd_open(con, false)) == -1) {
208                                 if (lerrno != errno) {
209                                         lerrno = errno;
210                                         syslog(LOG_WARNING, "dhcpcd_open: %m");
211                                 }
212                         }
213                 } else {
214                         if (n > 0 && ctx.pollfd.revents)
215                                 dhcpcd_dispatch(con);
216                 }
217         }
218
219         /* Impossible to reach here */
220         do_exit(con, EXIT_FAILURE);
221 }