"Fossies" - the Fresh Open Source Software Archive

Member "smbnetfs-0.6.0/src/common.c" (11 Jan 2015, 2905 Bytes) of archive /linux/misc/smbnetfs-0.6.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "common.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.5.3b_vs_0.6.0.

    1 #include "config.h"
    2 #include <stdio.h>
    3 #include <stdlib.h>
    4 #include <stdarg.h>
    5 #include <string.h>
    6 #include <pthread.h>
    7 #include <execinfo.h>
    8 #include <errno.h>
    9 #include <glib.h>
   10 
   11 #include "common.h"
   12 
   13 int         common_debug_level  = 0;
   14 static FILE*        common_stdlog       = NULL;
   15 static char     common_logfile[256] = "";
   16 static pthread_mutex_t  m_common        = PTHREAD_MUTEX_INITIALIZER;
   17 
   18 int common_set_smbnetfs_debug_level(int level){
   19     if ((level < 0) || (level > 10)) return 0;
   20     DPRINTF(8, "level=%d\n", level);
   21     g_atomic_int_set(&common_debug_level, level);
   22     return 1;
   23 }
   24 
   25 int common_set_log_file(const char *logfile){
   26     DPRINTF(7, "logfile=%s\n", logfile);
   27 
   28     pthread_mutex_lock(&m_common);
   29     if ( ! ((logfile != NULL) && (strcmp(common_logfile, logfile) == 0))){
   30     if (common_stdlog != NULL){
   31         fclose(common_stdlog);
   32         memset(common_logfile, 0, sizeof(common_logfile));
   33         common_stdlog = NULL;
   34     }
   35 
   36     if (logfile != NULL)
   37         strncpy(common_logfile, logfile, sizeof(common_logfile) - 1);
   38 
   39     if (*common_logfile != '\0'){
   40         common_stdlog = fopen(common_logfile, "a");
   41         if (common_stdlog == NULL){
   42         memset(common_logfile, 0, sizeof(common_logfile));
   43         /* actually we get here if strcmp(common_logfile, logfile) != 0, *
   44          * so we can use variable logfile instead of common_logfile      */
   45         pthread_mutex_unlock(&m_common);
   46         DPRINTF(0, "Can't open logfile '%s', error : %s.\n", logfile, strerror(errno));
   47         return 0;
   48         }
   49     }
   50     }
   51     pthread_mutex_unlock(&m_common);
   52     return 1;
   53 }
   54 
   55 void common_debug_print(const char *fmt, ...){
   56     va_list ap;
   57 
   58     pthread_mutex_lock(&m_common);
   59     va_start(ap, fmt);
   60     vfprintf(stderr, fmt, ap);
   61     va_end(ap);
   62     fflush(stderr);
   63     if (common_stdlog != NULL){
   64     va_start(ap, fmt);
   65     vfprintf(common_stdlog, fmt, ap);
   66     va_end(ap);
   67     fflush(common_stdlog);
   68     }
   69     pthread_mutex_unlock(&m_common);
   70 }
   71 
   72 void common_print_backtrace(void){
   73     static char     buf[256];
   74     int         fd;
   75   #ifdef HAVE_BACKTRACE
   76     void        *array[200];
   77     size_t      size;
   78   #endif /* HAVE_BACKTRACE */
   79 
   80     snprintf(buf, sizeof(buf), "%d->%s: dumping ...\n", getpid(), __FUNCTION__);
   81     buf[sizeof(buf) - 2] = '\n';
   82     buf[sizeof(buf) - 1] = '\0';
   83 
   84   #ifdef HAVE_BACKTRACE
   85     size = backtrace(array, 200);
   86   #endif /* HAVE_BACKTRACE */
   87 
   88     fd = fileno(stderr);
   89     write(fd, buf, strlen(buf));
   90   #ifdef HAVE_BACKTRACE
   91     backtrace_symbols_fd(array, size, fd);
   92   #endif /* HAVE_BACKTRACE */
   93     fsync(fd);
   94 
   95     if (common_stdlog != NULL){
   96     fd = fileno(common_stdlog);
   97     write(fd, buf, strlen(buf));
   98       #ifdef HAVE_BACKTRACE
   99     backtrace_symbols_fd(array, size, fd);
  100       #endif /* HAVE_BACKTRACE */
  101     fsync(fd);
  102     }
  103 }
  104 
  105 #ifndef HAVE_STRNDUP
  106 char* strndup(const char *s, size_t n){
  107     char    *p;
  108 
  109     if (strlen(s) <= n) return strdup(s);
  110     if ((p = malloc(n + 1)) == NULL) return NULL;
  111     memcpy(p, s, n);
  112     p[n] = '\0';
  113     return p;
  114 }
  115 #endif /* HAVE_STRNDUP */