Home | History | Annotate | Download | only in wpa_supplicant
      1 /*
      2  * WPA Supplicant / main() function for UNIX like OSes and MinGW
      3  * Copyright (c) 2003-2007, Jouni Malinen <j (at) 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 [-BddhKLqqtuvW] [-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\n"
     56 #ifdef CONFIG_DEBUG_FILE
     57 	       "  -f = log output to debug file instead of stdout\n"
     58 #endif /* CONFIG_DEBUG_FILE */
     59 	       "  -g = global ctrl_interface\n"
     60 	       "  -K = include keys (passwords, etc.) in debug output\n"
     61 	       "  -t = include timestamp in debug messages\n"
     62 	       "  -h = show this help text\n"
     63 	       "  -L = show license (GPL and BSD)\n");
     64 	printf("  -p = driver parameters\n"
     65 	       "  -P = PID file\n"
     66 	       "  -q = decrease debugging verbosity (-qq even less)\n"
     67 #ifdef CONFIG_CTRL_IFACE_DBUS
     68 	       "  -u = enable DBus control interface\n"
     69 #endif /* CONFIG_CTRL_IFACE_DBUS */
     70 	       "  -v = show version\n"
     71 	       "  -W = wait for a control interface monitor before starting\n"
     72 	       "  -N = start describing new interface\n");
     73 
     74 	printf("example:\n"
     75 	       "  wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant.conf\n");
     76 #endif /* CONFIG_NO_STDOUT_DEBUG */
     77 }
     78 
     79 
     80 static void license(void)
     81 {
     82 #ifndef CONFIG_NO_STDOUT_DEBUG
     83 	printf("%s\n\n%s%s%s%s%s\n",
     84 	       wpa_supplicant_version,
     85 	       wpa_supplicant_full_license1,
     86 	       wpa_supplicant_full_license2,
     87 	       wpa_supplicant_full_license3,
     88 	       wpa_supplicant_full_license4,
     89 	       wpa_supplicant_full_license5);
     90 #endif /* CONFIG_NO_STDOUT_DEBUG */
     91 }
     92 
     93 
     94 static void wpa_supplicant_fd_workaround(void)
     95 {
     96 #ifdef __linux__
     97 	int s, i;
     98 	/* When started from pcmcia-cs scripts, wpa_supplicant might start with
     99 	 * fd 0, 1, and 2 closed. This will cause some issues because many
    100 	 * places in wpa_supplicant are still printing out to stdout. As a
    101 	 * workaround, make sure that fd's 0, 1, and 2 are not used for other
    102 	 * sockets. */
    103 	for (i = 0; i < 3; i++) {
    104 		s = open("/dev/null", O_RDWR);
    105 		if (s > 2) {
    106 			close(s);
    107 			break;
    108 		}
    109 	}
    110 #endif /* __linux__ */
    111 }
    112 
    113 
    114 int main(int argc, char *argv[])
    115 {
    116 	int c, i;
    117 	struct wpa_interface *ifaces, *iface;
    118 	int iface_count, exitcode = -1;
    119 	struct wpa_params params;
    120 	struct wpa_global *global;
    121 
    122 	if (os_program_init())
    123 		return -1;
    124 
    125 	os_memset(&params, 0, sizeof(params));
    126 	params.wpa_debug_level = MSG_INFO;
    127 
    128 	iface = ifaces = os_zalloc(sizeof(struct wpa_interface));
    129 	if (ifaces == NULL)
    130 		return -1;
    131 	iface_count = 1;
    132 
    133 	wpa_supplicant_fd_workaround();
    134 
    135 	for (;;) {
    136 		c = getopt(argc, argv, "b:Bc:C:D:df:g:hi:KLNp:P:qtuvW");
    137 		if (c < 0)
    138 			break;
    139 		switch (c) {
    140 		case 'b':
    141 			iface->bridge_ifname = optarg;
    142 			break;
    143 		case 'B':
    144 			params.daemonize++;
    145 			break;
    146 		case 'c':
    147 			iface->confname = optarg;
    148 			break;
    149 		case 'C':
    150 			iface->ctrl_interface = optarg;
    151 			break;
    152 		case 'D':
    153 			iface->driver = optarg;
    154 			break;
    155 		case 'd':
    156 #ifdef CONFIG_NO_STDOUT_DEBUG
    157 			printf("Debugging disabled with "
    158 			       "CONFIG_NO_STDOUT_DEBUG=y build time "
    159 			       "option.\n");
    160 			goto out;
    161 #else /* CONFIG_NO_STDOUT_DEBUG */
    162 			params.wpa_debug_level--;
    163 			break;
    164 #endif /* CONFIG_NO_STDOUT_DEBUG */
    165 #ifdef CONFIG_DEBUG_FILE
    166 		case 'f':
    167 			params.wpa_debug_file_path = optarg;
    168 			break;
    169 #endif /* CONFIG_DEBUG_FILE */
    170 		case 'g':
    171 			params.ctrl_interface = optarg;
    172 			break;
    173 		case 'h':
    174 			usage();
    175 			exitcode = 0;
    176 			goto out;
    177 		case 'i':
    178 			iface->ifname = optarg;
    179 			break;
    180 		case 'K':
    181 			params.wpa_debug_show_keys++;
    182 			break;
    183 		case 'L':
    184 			license();
    185 			exitcode = 0;
    186 			goto out;
    187 		case 'p':
    188 			iface->driver_param = optarg;
    189 			break;
    190 		case 'P':
    191 			os_free(params.pid_file);
    192 			params.pid_file = os_rel2abs_path(optarg);
    193 			break;
    194 		case 'q':
    195 			params.wpa_debug_level++;
    196 			break;
    197 		case 't':
    198 			params.wpa_debug_timestamp++;
    199 			break;
    200 #ifdef CONFIG_CTRL_IFACE_DBUS
    201 		case 'u':
    202 			params.dbus_ctrl_interface = 1;
    203 			break;
    204 #endif /* CONFIG_CTRL_IFACE_DBUS */
    205 		case 'v':
    206 			printf("%s\n", wpa_supplicant_version);
    207 			exitcode = 0;
    208 			goto out;
    209 		case 'W':
    210 			params.wait_for_monitor++;
    211 			break;
    212 		case 'N':
    213 			iface_count++;
    214 			iface = os_realloc(ifaces, iface_count *
    215 					   sizeof(struct wpa_interface));
    216 			if (iface == NULL)
    217 				goto out;
    218 			ifaces = iface;
    219 			iface = &ifaces[iface_count - 1];
    220 			os_memset(iface, 0, sizeof(*iface));
    221 			break;
    222 		default:
    223 			usage();
    224 			exitcode = 0;
    225 			goto out;
    226 		}
    227 	}
    228 
    229 	exitcode = 0;
    230 	global = wpa_supplicant_init(&params);
    231 	if (global == NULL) {
    232 		wpa_printf(MSG_ERROR, "Failed to initialize wpa_supplicant");
    233 		exitcode = -1;
    234 		goto out;
    235 	}
    236 
    237 	for (i = 0; exitcode == 0 && i < iface_count; i++) {
    238 		if ((ifaces[i].confname == NULL &&
    239 		     ifaces[i].ctrl_interface == NULL) ||
    240 		    ifaces[i].ifname == NULL) {
    241 			if (iface_count == 1 && (params.ctrl_interface ||
    242 						 params.dbus_ctrl_interface))
    243 				break;
    244 			usage();
    245 			exitcode = -1;
    246 			break;
    247 		}
    248 		if (wpa_supplicant_add_iface(global, &ifaces[i]) == NULL)
    249 			exitcode = -1;
    250 	}
    251 
    252 	if (exitcode == 0)
    253 		exitcode = wpa_supplicant_run(global);
    254 
    255 	wpa_supplicant_deinit(global);
    256 
    257 out:
    258 	os_free(ifaces);
    259 	os_free(params.pid_file);
    260 
    261 	os_program_deinit();
    262 
    263 	return exitcode;
    264 }
    265