main.c revision 8d520ff1dc2da35cdca849e982051b86468016d8
1/*
2 * WPA Supplicant / main() function for UNIX like OSes and MinGW
3 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16#ifdef __linux__
17#include <fcntl.h>
18#endif /* __linux__ */
19
20#include "common.h"
21#include "wpa_supplicant_i.h"
22#include "driver_i.h"
23
24extern struct wpa_driver_ops *wpa_drivers[];
25
26
27static void usage(void)
28{
29	int i;
30	printf("%s\n\n%s\n"
31	       "usage:\n"
32	       "  wpa_supplicant [-BddhKLqqstuvW] [-P<pid file>] "
33	       "[-g<global ctrl>] \\\n"
34	       "        -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
35	       "[-p<driver_param>] \\\n"
36	       "        [-b<br_ifname>] [-f<debug file>] \\\n"
37	       "        [-o<override driver>] [-O<override ctrl>] \\\n"
38	       "        [-N -i<ifname> -c<conf> [-C<ctrl>] "
39	       "[-D<driver>] \\\n"
40	       "        [-p<driver_param>] [-b<br_ifname>] ...]\n"
41	       "\n"
42	       "drivers:\n",
43	       wpa_supplicant_version, wpa_supplicant_license);
44
45	for (i = 0; wpa_drivers[i]; i++) {
46		printf("  %s = %s\n",
47		       wpa_drivers[i]->name,
48		       wpa_drivers[i]->desc);
49	}
50
51#ifndef CONFIG_NO_STDOUT_DEBUG
52	printf("options:\n"
53	       "  -b = optional bridge interface name\n"
54	       "  -B = run daemon in the background\n"
55	       "  -c = Configuration file\n"
56	       "  -C = ctrl_interface parameter (only used if -c is not)\n"
57	       "  -i = interface name\n"
58	       "  -d = increase debugging verbosity (-dd even more)\n"
59	       "  -D = driver name (can be multiple drivers: nl80211,wext)\n");
60#ifdef CONFIG_DEBUG_FILE
61	printf("  -f = log output to debug file instead of stdout\n");
62#endif /* CONFIG_DEBUG_FILE */
63	printf("  -g = global ctrl_interface\n"
64	       "  -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 */
68	printf("  -t = include timestamp in debug messages\n"
69	       "  -h = show this help text\n"
70	       "  -L = show license (GPL and BSD)\n"
71	       "  -o = override driver parameter for new interfaces\n"
72	       "  -O = override ctrl_interface parameter for new interfaces\n"
73	       "  -p = driver parameters\n"
74	       "  -P = PID file\n"
75	       "  -q = decrease debugging verbosity (-qq even less)\n");
76#ifdef CONFIG_DBUS
77	printf("  -u = enable DBus control interface\n");
78#endif /* CONFIG_DBUS */
79	printf("  -v = show version\n"
80	       "  -W = wait for a control interface monitor before starting\n"
81	       "  -N = start describing new interface\n");
82
83	printf("example:\n"
84	       "  wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
85	       wpa_drivers[i] ? wpa_drivers[i]->name : "wext");
86#endif /* CONFIG_NO_STDOUT_DEBUG */
87}
88
89
90static void license(void)
91{
92#ifndef CONFIG_NO_STDOUT_DEBUG
93	printf("%s\n\n%s%s%s%s%s\n",
94	       wpa_supplicant_version,
95	       wpa_supplicant_full_license1,
96	       wpa_supplicant_full_license2,
97	       wpa_supplicant_full_license3,
98	       wpa_supplicant_full_license4,
99	       wpa_supplicant_full_license5);
100#endif /* CONFIG_NO_STDOUT_DEBUG */
101}
102
103
104static void wpa_supplicant_fd_workaround(void)
105{
106#ifdef __linux__
107	int s, i;
108	/* When started from pcmcia-cs scripts, wpa_supplicant might start with
109	 * fd 0, 1, and 2 closed. This will cause some issues because many
110	 * places in wpa_supplicant are still printing out to stdout. As a
111	 * workaround, make sure that fd's 0, 1, and 2 are not used for other
112	 * sockets. */
113	for (i = 0; i < 3; i++) {
114		s = open("/dev/null", O_RDWR);
115		if (s > 2) {
116			close(s);
117			break;
118		}
119	}
120#endif /* __linux__ */
121}
122
123
124int main(int argc, char *argv[])
125{
126	int c, i;
127	struct wpa_interface *ifaces, *iface;
128	int iface_count, exitcode = -1;
129	struct wpa_params params;
130	struct wpa_global *global;
131
132	if (os_program_init())
133		return -1;
134
135	os_memset(&params, 0, sizeof(params));
136	params.wpa_debug_level = MSG_INFO;
137
138	iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
139	if (ifaces == NULL)
140		return -1;
141	iface_count = 1;
142
143	wpa_supplicant_fd_workaround();
144
145	for (;;) {
146		c = getopt(argc, argv, "b:Bc:C:D:df:g:hi:KLNo:O:p:P:qstuvW");
147		if (c < 0)
148			break;
149		switch (c) {
150		case 'b':
151			iface->bridge_ifname = optarg;
152			break;
153		case 'B':
154			params.daemonize++;
155			break;
156		case 'c':
157			iface->confname = optarg;
158			break;
159		case 'C':
160			iface->ctrl_interface = optarg;
161			break;
162		case 'D':
163			iface->driver = optarg;
164			break;
165		case 'd':
166#ifdef CONFIG_NO_STDOUT_DEBUG
167			printf("Debugging disabled with "
168			       "CONFIG_NO_STDOUT_DEBUG=y build time "
169			       "option.\n");
170			goto out;
171#else /* CONFIG_NO_STDOUT_DEBUG */
172			params.wpa_debug_level--;
173			break;
174#endif /* CONFIG_NO_STDOUT_DEBUG */
175#ifdef CONFIG_DEBUG_FILE
176		case 'f':
177			params.wpa_debug_file_path = optarg;
178			break;
179#endif /* CONFIG_DEBUG_FILE */
180		case 'g':
181			params.ctrl_interface = optarg;
182			break;
183		case 'h':
184			usage();
185			exitcode = 0;
186			goto out;
187		case 'i':
188			iface->ifname = optarg;
189			break;
190		case 'K':
191			params.wpa_debug_show_keys++;
192			break;
193		case 'L':
194			license();
195			exitcode = 0;
196			goto out;
197		case 'o':
198			params.override_driver = optarg;
199			break;
200		case 'O':
201			params.override_ctrl_interface = optarg;
202			break;
203		case 'p':
204			iface->driver_param = optarg;
205			break;
206		case 'P':
207			os_free(params.pid_file);
208			params.pid_file = os_rel2abs_path(optarg);
209			break;
210		case 'q':
211			params.wpa_debug_level++;
212			break;
213#ifdef CONFIG_DEBUG_SYSLOG
214		case 's':
215			params.wpa_debug_syslog++;
216			break;
217#endif /* CONFIG_DEBUG_SYSLOG */
218		case 't':
219			params.wpa_debug_timestamp++;
220			break;
221#ifdef CONFIG_DBUS
222		case 'u':
223			params.dbus_ctrl_interface = 1;
224			break;
225#endif /* CONFIG_DBUS */
226		case 'v':
227			printf("%s\n", wpa_supplicant_version);
228			exitcode = 0;
229			goto out;
230		case 'W':
231			params.wait_for_monitor++;
232			break;
233		case 'N':
234			iface_count++;
235			iface = os_realloc(ifaces, iface_count *
236					   sizeof(struct wpa_interface));
237			if (iface == NULL)
238				goto out;
239			ifaces = iface;
240			iface = &ifaces[iface_count - 1];
241			os_memset(iface, 0, sizeof(*iface));
242			break;
243		default:
244			usage();
245			exitcode = 0;
246			goto out;
247		}
248	}
249
250	exitcode = 0;
251	global = wpa_supplicant_init(&params);
252	if (global == NULL) {
253		wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
254		exitcode = -1;
255		goto out;
256	}
257
258	for (i = 0; exitcode == 0 && i < iface_count; i++) {
259		if ((ifaces[i].confname == NULL &&
260		     ifaces[i].ctrl_interface == NULL) ||
261		    ifaces[i].ifname == NULL) {
262			if (iface_count == 1 && (params.ctrl_interface ||
263						 params.dbus_ctrl_interface))
264				break;
265			usage();
266			exitcode = -1;
267			break;
268		}
269		if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
270			exitcode = -1;
271	}
272
273	if (exitcode == 0)
274		exitcode = wpa_supplicant_run(global);
275
276	wpa_supplicant_deinit(global);
277
278out:
279	os_free(ifaces);
280	os_free(params.pid_file);
281
282	os_program_deinit();
283
284	return exitcode;
285}
286