Default kdump's data limit to 64 bytes and document how it can be disabled.
[dragonfly.git] / contrib / wpa_supplicant-0.4.9 / main.c
1 /*
2  * WPA Supplicant / main() function for UNIX like OSes and MinGW
3  * Copyright (c) 2003-2005, Jouni Malinen <jkmaline@cc.hut.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 <stdlib.h>
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <fcntl.h>
20
21 #include "common.h"
22 #include "wpa_supplicant_i.h"
23
24
25 extern const char *wpa_supplicant_version;
26 extern const char *wpa_supplicant_license;
27 #ifndef CONFIG_NO_STDOUT_DEBUG
28 extern const char *wpa_supplicant_full_license;
29 #endif /* CONFIG_NO_STDOUT_DEBUG */
30
31 extern struct wpa_driver_ops *wpa_supplicant_drivers[];
32
33
34 static void usage(void)
35 {
36         int i;
37         printf("%s\n\n%s\n"
38                "usage:\n"
39                "  wpa_supplicant [-BddehLqqvwW] [-P<pid file>] "
40                "[-g<global ctrl>] \\\n"
41                "        -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
42                "[-p<driver_param>] \\\n"
43                "        [-N -i<ifname> -c<conf> [-C<ctrl>] [-D<driver>] "
44                "[-p<driver_param>] ...]\n"
45                "\n"
46                "drivers:\n",
47                wpa_supplicant_version, wpa_supplicant_license);
48
49         for (i = 0; wpa_supplicant_drivers[i]; i++) {
50                 printf("  %s = %s\n",
51                        wpa_supplicant_drivers[i]->name,
52                        wpa_supplicant_drivers[i]->desc);
53         }
54
55 #ifndef CONFIG_NO_STDOUT_DEBUG
56         printf("options:\n"
57                "  -B = run daemon in the background\n"
58                "  -c = Configuration file\n"
59                "  -C = ctrl_interface parameter (only used if -c is not)\n"
60                "  -i = interface name\n"
61                "  -d = increase debugging verbosity (-dd even more)\n"
62                "  -D = driver name\n"
63                "  -g = global ctrl_interface\n"
64                "  -K = include keys (passwords, etc.) in debug output\n"
65                "  -t = include timestamp in debug messages\n"
66                "  -h = show this help text\n"
67                "  -L = show license (GPL and BSD)\n"
68                "  -p = driver parameters\n"
69                "  -P = PID file\n"
70                "  -q = decrease debugging verbosity (-qq even less)\n"
71                "  -v = show version\n"
72                "  -w = wait for interface to be added, if needed\n"
73                "  -W = wait for a control interface monitor before starting\n"
74                "  -N = start describing new interface\n");
75
76         printf("example:\n"
77                "  wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant.conf\n");
78 #endif /* CONFIG_NO_STDOUT_DEBUG */
79 }
80
81
82 static void license(void)
83 {
84 #ifndef CONFIG_NO_STDOUT_DEBUG
85         printf("%s\n\n%s\n",
86                wpa_supplicant_version, wpa_supplicant_full_license);
87 #endif /* CONFIG_NO_STDOUT_DEBUG */
88 }
89
90
91 static void wpa_supplicant_fd_workaround(void)
92 {
93         int s, i;
94         /* When started from pcmcia-cs scripts, wpa_supplicant might start with
95          * fd 0, 1, and 2 closed. This will cause some issues because many
96          * places in wpa_supplicant are still printing out to stdout. As a
97          * workaround, make sure that fd's 0, 1, and 2 are not used for other
98          * sockets. */
99         for (i = 0; i < 3; i++) {
100                 s = open("/dev/null", O_RDWR);
101                 if (s > 2) {
102                         close(s);
103                         break;
104                 }
105         }
106 }
107
108
109 int main(int argc, char *argv[])
110 {
111         int c, i;
112         struct wpa_interface *ifaces, *iface;
113         int iface_count, exitcode = -1;
114         struct wpa_params params;
115         struct wpa_global *global;
116
117 #ifdef CONFIG_NATIVE_WINDOWS
118         WSADATA wsaData;
119         if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
120                 printf("Could not find a usable WinSock.dll\n");
121                 return -1;
122         }
123 #endif /* CONFIG_NATIVE_WINDOWS */
124
125         memset(&params, 0, sizeof(params));
126         params.wpa_debug_level = MSG_INFO;
127
128         iface = ifaces = malloc(sizeof(struct wpa_interface));
129         if (ifaces == NULL)
130                 return -1;
131         memset(iface, 0, sizeof(*iface));
132         iface_count = 1;
133
134         wpa_supplicant_fd_workaround();
135
136         for (;;) {
137                 c = getopt(argc, argv, "Bc:C:D:dg:hi:KLNp:P:qtvwW");
138                 if (c < 0)
139                         break;
140                 switch (c) {
141                 case 'B':
142                         params.daemonize++;
143                         break;
144                 case 'c':
145                         iface->confname = optarg;
146                         break;
147                 case 'C':
148                         iface->ctrl_interface = optarg;
149                         break;
150                 case 'D':
151                         iface->driver = optarg;
152                         break;
153                 case 'd':
154 #ifdef CONFIG_NO_STDOUT_DEBUG
155                         printf("Debugging disabled with "
156                                "CONFIG_NO_STDOUT_DEBUG=y build time "
157                                "option.\n");
158                         goto out;
159 #else /* CONFIG_NO_STDOUT_DEBUG */
160                         params.wpa_debug_level--;
161                         break;
162 #endif /* CONFIG_NO_STDOUT_DEBUG */
163                 case 'g':
164                         params.ctrl_interface = optarg;
165                         break;
166                 case 'h':
167                         usage();
168                         goto out;
169                 case 'i':
170                         iface->ifname = optarg;
171                         break;
172                 case 'K':
173                         params.wpa_debug_show_keys++;
174                         break;
175                 case 'L':
176                         license();
177                         goto out;
178                 case 'p':
179                         iface->driver_param = optarg;
180                         break;
181                 case 'P':
182                         params.pid_file = rel2abs_path(optarg);
183                         break;
184                 case 'q':
185                         params.wpa_debug_level++;
186                         break;
187                 case 't':
188                         params.wpa_debug_timestamp++;
189                         break;
190                 case 'v':
191                         printf("%s\n", wpa_supplicant_version);
192                         goto out;
193                 case 'w':
194                         params.wait_for_interface++;
195                         break;
196                 case 'W':
197                         params.wait_for_monitor++;
198                         break;
199                 case 'N':
200                         iface_count++;
201                         iface = realloc(ifaces, iface_count *
202                                         sizeof(struct wpa_interface));
203                         if (iface == NULL)
204                                 goto out;
205                         ifaces = iface;
206                         iface = &ifaces[iface_count - 1]; 
207                         memset(iface, 0, sizeof(*iface));
208                         break;
209                 default:
210                         usage();
211                         goto out;
212                 }
213         }
214
215         exitcode = 0;
216         global = wpa_supplicant_init(&params);
217         if (global == NULL) {
218                 printf("Failed to initialize wpa_supplicant\n");
219                 exitcode = -1;
220         }
221
222         for (i = 0; exitcode == 0 && i < iface_count; i++) {
223                 if ((ifaces[i].confname == NULL &&
224                      ifaces[i].ctrl_interface == NULL) ||
225                     ifaces[i].ifname == NULL) {
226                         if (iface_count == 1 && params.ctrl_interface)
227                                 break;
228                         usage();
229                         exitcode = -1;
230                         break;
231                 }
232                 if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
233                         exitcode = -1;
234         }
235
236         if (exitcode == 0)
237                 exitcode = wpa_supplicant_run(global);
238
239         wpa_supplicant_deinit(global);
240
241 out:
242         free(ifaces);
243         free(params.pid_file);
244
245 #ifdef CONFIG_NATIVE_WINDOWS
246         WSACleanup();
247 #endif /* CONFIG_NATIVE_WINDOWS */
248
249         return exitcode;
250 }