blob: 1b3364cbe3a66377480b141746c73a74770cf376 [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"
17
18extern struct wpa_driver_ops *wpa_drivers[];
19
20
21static void usage(void)
22{
23 int i;
24 printf("%s\n\n%s\n"
25 "usage:\n"
26 " wpa_supplicant [-BddhKLqqstuvW] [-P<pid file>] "
27 "[-g<global ctrl>] \\\n"
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070028 " [-G<group>] \\\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070029 " -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
30 "[-p<driver_param>] \\\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -070031 " [-b<br_ifname>] [-f<debug file>] [-e<entropy file>] "
32 "\\\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033 " [-o<override driver>] [-O<override ctrl>] \\\n"
34 " [-N -i<ifname> -c<conf> [-C<ctrl>] "
35 "[-D<driver>] \\\n"
Jouni Malinen5d1c8ad2013-04-23 12:34:56 -070036 " [-p<driver_param>] [-b<br_ifname>] [-I<config file>] "
37 "...]\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038 "\n"
39 "drivers:\n",
40 wpa_supplicant_version, wpa_supplicant_license);
41
42 for (i = 0; wpa_drivers[i]; i++) {
43 printf(" %s = %s\n",
44 wpa_drivers[i]->name,
45 wpa_drivers[i]->desc);
46 }
47
48#ifndef CONFIG_NO_STDOUT_DEBUG
49 printf("options:\n"
50 " -b = optional bridge interface name\n"
51 " -B = run daemon in the background\n"
52 " -c = Configuration file\n"
53 " -C = ctrl_interface parameter (only used if -c is not)\n"
54 " -i = interface name\n"
Jouni Malinen5d1c8ad2013-04-23 12:34:56 -070055 " -I = additional configuration file\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056 " -d = increase debugging verbosity (-dd even more)\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -070057 " -D = driver name (can be multiple drivers: nl80211,wext)\n"
58 " -e = entropy file\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070059#ifdef CONFIG_DEBUG_FILE
60 printf(" -f = log output to debug file instead of stdout\n");
61#endif /* CONFIG_DEBUG_FILE */
62 printf(" -g = global ctrl_interface\n"
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070063 " -G = global ctrl_interface group\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064 " -K = include keys (passwords, etc.) in debug output\n");
65#ifdef CONFIG_DEBUG_SYSLOG
66 printf(" -s = log output to syslog instead of stdout\n");
67#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt04949592012-07-19 12:16:46 -070068#ifdef CONFIG_DEBUG_LINUX_TRACING
69 printf(" -T = record to Linux tracing in addition to logging\n");
70 printf(" (records all messages regardless of debug verbosity)\n");
71#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072 printf(" -t = include timestamp in debug messages\n"
73 " -h = show this help text\n"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080074 " -L = show license (BSD)\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070075 " -o = override driver parameter for new interfaces\n"
76 " -O = override ctrl_interface parameter for new interfaces\n"
77 " -p = driver parameters\n"
78 " -P = PID file\n"
79 " -q = decrease debugging verbosity (-qq even less)\n");
80#ifdef CONFIG_DBUS
81 printf(" -u = enable DBus control interface\n");
82#endif /* CONFIG_DBUS */
83 printf(" -v = show version\n"
84 " -W = wait for a control interface monitor before starting\n"
85 " -N = start describing new interface\n");
86
87 printf("example:\n"
88 " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
Dmitry Shmidt4b060592013-04-29 16:42:49 -070089 wpa_drivers[0] ? wpa_drivers[0]->name : "nl80211");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070090#endif /* CONFIG_NO_STDOUT_DEBUG */
91}
92
93
94static void license(void)
95{
96#ifndef CONFIG_NO_STDOUT_DEBUG
97 printf("%s\n\n%s%s%s%s%s\n",
98 wpa_supplicant_version,
99 wpa_supplicant_full_license1,
100 wpa_supplicant_full_license2,
101 wpa_supplicant_full_license3,
102 wpa_supplicant_full_license4,
103 wpa_supplicant_full_license5);
104#endif /* CONFIG_NO_STDOUT_DEBUG */
105}
106
107
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700108static void wpa_supplicant_fd_workaround(int start)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109{
110#ifdef __linux__
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700111 static int fd[3] = { -1, -1, -1 };
112 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113 /* When started from pcmcia-cs scripts, wpa_supplicant might start with
114 * fd 0, 1, and 2 closed. This will cause some issues because many
115 * places in wpa_supplicant are still printing out to stdout. As a
116 * workaround, make sure that fd's 0, 1, and 2 are not used for other
117 * sockets. */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700118 if (start) {
119 for (i = 0; i < 3; i++) {
120 fd[i] = open("/dev/null", O_RDWR);
121 if (fd[i] > 2) {
122 close(fd[i]);
123 fd[i] = -1;
124 break;
125 }
126 }
127 } else {
128 for (i = 0; i < 3; i++) {
129 if (fd[i] >= 0) {
130 close(fd[i]);
131 fd[i] = -1;
132 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133 }
134 }
135#endif /* __linux__ */
136}
137
138
139int main(int argc, char *argv[])
140{
141 int c, i;
142 struct wpa_interface *ifaces, *iface;
143 int iface_count, exitcode = -1;
144 struct wpa_params params;
145 struct wpa_global *global;
146
147 if (os_program_init())
148 return -1;
149
150 os_memset(&params, 0, sizeof(params));
151 params.wpa_debug_level = MSG_INFO;
152
153 iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
154 if (ifaces == NULL)
155 return -1;
156 iface_count = 1;
157
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700158 wpa_supplicant_fd_workaround(1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159
160 for (;;) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700161 c = getopt(argc, argv,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700162 "b:Bc:C:D:de:f:g:G:hi:I:KLNo:O:p:P:qsTtuvW");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700163 if (c < 0)
164 break;
165 switch (c) {
166 case 'b':
167 iface->bridge_ifname = optarg;
168 break;
169 case 'B':
170 params.daemonize++;
171 break;
172 case 'c':
173 iface->confname = optarg;
174 break;
175 case 'C':
176 iface->ctrl_interface = optarg;
177 break;
178 case 'D':
179 iface->driver = optarg;
180 break;
181 case 'd':
182#ifdef CONFIG_NO_STDOUT_DEBUG
183 printf("Debugging disabled with "
184 "CONFIG_NO_STDOUT_DEBUG=y build time "
185 "option.\n");
186 goto out;
187#else /* CONFIG_NO_STDOUT_DEBUG */
188 params.wpa_debug_level--;
189 break;
190#endif /* CONFIG_NO_STDOUT_DEBUG */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700191 case 'e':
192 params.entropy_file = optarg;
193 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194#ifdef CONFIG_DEBUG_FILE
195 case 'f':
196 params.wpa_debug_file_path = optarg;
197 break;
198#endif /* CONFIG_DEBUG_FILE */
199 case 'g':
200 params.ctrl_interface = optarg;
201 break;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700202 case 'G':
203 params.ctrl_interface_group = optarg;
204 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205 case 'h':
206 usage();
207 exitcode = 0;
208 goto out;
209 case 'i':
210 iface->ifname = optarg;
211 break;
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700212 case 'I':
213 iface->confanother = optarg;
214 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 case 'K':
216 params.wpa_debug_show_keys++;
217 break;
218 case 'L':
219 license();
220 exitcode = 0;
221 goto out;
222 case 'o':
223 params.override_driver = optarg;
224 break;
225 case 'O':
226 params.override_ctrl_interface = optarg;
227 break;
228 case 'p':
229 iface->driver_param = optarg;
230 break;
231 case 'P':
232 os_free(params.pid_file);
233 params.pid_file = os_rel2abs_path(optarg);
234 break;
235 case 'q':
236 params.wpa_debug_level++;
237 break;
238#ifdef CONFIG_DEBUG_SYSLOG
239 case 's':
240 params.wpa_debug_syslog++;
241 break;
242#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700243#ifdef CONFIG_DEBUG_LINUX_TRACING
244 case 'T':
245 params.wpa_debug_tracing++;
246 break;
247#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 case 't':
249 params.wpa_debug_timestamp++;
250 break;
251#ifdef CONFIG_DBUS
252 case 'u':
253 params.dbus_ctrl_interface = 1;
254 break;
255#endif /* CONFIG_DBUS */
256 case 'v':
257 printf("%s\n", wpa_supplicant_version);
258 exitcode = 0;
259 goto out;
260 case 'W':
261 params.wait_for_monitor++;
262 break;
263 case 'N':
264 iface_count++;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700265 iface = os_realloc_array(ifaces, iface_count,
266 sizeof(struct wpa_interface));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700267 if (iface == NULL)
268 goto out;
269 ifaces = iface;
270 iface = &ifaces[iface_count - 1];
271 os_memset(iface, 0, sizeof(*iface));
272 break;
273 default:
274 usage();
275 exitcode = 0;
276 goto out;
277 }
278 }
279
280 exitcode = 0;
281 global = wpa_supplicant_init(&params);
282 if (global == NULL) {
283 wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
284 exitcode = -1;
285 goto out;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700286 } else {
287 wpa_printf(MSG_INFO, "Successfully initialized "
288 "wpa_supplicant");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 }
290
291 for (i = 0; exitcode == 0 && i < iface_count; i++) {
292 if ((ifaces[i].confname == NULL &&
293 ifaces[i].ctrl_interface == NULL) ||
294 ifaces[i].ifname == NULL) {
295 if (iface_count == 1 && (params.ctrl_interface ||
296 params.dbus_ctrl_interface))
297 break;
298 usage();
299 exitcode = -1;
300 break;
301 }
302 if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
303 exitcode = -1;
304 }
305
306 if (exitcode == 0)
307 exitcode = wpa_supplicant_run(global);
308
309 wpa_supplicant_deinit(global);
310
311out:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700312 wpa_supplicant_fd_workaround(0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700313 os_free(ifaces);
314 os_free(params.pid_file);
315
316 os_program_deinit();
317
318 return exitcode;
319}