main.c revision 75ecf5267604f166b85a7ee2cf0d9cb682966680
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>] [-e<entropy file>] "
37	       "\\\n"
38	       "        [-o<override driver>] [-O<override ctrl>] \\\n"
39	       "        [-N -i<ifname> -c<conf> [-C<ctrl>] "
40	       "[-D<driver>] \\\n"
41	       "        [-p<driver_param>] [-b<br_ifname>] ...]\n"
42	       "\n"
43	       "drivers:\n",
44	       wpa_supplicant_version, wpa_supplicant_license);
45
46	for (i = 0; wpa_drivers[i]; i++) {
47		printf("  %s = %s\n",
48		       wpa_drivers[i]->name,
49		       wpa_drivers[i]->desc);
50	}
51
52#ifndef CONFIG_NO_STDOUT_DEBUG
53	printf("options:\n"
54	       "  -b = optional bridge interface name\n"
55	       "  -B = run daemon in the background\n"
56	       "  -c = Configuration file\n"
57	       "  -C = ctrl_interface parameter (only used if -c is not)\n"
58	       "  -i = interface name\n"
59	       "  -d = increase debugging verbosity (-dd even more)\n"
60	       "  -D = driver name (can be multiple drivers: nl80211,wext)\n"
61	       "  -e = entropy file\n");
62#ifdef CONFIG_DEBUG_FILE
63	printf("  -f = log output to debug file instead of stdout\n");
64#endif /* CONFIG_DEBUG_FILE */
65	printf("  -g = global ctrl_interface\n"
66	       "  -K = include keys (passwords, etc.) in debug output\n");
67#ifdef CONFIG_DEBUG_SYSLOG
68	printf("  -s = log output to syslog instead of stdout\n");
69#endif /* CONFIG_DEBUG_SYSLOG */
70	printf("  -t = include timestamp in debug messages\n"
71	       "  -h = show this help text\n"
72	       "  -L = show license (GPL and BSD)\n"
73	       "  -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",
87	       wpa_drivers[i] ? wpa_drivers[i]->name : "wext");
88#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
106static void wpa_supplicant_fd_workaround(void)
107{
108#ifdef __linux__
109	int s, i;
110	/* 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. */
115	for (i = 0; i < 3; i++) {
116		s = open("/dev/null", O_RDWR);
117		if (s > 2) {
118			close(s);
119			break;
120		}
121	}
122#endif /* __linux__ */
123}
124
125
126int main(int argc, char *argv[])
127{
128	int c, i;
129	struct wpa_interface *ifaces, *iface;
130	int iface_count, exitcode = -1;
131	struct wpa_params params;
132	struct wpa_global *global;
133
134	if (os_program_init())
135		return -1;
136
137	os_memset(&params, 0, sizeof(params));
138	params.wpa_debug_level = MSG_INFO;
139
140	iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
141	if (ifaces == NULL)
142		return -1;
143	iface_count = 1;
144
145	wpa_supplicant_fd_workaround();
146
147	for (;;) {
148		c = getopt(argc, argv, "b:Bc:C:D:de:f:g:hi:KLNo:O:p:P:qstuvW");
149		if (c < 0)
150			break;
151		switch (c) {
152		case 'b':
153			iface->bridge_ifname = optarg;
154			break;
155		case 'B':
156			params.daemonize++;
157			break;
158		case 'c':
159			iface->confname = optarg;
160			break;
161		case 'C':
162			iface->ctrl_interface = optarg;
163			break;
164		case 'D':
165			iface->driver = optarg;
166			break;
167		case 'd':
168#ifdef CONFIG_NO_STDOUT_DEBUG
169			printf("Debugging disabled with "
170			       "CONFIG_NO_STDOUT_DEBUG=y build time "
171			       "option.\n");
172			goto out;
173#else /* CONFIG_NO_STDOUT_DEBUG */
174			params.wpa_debug_level--;
175			break;
176#endif /* CONFIG_NO_STDOUT_DEBUG */
177		case 'e':
178			params.entropy_file = optarg;
179			break;
180#ifdef CONFIG_DEBUG_FILE
181		case 'f':
182			params.wpa_debug_file_path = optarg;
183			break;
184#endif /* CONFIG_DEBUG_FILE */
185		case 'g':
186			params.ctrl_interface = optarg;
187			break;
188		case 'h':
189			usage();
190			exitcode = 0;
191			goto out;
192		case 'i':
193			iface->ifname = optarg;
194			break;
195		case 'K':
196			params.wpa_debug_show_keys++;
197			break;
198		case 'L':
199			license();
200			exitcode = 0;
201			goto out;
202		case 'o':
203			params.override_driver = optarg;
204			break;
205		case 'O':
206			params.override_ctrl_interface = optarg;
207			break;
208		case 'p':
209			iface->driver_param = optarg;
210			break;
211		case 'P':
212			os_free(params.pid_file);
213			params.pid_file = os_rel2abs_path(optarg);
214			break;
215		case 'q':
216			params.wpa_debug_level++;
217			break;
218#ifdef CONFIG_DEBUG_SYSLOG
219		case 's':
220			params.wpa_debug_syslog++;
221			break;
222#endif /* CONFIG_DEBUG_SYSLOG */
223		case 't':
224			params.wpa_debug_timestamp++;
225			break;
226#ifdef CONFIG_DBUS
227		case 'u':
228			params.dbus_ctrl_interface = 1;
229			break;
230#endif /* CONFIG_DBUS */
231		case 'v':
232			printf("%s\n", wpa_supplicant_version);
233			exitcode = 0;
234			goto out;
235		case 'W':
236			params.wait_for_monitor++;
237			break;
238		case 'N':
239			iface_count++;
240			iface = os_realloc(ifaces, iface_count *
241					   sizeof(struct wpa_interface));
242			if (iface == NULL)
243				goto out;
244			ifaces = iface;
245			iface = &ifaces[iface_count - 1];
246			os_memset(iface, 0, sizeof(*iface));
247			break;
248		default:
249			usage();
250			exitcode = 0;
251			goto out;
252		}
253	}
254
255	exitcode = 0;
256	global = wpa_supplicant_init(&params);
257	if (global == NULL) {
258		wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
259		exitcode = -1;
260		goto out;
261	}
262
263	for (i = 0; exitcode == 0 && i < iface_count; i++) {
264		if ((ifaces[i].confname == NULL &&
265		     ifaces[i].ctrl_interface == NULL) ||
266		    ifaces[i].ifname == NULL) {
267			if (iface_count == 1 && (params.ctrl_interface ||
268						 params.dbus_ctrl_interface))
269				break;
270			usage();
271			exitcode = -1;
272			break;
273		}
274		if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
275			exitcode = -1;
276	}
277
278	if (exitcode == 0)
279		exitcode = wpa_supplicant_run(global);
280
281	wpa_supplicant_deinit(global);
282
283out:
284	os_free(ifaces);
285	os_free(params.pid_file);
286
287	os_program_deinit();
288
289	return exitcode;
290}
291