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