Line data Source code
1 : /*
2 : * WPA Supplicant / main() function for UNIX like OSes and MinGW
3 : * Copyright (c) 2003-2013, Jouni Malinen <j@w1.fi>
4 : *
5 : * This software may be distributed under the terms of the BSD license.
6 : * See README for more details.
7 : */
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 : #include "p2p_supplicant.h"
18 :
19 :
20 0 : static void usage(void)
21 : {
22 : int i;
23 0 : printf("%s\n\n%s\n"
24 : "usage:\n"
25 : " wpa_supplicant [-BddhKLqq"
26 : #ifdef CONFIG_DEBUG_SYSLOG
27 : "s"
28 : #endif /* CONFIG_DEBUG_SYSLOG */
29 : "t"
30 : #ifdef CONFIG_DBUS
31 : "u"
32 : #endif /* CONFIG_DBUS */
33 : "vW] [-P<pid file>] "
34 : "[-g<global ctrl>] \\\n"
35 : " [-G<group>] \\\n"
36 : " -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
37 : "[-p<driver_param>] \\\n"
38 : " [-b<br_ifname>] [-e<entropy file>]"
39 : #ifdef CONFIG_DEBUG_FILE
40 : " [-f<debug file>]"
41 : #endif /* CONFIG_DEBUG_FILE */
42 : " \\\n"
43 : " [-o<override driver>] [-O<override ctrl>] \\\n"
44 : " [-N -i<ifname> -c<conf> [-C<ctrl>] "
45 : "[-D<driver>] \\\n"
46 : #ifdef CONFIG_P2P
47 : " [-m<P2P Device config file>] \\\n"
48 : #endif /* CONFIG_P2P */
49 : " [-p<driver_param>] [-b<br_ifname>] [-I<config file>] "
50 : "...]\n"
51 : "\n"
52 : "drivers:\n",
53 : wpa_supplicant_version, wpa_supplicant_license);
54 :
55 0 : for (i = 0; wpa_drivers[i]; i++) {
56 0 : printf(" %s = %s\n",
57 0 : wpa_drivers[i]->name,
58 0 : wpa_drivers[i]->desc);
59 : }
60 :
61 : #ifndef CONFIG_NO_STDOUT_DEBUG
62 0 : printf("options:\n"
63 : " -b = optional bridge interface name\n"
64 : " -B = run daemon in the background\n"
65 : " -c = Configuration file\n"
66 : " -C = ctrl_interface parameter (only used if -c is not)\n"
67 : " -i = interface name\n"
68 : " -I = additional configuration file\n"
69 : " -d = increase debugging verbosity (-dd even more)\n"
70 : " -D = driver name (can be multiple drivers: nl80211,wext)\n"
71 : " -e = entropy file\n");
72 : #ifdef CONFIG_DEBUG_FILE
73 0 : printf(" -f = log output to debug file instead of stdout\n");
74 : #endif /* CONFIG_DEBUG_FILE */
75 0 : printf(" -g = global ctrl_interface\n"
76 : " -G = global ctrl_interface group\n"
77 : " -K = include keys (passwords, etc.) in debug output\n");
78 : #ifdef CONFIG_DEBUG_SYSLOG
79 : printf(" -s = log output to syslog instead of stdout\n");
80 : #endif /* CONFIG_DEBUG_SYSLOG */
81 : #ifdef CONFIG_DEBUG_LINUX_TRACING
82 0 : printf(" -T = record to Linux tracing in addition to logging\n");
83 0 : printf(" (records all messages regardless of debug verbosity)\n");
84 : #endif /* CONFIG_DEBUG_LINUX_TRACING */
85 0 : printf(" -t = include timestamp in debug messages\n"
86 : " -h = show this help text\n"
87 : " -L = show license (BSD)\n"
88 : " -o = override driver parameter for new interfaces\n"
89 : " -O = override ctrl_interface parameter for new interfaces\n"
90 : " -p = driver parameters\n"
91 : " -P = PID file\n"
92 : " -q = decrease debugging verbosity (-qq even less)\n");
93 : #ifdef CONFIG_DBUS
94 : printf(" -u = enable DBus control interface\n");
95 : #endif /* CONFIG_DBUS */
96 0 : printf(" -v = show version\n"
97 : " -W = wait for a control interface monitor before starting\n"
98 : #ifdef CONFIG_P2P
99 : " -m = Configuration file for the P2P Device interface\n"
100 : #endif /* CONFIG_P2P */
101 : " -N = start describing new interface\n");
102 :
103 0 : printf("example:\n"
104 : " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
105 0 : wpa_drivers[0] ? wpa_drivers[0]->name : "nl80211");
106 : #endif /* CONFIG_NO_STDOUT_DEBUG */
107 0 : }
108 :
109 :
110 0 : static void license(void)
111 : {
112 : #ifndef CONFIG_NO_STDOUT_DEBUG
113 0 : printf("%s\n\n%s%s%s%s%s\n",
114 : wpa_supplicant_version,
115 : wpa_supplicant_full_license1,
116 : wpa_supplicant_full_license2,
117 : wpa_supplicant_full_license3,
118 : wpa_supplicant_full_license4,
119 : wpa_supplicant_full_license5);
120 : #endif /* CONFIG_NO_STDOUT_DEBUG */
121 0 : }
122 :
123 :
124 8 : static void wpa_supplicant_fd_workaround(int start)
125 : {
126 : #ifdef __linux__
127 : static int fd[3] = { -1, -1, -1 };
128 : int i;
129 : /* When started from pcmcia-cs scripts, wpa_supplicant might start with
130 : * fd 0, 1, and 2 closed. This will cause some issues because many
131 : * places in wpa_supplicant are still printing out to stdout. As a
132 : * workaround, make sure that fd's 0, 1, and 2 are not used for other
133 : * sockets. */
134 8 : if (start) {
135 4 : for (i = 0; i < 3; i++) {
136 4 : fd[i] = open("/dev/null", O_RDWR);
137 4 : if (fd[i] > 2) {
138 4 : close(fd[i]);
139 4 : fd[i] = -1;
140 4 : break;
141 : }
142 : }
143 : } else {
144 16 : for (i = 0; i < 3; i++) {
145 12 : if (fd[i] >= 0) {
146 0 : close(fd[i]);
147 0 : fd[i] = -1;
148 : }
149 : }
150 : }
151 : #endif /* __linux__ */
152 8 : }
153 :
154 :
155 4 : int main(int argc, char *argv[])
156 : {
157 : int c, i;
158 : struct wpa_interface *ifaces, *iface;
159 4 : int iface_count, exitcode = -1;
160 : struct wpa_params params;
161 : struct wpa_global *global;
162 :
163 4 : if (os_program_init())
164 0 : return -1;
165 :
166 4 : os_memset(¶ms, 0, sizeof(params));
167 4 : params.wpa_debug_level = MSG_INFO;
168 :
169 4 : iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
170 4 : if (ifaces == NULL)
171 0 : return -1;
172 4 : iface_count = 1;
173 :
174 4 : wpa_supplicant_fd_workaround(1);
175 :
176 : for (;;) {
177 41 : c = getopt(argc, argv,
178 : "b:Bc:C:D:de:f:g:G:hi:I:KLm:No:O:p:P:qsTtuvW");
179 41 : if (c < 0)
180 4 : break;
181 37 : switch (c) {
182 : case 'b':
183 0 : iface->bridge_ifname = optarg;
184 0 : break;
185 : case 'B':
186 0 : params.daemonize++;
187 0 : break;
188 : case 'c':
189 3 : iface->confname = optarg;
190 3 : break;
191 : case 'C':
192 0 : iface->ctrl_interface = optarg;
193 0 : break;
194 : case 'D':
195 3 : iface->driver = optarg;
196 3 : break;
197 : case 'd':
198 : #ifdef CONFIG_NO_STDOUT_DEBUG
199 : printf("Debugging disabled with "
200 : "CONFIG_NO_STDOUT_DEBUG=y build time "
201 : "option.\n");
202 : goto out;
203 : #else /* CONFIG_NO_STDOUT_DEBUG */
204 8 : params.wpa_debug_level--;
205 8 : break;
206 : #endif /* CONFIG_NO_STDOUT_DEBUG */
207 : case 'e':
208 0 : params.entropy_file = optarg;
209 0 : break;
210 : #ifdef CONFIG_DEBUG_FILE
211 : case 'f':
212 4 : params.wpa_debug_file_path = optarg;
213 4 : break;
214 : #endif /* CONFIG_DEBUG_FILE */
215 : case 'g':
216 4 : params.ctrl_interface = optarg;
217 4 : break;
218 : case 'G':
219 4 : params.ctrl_interface_group = optarg;
220 4 : break;
221 : case 'h':
222 0 : usage();
223 0 : exitcode = 0;
224 0 : goto out;
225 : case 'i':
226 3 : iface->ifname = optarg;
227 3 : break;
228 : case 'I':
229 0 : iface->confanother = optarg;
230 0 : break;
231 : case 'K':
232 4 : params.wpa_debug_show_keys++;
233 4 : break;
234 : case 'L':
235 0 : license();
236 0 : exitcode = 0;
237 0 : goto out;
238 : #ifdef CONFIG_P2P
239 : case 'm':
240 0 : iface->conf_p2p_dev = optarg;
241 0 : break;
242 : #endif /* CONFIG_P2P */
243 : case 'o':
244 0 : params.override_driver = optarg;
245 0 : break;
246 : case 'O':
247 0 : params.override_ctrl_interface = optarg;
248 0 : break;
249 : case 'p':
250 0 : iface->driver_param = optarg;
251 0 : break;
252 : case 'P':
253 0 : os_free(params.pid_file);
254 0 : params.pid_file = os_rel2abs_path(optarg);
255 0 : break;
256 : case 'q':
257 0 : params.wpa_debug_level++;
258 0 : break;
259 : #ifdef CONFIG_DEBUG_SYSLOG
260 : case 's':
261 : params.wpa_debug_syslog++;
262 : break;
263 : #endif /* CONFIG_DEBUG_SYSLOG */
264 : #ifdef CONFIG_DEBUG_LINUX_TRACING
265 : case 'T':
266 0 : params.wpa_debug_tracing++;
267 0 : break;
268 : #endif /* CONFIG_DEBUG_LINUX_TRACING */
269 : case 't':
270 4 : params.wpa_debug_timestamp++;
271 4 : break;
272 : #ifdef CONFIG_DBUS
273 : case 'u':
274 : params.dbus_ctrl_interface = 1;
275 : break;
276 : #endif /* CONFIG_DBUS */
277 : case 'v':
278 0 : printf("%s\n", wpa_supplicant_version);
279 0 : exitcode = 0;
280 0 : goto out;
281 : case 'W':
282 0 : params.wait_for_monitor++;
283 0 : break;
284 : case 'N':
285 0 : iface_count++;
286 0 : iface = os_realloc_array(ifaces, iface_count,
287 : sizeof(struct wpa_interface));
288 0 : if (iface == NULL)
289 0 : goto out;
290 0 : ifaces = iface;
291 0 : iface = &ifaces[iface_count - 1];
292 0 : os_memset(iface, 0, sizeof(*iface));
293 0 : break;
294 : default:
295 0 : usage();
296 0 : exitcode = 0;
297 0 : goto out;
298 : }
299 37 : }
300 :
301 4 : exitcode = 0;
302 4 : global = wpa_supplicant_init(¶ms);
303 4 : if (global == NULL) {
304 0 : wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
305 0 : exitcode = -1;
306 0 : goto out;
307 : } else {
308 4 : wpa_printf(MSG_INFO, "Successfully initialized "
309 : "wpa_supplicant");
310 : }
311 :
312 7 : for (i = 0; exitcode == 0 && i < iface_count; i++) {
313 : struct wpa_supplicant *wpa_s;
314 :
315 5 : if ((ifaces[i].confname == NULL &&
316 4 : ifaces[i].ctrl_interface == NULL) ||
317 3 : ifaces[i].ifname == NULL) {
318 1 : if (iface_count == 1 && (params.ctrl_interface ||
319 0 : params.dbus_ctrl_interface))
320 : break;
321 0 : usage();
322 0 : exitcode = -1;
323 0 : break;
324 : }
325 3 : wpa_s = wpa_supplicant_add_iface(global, &ifaces[i]);
326 3 : if (wpa_s == NULL) {
327 0 : exitcode = -1;
328 0 : break;
329 : }
330 : #ifdef CONFIG_P2P
331 3 : if (wpa_s->global->p2p == NULL &&
332 0 : (wpa_s->drv_flags &
333 0 : WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
334 0 : wpas_p2p_add_p2pdev_interface(wpa_s) < 0)
335 0 : exitcode = -1;
336 : #endif /* CONFIG_P2P */
337 : }
338 :
339 4 : if (exitcode == 0)
340 4 : exitcode = wpa_supplicant_run(global);
341 :
342 4 : wpa_supplicant_deinit(global);
343 :
344 : out:
345 4 : wpa_supplicant_fd_workaround(0);
346 4 : os_free(ifaces);
347 4 : os_free(params.pid_file);
348 :
349 4 : os_program_deinit();
350 :
351 4 : return exitcode;
352 : }
|