File: event.c

package info (click to toggle)
smbnetfs 0.5.3a-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,256 kB
  • sloc: ansic: 6,743; sh: 1,044; makefile: 30
file content (325 lines) | stat: -rw-r--r-- 8,595 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#include <libsmbclient.h>
#include <fuse/fuse.h>

#include "common.h"
#include "smbitem.h"
#include "auth.h"
#include "process.h"
#include "samba.h"
#include "stat_workaround.h"
#include "reconfigure.h"

int		event_query_browser_flag	= 1;
int		event_time_step			= 10;
int		event_smb_tree_scan_period	= 300;
int		event_smb_tree_elements_ttl	= 900;
int		event_config_update_period	= 300;

time_t		event_last_smb_tree_scan	= (time_t) 0;
time_t		event_last_config_update	= (time_t) 0;

pthread_mutex_t	m_evthread			= PTHREAD_MUTEX_INITIALIZER;

pthread_t	event_ev_thread_id;
pthread_t	event_smb_thread_id;

int event_set_query_browser_flag(int flag){
    DPRINTF(7, "flag=%d\n", flag);
    pthread_mutex_lock(&m_evthread);
    event_query_browser_flag = flag;
    pthread_mutex_unlock(&m_evthread);
    return 1;
}

int event_get_query_browser_flag(void){
    int flag;

    pthread_mutex_lock(&m_evthread);
    flag = event_query_browser_flag;
    pthread_mutex_unlock(&m_evthread);
    DPRINTF(7, "flag=%d\n", flag);
    return flag;
}

int event_set_time_step(int step){
    if (step < 1) return 0;
    DPRINTF(7, "step=%d\n", step);
    pthread_mutex_lock(&m_evthread);
    event_time_step = step;
    pthread_mutex_unlock(&m_evthread);
    return 1;
}

int event_get_time_step(void){
    int step;

    pthread_mutex_lock(&m_evthread);
    step = event_time_step;
    pthread_mutex_unlock(&m_evthread);
    DPRINTF(7, "step=%d\n", step);
    return step;
}

int event_set_smb_tree_elements_ttl(int ttl){
    DPRINTF(7, "ttl=%d\n", ttl);
    pthread_mutex_lock(&m_evthread);
    if (ttl < event_smb_tree_scan_period) ttl = -1;
    else event_smb_tree_elements_ttl = ttl;
    pthread_mutex_unlock(&m_evthread);
    return (ttl > 0) ? 1 : 0;
}

int event_get_smb_tree_elements_ttl(void){
    int ttl;

    pthread_mutex_lock(&m_evthread);
    ttl = event_smb_tree_elements_ttl;
    pthread_mutex_unlock(&m_evthread);
    DPRINTF(7, "ttl=%d\n", ttl);
    return ttl;
}

int event_set_smb_tree_scan_period(int period){
    DPRINTF(7, "period=%d\n", period);
    pthread_mutex_lock(&m_evthread);
    if (period < event_time_step) period = -1;
    else event_smb_tree_scan_period = period;
    pthread_mutex_unlock(&m_evthread);
    return (period > 0) ? 1 : 0;
}

void event_set_last_smb_tree_scan(time_t scan_time){
    pthread_mutex_lock(&m_evthread);
    event_last_smb_tree_scan = scan_time;
    pthread_mutex_unlock(&m_evthread);
}

int event_is_time_for_smb_tree_scan(void){
    int flag;

    pthread_mutex_lock(&m_evthread);
    flag = (time(NULL) >= event_last_smb_tree_scan +
			  event_smb_tree_scan_period) ? 1 : 0;
    pthread_mutex_unlock(&m_evthread);
    return flag;
}

int event_set_config_update_period(int period){
    DPRINTF(7, "period=%d\n", period);
    pthread_mutex_lock(&m_evthread);
    if (period < event_time_step) period = -1;
    else event_config_update_period = period;
    pthread_mutex_unlock(&m_evthread);
    return (period > 0) ? 1 : 0;
}

void event_set_last_config_update(time_t update_time){
    pthread_mutex_lock(&m_evthread);
    event_last_config_update = update_time;
    pthread_mutex_unlock(&m_evthread);
}

int event_is_time_for_config_update(void){
    int flag;

    pthread_mutex_lock(&m_evthread);
    flag = (time(NULL) >= event_last_config_update +
			  event_config_update_period) ? 1 : 0;
    pthread_mutex_unlock(&m_evthread);
    return flag;
}

void event_scan_samba_group(const char *group){
    char		buf[4096], name[256], link[256];
    int			count;
    samba_fd		fd;

    DPRINTF(5, "group=%s\n", group);
    snprintf(name, sizeof(name), "/%s", group);
    fd = samba_opendir(name);
    while(1){
	struct smb_conn_dirent_rec	*rec;

	count = samba_readdir(fd, buf, sizeof(buf));
	if (count <= 0) break;

	rec = (struct smb_conn_dirent_rec *) buf;
	for( ; count >= (int) sizeof(struct smb_conn_dirent_rec);
			count -= sizeof(struct smb_conn_dirent_rec)){
	    switch(rec->smbc_type){
		case SMBC_SERVER:
		    smbitem_mkhost(rec->d_name, group, 1, SMBITEM_SAMBA_TREE);
		    snprintf(name, sizeof(name), "%s/%s", group, rec->d_name);
		    snprintf(link, sizeof(link), "../%s", rec->d_name);
		    smbitem_mklink(name, link, SMBITEM_SAMBA_TREE);
		    break;
		default:
		    DPRINTF(6, "ups..., smbc_type=%d, d_name=%s\n",
				rec->smbc_type, rec->d_name);
	    }
	    rec++;
	}
    }
    samba_closedir(fd);
}

