blob: d56935d8cdffb4fa3b9654ec7acec85aff578c60 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / main() function for UNIX like OSes and MinGW
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003 * Copyright (c) 2003-2013, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10#ifdef __linux__
11#include <fcntl.h>
12#endif /* __linux__ */
13
14#include "common.h"
15#include "wpa_supplicant_i.h"
16#include "driver_i.h"
Dmitry Shmidt34af3062013-07-11 10:46:32 -070017#include "p2p_supplicant.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019
20static void usage(void)
21{
22 int i;
23 printf("%s\n\n%s\n"
24 "usage:\n"
Dmitry Shmidt18463232014-01-24 12:29:41 -080025 " wpa_supplicant [-BddhKLqq"
26#ifdef CONFIG_DEBUG_SYSLOG
27 "s"
28#endif /* CONFIG_DEBUG_SYSLOG */
29 "t"
30#ifdef CONFIG_DBUS
31 "u"
32#endif /* CONFIG_DBUS */
33 "vW] [-P<pid file>] "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034 "[-g<global ctrl>] \\\n"
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070035 " [-G<group>] \\\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036 " -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
37 "[-p<driver_param>] \\\n"
Dmitry Shmidt18463232014-01-24 12:29:41 -080038 " [-b<br_ifname>] [-e<entropy file>]"
39#ifdef CONFIG_DEBUG_FILE
40 " [-f<debug file>]"
41#endif /* CONFIG_DEBUG_FILE */
42 " \\\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043 " [-o<override driver>] [-O<override ctrl>] \\\n"
44 " [-N -i<ifname> -c<conf> [-C<ctrl>] "
45 "[-D<driver>] \\\n"
Jouni Malinen5d1c8ad2013-04-23 12:34:56 -070046 " [-p<driver_param>] [-b<br_ifname>] [-I<config file>] "
47 "...]\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048 "\n"
49 "drivers:\n",
50 wpa_supplicant_version, wpa_supplicant_license);
51
52 for (i = 0; wpa_drivers[i]; i++) {
53 printf(" %s = %s\n",
54 wpa_drivers[i]->name,
55 wpa_drivers[i]->desc);
56 }
57
58#ifndef CONFIG_NO_STDOUT_DEBUG
59 printf("options:\n"
60 " -b = optional bridge interface name\n"
61 " -B = run daemon in the background\n"
62 " -c = Configuration file\n"
63 " -C = ctrl_interface parameter (only used if -c is not)\n"
64 " -i = interface name\n"
Jouni Malinen5d1c8ad2013-04-23 12:34:56 -070065 " -I = additional configuration file\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070066 " -d = increase debugging verbosity (-dd even more)\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -070067 " -D = driver name (can be multiple drivers: nl80211,wext)\n"
68 " -e = entropy file\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069#ifdef CONFIG_DEBUG_FILE
70 printf(" -f = log output to debug file instead of stdout\n");
71#endif /* CONFIG_DEBUG_FILE */
72 printf(" -g = global ctrl_interface\n"
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070073 " -G = global ctrl_interface group\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074 " -K = include keys (passwords, etc.) in debug output\n");
75#ifdef CONFIG_DEBUG_SYSLOG
76 printf(" -s = log output to syslog instead of stdout\n");
77#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt04949592012-07-19 12:16:46 -070078#ifdef CONFIG_DEBUG_LINUX_TRACING
79 printf(" -T = record to Linux tracing in addition to logging\n");
80 printf(" (records all messages regardless of debug verbosity)\n");
81#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082 printf(" -t = include timestamp in debug messages\n"
83 " -h = show this help text\n"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080084 " -L = show license (BSD)\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070085 " -o = override driver parameter for new interfaces\n"
86 " -O = override ctrl_interface parameter for new interfaces\n"
87 " -p = driver parameters\n"
88 " -P = PID file\n"
89 " -q = decrease debugging verbosity (-qq even less)\n");
90#ifdef CONFIG_DBUS
91 printf(" -u = enable DBus control interface\n");
92#endif /* CONFIG_DBUS */
93 printf(" -v = show version\n"
94 " -W = wait for a control interface monitor before starting\n"
95 " -N = start describing new interface\n");
96
97 printf("example:\n"
98 " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
Dmitry Shmidt4b060592013-04-29 16:42:49 -070099 wpa_drivers[0] ? wpa_drivers[0]->name : "nl80211");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100#endif /* CONFIG_NO_STDOUT_DEBUG */
101}
102
103
104static void license(void)
105{
106#ifndef CONFIG_NO_STDOUT_DEBUG
107 printf("%s\n\n%s%s%s%s%s\n",
108 wpa_supplicant_version,
109 wpa_supplicant_full_license1,
110 wpa_supplicant_full_license2,
111 wpa_supplicant_full_license3,
112 wpa_supplicant_full_license4,
113 wpa_supplicant_full_license5);
114#endif /* CONFIG_NO_STDOUT_DEBUG */
115}
116
117
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700118static void wpa_supplicant_fd_workaround(int start)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119{
120#ifdef __linux__
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700121 static int fd[3] = { -1, -1, -1 };
122 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700123 /* When started from pcmcia-cs scripts, wpa_supplicant might start with
124 * fd 0, 1, and 2 closed. This will cause some issues because many
125 * places in wpa_supplicant are still printing out to stdout. As a
126 * workaround, make sure that fd's 0, 1, and 2 are not used for other
127 * sockets. */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700128 if (start) {
129 for (i = 0; i < 3; i++) {
130 fd[i] = open("/dev/null", O_RDWR);
131 if (fd[i] > 2) {
132 close(fd[i]);
133 fd[i] = -1;
134 break;
135 }
136 }
137 } else {
138 for (i = 0; i < 3; i++) {
139 if (fd[i] >= 0) {
140 close(fd[i]);
141 fd[i] = -1;
142 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700143 }
144 }
145#endif /* __linux__ */
146}
147
148
149int main(int argc, char *argv[])
150{
151 int c, i;
152 struct wpa_interface *ifaces, *iface;
153 int iface_count, exitcode = -1;
154 struct wpa_params params;
155 struct wpa_global *global;
156
157 if (os_program_init())
158 return -1;
159
160 os_memset(&params, 0, sizeof(params));
161 params.wpa_debug_level = MSG_INFO;
162
163 iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
164 if (ifaces == NULL)
165 return -1;
166 iface_count = 1;
167
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700168 wpa_supplicant_fd_workaround(1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169
170 for (;;) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700171 c = getopt(argc, argv,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700172 "b:Bc:C:D:de:f:g:G:hi:I:KLNo:O:p:P:qsTtuvW");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173 if (c < 0)
174 break;
175 switch (c) {
176 case 'b':
177 iface->bridge_ifname = optarg;
178 break;
179 case 'B':
180 params.daemonize++;
181 break;
182 case 'c':
183 iface->confname = optarg;
184 break;
185 case 'C':
186 iface->ctrl_interface = optarg;
187 break;
188 case 'D':
189 iface->driver = optarg;
190 break;
191 case 'd':
192#ifdef CONFIG_NO_STDOUT_DEBUG
193 printf("Debugging disabled with "
194 "CONFIG_NO_STDOUT_DEBUG=y build time "
195 "option.\n");
196 goto out;
197#else /* CONFIG_NO_STDOUT_DEBUG */
198 params.wpa_debug_level--;
199 break;
200#endif /* CONFIG_NO_STDOUT_DEBUG */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700201 case 'e':
202 params.entropy_file = optarg;
203 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204#ifdef CONFIG_DEBUG_FILE
205 case 'f':
206 params.wpa_debug_file_path = optarg;
207 break;
208#endif /* CONFIG_DEBUG_FILE */
209 case 'g':
210 params.ctrl_interface = optarg;
211 break;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700212 case 'G':
213 params.ctrl_interface_group = optarg;
214 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 case 'h':
216 usage();
217 exitcode = 0;
218 goto out;
219 case 'i':
220 iface->ifname = optarg;
221 break;
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700222 case 'I':
223 iface->confanother = optarg;
224 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225 case 'K':
226 params.wpa_debug_show_keys++;
227 break;
228 case 'L':
229 license();
230 exitcode = 0;
231 goto out;
232 case 'o':
233 params.override_driver = optarg;
234 break;
235 case 'O':
236 params.override_ctrl_interface = optarg;
237 break;
238 case 'p':
239 iface->driver_param = optarg;
240 break;
241 case 'P':
242 os_free(params.pid_file);
243 params.pid_file = os_rel2abs_path(optarg);
244 break;
245 case 'q':
246 params.wpa_debug_level++;
247 break;
248#ifdef CONFIG_DEBUG_SYSLOG
249 case 's':
250 params.wpa_debug_syslog++;
251 break;
252#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700253#ifdef CONFIG_DEBUG_LINUX_TRACING
254 case 'T':
255 params.wpa_debug_tracing++;
256 break;
257#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700258 case 't':
259 params.wpa_debug_timestamp++;
260 break;
261#ifdef CONFIG_DBUS
262 case 'u':
263 params.dbus_ctrl_interface = 1;
264 break;
265#endif /* CONFIG_DBUS */
266 case 'v':
267 printf("%s\n", wpa_supplicant_version);
268 exitcode = 0;
269 goto out;
270 case 'W':
271 params.wait_for_monitor++;
272 break;
273 case 'N':
274 iface_count++;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700275 iface = os_realloc_array(ifaces, iface_count,
276 sizeof(struct wpa_interface));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 if (iface == NULL)
278 goto out;
279 ifaces = iface;
280 iface = &ifaces[iface_count - 1];
281 os_memset(iface, 0, sizeof(*iface));
282 break;
283 default:
284 usage();
285 exitcode = 0;
286 goto out;
287 }
288 }
289
290 exitcode = 0;
291 global = wpa_supplicant_init(&params);
292 if (global == NULL) {
293 wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
294 exitcode = -1;
295 goto out;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700296 } else {
297 wpa_printf(MSG_INFO, "Successfully initialized "
298 "wpa_supplicant");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299 }
300
301 for (i = 0; exitcode == 0 && i < iface_count; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700302 struct wpa_supplicant *wpa_s;
303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304 if ((ifaces[i].confname == NULL &&
305 ifaces[i].ctrl_interface == NULL) ||
306 ifaces[i].ifname == NULL) {
307 if (iface_count == 1 && (params.ctrl_interface ||
308 params.dbus_ctrl_interface))
309 break;
310 usage();
311 exitcode = -1;
312 break;
313 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700314 wpa_s = wpa_supplicant_add_iface(global, &ifaces[i]);
315 if (wpa_s == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316 exitcode = -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700317 break;
318 }
319#ifdef CONFIG_P2P
320 if (wpa_s->global->p2p == NULL &&
321 (wpa_s->drv_flags &
322 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
323 wpas_p2p_add_p2pdev_interface(wpa_s) < 0)
324 exitcode = -1;
325#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326 }
327
328 if (exitcode == 0)
329 exitcode = wpa_supplicant_run(global);
330
331 wpa_supplicant_deinit(global);
332
333out:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700334 wpa_supplicant_fd_workaround(0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 os_free(ifaces);
336 os_free(params.pid_file);
337
338 os_program_deinit();
339
340 return exitcode;
341}