blob: f45c1b7d13893a4f6f3da81258bdaebab2fb00be [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"
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"
62 " -K = include keys (passwords, etc.) in debug output\n");
63#ifdef CONFIG_DEBUG_SYSLOG
64 printf(" -s = log output to syslog instead of stdout\n");
65#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt04949592012-07-19 12:16:46 -070066#ifdef CONFIG_DEBUG_LINUX_TRACING
67 printf(" -T = record to Linux tracing in addition to logging\n");
68 printf(" (records all messages regardless of debug verbosity)\n");
69#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070 printf(" -t = include timestamp in debug messages\n"
71 " -h = show this help text\n"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080072 " -L = show license (BSD)\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073 " -o = override driver parameter for new interfaces\n"
74 " -O = override ctrl_interface parameter for new interfaces\n"
75 " -p = driver parameters\n"
76 " -P = PID file\n"
77 " -q = decrease debugging verbosity (-qq even less)\n");
78#ifdef CONFIG_DBUS
79 printf(" -u = enable DBus control interface\n");
80#endif /* CONFIG_DBUS */
81 printf(" -v = show version\n"
82 " -W = wait for a control interface monitor before starting\n"
83 " -N = start describing new interface\n");
84
85 printf("example:\n"
86 " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
Dmitry Shmidt4b060592013-04-29 16:42:49 -070087 wpa_drivers[0] ? wpa_drivers[0]->name : "nl80211");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088#endif /* CONFIG_NO_STDOUT_DEBUG */
89}
90
91
92static void license(void)
93{
94#ifndef CONFIG_NO_STDOUT_DEBUG
95 printf("%s\n\n%s%s%s%s%s\n",
96 wpa_supplicant_version,
97 wpa_supplicant_full_license1,
98 wpa_supplicant_full_license2,
99 wpa_supplicant_full_license3,
100 wpa_supplicant_full_license4,
101 wpa_supplicant_full_license5);
102#endif /* CONFIG_NO_STDOUT_DEBUG */
103}
104
105
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700106static void wpa_supplicant_fd_workaround(int start)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107{
108#ifdef __linux__
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700109 static int fd[3] = { -1, -1, -1 };
110 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111 /* When started from pcmcia-cs scripts, wpa_supplicant might start with
112 * fd 0, 1, and 2 closed. This will cause some issues because many
113 * places in wpa_supplicant are still printing out to stdout. As a
114 * workaround, make sure that fd's 0, 1, and 2 are not used for other
115 * sockets. */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700116 if (start) {
117 for (i = 0; i < 3; i++) {
118 fd[i] = open("/dev/null", O_RDWR);
119 if (fd[i] > 2) {
120 close(fd[i]);
121 fd[i] = -1;
122 break;
123 }
124 }
125 } else {
126 for (i = 0; i < 3; i++) {
127 if (fd[i] >= 0) {
128 close(fd[i]);
129 fd[i] = -1;
130 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 }
132 }
133#endif /* __linux__ */
134}
135
136
137int main(int argc, char *argv[])
138{
139 int c, i;
140 struct wpa_interface *ifaces, *iface;
141 int iface_count, exitcode = -1;
142 struct wpa_params params;
143 struct wpa_global *global;
144
145 if (os_program_init())
146 return -1;
147
148 os_memset(&params, 0, sizeof(params));
149 params.wpa_debug_level = MSG_INFO;
150
151 iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
152 if (ifaces == NULL)
153 return -1;
154 iface_count = 1;
155
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700156 wpa_supplicant_fd_workaround(1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700157
158 for (;;) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700159 c = getopt(argc, argv,
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700160 "b:Bc:C:D:de:f:g:hi:I:KLNo:O:p:P:qsTtuvW");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161 if (c < 0)
162 break;
163 switch (c) {
164 case 'b':
165 iface->bridge_ifname = optarg;
166 break;
167 case 'B':
168 params.daemonize++;
169 break;
170 case 'c':
171 iface->confname = optarg;
172 break;
173 case 'C':
174 iface->ctrl_interface = optarg;
175 break;
176 case 'D':
177 iface->driver = optarg;
178 break;
179 case 'd':
180#ifdef CONFIG_NO_STDOUT_DEBUG
181 printf("Debugging disabled with "
182 "CONFIG_NO_STDOUT_DEBUG=y build time "
183 "option.\n");
184 goto out;
185#else /* CONFIG_NO_STDOUT_DEBUG */
186 params.wpa_debug_level--;
187 break;
188#endif /* CONFIG_NO_STDOUT_DEBUG */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700189 case 'e':
190 params.entropy_file = optarg;
191 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192#ifdef CONFIG_DEBUG_FILE
193 case 'f':
194 params.wpa_debug_file_path = optarg;
195 break;
196#endif /* CONFIG_DEBUG_FILE */
197 case 'g':
198 params.ctrl_interface = optarg;
199 break;
200 case 'h':
201 usage();
202 exitcode = 0;
203 goto out;
204 case 'i':
205 iface->ifname = optarg;
206 break;
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700207 case 'I':
208 iface->confanother = optarg;
209 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210 case 'K':
211 params.wpa_debug_show_keys++;
212 break;
213 case 'L':
214 license();
215 exitcode = 0;
216 goto out;
217 case 'o':
218 params.override_driver = optarg;
219 break;
220 case 'O':
221 params.override_ctrl_interface = optarg;
222 break;
223 case 'p':
224 iface->driver_param = optarg;
225 break;
226 case 'P':
227 os_free(params.pid_file);
228 params.pid_file = os_rel2abs_path(optarg);
229 break;
230 case 'q':
231 params.wpa_debug_level++;
232 break;
233#ifdef CONFIG_DEBUG_SYSLOG
234 case 's':
235 params.wpa_debug_syslog++;
236 break;
237#endif /* CONFIG_DEBUG_SYSLOG */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700238#ifdef CONFIG_DEBUG_LINUX_TRACING
239 case 'T':
240 params.wpa_debug_tracing++;
241 break;
242#endif /* CONFIG_DEBUG_LINUX_TRACING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243 case 't':
244 params.wpa_debug_timestamp++;
245 break;
246#ifdef CONFIG_DBUS
247 case 'u':
248 params.dbus_ctrl_interface = 1;
249 break;
250#endif /* CONFIG_DBUS */
251 case 'v':
252 printf("%s\n", wpa_supplicant_version);
253 exitcode = 0;
254 goto out;
255 case 'W':
256 params.wait_for_monitor++;
257 break;
258 case 'N':
259 iface_count++;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700260 iface = os_realloc_array(ifaces, iface_count,
261 sizeof(struct wpa_interface));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262 if (iface == NULL)
263 goto out;
264 ifaces = iface;
265 iface = &ifaces[iface_count - 1];
266 os_memset(iface, 0, sizeof(*iface));
267 break;
268 default:
269 usage();
270 exitcode = 0;
271 goto out;
272 }
273 }
274
275 exitcode = 0;
276 global = wpa_supplicant_init(&params);
277 if (global == NULL) {
278 wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
279 exitcode = -1;
280 goto out;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700281 } else {
282 wpa_printf(MSG_INFO, "Successfully initialized "
283 "wpa_supplicant");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 }
285
286 for (i = 0; exitcode == 0 && i < iface_count; i++) {
287 if ((ifaces[i].confname == NULL &&
288 ifaces[i].ctrl_interface == NULL) ||
289 ifaces[i].ifname == NULL) {
290 if (iface_count == 1 && (params.ctrl_interface ||
291 params.dbus_ctrl_interface))
292 break;
293 usage();
294 exitcode = -1;
295 break;
296 }
297 if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
298 exitcode = -1;
299 }
300
301 if (exitcode == 0)
302 exitcode = wpa_supplicant_run(global);
303
304 wpa_supplicant_deinit(global);
305
306out:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700307 wpa_supplicant_fd_workaround(0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308 os_free(ifaces);
309 os_free(params.pid_file);
310
311 os_program_deinit();
312
313 return exitcode;
314}