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 1426431149 Lines: 231 314 73.6 %
Date: 2015-03-15 Functions: 22 23 95.7 %

          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     3842674 : void wpa_debug_print_timestamp(void)
      66             : {
      67             : #ifndef CONFIG_ANDROID_LOG
      68             :         struct os_time tv;
      69             : 
      70     3842674 :         if (!wpa_debug_timestamp)
      71     3842702 :                 return;
      72             : 
      73     3842646 :         os_get_time(&tv);
      74             : #ifdef CONFIG_DEBUG_FILE
      75     3842646 :         if (out_file) {
      76     3610450 :                 fprintf(out_file, "%ld.%06u: ", (long) tv.sec,
      77     3610450 :                         (unsigned int) tv.usec);
      78             :         } else
      79             : #endif /* CONFIG_DEBUG_FILE */
      80      232196 :         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           0 : int wpa_debug_open_linux_tracing(void)
     125             : {
     126             :         int mounts, trace_fd;
     127           0 :         char buf[4096] = {};
     128             :         ssize_t buflen;
     129           0 :         char *line, *tmp1, *path = NULL;
     130             : 
     131           0 :         mounts = open("/proc/mounts", O_RDONLY);
     132           0 :         if (mounts < 0) {
     133           0 :                 printf("no /proc/mounts\n");
     134           0 :                 return -1;
     135             :         }
     136             : 
     137           0 :         buflen = read(mounts, buf, sizeof(buf) - 1);
     138           0 :         close(mounts);
     139           0 :         if (buflen < 0) {
     140           0 :                 printf("failed to read /proc/mounts\n");
     141           0 :                 return -1;
     142             :         }
     143             : 
     144           0 :         line = strtok_r(buf, "\n", &tmp1);
     145           0 :         while (line) {
     146             :                 char *tmp2, *tmp_path, *fstype;
     147             :                 /* "<dev> <mountpoint> <fs type> ..." */
     148           0 :                 strtok_r(line, " ", &tmp2);
     149           0 :                 tmp_path = strtok_r(NULL, " ", &tmp2);
     150           0 :                 fstype = strtok_r(NULL, " ", &tmp2);
     151           0 :                 if (strcmp(fstype, "debugfs") == 0) {
     152           0 :                         path = tmp_path;
     153           0 :                         break;
     154             :                 }
     155             : 
     156           0 :                 line = strtok_r(NULL, "\n", &tmp1);
     157             :         }
     158             : 
     159           0 :         if (path == NULL) {
     160           0 :                 printf("debugfs mountpoint not found\n");
     161           0 :                 return -1;
     162             :         }
     163             : 
     164           0 :         snprintf(buf, sizeof(buf) - 1, "%s/tracing/trace_marker", path);
     165             : 
     166           0 :         trace_fd = open(buf, O_WRONLY);
     167           0 :         if (trace_fd < 0) {
     168           0 :                 printf("failed to open trace_marker file\n");
     169           0 :                 return -1;
     170             :         }
     171           0 :         wpa_debug_tracing_file = fdopen(trace_fd, "w");
     172           0 :         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           0 :         return 0;
     179             : }
     180             : 
     181             : 
     182          40 : void wpa_debug_close_linux_tracing(void)
     183             : {
     184          40 :         if (wpa_debug_tracing_file == NULL)
     185          80 :                 return;
     186           0 :         fclose(wpa_debug_tracing_file);
     187           0 :         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     3509646 : void wpa_printf(int level, const char *fmt, ...)
     205             : {
     206             :         va_list ap;
     207             : 
     208     3509646 :         va_start(ap, fmt);
     209     3509646 :         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     3338620 :                 wpa_debug_print_timestamp();
     220             : #ifdef CONFIG_DEBUG_FILE
     221     3338620 :                 if (out_file) {
     222     3133779 :                         vfprintf(out_file, fmt, ap);
     223     3133779 :                         fprintf(out_file, "\n");
     224             :                 } else {
     225             : #endif /* CONFIG_DEBUG_FILE */
     226      204841 :                 vprintf(fmt, ap);
     227      204841 :                 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     3509646 :         va_end(ap);
     237             : 
     238             : #ifdef CONFIG_DEBUG_LINUX_TRACING
     239     3509646 :         if (wpa_debug_tracing_file != NULL) {
     240           0 :                 va_start(ap, fmt);
     241           0 :                 fprintf(wpa_debug_tracing_file, WPAS_TRACE_PFX, level);
     242           0 :                 vfprintf(wpa_debug_tracing_file, fmt, ap);
     243           0 :                 fprintf(wpa_debug_tracing_file, "\n");
     244           0 :                 fflush(wpa_debug_tracing_file);
     245           0 :                 va_end(ap);
     246             :         }
     247             : #endif /* CONFIG_DEBUG_LINUX_TRACING */
     248     3509646 : }
     249             : 
     250             : 
     251      362215 : 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      362215 :         if (wpa_debug_tracing_file != NULL) {
     258           0 :                 fprintf(wpa_debug_tracing_file,
     259             :                         WPAS_TRACE_PFX "%s - hexdump(len=%lu):",
     260             :                         level, title, (unsigned long) len);
     261           0 :                 if (buf == NULL) {
     262           0 :                         fprintf(wpa_debug_tracing_file, " [NULL]\n");
     263           0 :                 } else if (!show) {
     264           0 :                         fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
     265             :                 } else {
     266           0 :                         for (i = 0; i < len; i++)
     267           0 :                                 fprintf(wpa_debug_tracing_file,
     268           0 :                                         " %02x", buf[i]);
     269             :                 }
     270           0 :                 fflush(wpa_debug_tracing_file);
     271             :         }
     272             : #endif /* CONFIG_DEBUG_LINUX_TRACING */
     273             : 
     274      362215 :         if (level < wpa_debug_level)
     275      368227 :                 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             :                 os_free(strbuf);
     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             :                 os_free(strbuf);
     343             :                 return;
     344             :         }
     345             : #endif /* CONFIG_DEBUG_SYSLOG */
     346      356203 :         wpa_debug_print_timestamp();
     347             : #ifdef CONFIG_DEBUG_FILE
     348      356203 :         if (out_file) {
     349      331332 :                 fprintf(out_file, "%s - hexdump(len=%lu):",
     350             :                         title, (unsigned long) len);
     351      331332 :                 if (buf == NULL) {
     352       11443 :                         fprintf(out_file, " [NULL]");
     353      319889 :                 } else if (show) {
     354    16683117 :                         for (i = 0; i < len; i++)
     355    16363228 :                                 fprintf(out_file, " %02x", buf[i]);
     356             :                 } else {
     357           0 :                         fprintf(out_file, " [REMOVED]");
     358             :                 }
     359      331332 :                 fprintf(out_file, "\n");
     360             :         } else {
     361             : #endif /* CONFIG_DEBUG_FILE */
     362       24871 :         printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
     363       24871 :         if (buf == NULL) {
     364         169 :                 printf(" [NULL]");
     365       24702 :         } else if (show) {
     366     2557997 :                 for (i = 0; i < len; i++)
     367     2533295 :                         printf(" %02x", buf[i]);
     368             :         } else {
     369           0 :                 printf(" [REMOVED]");
     370             :         }
     371       24871 :         printf("\n");
     372             : #ifdef CONFIG_DEBUG_FILE
     373             :         }
     374             : #endif /* CONFIG_DEBUG_FILE */
     375             : #endif /* CONFIG_ANDROID_LOG */
     376             : }
     377             : 
     378      289012 : void wpa_hexdump(int level, const char *title, const void *buf, size_t len)
     379             : {
     380      289012 :         _wpa_hexdump(level, title, buf, len, 1);
     381      289012 : }
     382             : 
     383             : 
     384       73203 : void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len)
     385             : {
     386       73203 :         _wpa_hexdump(level, title, buf, len, wpa_debug_show_keys);
     387       73203 : }
     388             : 
     389             : 
     390       77043 : 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       77043 :         const u8 *pos = buf;
     395       77043 :         const size_t line_len = 16;
     396             : 
     397             : #ifdef CONFIG_DEBUG_LINUX_TRACING
     398       77043 :         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       77043 :         if (level < wpa_debug_level)
     417        3334 :                 return;
     418             : #ifdef CONFIG_ANDROID_LOG
     419             :         _wpa_hexdump(level, title, buf, len, show);
     420             : #else /* CONFIG_ANDROID_LOG */
     421       73709 :         wpa_debug_print_timestamp();
     422             : #ifdef CONFIG_DEBUG_FILE
     423       73709 :         if (out_file) {
     424       71205 :                 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       71205 :                 if (buf == NULL) {
     431        1745 :                         fprintf(out_file,
     432             :                                 "%s - hexdump_ascii(len=%lu): [NULL]\n",
     433             :                                 title, (unsigned long) len);
     434        1745 :                         return;
     435             :                 }
     436       69460 :                 fprintf(out_file, "%s - hexdump_ascii(len=%lu):\n",
     437             :                         title, (unsigned long) len);
     438      242922 :                 while (len) {
     439      104002 :                         llen = len > line_len ? line_len : len;
     440      104002 :                         fprintf(out_file, "    ");
     441     1209996 :                         for (i = 0; i < llen; i++)
     442     1105994 :                                 fprintf(out_file, " %02x", pos[i]);
     443      662040 :                         for (i = llen; i < line_len; i++)
     444      558038 :                                 fprintf(out_file, "   ");
     445      104002 :                         fprintf(out_file, "   ");
     446     1209996 :                         for (i = 0; i < llen; i++) {
     447     1105994 :                                 if (isprint(pos[i]))
     448     1087695 :                                         fprintf(out_file, "%c", pos[i]);
     449             :                                 else
     450       18299 :                                         fprintf(out_file, "_");
     451             :                         }
     452      662040 :                         for (i = llen; i < line_len; i++)
     453      558038 :                                 fprintf(out_file, " ");
     454      104002 :                         fprintf(out_file, "\n");
     455      104002 :                         pos += llen;
     456      104002 :                         len -= llen;
     457             :                 }
     458             :         } else {
     459             : #endif /* CONFIG_DEBUG_FILE */
     460        2504 :         if (!show) {
     461           0 :                 printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
     462             :                        title, (unsigned long) len);
     463           0 :                 return;
     464             :         }
     465        2504 :         if (buf == NULL) {
     466           0 :                 printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
     467             :                        title, (unsigned long) len);
     468           0 :                 return;
     469             :         }
     470        2504 :         printf("%s - hexdump_ascii(len=%lu):\n", title, (unsigned long) len);
     471        9122 :         while (len) {
     472        4114 :                 llen = len > line_len ? line_len : len;
     473        4114 :                 printf("    ");
     474       49615 :                 for (i = 0; i < llen; i++)
     475       45501 :                         printf(" %02x", pos[i]);
     476       24437 :                 for (i = llen; i < line_len; i++)
     477       20323 :                         printf("   ");
     478        4114 :                 printf("   ");
     479       49615 :                 for (i = 0; i < llen; i++) {
     480       45501 :                         if (isprint(pos[i]))
     481       45492 :                                 printf("%c", pos[i]);
     482             :                         else
     483           9 :                                 printf("_");
     484             :                 }
     485       24437 :                 for (i = llen; i < line_len; i++)
     486       20323 :                         printf(" ");
     487        4114 :                 printf("\n");
     488        4114 :                 pos += llen;
     489        4114 :                 len -= llen;
     490             :         }
     491             : #ifdef CONFIG_DEBUG_FILE
     492             :         }
     493             : #endif /* CONFIG_DEBUG_FILE */
     494             : #endif /* CONFIG_ANDROID_LOG */
     495             : }
     496             : 
     497             : 
     498       65673 : void wpa_hexdump_ascii(int level, const char *title, const void *buf,
     499             :                        size_t len)
     500             : {
     501       65673 :         _wpa_hexdump_ascii(level, title, buf, len, 1);
     502       65673 : }
     503             : 
     504             : 
     505       11370 : void wpa_hexdump_ascii_key(int level, const char *title, const void *buf,
     506             :                            size_t len)
     507             : {
     508       11370 :         _wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
     509       11370 : }
     510             : 
     511             : 
     512             : #ifdef CONFIG_DEBUG_FILE
     513             : static char *last_path = NULL;
     514             : #endif /* CONFIG_DEBUG_FILE */
     515             : 
     516        5527 : int wpa_debug_reopen_file(void)
     517             : {
     518             : #ifdef CONFIG_DEBUG_FILE
     519             :         int rv;
     520        5527 :         if (last_path) {
     521        5527 :                 char *tmp = os_strdup(last_path);
     522        5527 :                 wpa_debug_close_file();
     523        5527 :                 rv = wpa_debug_open_file(tmp);
     524        5527 :                 os_free(tmp);
     525             :         } else {
     526           0 :                 wpa_printf(MSG_ERROR, "Last-path was not set, cannot "
     527             :                            "re-open log file.");
     528           0 :                 rv = -1;
     529             :         }
     530        5527 :         return rv;
     531             : #else /* CONFIG_DEBUG_FILE */
     532             :         return 0;
     533             : #endif /* CONFIG_DEBUG_FILE */
     534             : }
     535             : 
     536             : 
     537        5552 : int wpa_debug_open_file(const char *path)
     538             : {
     539             : #ifdef CONFIG_DEBUG_FILE
     540        5552 :         if (!path)
     541           0 :                 return 0;
     542             : 
     543        5552 :         if (last_path == NULL || os_strcmp(last_path, path) != 0) {
     544             :                 /* Save our path to enable re-open */
     545        5552 :                 os_free(last_path);
     546        5552 :                 last_path = os_strdup(path);
     547             :         }
     548             : 
     549        5552 :         out_file = fopen(path, "a");
     550        5552 :         if (out_file == NULL) {
     551           0 :                 wpa_printf(MSG_ERROR, "wpa_debug_open_file: Failed to open "
     552             :                            "output file, using standard output");
     553           0 :                 return -1;
     554             :         }
     555             : #ifndef _WIN32
     556        5552 :         setvbuf(out_file, NULL, _IOLBF, 0);
     557             : #endif /* _WIN32 */
     558             : #else /* CONFIG_DEBUG_FILE */
     559             :         (void)path;
     560             : #endif /* CONFIG_DEBUG_FILE */
     561        5552 :         return 0;
     562             : }
     563             : 
     564             : 
     565        5562 : void wpa_debug_close_file(void)
     566             : {
     567             : #ifdef CONFIG_DEBUG_FILE
     568        5562 :         if (!out_file)
     569        5572 :                 return;
     570        5552 :         fclose(out_file);
     571        5552 :         out_file = NULL;
     572        5552 :         os_free(last_path);
     573        5552 :         last_path = NULL;
     574             : #endif /* CONFIG_DEBUG_FILE */
     575             : }
     576             : 
     577             : 
     578          15 : void wpa_debug_setup_stdout(void)
     579             : {
     580             : #ifndef _WIN32
     581          15 :         setvbuf(stdout, NULL, _IOLBF, 0);
     582             : #endif /* _WIN32 */
     583          15 : }
     584             : 
     585             : #endif /* CONFIG_NO_STDOUT_DEBUG */
     586             : 
     587             : 
     588             : #ifndef CONFIG_NO_WPA_MSG
     589             : static wpa_msg_cb_func wpa_msg_cb = NULL;
     590             : 
     591        1382 : void wpa_msg_register_cb(wpa_msg_cb_func func)
     592             : {
     593        1382 :         wpa_msg_cb = func;
     594        1382 : }
     595             : 
     596             : 
     597             : static wpa_msg_get_ifname_func wpa_msg_ifname_cb = NULL;
     598             : 
     599          40 : void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)
     600             : {
     601          40 :         wpa_msg_ifname_cb = func;
     602          40 : }
     603             : 
     604             : 
     605      453405 : void wpa_msg(void *ctx, int level, const char *fmt, ...)
     606             : {
     607             :         va_list ap;
     608             :         char *buf;
     609             :         int buflen;
     610             :         int len;
     611             :         char prefix[130];
     612             : 
     613      453405 :         va_start(ap, fmt);
     614      453405 :         buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
     615      453405 :         va_end(ap);
     616             : 
     617      453405 :         buf = os_malloc(buflen);
     618      453405 :         if (buf == NULL) {
     619          70 :                 wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
     620             :                            "buffer");
     621      453475 :                 return;
     622             :         }
     623      453335 :         va_start(ap, fmt);
     624      453335 :         prefix[0] = '\0';
     625      453335 :         if (wpa_msg_ifname_cb) {
     626      453335 :                 const char *ifname = wpa_msg_ifname_cb(ctx);
     627      453335 :                 if (ifname) {
     628      453099 :                         int res = os_snprintf(prefix, sizeof(prefix), "%s: ",
     629             :                                               ifname);
     630      453099 :                         if (os_snprintf_error(sizeof(prefix), res))
     631           0 :                                 prefix[0] = '\0';
     632             :                 }
     633             :         }
     634      453335 :         len = vsnprintf(buf, buflen, fmt, ap);
     635      453335 :         va_end(ap);
     636      453335 :         wpa_printf(level, "%s%s", prefix, buf);
     637      453335 :         if (wpa_msg_cb)
     638      453335 :                 wpa_msg_cb(ctx, level, 0, buf, len);
     639      453335 :         os_free(buf);
     640             : }
     641             : 
     642             : 
     643       14566 : void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...)
     644             : {
     645             :         va_list ap;
     646             :         char *buf;
     647             :         int buflen;
     648             :         int len;
     649             : 
     650       14566 :         if (!wpa_msg_cb)
     651           0 :                 return;
     652             : 
     653       14566 :         va_start(ap, fmt);
     654       14566 :         buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
     655       14566 :         va_end(ap);
     656             : 
     657       14566 :         buf = os_malloc(buflen);
     658       14566 :         if (buf == NULL) {
     659           0 :                 wpa_printf(MSG_ERROR, "wpa_msg_ctrl: Failed to allocate "
     660             :                            "message buffer");
     661           0 :                 return;
     662             :         }
     663       14566 :         va_start(ap, fmt);
     664       14566 :         len = vsnprintf(buf, buflen, fmt, ap);
     665       14566 :         va_end(ap);
     666       14566 :         wpa_msg_cb(ctx, level, 0, buf, len);
     667       14566 :         os_free(buf);
     668             : }
     669             : 
     670             : 
     671       95761 : void wpa_msg_global(void *ctx, int level, const char *fmt, ...)
     672             : {
     673             :         va_list ap;
     674             :         char *buf;
     675             :         int buflen;
     676             :         int len;
     677             : 
     678       95761 :         va_start(ap, fmt);
     679       95761 :         buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
     680       95761 :         va_end(ap);
     681             : 
     682       95761 :         buf = os_malloc(buflen);
     683       95761 :         if (buf == NULL) {
     684           0 :                 wpa_printf(MSG_ERROR, "wpa_msg_global: Failed to allocate "
     685             :                            "message buffer");
     686       95761 :                 return;
     687             :         }
     688       95761 :         va_start(ap, fmt);
     689       95761 :         len = vsnprintf(buf, buflen, fmt, ap);
     690       95761 :         va_end(ap);
     691       95761 :         wpa_printf(level, "%s", buf);
     692       95761 :         if (wpa_msg_cb)
     693       95761 :                 wpa_msg_cb(ctx, level, 1, buf, len);
     694       95761 :         os_free(buf);
     695             : }
     696             : 
     697             : 
     698         483 : void wpa_msg_global_ctrl(void *ctx, int level, const char *fmt, ...)
     699             : {
     700             :         va_list ap;
     701             :         char *buf;
     702             :         int buflen;
     703             :         int len;
     704             : 
     705         483 :         if (!wpa_msg_cb)
     706           0 :                 return;
     707             : 
     708         483 :         va_start(ap, fmt);
     709         483 :         buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
     710         483 :         va_end(ap);
     711             : 
     712         483 :         buf = os_malloc(buflen);
     713         483 :         if (buf == NULL) {
     714           0 :                 wpa_printf(MSG_ERROR,
     715             :                            "wpa_msg_global_ctrl: Failed to allocate message buffer");
     716           0 :                 return;
     717             :         }
     718         483 :         va_start(ap, fmt);
     719         483 :         len = vsnprintf(buf, buflen, fmt, ap);
     720         483 :         va_end(ap);
     721         483 :         wpa_msg_cb(ctx, level, 1, buf, len);
     722         483 :         os_free(buf);
     723             : }
     724             : 
     725             : 
     726          42 : void wpa_msg_no_global(void *ctx, int level, const char *fmt, ...)
     727             : {
     728             :         va_list ap;
     729             :         char *buf;
     730             :         int buflen;
     731             :         int len;
     732             : 
     733          42 :         va_start(ap, fmt);
     734          42 :         buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
     735          42 :         va_end(ap);
     736             : 
     737          42 :         buf = os_malloc(buflen);
     738          42 :         if (buf == NULL) {
     739           0 :                 wpa_printf(MSG_ERROR, "wpa_msg_no_global: Failed to allocate "
     740             :                            "message buffer");
     741          42 :                 return;
     742             :         }
     743          42 :         va_start(ap, fmt);
     744          42 :         len = vsnprintf(buf, buflen, fmt, ap);
     745          42 :         va_end(ap);
     746          42 :         wpa_printf(level, "%s", buf);
     747          42 :         if (wpa_msg_cb)
     748          42 :                 wpa_msg_cb(ctx, level, 2, buf, len);
     749          42 :         os_free(buf);
     750             : }
     751             : 
     752             : #endif /* CONFIG_NO_WPA_MSG */
     753             : 
     754             : 
     755             : #ifndef CONFIG_NO_HOSTAPD_LOGGER
     756             : static hostapd_logger_cb_func hostapd_logger_cb = NULL;
     757             : 
     758          10 : void hostapd_logger_register_cb(hostapd_logger_cb_func func)
     759             : {
     760          10 :         hostapd_logger_cb = func;
     761          10 : }
     762             : 
     763             : 
     764       84895 : void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
     765             :                     const char *fmt, ...)
     766             : {
     767             :         va_list ap;
     768             :         char *buf;
     769             :         int buflen;
     770             :         int len;
     771             : 
     772       84895 :         va_start(ap, fmt);
     773       84895 :         buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
     774       84895 :         va_end(ap);
     775             : 
     776       84895 :         buf = os_malloc(buflen);
     777       84895 :         if (buf == NULL) {
     778          42 :                 wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
     779             :                            "message buffer");
     780       84937 :                 return;
     781             :         }
     782       84853 :         va_start(ap, fmt);
     783       84853 :         len = vsnprintf(buf, buflen, fmt, ap);
     784       84853 :         va_end(ap);
     785       84853 :         if (hostapd_logger_cb)
     786       74890 :                 hostapd_logger_cb(ctx, addr, module, level, buf, len);
     787        9963 :         else if (addr)
     788       59772 :                 wpa_printf(MSG_DEBUG, "hostapd_logger: STA " MACSTR " - %s",
     789       59772 :                            MAC2STR(addr), buf);
     790             :         else
     791           1 :                 wpa_printf(MSG_DEBUG, "hostapd_logger: %s", buf);
     792       84853 :         os_free(buf);
     793             : }
     794             : #endif /* CONFIG_NO_HOSTAPD_LOGGER */

Generated by: LCOV version 1.10