]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - build-utilities/hostap.git/blob - wpa_supplicant/main.c
Re-initialize hostapd/wpa_supplicant git repository based on 0.6.3 release
[build-utilities/hostap.git] / wpa_supplicant / main.c
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  */
15 #include "includes.h"
16 #ifdef __linux__
17 #include <fcntl.h>
18 #endif /* __linux__ */
20 #include "common.h"
21 #include "wpa_supplicant_i.h"
24 extern const char *wpa_supplicant_version;
25 extern const char *wpa_supplicant_license;
26 #ifndef CONFIG_NO_STDOUT_DEBUG
27 extern const char *wpa_supplicant_full_license1;
28 extern const char *wpa_supplicant_full_license2;
29 extern const char *wpa_supplicant_full_license3;
30 extern const char *wpa_supplicant_full_license4;
31 extern const char *wpa_supplicant_full_license5;
32 #endif /* CONFIG_NO_STDOUT_DEBUG */
34 extern struct wpa_driver_ops *wpa_supplicant_drivers[];
37 static void usage(void)
38 {
39         int i;
40         printf("%s\n\n%s\n"
41                "usage:\n"
42                "  wpa_supplicant [-BddhKLqqtuvwW] [-P<pid file>] "
43                "[-g<global ctrl>] \\\n"
44                "        -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
45                "[-p<driver_param>] \\\n"
46                "        [-b<br_ifname>] [-f<debug file>] \\\n"
47                "        [-N -i<ifname> -c<conf> [-C<ctrl>] "
48                "[-D<driver>] \\\n"
49                "        [-p<driver_param>] [-b<br_ifname>] ...]\n"
50                "\n"
51                "drivers:\n",
52                wpa_supplicant_version, wpa_supplicant_license);
54         for (i = 0; wpa_supplicant_drivers[i]; i++) {
55                 printf("  %s = %s\n",
56                        wpa_supplicant_drivers[i]->name,
57                        wpa_supplicant_drivers[i]->desc);
58         }
60 #ifndef CONFIG_NO_STDOUT_DEBUG
61         printf("options:\n"
62                "  -b = optional bridge interface name\n"
63                "  -B = run daemon in the background\n"
64                "  -c = Configuration file\n"
65                "  -C = ctrl_interface parameter (only used if -c is not)\n"
66                "  -i = interface name\n"
67                "  -d = increase debugging verbosity (-dd even more)\n"
68                "  -D = driver name\n"
69 #ifdef CONFIG_DEBUG_FILE
70                "  -f = log output to debug file instead of stdout\n"
71 #endif /* CONFIG_DEBUG_FILE */
72                "  -g = global ctrl_interface\n"
73                "  -K = include keys (passwords, etc.) in debug output\n"
74                "  -t = include timestamp in debug messages\n"
75                "  -h = show this help text\n"
76                "  -L = show license (GPL and BSD)\n");
77         printf("  -p = driver parameters\n"
78                "  -P = PID file\n"
79                "  -q = decrease debugging verbosity (-qq even less)\n"
80 #ifdef CONFIG_CTRL_IFACE_DBUS
81                "  -u = enable DBus control interface\n"
82 #endif /* CONFIG_CTRL_IFACE_DBUS */
83                "  -v = show version\n"
84                "  -W = wait for a control interface monitor before starting\n"
85                "  -N = start describing new interface\n");
87         printf("example:\n"
88                "  wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant.conf\n");
89 #endif /* CONFIG_NO_STDOUT_DEBUG */
90 }
93 static void license(void)
94 {
95 #ifndef CONFIG_NO_STDOUT_DEBUG
96         printf("%s\n\n%s%s%s%s%s\n",
97                wpa_supplicant_version,
98                wpa_supplicant_full_license1,
99                wpa_supplicant_full_license2,
100                wpa_supplicant_full_license3,
101                wpa_supplicant_full_license4,
102                wpa_supplicant_full_license5);
103 #endif /* CONFIG_NO_STDOUT_DEBUG */
107 static void wpa_supplicant_fd_workaround(void)
109 #ifdef __linux__
110         int s, i;
111         /* 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. */
116         for (i = 0; i < 3; i++) {
117                 s = open("/dev/null", O_RDWR);
118                 if (s > 2) {
119                         close(s);
120                         break;
121                 }
122         }
123 #endif /* __linux__ */
127 int main(int argc, char *argv[])
129         int c, i;
130         struct wpa_interface *ifaces, *iface;
131         int iface_count, exitcode = -1;
132         struct wpa_params params;
133         struct wpa_global *global;
135         if (os_program_init())
136                 return -1;
138         os_memset(&params, 0, sizeof(params));
139         params.wpa_debug_level = MSG_INFO;
141         iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
142         if (ifaces == NULL)
143                 return -1;
144         iface_count = 1;
146         wpa_supplicant_fd_workaround();
148         for (;;) {
149                 c = getopt(argc, argv, "b:Bc:C:D:df:g:hi:KLNp:P:qtuvW");
150                 if (c < 0)
151                         break;
152                 switch (c) {
153                 case 'b':
154                         iface->bridge_ifname = optarg;
155                         break;
156                 case 'B':
157                         params.daemonize++;
158                         break;
159                 case 'c':
160                         iface->confname = optarg;
161                         break;
162                 case 'C':
163                         iface->ctrl_interface = optarg;
164                         break;
165                 case 'D':
166                         iface->driver = optarg;
167                         break;
168                 case 'd':
169 #ifdef CONFIG_NO_STDOUT_DEBUG
170                         printf("Debugging disabled with "
171                                "CONFIG_NO_STDOUT_DEBUG=y build time "
172                                "option.\n");
173                         goto out;
174 #else /* CONFIG_NO_STDOUT_DEBUG */
175                         params.wpa_debug_level--;
176                         break;
177 #endif /* CONFIG_NO_STDOUT_DEBUG */
178 #ifdef CONFIG_DEBUG_FILE
179                 case 'f':
180                         params.wpa_debug_file_path = optarg;
181                         break;
182 #endif /* CONFIG_DEBUG_FILE */
183                 case 'g':
184                         params.ctrl_interface = optarg;
185                         break;
186                 case 'h':
187                         usage();
188                         exitcode = 0;
189                         goto out;
190                 case 'i':
191                         iface->ifname = optarg;
192                         break;
193                 case 'K':
194                         params.wpa_debug_show_keys++;
195                         break;
196                 case 'L':
197                         license();
198                         exitcode = 0;
199                         goto out;
200                 case 'p':
201                         iface->driver_param = optarg;
202                         break;
203                 case 'P':
204                         os_free(params.pid_file);
205                         params.pid_file = os_rel2abs_path(optarg);
206                         break;
207                 case 'q':
208                         params.wpa_debug_level++;
209                         break;
210                 case 't':
211                         params.wpa_debug_timestamp++;
212                         break;
213 #ifdef CONFIG_CTRL_IFACE_DBUS
214                 case 'u':
215                         params.dbus_ctrl_interface = 1;
216                         break;
217 #endif /* CONFIG_CTRL_IFACE_DBUS */
218                 case 'v':
219                         printf("%s\n", wpa_supplicant_version);
220                         exitcode = 0;
221                         goto out;
222                 case 'W':
223                         params.wait_for_monitor++;
224                         break;
225                 case 'N':
226                         iface_count++;
227                         iface = os_realloc(ifaces, iface_count *
228                                            sizeof(struct wpa_interface));
229                         if (iface == NULL)
230                                 goto out;
231                         ifaces = iface;
232                         iface = &ifaces[iface_count - 1]; 
233                         os_memset(iface, 0, sizeof(*iface));
234                         break;
235                 default:
236                         usage();
237                         exitcode = 0;
238                         goto out;
239                 }
240         }
242         exitcode = 0;
243         global = wpa_supplicant_init(&params);
244         if (global == NULL) {
245                 wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
246                 exitcode = -1;
247                 goto out;
248         }
250         for (i = 0; exitcode == 0 && i < iface_count; i++) {
251                 if ((ifaces[i].confname == NULL &&
252                      ifaces[i].ctrl_interface == NULL) ||
253                     ifaces[i].ifname == NULL) {
254                         if (iface_count == 1 && (params.ctrl_interface ||
255                                                  params.dbus_ctrl_interface))
256                                 break;
257                         usage();
258                         exitcode = -1;
259                         break;
260                 }
261                 if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
262                         exitcode = -1;
263         }
265         if (exitcode == 0)
266                 exitcode = wpa_supplicant_run(global);
268         wpa_supplicant_deinit(global);
270 out:
271         os_free(ifaces);
272         os_free(params.pid_file);
274         os_program_deinit();
276         return exitcode;