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