smbnetfs  0.6.3
About: SMBNetFS is a Linux/FreeBSD filesystem that allow you to use samba/microsoft network in the same manner as the network neighborhood in Microsoft Windows.
  Fossies Dox: smbnetfs-0.6.3.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

common.c
Go to the documentation of this file.
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 <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 
13 #include "common.h"
14 
16 static int common_logfd[2] = {2, -1};
17 static char common_logfile[256] = "";
18 static pthread_mutex_t m_common = PTHREAD_MUTEX_INITIALIZER;
19 
20 
21 void common_init(void){
22  common_logfd[0] = fileno(stderr);
23  common_logfd[1] = -1;
24 }
25 
27  if ((level < 0) || (level > 10)) return 0;
28  DPRINTF(8, "level=%d\n", level);
29  g_atomic_int_set(&common_debug_level, level);
30  return 1;
31 }
32 
33 int common_set_log_file(const char *logfile){
34  DPRINTF(7, "logfile=%s\n", logfile);
35 
36  pthread_mutex_lock(&m_common);
37  if ( ! ((logfile != NULL) && (strcmp(common_logfile, logfile) == 0))){
38  if (common_logfd[1] != -1){
39  close(common_logfd[1]);
40  memset(common_logfile, 0, sizeof(common_logfile));
41  common_logfd[1] = -1;
42  }
43 
44  if (logfile != NULL)
45  strncpy(common_logfile, logfile, sizeof(common_logfile) - 1);
46 
47  if (*common_logfile != '\0'){
48  common_logfd[1] = open(common_logfile, O_WRONLY | O_APPEND | O_CREAT, 0644);
49  if (common_logfd[1] == -1){
50  memset(common_logfile, 0, sizeof(common_logfile));
51  /* actually we get here if strcmp(common_logfile, logfile) != 0, *
52  * so we can use variable logfile instead of common_logfile */
53  pthread_mutex_unlock(&m_common);
54  DPRINTF(0, "Can't open logfile '%s', error : %s.\n", logfile, strerror(errno));
55  return 0;
56  }
57  }
58  }
59  pthread_mutex_unlock(&m_common);
60  return 1;
61 }
62 
63 void common_debug_print(const char *fmt, ...){
64  int i;
65  va_list ap;
66 
67  pthread_mutex_lock(&m_common);
68  for(i = 0; i < 2; i++){
69  if (common_logfd[i] == -1) continue;
70  va_start(ap, fmt);
71  vdprintf(common_logfd[i], fmt, ap);
72  va_end(ap);
73  }
74  pthread_mutex_unlock(&m_common);
75 }
76 
78  int i;
79  #ifdef HAVE_BACKTRACE
80  void *array[200];
81  size_t size;
82 
83  size = backtrace(array, sizeof(array) / sizeof(array[0]));
84  #endif /* HAVE_BACKTRACE */
85  for(i = 0; i < 2; i++){
86  if (common_logfd[i] == -1) continue;
87  dprintf(common_logfd[i], "%d->%s: dumping ...\n", getpid(), __FUNCTION__);
88  #ifdef HAVE_BACKTRACE
89  backtrace_symbols_fd(array, size, common_logfd[i]);
90  #endif /* HAVE_BACKTRACE */
91  }
92 }
void common_init(void)
Definition: common.c:21
void common_debug_print(const char *fmt,...)
Definition: common.c:63
int common_set_log_file(const char *logfile)
Definition: common.c:33
static char common_logfile[256]
Definition: common.c:17
void common_print_backtrace(void)
Definition: common.c:77
static pthread_mutex_t m_common
Definition: common.c:18
int common_debug_level
Definition: common.c:15
int common_set_smbnetfs_debug_level(int level)
Definition: common.c:26
static int common_logfd[2]
Definition: common.c:16
#define DPRINTF(level, fmt, args...)
Definition: common.h:47