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