]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/main.c
P2P: Fix interface deinit for failed group interface initialization
[thirdparty/hostap.git] / wpa_supplicant / main.c
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 static void usage(void)
21 {
22 int i;
23 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 for (i = 0; wpa_drivers[i]; i++) {
56 printf(" %s = %s\n",
57 wpa_drivers[i]->name,
58 wpa_drivers[i]->desc);
59 }
60
61 #ifndef CONFIG_NO_STDOUT_DEBUG
62 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 printf(" -f = log output to debug file instead of stdout\n");
74 #endif /* CONFIG_DEBUG_FILE */
75 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 printf(" -T = record to Linux tracing in addition to logging\n");
83 printf(" (records all messages regardless of debug verbosity)\n");
84 #endif /* CONFIG_DEBUG_LINUX_TRACING */
85 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 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 printf("example:\n"
104 " wpa_supplicant -D%s -iwlan0 -c/etc/wpa_supplicant.conf\n",
105 wpa_drivers[0] ? wpa_drivers[0]->name : "nl80211");
106 #endif /* CONFIG_NO_STDOUT_DEBUG */
107 }
108
109
110 static void license(void)
111 {
112 #ifndef CONFIG_NO_STDOUT_DEBUG
113 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 }
122
123
124 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 if (start) {
135 for (i = 0; i < 3; i++) {
136 fd[i] = open("/dev/null", O_RDWR);
137 if (fd[i] > 2) {
138 close(fd[i]);
139 fd[i] = -1;
140 break;
141 }
142 }
143 } else {
144 for (i = 0; i < 3; i++) {
145 if (fd[i] >= 0) {
146 close(fd[i]);
147 fd[i] = -1;
148 }
149 }
150 }
151 #endif /* __linux__ */
152 }
153
154
155 int main(int argc, char *argv[])
156 {
157 int c, i;
158 struct wpa_interface *ifaces, *iface;
159 int iface_count, exitcode = -1;
160 struct wpa_params params;
161 struct wpa_global *global;
162
163 if (os_program_init())
164 return -1;
165
166 os_memset(&params, 0, sizeof(params));
167 params.wpa_debug_level = MSG_INFO;
168
169 iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
170 if (ifaces == NULL)
171 return -1;
172 iface_count = 1;
173
174 wpa_supplicant_fd_workaround(1);
175
176 for (;;) {
177 c = getopt(argc, argv,
178 "b:Bc:C:D:de:f:g:G:hi:I:KLm:No:O:p:P:qsTtuvW");
179 if (c < 0)
180 break;
181 switch (c) {
182 case 'b':
183 iface->bridge_ifname = optarg;
184 break;
185 case 'B':
186 params.daemonize++;
187 break;
188 case 'c':
189 iface->confname = optarg;
190 break;
191 case 'C':
192 iface->ctrl_interface = optarg;
193 break;
194 case 'D':
195 iface->driver = optarg;
196 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 params.wpa_debug_level--;
205 break;
206 #endif /* CONFIG_NO_STDOUT_DEBUG */
207 case 'e':
208 params.entropy_file = optarg;
209 break;
210 #ifdef CONFIG_DEBUG_FILE
211 case 'f':
212 params.wpa_debug_file_path = optarg;
213 break;
214 #endif /* CONFIG_DEBUG_FILE */
215 case 'g':
216 params.ctrl_interface = optarg;
217 break;
218 case 'G':
219 params.ctrl_interface_group = optarg;
220 break;
221 case 'h':
222 usage();
223 exitcode = 0;
224 goto out;
225 case 'i':
226 iface->ifname = optarg;
227 break;
228 case 'I':
229 iface->confanother = optarg;
230 break;
231 case 'K':
232 params.wpa_debug_show_keys++;
233 break;
234 case 'L':
235 license();
236 exitcode = 0;
237 goto out;
238 #ifdef CONFIG_P2P
239 case 'm':
240 iface->conf_p2p_dev = optarg;
241 break;
242 #endif /* CONFIG_P2P */
243 case 'o':
244 params.override_driver = optarg;
245 break;
246 case 'O':
247 params.override_ctrl_interface = optarg;
248 break;
249 case 'p':
250 iface->driver_param = optarg;
251 break;
252 case 'P':
253 os_free(params.pid_file);
254 params.pid_file = os_rel2abs_path(optarg);
255 break;
256 case 'q':
257 params.wpa_debug_level++;
258 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 params.wpa_debug_tracing++;
267 break;
268 #endif /* CONFIG_DEBUG_LINUX_TRACING */
269 case 't':
270 params.wpa_debug_timestamp++;
271 break;
272 #ifdef CONFIG_DBUS
273 case 'u':
274 params.dbus_ctrl_interface = 1;
275 break;
276 #endif /* CONFIG_DBUS */
277 case 'v':
278 printf("%s\n", wpa_supplicant_version);
279 exitcode = 0;
280 goto out;
281 case 'W':
282 params.wait_for_monitor++;
283 break;
284 case 'N':
285 iface_count++;
286 iface = os_realloc_array(ifaces, iface_count,
287 sizeof(struct wpa_interface));
288 if (iface == NULL)
289 goto out;
290 ifaces = iface;
291 iface = &ifaces[iface_count - 1];
292 os_memset(iface, 0, sizeof(*iface));
293 break;
294 default:
295 usage();
296 exitcode = 0;
297 goto out;
298 }
299 }
300
301 exitcode = 0;
302 global = wpa_supplicant_init(&params);
303 if (global == NULL) {
304 wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
305 exitcode = -1;
306 goto out;
307 } else {
308 wpa_printf(MSG_INFO, "Successfully initialized "
309 "wpa_supplicant");
310 }
311
312 for (i = 0; exitcode == 0 && i < iface_count; i++) {
313 struct wpa_supplicant *wpa_s;
314
315 if ((ifaces[i].confname == NULL &&
316 ifaces[i].ctrl_interface == NULL) ||
317 ifaces[i].ifname == NULL) {
318 if (iface_count == 1 && (params.ctrl_interface ||
319 params.dbus_ctrl_interface))
320 break;
321 usage();
322 exitcode = -1;
323 break;
324 }
325 wpa_s = wpa_supplicant_add_iface(global, &ifaces[i], NULL);
326 if (wpa_s == NULL) {
327 exitcode = -1;
328 break;
329 }
330 }
331
332 if (exitcode == 0)
333 exitcode = wpa_supplicant_run(global);
334
335 wpa_supplicant_deinit(global);
336
337 out:
338 wpa_supplicant_fd_workaround(0);
339 os_free(ifaces);
340 os_free(params.pid_file);
341
342 os_program_deinit();
343
344 return exitcode;
345 }