blob: 37f0c78138bf395cd50e8e36d5579c5aab1f0b79 [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"
25 " wpa_supplicant [-BddhKLqqstuvW] [-P<pid file>] "
26 "[-g<global ctrl>] \\\n"
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070027 " [-G<group>] \\\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028 " -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
29 "[-p<driver_param>] \\\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -070030 " [-b<br_ifname>] [-f<debug file>] [-e<entropy file>] "
31 "\\\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032 " [-o<override driver>] [-O<override ctrl>] \\\n"
33 " [-N -i<ifname> -c<conf> [-C<ctrl>] "
34 "[-D<driver>] \\\n"
Jouni Malinen5d1c8ad2013-04-23 12:34:56 -070035 " [-p<driver_param>] [-b<br_ifname>] [-I<config file>] "
36 "...]\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037 "\n"
38 "drivers:\n",
39 wpa_supplicant_version, wpa_supplicant_license);
40
41 for (i = 0; wpa_drivers[i]; i++) {
42 printf(" %s = %s\n",
43 wpa_drivers[i]->name,
44 wpa_drivers[i]->desc);
45 }
46
47#ifndef CONFIG_NO_STDOUT_DEBUG
48 printf("options:\n"
49 " -b = optional bridge interface name\n"
50 " -B = run daemon in the background\n"
51 " -c = Configuration file\n"
52 " -C = ctrl_interface parameter (only used if -c is not)\n"
53 " -i = interface name\n"
Jouni Malinen5d1c8ad2013-04-23 12:34:56 -070054 " -I = additional configuration file\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055 " -d = increase debugging verbosity (-dd even more)\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -070056 " -D = driver name (can be multiple drivers: nl80211,wext)\n"
57 " -e = entropy file\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058#ifdef CONFIG_DEBUG_FILE
59 printf(" -f = log output to debug file instead of stdout\n");
60#endif /* CONFIG_DEBUG_FILE */
61 printf(" -g = global ctrl_interface\n"
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070062 " -G = global ctrl_interface group\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063 " -K = include keys (passwords, etc.) in debug output\n");
64#ifdef CONFIG_DEBUG_SYSLOG
65 printf(" -s = log output to syslog instead of stdout\n");
66#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt04949592012-07-19 12:16:46 -070067#ifdef CONFIG_DEBUG_LINUX_TRACING
68 printf(" -T = record to Linux tracing in addition to logging\n");
69 printf(" (records all messages regardless of debug verbosity)\n");
70#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070071 printf(" -t = include timestamp in debug messages\n"
72 " -h = show this help text\n"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080073 " -L = show license (BSD)\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074 " -o = override driver parameter for new interfaces\n"
75 " -O = override ctrl_interface parameter for new interfaces\n"
76 " -p = driver parameters\n"
77 " -P = PID file\n"
78 " -q = decrease debugging verbosity (-qq even less)\n");
79#ifdef CONFIG_DBUS
80 printf(" -u = enable DBus control interface\n");
81#endif /* CONFIG_DBUS */
82 printf(" -v = show version\n"
83 " -W = wait for a control interface monitor before starting\n"
84 " -N = start describing new interface\n");
85
86 printf("example:\n"
87 " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
Dmitry Shmidt4b060592013-04-29 16:42:49 -070088 wpa_drivers[0] ? wpa_drivers[0]->name : "nl80211");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089#endif /* CONFIG_NO_STDOUT_DEBUG */
90}
91
92
93static void license(void)
94{
95#ifndef CONFIG_NO_STDOUT_DEBUG
96 printf("%s\n\n%s%s%s%s%s\n",
97 wpa_supplicant_version,
98 wpa_supplicant_full_license1,
99 wpa_supplicant_full_license2,
100 wpa_supplicant_full_license3,
101 wpa_supplicant_full_license4,
102 wpa_supplicant_full_license5);
103#endif /* CONFIG_NO_STDOUT_DEBUG */
104}
105
106
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700107static void wpa_supplicant_fd_workaround(int start)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108{
109#ifdef __linux__
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700110 static int fd[3] = { -1, -1, -1 };
111 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112 /* When started from pcmcia-cs scripts, wpa_supplicant might start with
113 * fd 0, 1, and 2 closed. This will cause some issues because many
114 * places in wpa_supplicant are still printing out to stdout. As a
115 * workaround, make sure that fd's 0, 1, and 2 are not used for other
116 * sockets. */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700117 if (start) {
118 for (i = 0; i < 3; i++) {
119 fd[i] = open("/dev/null", O_RDWR);
120 if (fd[i] > 2) {
121 close(fd[i]);
122 fd[i] = -1;
123 break;
124 }
125 }
126 } else {
127 for (i = 0; i < 3; i++) {
128 if (fd[i] >= 0) {
129 close(fd[i]);
130 fd[i] = -1;
131 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700132 }
133 }
134#endif /* __linux__ */
135}
136
137
138int main(int argc, char *argv[])
139{
140 int c, i;
141 struct wpa_interface *ifaces, *iface;
142 int iface_count, exitcode = -1;
143 struct wpa_params params;
144 struct wpa_global *global;
145
146 if (os_program_init())
147 return -1;
148
149 os_memset(&params, 0, sizeof(params));
150 params.wpa_debug_level = MSG_INFO;
151
152 iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
153 if (ifaces == NULL)
154 return -1;
155 iface_count = 1;
156
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700157 wpa_supplicant_fd_workaround(1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158
159 for (;;) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700160 c = getopt(argc, argv,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700161 "b:Bc:C:D:de:f:g:G:hi:I:KLNo:O:p:P:qsTtuvW");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162 if (c < 0)
163 break;
164 switch (c) {
165 case 'b':
166 iface->bridge_ifname = optarg;
167 break;
168 case 'B':
169 params.daemonize++;
170 break;
171 case 'c':
172 iface->confname = optarg;
173 break;
174 case 'C':
175 iface->ctrl_interface = optarg;
176 break;
177 case 'D':
178 iface->driver = optarg;
179 break;
180 case 'd':
181#ifdef CONFIG_NO_STDOUT_DEBUG
182 printf("Debugging disabled with "
183 "CONFIG_NO_STDOUT_DEBUG=y build time "
184 "option.\n");
185 goto out;
186#else /* CONFIG_NO_STDOUT_DEBUG */
187 params.wpa_debug_level--;
188 break;
189#endif /* CONFIG_NO_STDOUT_DEBUG */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700190 case 'e':
191 params.entropy_file = optarg;
192 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193#ifdef CONFIG_DEBUG_FILE
194 case 'f':
195 params.wpa_debug_file_path = optarg;
196 break;
197#endif /* CONFIG_DEBUG_FILE */
198 case 'g':
199 params.ctrl_interface = optarg;
200 break;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700201 case 'G':
202 params.ctrl_interface_group = optarg;
203 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204 case 'h':
205 usage();
206 exitcode = 0;
207 goto out;
208 case 'i':
209 iface->ifname = optarg;
210 break;
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700211 case 'I':
212 iface->confanother = optarg;
213 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214 case 'K':
215 params.wpa_debug_show_keys++;
216 break;
217 case 'L':
218 license();
219 exitcode = 0;
220 goto out;
221 case 'o':
222 params.override_driver = optarg;
223 break;
224 case 'O':
225 params.override_ctrl_interface = optarg;
226 break;
227 case 'p':
228 iface->driver_param = optarg;
229 break;
230 case 'P':
231 os_free(params.pid_file);
232 params.pid_file = os_rel2abs_path(optarg);
233 break;
234 case 'q':
235 params.wpa_debug_level++;
236 break;
237#ifdef CONFIG_DEBUG_SYSLOG
238 case 's':
239 params.wpa_debug_syslog++;
240 break;
241#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700242#ifdef CONFIG_DEBUG_LINUX_TRACING
243 case 'T':
244 params.wpa_debug_tracing++;
245 break;
246#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700247 case 't':
248 params.wpa_debug_timestamp++;
249 break;
250#ifdef CONFIG_DBUS
251 case 'u':
252 params.dbus_ctrl_interface = 1;
253 break;
254#endif /* CONFIG_DBUS */
255 case 'v':
256 printf("%s\n", wpa_supplicant_version);
257 exitcode = 0;
258 goto out;
259 case 'W':
260 params.wait_for_monitor++;
261 break;
262 case 'N':
263 iface_count++;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700264 iface = os_realloc_array(ifaces, iface_count,
265 sizeof(struct wpa_interface));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266 if (iface == NULL)
267 goto out;
268 ifaces = iface;
269 iface = &ifaces[iface_count - 1];
270 os_memset(iface, 0, sizeof(*iface));
271 break;
272 default:
273 usage();
274 exitcode = 0;
275 goto out;
276 }
277 }
278
279 exitcode = 0;
280 global = wpa_supplicant_init(&params);
281 if (global == NULL) {
282 wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
283 exitcode = -1;
284 goto out;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700285 } else {
286 wpa_printf(MSG_INFO, "Successfully initialized "
287 "wpa_supplicant");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288 }
289
290 for (i = 0; exitcode == 0 && i < iface_count; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700291 struct wpa_supplicant *wpa_s;
292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 if ((ifaces[i].confname == NULL &&
294 ifaces[i].ctrl_interface == NULL) ||
295 ifaces[i].ifname == NULL) {
296 if (iface_count == 1 && (params.ctrl_interface ||
297 params.dbus_ctrl_interface))
298 break;
299 usage();
300 exitcode = -1;
301 break;
302 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700303 wpa_s = wpa_supplicant_add_iface(global, &ifaces[i]);
304 if (wpa_s == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305 exitcode = -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700306 break;
307 }
308#ifdef CONFIG_P2P
309 if (wpa_s->global->p2p == NULL &&
310 (wpa_s->drv_flags &
311 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
312 wpas_p2p_add_p2pdev_interface(wpa_s) < 0)
313 exitcode = -1;
314#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315 }
316
317 if (exitcode == 0)
318 exitcode = wpa_supplicant_run(global);
319
320 wpa_supplicant_deinit(global);
321
322out:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700323 wpa_supplicant_fd_workaround(0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 os_free(ifaces);
325 os_free(params.pid_file);
326
327 os_program_deinit();
328
329 return exitcode;
330}