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