LCOV - code coverage report
Current view: top level - src/utils - wpa_debug.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1475438200 Lines: 308 353 87.3 %
Date: 2016-10-02 Functions: 26 26 100.0 %

          Line data    Source code
       1             : /*
       2             :  * wpa_supplicant/hostapd / Debug prints
       3             :  * Copyright (c) 2002-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             : 
      11             : #include "common.h"
      12             : 
      13             : #ifdef CONFIG_DEBUG_SYSLOG
      14             : #include <syslog.h>
      15             : 
      16             : static int wpa_debug_syslog = 0;
      17             : #endif /* CONFIG_DEBUG_SYSLOG */
      18             : 
      19             : #ifdef CONFIG_DEBUG_LINUX_TRACING
      20             : #include <sys/types.h>
      21             : #include <sys/stat.h>
      22             : #include <fcntl.h>
      23             : #include <string.h>
      24             : #include <stdio.h>
      25             : 
      26             : static FILE *wpa_debug_tracing_file = NULL;
      27             : 
      28             : #define WPAS_TRACE_PFX "wpas <%d>: "
      29             : #endif /* CONFIG_DEBUG_LINUX_TRACING */
      30             : 
      31             : 
      32             : int wpa_debug_level = MSG_INFO;
      33             : int wpa_debug_show_keys = 0;
      34             : int wpa_debug_timestamp = 0;
      35             : 
      36             : 
      37             : #ifdef CONFIG_ANDROID_LOG
      38             : 
      39             : #include <android/log.h>
      40             : 
      41             : #ifndef ANDROID_LOG_NAME
      42             : #define ANDROID_LOG_NAME        "wpa_supplicant"
      43             : #endif /* ANDROID_LOG_NAME */
      44             : 
      45             : static int wpa_to_android_level(int level)
      46             : {
      47             :         if (level == MSG_ERROR)
      48             :                 return ANDROID_LOG_ERROR;
      49             :         if (level == MSG_WARNING)
      50             :                 return ANDROID_LOG_WARN;
      51             :         if (level == MSG_INFO)
      52             :                 return ANDROID_LOG_INFO;
      53             :         return ANDROID_LOG_DEBUG;
      54             : }
      55             : 
      56             : #endif /* CONFIG_ANDROID_LOG */
      57             : 
      58             : #ifndef CONFIG_NO_STDOUT_DEBUG
      59             : 
      60             : #ifdef CONFIG_DEBUG_FILE
      61             : static FILE *out_file = NULL;
      62             : #endif /* CONFIG_DEBUG_FILE */
      63             : 
      64             : 
      65     8573941 : void wpa_debug_print_timestamp(void)
      66             : {
      67             : #ifndef CONFIG_ANDROID_LOG
      68             :         struct os_time tv;
      69             : 
      70     8573941 :         if (!wpa_debug_timestamp)
      71     8574136 :                 return;
      72             : 
      73     8573746 :         os_get_time(&tv);
      74             : #ifdef CONFIG_DEBUG_FILE
      75     8573746 :         if (out_file) {
      76     8056353 :                 fprintf(out_file, "%ld.%06u: ", (long) tv.sec,
      77     8056353 :                         (unsigned int) tv.usec);
      78             :         } else
      79             : #endif /* CONFIG_DEBUG_FILE */
      80      517393 :         printf("%ld.%06u: ", (long) tv.sec, (unsigned int) tv.usec);
      81             : #endif /* CONFIG_ANDROID_LOG */
      82             : }
      83             : 
      84             : 
      85             : #ifdef CONFIG_DEBUG_SYSLOG
      86             : #ifndef LOG_HOSTAPD
      87             : #define LOG_HOSTAPD LOG_DAEMON
      88             : #endif /* LOG_HOSTAPD */
      89             : 
      90             : void wpa_debug_open_syslog(void)
      91             : {
      92             :         openlog("wpa_supplicant", LOG_PID | LOG_NDELAY, LOG_HOSTAPD);
      93             :         wpa_debug_syslog++;
      94             : }
      95             : 
      96             : 
      97             : void wpa_debug_close_syslog(void)
      98             : {
      99             :         if (wpa_debug_syslog)
     100             :                 closelog();
     101             : }
     102             : 
     103             : 
     104             : static int syslog_priority(int level)
     105             : {
     106             :         switch (level) {
     107             :         case MSG_MSGDUMP:
     108             :         case MSG_DEBUG:
     109             :                 return LOG_DEBUG;
     110             :         case MSG_INFO:
     111             :                 return LOG_NOTICE;
     112             :         case MSG_WARNING:
     113             :                 return LOG_WARNING;
     114             :         case MSG_ERROR:
     115             :                 return LOG_ERR;
     116             :         }
     117             :         return LOG_INFO;
     118             : }
     119             : #endif /* CONFIG_DEBUG_SYSLOG */
     120             : 
     121             : 
     122             : #ifdef CONFIG_DEBUG_LINUX_TRACING
     123             : 
     124           1 : int wpa_debug_open_linux_tracing(void)
     125             : {
     126             :         int mounts, trace_fd;
     127           1 :         char buf[4096] = {};
     128             :         ssize_t buflen;
     129           1 :         char *line, *tmp1, *path = NULL;
     130             : 
     131           1 :         mounts = open("/proc/mounts", O_RDONLY);
     132           1 :         if (mounts < 0) {
     133           0 :                 printf("no /proc/mounts\n");
     134           0 :                 return -1;
     135             :         }
     136             : 
     137           1 :         buflen = read(mounts, buf, sizeof(buf) - 1);
     138           1 :         close(mounts);
     139           1 :         if (buflen < 0) {
     140           0 :                 printf("failed to read /proc/mounts\n");
     141           0 :                 return -1;
     142             :         }
     143             : 
     144           1 :         line = strtok_r(buf, "\n", &tmp1);
     145           9 :         while (line) {
     146             :                 char *tmp2, *tmp_path, *fstype;
     147             :                 /* "<dev> <mountpoint> <fs type> ..." */
     148           8 :                 strtok_r(line, " ", &tmp2);
     149           8 :                 tmp_path = strtok_r(NULL, " ", &tmp2);
     150           8 :                 fstype = strtok_r(NULL, " ", &tmp2);
     151           8 :                 if (fstype && strcmp(fstype, "debugfs") == 0) {
     152           1 :                         path = tmp_path;
     153           1 :                         break;
     154             :                 }
     155             : 
     156           7 :                 line = strtok_r(NULL, "\n", &tmp1);
     157             :         }
     158             : 
     159           1 :         if (path == NULL) {
     160           0 :                 printf("debugfs mountpoint not found\n");
     161           0 :                 return -1;
     162             :         }
     163             : 
     164           1 :         snprintf(buf, sizeof(buf) - 1, "%s/tracing/trace_marker", path);
     165             : 
     166           1 :         trace_fd = open(buf, O_WRONLY);
     167           1 :         if (trace_fd < 0) {
     168           0 :                 printf("failed to open trace_marker file\n");
     169           0 :                 return -1;
     170             :         }
     171           1 :         wpa_debug_tracing_file = fdopen(trace_fd, "w");
     172           1 :         if (wpa_debug_tracing_file == NULL) {
     173           0 :                 close(trace_fd);
     174           0 :                 printf("failed to fdopen()\n");
     175           0 :                 return -1;
     176             :         }
     177             : 
     178           1 :         return 0;
     179             : }
     180             : 
     181             : 
     182          74 : void wpa_debug_close_linux_tracing(void)
     183             : {
     184          74 :         if (wpa_debug_tracing_file == NULL)
     185         147 :                 return;
     186           1 :         fclose(wpa_debug_tracing_file);
     187           1 :         wpa_debug_tracing_file = NULL;
     188             : }
     189             : 
     190             : #endif /* CONFIG_DEBUG_LINUX_TRACING */
     191             : 
     192             : 
     193             : /**
     194             :  * wpa_printf - conditional printf
     195             :  * @level: priority level (MSG_*) of the message
     196             :  * @fmt: printf format string, followed by optional arguments
     197             :  *
     198             :  * This function is used to print conditional debugging and error messages. The
     199             :  * output may be directed to stdout, stderr, and/or syslog based on
     200             :  * configuration.
     201             :  *
     202             :  * Note: New line '\n' is added to the end of the text when printing to stdout.
     203             :  */
     204     8050692 : void wpa_printf(int level, const char *fmt, ...)
     205             : {
     206             :         va_list ap;
     207             : 
     208     8050692 :         va_start(ap, fmt);
     209     8050692 :         if (level >= wpa_debug_level) {
     210             : #ifdef CONFIG_ANDROID_LOG
     211             :                 __android_log_vprint(wpa_to_android_level(level),
     212             :                                      ANDROID_LOG_NAME, fmt, ap);
     213             : #else /* CONFIG_ANDROID_LOG */
     214             : #ifdef CONFIG_DEBUG_SYSLOG
     215             :                 if (wpa_debug_syslog) {
     216             :                         vsyslog(syslog_priority(level), fmt, ap);
     217             :                 } else {
     218             : #endif /* CONFIG_DEBUG_SYSLOG */
     219     7558404 :                 wpa_debug_print_timestamp();
     220             : #ifdef CONFIG_DEBUG_FILE
     221     7558404 :                 if (out_file) {
     222     7103494 :                         vfprintf(out_file, fmt, ap);
     223     7103494 :                         fprintf(out_file, "\n");
     224             :                 } else {
     225             : #endif /* CONFIG_DEBUG_FILE */
     226      454910 :                 vprintf(fmt, ap);
     227      454910 :                 printf("\n");
     228             : #ifdef CONFIG_DEBUG_FILE
     229             :                 }
     230             : #endif /* CONFIG_DEBUG_FILE */
     231             : #ifdef CONFIG_DEBUG_SYSLOG
     232             :                 }
     233             : #endif /* CONFIG_DEBUG_SYSLOG */
     234             : #endif /* CONFIG_ANDROID_LOG */
     235             :         }
     236     8050692 :         va_end(ap);
     237             : 
     238             : #ifdef CONFIG_DEBUG_LINUX_TRACING
     239     8050692 :         if (wpa_debug_tracing_file != NULL) {
     240         151 :                 va_start(ap, fmt);
     241         151 :                 fprintf(wpa_debug_tracing_file, WPAS_TRACE_PFX, level);
     242         151 :                 vfprintf(wpa_debug_tracing_file, fmt, ap);
     243         151 :                 fprintf(wpa_debug_tracing_file, "\n");
     244         151 :                 fflush(wpa_debug_tracing_file);
     245         151 :                 va_end(ap);
     246             :         }
     247             : #endif /* CONFIG_DEBUG_LINUX_TRACING */
     248     8050692 : }
     249             : 
     250             : 
     251      716458 : static void _wpa_hexdump(int level, const char *title, const u8 *buf,
     252             :                          size_t len, int show)
     253             : {
     254             :         size_t i;
     255             : 
     256             : #ifdef CONFIG_DEBUG_LINUX_TRACING
     257      716458 :         if (wpa_debug_tracing_file != NULL) {
     258           7 :                 fprintf(wpa_debug_tracing_file,
     259             :                         WPAS_TRACE_PFX "%s - hexdump(len=%lu):",
     260             :                         level, title, (unsigned long) len);
     261           7 :                 if (buf == NULL) {
     262           0 :                         fprintf(wpa_debug_tracing_file, " [NULL]\n");
     263           7 :                 } else if (!show) {
     264           0 :                         fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
     265             :                 } else {
     266          69 :                         for (i = 0; i < len; i++)
     267          62 :                                 fprintf(wpa_debug_tracing_file,
     268          62 :                                         " %02x", buf[i]);
     269             :                 }
     270           7 :                 fflush(wpa_debug_tracing_file);
     271             :         }
     272             : #endif /* CONFIG_DEBUG_LINUX_TRACING */
     273             : 
     274      716458 :         if (level < wpa_debug_level)
     275      728121 :                 return;
     276             : #ifdef CONFIG_ANDROID_LOG
     277             :         {
     278             :                 const char *display;
     279             :                 char *strbuf = NULL;
     280             :                 size_t slen = len;
     281             :                 if (buf == NULL) {
     282             :                         display = " [NULL]";
     283             :                 } else if (len == 0) {
     284             :                         display = "";
     285             :                 } else if (show && len) {
     286             :                         /* Limit debug message length for Android log */
     287             :                         if (slen > 32)
     288             :                                 slen = 32;
     289             :                         strbuf = os_malloc(1 + 3 * slen);
     290             :                         if (strbuf == NULL) {
     291             :                                 wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
     292             :                                            "allocate message buffer");
     293             :                                 return;
     294             :                         }
     295             : 
     296             :                         for (i = 0; i < slen; i++)
     297             :                                 os_snprintf(&strbuf[i * 3], 4, " %02x",
     298             :                                             buf[i]);
     299             : 
     300             :                         display = strbuf;
     301             :                 } else {
     302             :                         display = " [REMOVED]";
     303             :                 }
     304             : 
     305             :                 __android_log_print(wpa_to_android_level(level),
     306             :                                     ANDROID_LOG_NAME,
     307             :                                     "%s - hexdump(len=%lu):%s%s",
     308             :                                     title, (long unsigned int) len, display,
     309             :                                     len > slen ? " ..." : "");
     310             :                 bin_clear_free(strbuf, 1 + 3 * slen);
     311             :                 return;
     312             :         }
     313             : #else /* CONFIG_ANDROID_LOG */
     314             : #ifdef CONFIG_DEBUG_SYSLOG
     315             :         if (wpa_debug_syslog) {
     316             :                 const char *display;
     317             :                 char *strbuf = NULL;
     318             : 
     319             :                 if (buf == NULL) {
     320             :                         display = " [NULL]";
     321             :                 } else if (len == 0) {
     322             :                         display = "";
     323             :                 } else if (show && len) {
     324             :                         strbuf = os_malloc(1 + 3 * len);
     325             :                         if (strbuf == NULL) {
     326             :                                 wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
     327             :                                            "allocate message buffer");
     328             :                                 return;
     329             :                         }
     330             : 
     331             :                         for (i = 0; i < len; i++)
     332             :                                 os_snprintf(&strbuf[i * 3], 4, " %02x",
     333             :                                             buf[i]);
     334             : 
     335             :                         display = strbuf;
     336             :                 } else {
     337             :                         display = " [REMOVED]";
     338             :                 }
     339             : 
     340             :                 syslog(syslog_priority(level), "%s - hexdump(len=%lu):%s",
     341             :                        title, (unsigned long) len, display);
     342             :                 bin_clear_free(strbuf, 1 + 3 * len);
     343             :                 return;
     344             :         }
     345             : #endif /* CONFIG_DEBUG_SYSLOG */
     346      704795 :         wpa_debug_print_timestamp();
     347             : #ifdef CONFIG_DEBUG_FILE
     348      704795 :         if (out_file) {
     349      648091 :                 fprintf(out_file, "%s - hexdump(len=%lu):",
     350             :                         title, (unsigned long) len);
     351      648091 :                 if (buf == NULL) {
     352       20496 :                         fprintf(out_file, " [NULL]");
     353      627595 :                 } else if (show) {
     354    32545299 :                         for (i = 0; i < len; i++)
     355    31917704 :                                 fprintf(out_file, " %02x", buf[i]);
     356             :                 } else {
     357           0 :                         fprintf(out_file, " [REMOVED]");
     358             :                 }
     359      648091 :                 fprintf(out_file, "\n");
     360             :         } else {
     361             : #endif /* CONFIG_DEBUG_FILE */
     362       56704 :         printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
     363       56704 :         if (buf == NULL) {
     364         346 :                 printf(" [NULL]");
     365       56358 :         } else if (show) {
     366     6281706 :                 for (i = 0; i < len; i++)
     367     6225348 :                         printf(" %02x", buf[i]);
     368             :         } else {
     369           0 :                 printf(" [REMOVED]");
     370             :         }
     371       56704 :         printf("\n");
     372             : #ifdef CONFIG_DEBUG_FILE
     373             :         }
     374             : #endif /* CONFIG_DEBUG_FILE */
     375             : #endif /* CONFIG_ANDROID_LOG */
     376             : }
     377             : 
     378      558181 : void wpa_hexdump(int level, const char *title, const void *buf, size_t len)
     379             : {
     380      558181 :         _wpa_hexdump(level, title, buf, len, 1);
     381      558181 : }
     382             : 
     383             : 
     384      158277 : void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len)
     385             : {
     386      158277 :         _wpa_hexdump(level, title, buf, len, wpa_debug_show_keys);
     387      158277 : }
     388             : 
     389             : 
     390      164023 : static void _wpa_hexdump_ascii(int level, const char *title, const void *buf,
     391             :                                size_t len, int show)
     392             : {
     393             :         size_t i, llen;
     394      164023 :         const u8 *pos = buf;
     395      164023 :         const size_t line_len = 16;
     396             : 
     397             : #ifdef CONFIG_DEBUG_LINUX_TRACING
     398      164023 :         if (wpa_debug_tracing_file != NULL) {
     399           0 :                 fprintf(wpa_debug_tracing_file,
     400             :                         WPAS_TRACE_PFX "%s - hexdump_ascii(len=%lu):",
     401             :                         level, title, (unsigned long) len);
     402           0 :                 if (buf == NULL) {
     403           0 :                         fprintf(wpa_debug_tracing_file, " [NULL]\n");
     404           0 :                 } else if (!show) {
     405           0 :                         fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
     406             :                 } else {
     407             :                         /* can do ascii processing in userspace */
     408           0 :                         for (i = 0; i < len; i++)
     409           0 :                                 fprintf(wpa_debug_tracing_file,
     410           0 :                                         " %02x", pos[i]);
     411             :                 }
     412           0 :                 fflush(wpa_debug_tracing_file);
     413             :         }
     414             : #endif /* CONFIG_DEBUG_LINUX_TRACING */
     415             : 
     416      164023 :         if (level < wpa_debug_level)
     417        8606 :                 return;
     418             : #ifdef CONFIG_ANDROID_LOG
     419             :         _wpa_hexdump(level, title, buf, len, show);
     420             : #else /* CONFIG_ANDROID_LOG */
     421      155417 :         wpa_debug_print_timestamp();
     422             : #ifdef CONFIG_DEBUG_FILE
     423      155417 :         if (out_file) {
     424      149613 :                 if (!show) {
     425           0 :                         fprintf(out_file,
     426             :                                 "%s - hexdump_ascii(len=%lu): [REMOVED]\n",
     427             :                                 title, (unsigned long) len);
     428           0 :                         return;
     429             :                 }
     430      149613 :                 if (buf == NULL) {
     431        4960 :                         fprintf(out_file,
     432             :                                 "%s - hexdump_ascii(len=%lu): [NULL]\n",
     433             :                                 title, (unsigned long) len);
     434        4960 :                         return;
     435             :                 }
     436      144653 :                 fprintf(out_file, "%s - hexdump_ascii(len=%lu):\n",
     437             :                         title, (unsigned long) len);
     438      568449 :                 while (len) {
     439      279143 :                         llen = len > line_len ? line_len : len;
     440      279143 :                         fprintf(out_file, "    ");
     441     3620283 :                         for (i = 0; i < llen; i++)
     442     3341140 :                                 fprintf(out_file, " %02x", pos[i]);
     443     1404291 :                         for (i = llen; i < line_len; i++)
     444     1125148 :                                 fprintf(out_file, "   ");
     445      279143 :                         fprintf(out_file, "   ");
     446     3620283 :                         for (i = 0; i < llen; i++) {
     447     3341140 :                                 if (isprint(pos[i]))
     448     3242733 :                                         fprintf(out_file, "%c", pos[i]);
     449             :                                 else
     450       98407 :                                         fprintf(out_file, "_");
     451             :                         }
     452     1404291 :                         for (i = llen; i < line_len; i++)
     453     1125148 :                                 fprintf(out_file, " ");
     454      279143 :                         fprintf(out_file, "\n");
     455      279143 :                         pos += llen;
     456      279143 :                         len -= llen;
     457             :                 }
     458             :         } else {
     459             : #endif /* CONFIG_DEBUG_FILE */
     460        5804 :         if (!show) {
     461           0 :                 printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
     462             :                        title, (unsigned long) len);
     463           0 :                 return;
     464             :         }
     465        5804 :         if (buf == NULL) {
     466           0 :                 printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
     467             :                        title, (unsigned long) len);
     468           0 :                 return;
     469             :         }
     470        5804 :         printf("%s - hexdump_ascii(len=%lu):\n", title, (unsigned long) len);
     471       20409 :         while (len) {
     472        8801 :                 llen = len > line_len ? line_len : len;
     473        8801 :                 printf("    ");
     474      101866 :                 for (i = 0; i < llen; i++)
     475       93065 :                         printf(" %02x", pos[i]);
     476       56552 :                 for (i = llen; i < line_len; i++)
     477       47751 :                         printf("   ");
     478        8801 :                 printf("   ");
     479      101866 :                 for (i = 0; i < llen; i++) {
     480       93065 :                         if (isprint(pos[i]))
     481       93038 :                                 printf("%c", pos[i]);
     482             :                         else
     483          27 :                                 printf("_");
     484             :                 }
     485       56552 :                 for (i = llen; i < line_len; i++)
     486       47751 :                         printf(" ");
     487        8801 :                 printf("\n");
     488        8801 :                 pos += llen;
     489        8801 :                 len -= llen;
     490             :         }
     491             : #ifdef CONFIG_DEBUG_FILE
     492             :         }
     493             : #endif /* CONFIG_DEBUG_FILE */
     494             : #endif /* CONFIG_ANDROID_LOG */
     495             : }
     496             : 
     497             : 
     498      137344 : void wpa_hexdump_ascii(int level, const char *title, const void *buf,
     499             :                        size_t len)
     500             : {
     501      137344 :         _wpa_hexdump_ascii(level, title, buf, len, 1);
     502      137344 : }
     503             : 
     504             : 
     505       26679 : void wpa_hexdump_ascii_key(int level, const char *title, const void *buf,
     506             :                            size_t len)
     507             : {
     508       26679 :         _wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
     509       26679 : }
     510             : 
     511             : 
     512             : #ifdef CONFIG_DEBUG_FILE
     513             : static char *last_path = NULL;
     514             : #endif /* CONFIG_DEBUG_FILE */
     515             : 
     516       10043 : int wpa_debug_reopen_file(void)
     517             : {
     518             : #ifdef CONFIG_DEBUG_FILE
     519             :         int rv;
     520             :         char *tmp;
     521             : 
     522       10043 :         if (!last_path)
     523           0 :                 return 0; /* logfile not used */
     524             : 
     525       10043 :         tmp = os_strdup(last_path);
     526       10043 :         if (!tmp)
     527           0 :                 return -1;
     528             : 
     529       10043 :         wpa_debug_close_file();
     530       10043 :         rv = wpa_debug_open_file(tmp);
     531       10043 :         os_free(tmp);
     532       10043 :         return rv;
     533             : #else /* CONFIG_DEBUG_FILE */
     534             :         return 0;
     535             : #endif /* CONFIG_DEBUG_FILE */
     536             : }
     537             : 
     538             : 
     539       10098 : int wpa_debug_open_file(const char *path)
     540             : {
     541             : #ifdef CONFIG_DEBUG_FILE
     542       10098 :         if (!path)
     543           0 :                 return 0;
     544             : 
     545       10098 :         if (last_path == NULL || os_strcmp(last_path, path) != 0) {
     546             :                 /* Save our path to enable re-open */
     547       10098 :                 os_free(last_path);
     548       10098 :                 last_path = os_strdup(path);
     549             :         }
     550             : 
     551       10098 :         out_file = fopen(path, "a");
     552       10098 :         if (out_file == NULL) {
     553           0 :                 wpa_printf(MSG_ERROR, "wpa_debug_open_file: Failed to open "
     554             :                            "output file, using standard output");
     555           0 :                 return -1;
     556             :         }
     557             : #ifndef _WIN32
     558       10098 :         setvbuf(out_file, NULL, _IOLBF, 0);
     559             : #endif /* _WIN32 */
     560             : #else /* CONFIG_DEBUG_FILE */
     561             :         (void)path;
     562             : #endif /* CONFIG_DEBUG_FILE */
     563       10098 :         return 0;
     564             : }
     565             : 
     566             : 
     567       10111 : void wpa_debug_close_file(void)
     568             : {
     569             : #ifdef CONFIG_DEBUG_FILE
     570       10111 :         if (!out_file)
     571       10124 :                 return;
     572       10098 :         fclose(out_file);
     573       10098 :         out_file = NULL;
     574       10098 :         os_free(last_path);
     575       10098 :         last_path = NULL;
     576             : #endif /* CONFIG_DEBUG_FILE */
     577             : }
     578             : 
     579             : 
     580          19 : void wpa_debug_setup_stdout(void)
     581             : {
     582             : #ifndef _WIN32
     583          19 :         setvbuf(stdout, NULL, _IOLBF, 0);
     584             : #endif /* _WIN32 */
     585          19 : }
     586             : 
     587             : #endif /* CONFIG_NO_STDOUT_DEBUG */
     588             : 
     589             : 
     590             : #ifndef CONFIG_NO_WPA_MSG
     591             : static wpa_msg_cb_func wpa_msg_cb = NULL;
     592             : 
     593        2780 : void wpa_msg_register_cb(wpa_msg_cb_func func)
     594             : {
     595        2780 :         wpa_msg_cb = func;
     596        2780 : }
     597             : 
     598             : 
     599             : static wpa_msg_get_ifname_func wpa_msg_ifname_cb = NULL;
     600             : 
     601          74 : void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)
     602             : {
     603          74 :         wpa_msg_ifname_cb = func;
     604          74 : }
     605             : 
     606             : 
     607     1204622 : void wpa_msg(void *ctx, int level, const char *fmt, ...)
     608             : {
     609             :         va_list ap;
     610             :         char *buf;
     611             :         int buflen;
     612             :         int len;
     613             :         char prefix[130];
     614             : 
     615     1204622 :         va_start(ap, fmt);
     616     1204622 :         buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
     617     1204622 :         va_end(ap);
     618             : 
     619     1204622 :         buf = os_malloc(buflen);
     620     1204622 :         if (buf == NULL) {
     621          85 :                 wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
     622             :                            "buffer");
     623     1204707 :                 return;
     624             :         }
     625     1204537 :         va_start(ap, fmt);
     626     1204537 :         prefix[0] = '\0';
     627     1204537 :         if (wpa_msg_ifname_cb) {
     628     1204537 :                 const char *ifname = wpa_msg_ifname_cb(ctx);
     629     1204537 :                 if (ifname) {
     630     1203264 :                         int res = os_snprintf(prefix, sizeof(prefix), "%s: ",
     631             :                                               ifname);
     632     1203264 :                         if (os_snprintf_error(sizeof(prefix), res))
     633           0 :                                 prefix[0] = '\0';
     634             :                 }
     635             :         }
     636     1204537 :         len = vsnprintf(buf, buflen, fmt, ap);
     637     1204537 :         va_end(ap);
     638     1204537 :         wpa_printf(level, "%s%s", prefix, buf);
     639     1204537 :         if (wpa_msg_cb)
     640     1204524 :                 wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
     641     1204537 :         bin_clear_free(buf, buflen);
     642             : }
     643             : 
     644             : 
     645       32305 : void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...)
     646             : {
     647             :         va_list ap;
     648             :         char *buf;
     649             :         int buflen;
     650             :         int len;
     651             : 
     652       32305 :         if (!wpa_msg_cb)
     653           0 :                 return;
     654             : 
     655       32305 :         va_start(ap, fmt);
     656       32305 :         buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
     657       32305 :         va_end(ap);
     658             : 
     659       32305 :         buf = os_malloc(buflen);
     660       32305 :         if (buf == NULL) {
     661           0 :                 wpa_printf(MSG_ERROR, "wpa_msg_ctrl: Failed to allocate "
     662             :                            "message buffer");
     663           0 :                 return;
     664             :         }
     665       32305 :         va_start(ap, fmt);
     666       32305 :         len = vsnprintf(buf, buflen, fmt, ap);
     667       32305 :         va_end(ap);
     668       32305 :         wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
     669       32305 :         bin_clear_free(buf, buflen);
     670             : }
     671             : 
     672             : 
     673      334869 : void wpa_msg_global(void *ctx, int level, const char *fmt, ...)
     674             : {
     675             :         va_list ap;
     676             :         char *buf;
     677             :         int buflen;
     678             :         int len;
     679             : 
     680      334869 :         va_start(ap, fmt);
     681      334869 :         buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
     682      334869 :         va_end(ap);
     683             : 
     684      334869 :         buf = os_malloc(buflen);
     685      334869 :         if (buf == NULL) {
     686           8 :                 wpa_printf(MSG_ERROR, "wpa_msg_global: Failed to allocate "
     687             :                            "message buffer");
     688      334877 :                 return;
     689             :         }
     690      334861 :         va_start(ap, fmt);
     691      334861 :         len = vsnprintf(buf, buflen, fmt, ap);
     692      334861 :         va_end(ap);
     693      334861 :         wpa_printf(level, "%s", buf);
     694      334861 :         if (wpa_msg_cb)
     695      334861 :                 wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
     696      334861 :         bin_clear_free(buf, buflen);
     697             : }
     698             : 
     699             : 
     700         667 : void wpa_msg_global_ctrl(void *ctx, int level, const char *fmt, ...)
     701             : {
     702             :         va_list ap;
     703             :         char *buf;
     704             :         int buflen;
     705             :         int len;
     706             : 
     707         667 :         if (!wpa_msg_cb)
     708           0 :                 return;
     709             : 
     710         667 :         va_start(ap, fmt);
     711         667 :         buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
     712         667 :         va_end(ap);
     713             : 
     714         667 :         buf = os_malloc(buflen);
     715         667 :         if (buf == NULL) {
     716           0 :                 wpa_printf(MSG_ERROR,
     717             :                            "wpa_msg_global_ctrl: Failed to allocate message buffer");
     718           0 :                 return;
     719             :         }
     720         667 :         va_start(ap, fmt);
     721         667 :         len = vsnprintf(buf, buflen, fmt, ap);
     722         667 :         va_end(ap);
     723         667 :         wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
     724         667 :         bin_clear_free(buf, buflen);
     725             : }
     726             : 
     727             : 
     728         110 : void wpa_msg_no_global(void *ctx, int level, const char *fmt, ...)
     729             : {
     730             :         va_list ap;
     731             :         char *buf;
     732             :         int buflen;
     733             :         int len;
     734             : 
     735         110 :         va_start(ap, fmt);
     736         110 :         buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
     737         110 :         va_end(ap);
     738             : 
     739         110 :         buf = os_malloc(buflen);
     740         110 :         if (buf == NULL) {
     741           0 :                 wpa_printf(MSG_ERROR, "wpa_msg_no_global: Failed to allocate "
     742             :                            "message buffer");
     743         110 :                 return;
     744             :         }
     745         110 :         va_start(ap, fmt);
     746         110 :         len = vsnprintf(buf, buflen, fmt, ap);
     747         110 :         va_end(ap);
     748         110 :         wpa_printf(level, "%s", buf);
     749         110 :         if (wpa_msg_cb)
     750         110 :                 wpa_msg_cb(ctx, level, WPA_MSG_NO_GLOBAL, buf, len);
     751         110 :         bin_clear_free(buf, buflen);
     752             : }
     753             : 
     754             : 
     755        3969 : void wpa_msg_global_only(void *ctx, int level, const char *fmt, ...)
     756             : {
     757             :         va_list ap;
     758             :         char *buf;
     759             :         int buflen;
     760             :         int len;
     761             : 
     762        3969 :         va_start(ap, fmt);
     763        3969 :         buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
     764        3969 :         va_end(ap);
     765             : 
     766        3969 :         buf = os_malloc(buflen);
     767        3969 :         if (buf == NULL) {
     768           0 :                 wpa_printf(MSG_ERROR, "%s: Failed to allocate message buffer",
     769             :                            __func__);
     770        3969 :                 return;
     771             :         }
     772        3969 :         va_start(ap, fmt);
     773        3969 :         len = vsnprintf(buf, buflen, fmt, ap);
     774        3969 :         va_end(ap);
     775        3969 :         wpa_printf(level, "%s", buf);
     776        3969 :         if (wpa_msg_cb)
     777        3969 :                 wpa_msg_cb(ctx, level, WPA_MSG_ONLY_GLOBAL, buf, len);
     778        3969 :         os_free(buf);
     779             : }
     780             : 
     781             : #endif /* CONFIG_NO_WPA_MSG */
     782             : 
     783             : 
     784             : #ifndef CONFIG_NO_HOSTAPD_LOGGER
     785             : static hostapd_logger_cb_func hostapd_logger_cb = NULL;
     786             : 
     787          25 : void hostapd_logger_register_cb(hostapd_logger_cb_func func)
     788             : {
     789          25 :         hostapd_logger_cb = func;
     790          25 : }
     791             : 
     792             : 
     793      170518 : void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
     794             :                     const char *fmt, ...)
     795             : {
     796             :         va_list ap;
     797             :         char *buf;
     798             :         int buflen;
     799             :         int len;
     800             : 
     801      170518 :         va_start(ap, fmt);
     802      170518 :         buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
     803      170518 :         va_end(ap);
     804             : 
     805      170518 :         buf = os_malloc(buflen);
     806      170518 :         if (buf == NULL) {
     807          37 :                 wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
     808             :                            "message buffer");
     809      170555 :                 return;
     810             :         }
     811      170481 :         va_start(ap, fmt);
     812      170481 :         len = vsnprintf(buf, buflen, fmt, ap);
     813      170481 :         va_end(ap);
     814      170481 :         if (hostapd_logger_cb)
     815      156501 :                 hostapd_logger_cb(ctx, addr, module, level, buf, len);
     816       13980 :         else if (addr)
     817       83862 :                 wpa_printf(MSG_DEBUG, "hostapd_logger: STA " MACSTR " - %s",
     818       83862 :                            MAC2STR(addr), buf);
     819             :         else
     820           3 :                 wpa_printf(MSG_DEBUG, "hostapd_logger: %s", buf);
     821      170481 :         bin_clear_free(buf, buflen);
     822             : }
     823             : #endif /* CONFIG_NO_HOSTAPD_LOGGER */
     824             : 
     825             : 
     826          20 : const char * debug_level_str(int level)
     827             : {
     828          20 :         switch (level) {
     829             :         case MSG_EXCESSIVE:
     830           2 :                 return "EXCESSIVE";
     831             :         case MSG_MSGDUMP:
     832          10 :                 return "MSGDUMP";
     833             :         case MSG_DEBUG:
     834           2 :                 return "DEBUG";
     835             :         case MSG_INFO:
     836           2 :                 return "INFO";
     837             :         case MSG_WARNING:
     838           2 :                 return "WARNING";
     839             :         case MSG_ERROR:
     840           2 :                 return "ERROR";
     841             :         default:
     842           0 :                 return "?";
     843             :         }
     844             : }
     845             : 
     846             : 
     847          21 : int str_to_debug_level(const char *s)
     848             : {
     849          21 :         if (os_strcasecmp(s, "EXCESSIVE") == 0)
     850           2 :                 return MSG_EXCESSIVE;
     851          19 :         if (os_strcasecmp(s, "MSGDUMP") == 0)
     852           9 :                 return MSG_MSGDUMP;
     853          10 :         if (os_strcasecmp(s, "DEBUG") == 0)
     854           2 :                 return MSG_DEBUG;
     855           8 :         if (os_strcasecmp(s, "INFO") == 0)
     856           2 :                 return MSG_INFO;
     857           6 :         if (os_strcasecmp(s, "WARNING") == 0)
     858           2 :                 return MSG_WARNING;
     859           4 :         if (os_strcasecmp(s, "ERROR") == 0)
     860           2 :                 return MSG_ERROR;
     861           2 :         return -1;
     862             : }

Generated by: LCOV version 1.10