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)  

main.c
Go to the documentation of this file.
1 #include "config.h"
2 #include <errno.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <locale.h>
8 #include <signal.h>
9 #include <sys/mman.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <sys/wait.h>
13 #include <pthread.h>
14 #include <libsmbclient.h>
15 #include <pwd.h>
16 
17 #include "common.h"
18 #include "smbitem.h"
19 #include "auth.h"
20 #include "auth-libsecret.h"
21 #include "process.h"
22 #include "samba.h"
23 #include "function.h"
24 #include "event.h"
25 #include "reconfigure.h"
26 
27 
28 static void check_samba_version(void){
29  const char *samba_version;
30  int major, minor;
31 
32  samba_version = smbc_version();
33  if (sscanf(samba_version, "%d.%d.%*d", &major, &minor) != 2){
34  fprintf(stderr, "ERROR: Can't parse libsmbclient version: %s\n",
35  samba_version);
36  exit(EXIT_FAILURE);
37  }
38 
39  if (major >= 5) goto to_new;
40  if (major == 4) goto ok;
41  if (major == 3){
42  if (minor >= 2) goto ok;
43  else goto unsupported;
44  }
45 
46  unsupported:
47  fprintf(stderr, "ERROR: Unsupported libsmbclient version: %s\n"
48  " Please consider upgrade to libsmbclient >= 3.2\n"
49  "\n",
50  samba_version);
51  exit(EXIT_FAILURE);
52 
53  ok:
54  /* libsmbclient >= 3.2 is perfectly OK */
55  return;
56 
57  to_new:
58  fprintf(stderr, "WARNING: Unknown libsmbclient version: %s\n"
59  " " PACKAGE_NAME " may not work as expected.\n"
60  "\n",
61  samba_version);
62  /* Hm... libsmbclient version is too new, trying to continue anyway. */
63  return;
64 }
65 
66 static inline size_t get_default_rw_block_size(void){
67  return (strncmp(smbc_version(), "3.0.", 4) == 0) ? 48 : 128;
68 }
69 
70 static void sig_handler(int signum){
71  fprintf(stderr, "%d->%s: signal %d received\n",
72  getpid(), __FUNCTION__, signum);
74  exit(signum);
75 }
76 
77 static void set_signal_reactions(void){
78  struct{
79  int signum;
80  char *name;
81  } sig[] = { {SIGILL, "SIGILL" },
82  {SIGSEGV, "SIGSEGV"},
83  {SIGABRT, "SIGABRT"} };
84  int i;
85  struct sigaction action;
86 
87  sigemptyset(&action.sa_mask);
88  action.sa_handler = sig_handler;
89  action.sa_flags = SA_RESTART;
90 
91  for(i = 0; i < 3; i++){
92  if (sigaction(sig[i].signum, &action, NULL) < 0){
93  fprintf(stderr, "Can't set %s handler\n", sig[i].name);
94  exit(EXIT_FAILURE);
95  }
96  }
97 
98  sigemptyset(&action.sa_mask);
99  sigaddset(&action.sa_mask, SIGHUP);
100  sigaddset(&action.sa_mask, SIGCHLD);
101  if (pthread_sigmask(SIG_BLOCK, &action.sa_mask, NULL) != 0){
102  fprintf(stderr, "Can't block SIGHUP and SIGCHLD signals.\n");
103  exit(EXIT_FAILURE);
104  }
105 }
106 
107 static void print_help(struct fuse_args *outargs){
108  fprintf(stderr,
109  "usage: %s mountpoint [options]\n"
110  "\n"
111  "general options:\n"
112  " -o opt,[opt...] mount options\n"
113  " -h --help print help\n"
114  " -V --version print version\n"
115  "\n"
116  "SMBNetFS options:\n"
117  "%s"
118  "\n", outargs->argv[0], smbnetfs_option_list);
119  fuse_opt_add_arg(outargs, "-ho");
120  fuse_main(outargs->argc, outargs->argv, &smb_oper, NULL);
121 }
122 
123 static int smbnetfs_opt_proc(void *data, const char *arg, int key, struct fuse_args *outargs){
124  const char *value;
125  char *name;
126  int result;
127 
128  (void) data;
129  (void) key;
130 
131  if ((strcmp(arg, "--version") == 0) || (strcmp(arg, "-V") == 0)){
132  fprintf(stderr, "SMBNetFS version " PACKAGE_VERSION "\n");
133  fprintf(stderr, "libsmbclient version %s\n", smbc_version());
134  fuse_opt_add_arg(outargs, "--version");
135  fuse_main(outargs->argc, outargs->argv, &smb_oper, NULL);
136  exit(EXIT_SUCCESS);
137  }
138  if ((strcmp(arg, "--help") == 0) || (strcmp(arg, "-h") == 0)){
139  print_help(outargs);
140  exit(EXIT_FAILURE);
141  }
142 
143  if ((value = strchr(arg, '=')) == NULL) return 1;
144  if (value++ == arg) return 1;
145  if (strlen(value) == 0) return 1;
146  if ((name = strndup(arg, value - arg - 1)) == NULL) return 1;
147 
148  /* check for specific SMBNetFS options */
149  result = reconfigure_analyse_cmdline_option(name, (char*) value);
150 
151  free(name);
152  return result ? 0 : 1;
153 }
154 
155 int main(int argc, char *argv[]){
156  struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
157 
158  setlocale(LC_ALL, "");
161 
162  /* init all subsystems with their default values */
163  common_init();
165 #ifdef HAVE_LIBSECRET
166  libsecret_init();
167 #endif /* HAVE_LIBSECRET */
168  smbitem_init();
169  process_init();
172 
173  /* parse command line options */
174  if (fuse_opt_parse(&args, NULL, NULL, smbnetfs_opt_proc) == -1){
175  fprintf(stderr, "Can't parse command line, please verify it.\n");
176  exit(EXIT_FAILURE);
177  }
178 
181 
182  fuse_main(args.argc, args.argv, &smb_oper, NULL);
185  smbitem_delete_obsolete(time(NULL) + 10, SMBITEM_USER_TREE);
186  auth_delete_obsolete(time(NULL) + 10);
187  smbitem_done();
189 #ifdef HAVE_LIBSECRET
190  libsecret_done();
191 #endif /* HAVE_LIBSECRET */
192  return 0;
193 }
void auth_delete_obsolete(time_t threshold)
Definition: auth.c:355
void common_init(void)
Definition: common.c:21
void common_print_backtrace(void)
Definition: common.c:77
void event_set_event_handler(struct fuse_operations *file_oper)
Definition: event.c:309
struct fuse_operations smb_oper
Definition: function.c:591
int main(int argc, char *argv[])
Definition: main.c:155
static int smbnetfs_opt_proc(void *data, const char *arg, int key, struct fuse_args *outargs)
Definition: main.c:123
static size_t get_default_rw_block_size(void)
Definition: main.c:66
static void set_signal_reactions(void)
Definition: main.c:77
static void sig_handler(int signum)
Definition: main.c:70
static void print_help(struct fuse_args *outargs)
Definition: main.c:107
static void check_samba_version(void)
Definition: main.c:28
int process_init(void)
Definition: process.c:38
void process_cleanup_from_zombies(void)
Definition: process.c:276
void reconfigure_set_default_login_and_configdir(void)
Definition: reconfigure.c:95
const char * smbnetfs_option_list
Definition: reconfigure.c:42
int reconfigure_analyse_cmdline_option(const char *option, char *value)
Definition: reconfigure.c:372
int reconfigure_read_config(int flags)
Definition: reconfigure.c:664
#define CONFIG_OPT_STARTUP
Definition: reconfigure.h:6
void samba_allocate_ctxs(void)
Definition: samba.c:154
void samba_destroy_unused_ctxs(void)
Definition: samba.c:164
int samba_init(size_t max_rw_block_size)
Definition: samba.c:30
void smbitem_done(void)
Definition: smbitem.c:204
void smbitem_delete_obsolete(time_t threshold, enum smbitem_tree_t tree)
Definition: smbitem.c:523
int smbitem_init(void)
Definition: smbitem.c:188
@ SMBITEM_USER_TREE
Definition: smbitem.h:13
@ SMBITEM_SAMBA_TREE
Definition: smbitem.h:14