void event_scan_smb_root(void){
    char		buf[4096];
    int			count;
    samba_fd		fd;

    DPRINTF(5, "reading group list\n");
    fd = samba_opendir("/");
    while(1){
	struct smb_conn_dirent_rec	*rec;

	count = samba_readdir(fd, buf, sizeof(buf));
	if (count <= 0) break;

	rec = (struct smb_conn_dirent_rec *) buf;
	for( ; count >= (int) sizeof(struct smb_conn_dirent_rec);
			count -= sizeof(struct smb_conn_dirent_rec)){
	    switch(rec->smbc_type){
		case SMBC_WORKGROUP:
		    smbitem_mkgroup(rec->d_name, SMBITEM_SAMBA_TREE);
		    break;
		default:
		    DPRINTF(6, "ups..., smbc_type=%d, d_name=%s\n",
				rec->smbc_type, rec->d_name);
	    }
	    rec++;
	}
    }
    samba_closedir(fd);
}

void event_scan_smb_tree(void){
    int			i;
    struct smbitem	*dir;

    if (event_get_query_browser_flag()) event_scan_smb_root();
    dir = smbitem_get_samba_groups();
    for(i = 0; i < dir->child_cnt; i++){
	if (dir->childs[i]->type != SMBITEM_GROUP) continue;
	event_scan_samba_group(dir->childs[i]->name);
    }
    smbitem_release_dir(dir);
}

void* event_update_smb_tree_thread(void *data){
    time_t		scan_time;
    time_t		die_threshold;
    int			time_step;

    (void)data;

    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);

    scan_time = time(NULL);
    event_scan_smb_tree();
    event_set_last_smb_tree_scan(scan_time);

    while(1){
	time_step = event_get_time_step();
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
	sleep(time_step);
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);

	if (event_is_time_for_smb_tree_scan()){
	    scan_time = time(NULL);
	    die_threshold = scan_time - event_get_smb_tree_elements_ttl();
	    DPRINTF(5, "start at timestamp=%u, die_threshold=%u\n",
			(unsigned) scan_time, (unsigned) die_threshold);

	    event_scan_smb_tree();
	    smbitem_delete_obsolete(die_threshold, SMBITEM_SAMBA_TREE);
	    event_set_last_smb_tree_scan(scan_time);
	}
    }
    return NULL;
}

void event_reread_config(void){
    time_t		reread_time;

    reread_time = time(NULL);
    DPRINTF(5, "start at timestamp=%u\n", (unsigned) reread_time);

    reconfigure_read_config(0);
    smbitem_delete_obsolete(reread_time, SMBITEM_USER_TREE);
    auth_delete_obsolete(reread_time);
    stat_workaround_delete_obsolete(reread_time);
    event_set_last_config_update(reread_time);
}

void* event_thread(void *data){
    siginfo_t		siginfo;
    sigset_t		signal_set;
    time_t		start_time;
    struct timespec	timeout;
    int 		sigret;

    (void)data;

    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);

    start_time = time(NULL);
    event_set_last_config_update(start_time);

    /* set signals to watch */
    sigemptyset(&signal_set);
    sigaddset(&signal_set, SIGHUP);
    sigaddset(&signal_set, SIGCHLD);

    while(1){
	timeout.tv_sec  = event_get_time_step();
	timeout.tv_nsec = 0;
	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
	sigret = sigtimedwait(&signal_set, &siginfo, &timeout);
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);

	process_cleanup_from_zombies();
	if ((sigret != -1 && siginfo.si_signo == SIGHUP)
	    || event_is_time_for_config_update())
		event_reread_config();
    }
    return NULL;
}

static void* event_init(struct fuse_conn_info *conn){
    (void) conn;

    if (pthread_create(&event_smb_thread_id, NULL,
			event_update_smb_tree_thread, NULL) != 0){
	fprintf(stderr, "Could not create smb_tree thread\n");
	exit(1);
    }
    if (pthread_create(&event_ev_thread_id, NULL,
			event_thread, NULL) != 0){
	fprintf(stderr, "Could not create event thread\n");
	exit(1);
    }
    return NULL;
}

static void event_destroy(void *private_data){
    (void)private_data;

    DPRINTF(1, "Destroy cfg and smb_tree threads\n");
    process_disable_new_smb_conn_starting();
    process_kill_all();
    pthread_cancel(event_ev_thread_id);
    pthread_cancel(event_smb_thread_id);
    pthread_join(event_ev_thread_id, NULL);
    pthread_join(event_smb_thread_id, NULL);
}

void event_set_event_handler(struct fuse_operations *file_oper){
    file_oper->init	= event_init;
    file_oper->destroy	= event_destroy;
}