LCOV - code coverage report
Current view: top level - wpa_supplicant - main.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1422976643 Lines: 134 144 93.1 %
Date: 2015-02-03 Functions: 4 4 100.0 %

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

Generated by: LCOV version 1.10