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 1393793999 Lines: 63 144 43.8 %
Date: 2014-03-02 Functions: 2 4 50.0 %
Branches: 33 76 43.4 %

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

Generated by: LCOV version 1.9