"Fossies" - the Fresh Open Source Software Archive 
Member "smbnetfs-0.6.3/src/main.c" (2 Jan 2018, 5287 Bytes) of package /linux/misc/smbnetfs-0.6.3.tar.bz2:
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 "main.c" see the
Fossies "Dox" file reference documentation and the last
Fossies "Diffs" side-by-side code changes report:
0.6.0_vs_0.6.1.
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);
73 common_print_backtrace();
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, "");
159 check_samba_version();
160 set_signal_reactions();
161
162 /* init all subsystems with their default values */
163 common_init();
164 reconfigure_set_default_login_and_configdir();
165 #ifdef HAVE_LIBSECRET
166 libsecret_init();
167 #endif /* HAVE_LIBSECRET */
168 smbitem_init();
169 process_init();
170 samba_init(1024 * get_default_rw_block_size());
171 event_set_event_handler(&smb_oper);
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
179 reconfigure_read_config(CONFIG_OPT_STARTUP);
180 samba_allocate_ctxs();
181
182 fuse_main(args.argc, args.argv, &smb_oper, NULL);
183 samba_destroy_unused_ctxs();
184 smbitem_delete_obsolete(time(NULL) + 10, SMBITEM_SAMBA_TREE);
185 smbitem_delete_obsolete(time(NULL) + 10, SMBITEM_USER_TREE);
186 auth_delete_obsolete(time(NULL) + 10);
187 smbitem_done();
188 process_cleanup_from_zombies();
189 #ifdef HAVE_LIBSECRET
190 libsecret_done();
191 #endif /* HAVE_LIBSECRET */
192 return 0;
193 }