blob: c3ecbb3d3943a788d254eb48a2bcea2de81c4273 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / main() function for UNIX like OSes and MinGW
3 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
4 *
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"
28 " -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"
Dmitry Shmidt64f47c52013-04-16 10:41:54 -070035 " [-p<driver_param>] [-b<br_ifname>] [-I<config file>]\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036 "\n"
37 "drivers:\n",
38 wpa_supplicant_version, wpa_supplicant_license);
39
40 for (i = 0; wpa_drivers[i]; i++) {
41 printf(" %s = %s\n",
42 wpa_drivers[i]->name,
43 wpa_drivers[i]->desc);
44 }
45
46#ifndef CONFIG_NO_STDOUT_DEBUG
47 printf("options:\n"
48 " -b = optional bridge interface name\n"
49 " -B = run daemon in the background\n"
50 " -c = Configuration file\n"
51 " -C = ctrl_interface parameter (only used if -c is not)\n"
52 " -i = interface name\n"
Dmitry Shmidt64f47c52013-04-16 10:41:54 -070053 " -I = additional Configuration file\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070054 " -d = increase debugging verbosity (-dd even more)\n"
Jouni Malinen75ecf522011-06-27 15:19:46 -070055 " -D = driver name (can be multiple drivers: nl80211,wext)\n"
56 " -e = entropy file\n");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057#ifdef CONFIG_DEBUG_FILE
58 printf(" -f = log output to debug file instead of stdout\n");
59#endif /* CONFIG_DEBUG_FILE */
60 printf(" -g = global ctrl_interface\n"
61 " -K = include keys (passwords, etc.) in debug output\n");
62#ifdef CONFIG_DEBUG_SYSLOG
63 printf(" -s = log output to syslog instead of stdout\n");
64#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt04949592012-07-19 12:16:46 -070065#ifdef CONFIG_DEBUG_LINUX_TRACING
66 printf(" -T = record to Linux tracing in addition to logging\n");
67 printf(" (records all messages regardless of debug verbosity)\n");
68#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069 printf(" -t = include timestamp in debug messages\n"
70 " -h = show this help text\n"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080071 " -L = show license (BSD)\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072 " -o = override driver parameter for new interfaces\n"
73 " -O = override ctrl_interface parameter for new interfaces\n"
74 " -p = driver parameters\n"
75 " -P = PID file\n"
76 " -q = decrease debugging verbosity (-qq even less)\n");
77#ifdef CONFIG_DBUS
78 printf(" -u = enable DBus control interface\n");
79#endif /* CONFIG_DBUS */
80 printf(" -v = show version\n"
81 " -W = wait for a control interface monitor before starting\n"
82 " -N = start describing new interface\n");
83
84 printf("example:\n"
85 " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
86 wpa_drivers[i] ? wpa_drivers[i]->name : "wext");
87#endif /* CONFIG_NO_STDOUT_DEBUG */
88}
89
90
91static void license(void)
92{
93#ifndef CONFIG_NO_STDOUT_DEBUG
94 printf("%s\n\n%s%s%s%s%s\n",
95 wpa_supplicant_version,
96 wpa_supplicant_full_license1,
97 wpa_supplicant_full_license2,
98 wpa_supplicant_full_license3,
99 wpa_supplicant_full_license4,
100 wpa_supplicant_full_license5);
101#endif /* CONFIG_NO_STDOUT_DEBUG */
102}
103
104
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700105static void wpa_supplicant_fd_workaround(int start)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106{
107#ifdef __linux__
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700108 static int fd[3] = { -1, -1, -1 };
109 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700110 /* When started from pcmcia-cs scripts, wpa_supplicant might start with
111 * fd 0, 1, and 2 closed. This will cause some issues because many
112 * places in wpa_supplicant are still printing out to stdout. As a
113 * workaround, make sure that fd's 0, 1, and 2 are not used for other
114 * sockets. */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700115 if (start) {
116 for (i = 0; i < 3; i++) {
117 fd[i] = open("/dev/null", O_RDWR);
118 if (fd[i] > 2) {
119 close(fd[i]);
120 fd[i] = -1;
121 break;
122 }
123 }
124 } else {
125 for (i = 0; i < 3; i++) {
126 if (fd[i] >= 0) {
127 close(fd[i]);
128 fd[i] = -1;
129 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130 }
131 }
132#endif /* __linux__ */
133}
134
135
136int main(int argc, char *argv[])
137{
138 int c, i;
139 struct wpa_interface *ifaces, *iface;
140 int iface_count, exitcode = -1;
141 struct wpa_params params;
142 struct wpa_global *global;
143
144 if (os_program_init())
145 return -1;
146
147 os_memset(&params, 0, sizeof(params));
148 params.wpa_debug_level = MSG_INFO;
149
150 iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
151 if (ifaces == NULL)
152 return -1;
153 iface_count = 1;
154
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700155 wpa_supplicant_fd_workaround(1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700156
157 for (;;) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700158 c = getopt(argc, argv,
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700159 "b:Bc:C:D:de:f:g:hi:I:KLNo:O:p:P:qsTtuvW");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160 if (c < 0)
161 break;
162 switch (c) {
163 case 'b':
164 iface->bridge_ifname = optarg;
165 break;
166 case 'B':
167 params.daemonize++;
168 break;
169 case 'c':
170 iface->confname = optarg;
171 break;
172 case 'C':
173 iface->ctrl_interface = optarg;
174 break;
175 case 'D':
176 iface->driver = optarg;
177 break;
178 case 'd':
179#ifdef CONFIG_NO_STDOUT_DEBUG
180 printf("Debugging disabled with "
181 "CONFIG_NO_STDOUT_DEBUG=y build time "
182 "option.\n");
183 goto out;
184#else /* CONFIG_NO_STDOUT_DEBUG */
185 params.wpa_debug_level--;
186 break;
187#endif /* CONFIG_NO_STDOUT_DEBUG */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700188 case 'e':
189 params.entropy_file = optarg;
190 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191#ifdef CONFIG_DEBUG_FILE
192 case 'f':
193 params.wpa_debug_file_path = optarg;
194 break;
195#endif /* CONFIG_DEBUG_FILE */
196 case 'g':
197 params.ctrl_interface = optarg;
198 break;
199 case 'h':
200 usage();
201 exitcode = 0;
202 goto out;
203 case 'i':
204 iface->ifname = optarg;
205 break;
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700206 case 'I':
207 iface->confanother = optarg;
208 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700209 case 'K':
210 params.wpa_debug_show_keys++;
211 break;
212 case 'L':
213 license();
214 exitcode = 0;
215 goto out;
216 case 'o':
217 params.override_driver = optarg;
218 break;
219 case 'O':
220 params.override_ctrl_interface = optarg;
221 break;
222 case 'p':
223 iface->driver_param = optarg;
224 break;
225 case 'P':
226 os_free(params.pid_file);
227 params.pid_file = os_rel2abs_path(optarg);
228 break;
229 case 'q':
230 params.wpa_debug_level++;
231 break;
232#ifdef CONFIG_DEBUG_SYSLOG
233 case 's':
234 params.wpa_debug_syslog++;
235 break;
236#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700237#ifdef CONFIG_DEBUG_LINUX_TRACING
238 case 'T':
239 params.wpa_debug_tracing++;
240 break;
241#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700242 case 't':
243 params.wpa_debug_timestamp++;
244 break;
245#ifdef CONFIG_DBUS
246 case 'u':
247 params.dbus_ctrl_interface = 1;
248 break;
249#endif /* CONFIG_DBUS */
250 case 'v':
251 printf("%s\n", wpa_supplicant_version);
252 exitcode = 0;
253 goto out;
254 case 'W':
255 params.wait_for_monitor++;
256 break;
257 case 'N':
258 iface_count++;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700259 iface = os_realloc_array(ifaces, iface_count,
260 sizeof(struct wpa_interface));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261 if (iface == NULL)
262 goto out;
263 ifaces = iface;
264 iface = &ifaces[iface_count - 1];
265 os_memset(iface, 0, sizeof(*iface));
266 break;
267 default:
268 usage();
269 exitcode = 0;
270 goto out;
271 }
272 }
273
274 exitcode = 0;
275 global = wpa_supplicant_init(&params);
276 if (global == NULL) {
277 wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
278 exitcode = -1;
279 goto out;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700280 } else {
281 wpa_printf(MSG_INFO, "Successfully initialized "
282 "wpa_supplicant");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283 }
284
285 for (i = 0; exitcode == 0 && i < iface_count; i++) {
286 if ((ifaces[i].confname == NULL &&
287 ifaces[i].ctrl_interface == NULL) ||
288 ifaces[i].ifname == NULL) {
289 if (iface_count == 1 && (params.ctrl_interface ||
290 params.dbus_ctrl_interface))
291 break;
292 usage();
293 exitcode = -1;
294 break;
295 }
296 if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
297 exitcode = -1;
298 }
299
300 if (exitcode == 0)
301 exitcode = wpa_supplicant_run(global);
302
303 wpa_supplicant_deinit(global);
304
305out:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700306 wpa_supplicant_fd_workaround(0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307 os_free(ifaces);
308 os_free(params.pid_file);
309
310 os_program_deinit();
311
312 return exitcode;
313}