[r109]: branches / smbnetfs-0.3 / src / event_thread.c  Maximize  Restore  History

Download this file

359 lines (291 with data), 9.4 kB

  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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include <fuse/fuse.h>
#include "common.h"
#include "charset.h"
#include "auth.h"
#include "smbitem.h"
#include "filestruct.h"
#include "function.h"
#include "reconfigure.h"
extern const size_t convert_buffer_size;
int query_browser_flag = 1;
int fast_shutdown = 1;
int update_delta = 5;
int ctx_update_interval = 300;
int smbitem_update_interval = 300;
int smbitem_time_to_live = 900;
int config_update_interval = 300;
time_t last_smbitem_scan_time = (time_t) 0;
time_t last_config_update = (time_t) 0;
time_t last_event_time = (time_t) 0;
pthread_mutex_t m_evthread = PTHREAD_MUTEX_INITIALIZER;
pthread_t evthread;
int SetQueryBrowserFlag(int flag){
DPRINT(7, "flag=%d\n", flag);
pthread_mutex_lock(&m_evthread);
query_browser_flag = flag;
pthread_mutex_unlock(&m_evthread);
return 1;
}
int GetQueryBrowserFlag(void){
int flag;
pthread_mutex_lock(&m_evthread);
flag = query_browser_flag;
pthread_mutex_unlock(&m_evthread);
DPRINT(7, "flag=%d\n", flag);
return flag;
}
int SetFastShutdown(int flag){
DPRINT(7, "flag=%d\n", flag);
pthread_mutex_lock(&m_evthread);
fast_shutdown = flag;
pthread_mutex_unlock(&m_evthread);
return 1;
}
int GetFastShutdown(void){
int flag;
pthread_mutex_lock(&m_evthread);
flag = fast_shutdown;
pthread_mutex_unlock(&m_evthread);
DPRINT(7, "flag=%d\n", flag);
return flag;
}
int SetUpdateTimeDelta(int delta){
struct itimerval value;
if (delta < 1) return 0;
DPRINT(7, "delta=%d\n", delta);
pthread_mutex_lock(&m_evthread);
if (delta != update_delta){
update_delta = delta;
/* disable timer */
memset(&value, 0, sizeof(struct itimerval));
setitimer(ITIMER_REAL, &value, NULL);
/* calculate timer start and period */
value.it_value.tv_sec = (time(NULL) + delta - last_event_time);
if (value.it_value.tv_sec <= 0){
value.it_value.tv_sec = 0;
value.it_value.tv_usec = 1000;
}
value.it_interval.tv_sec = delta;
/* set timer again */
setitimer(ITIMER_REAL, &value, NULL);
}
pthread_mutex_unlock(&m_evthread);
return 1;
}
int GetUpdateTimeDelta(void){
int delta;
pthread_mutex_lock(&m_evthread);
delta = update_delta;
pthread_mutex_unlock(&m_evthread);
DPRINT(7, "delta=%d\n", delta);
return delta;
}
int SetSmbCtxUpdateInterval(int interval){
if (interval < GetUpdateTimeDelta()) return 0;
DPRINT(7, "interval=%d\n", interval);
pthread_mutex_lock(&m_evthread);
ctx_update_interval = interval;
pthread_mutex_unlock(&m_evthread);
return 1;
}
int GetSmbCtxUpdateInterval(void){
int interval;
pthread_mutex_lock(&m_evthread);
interval = ctx_update_interval;
pthread_mutex_unlock(&m_evthread);
DPRINT(7, "interval=%d\n", interval);
return interval;
}
int SetSmbItemUpdateInterval(int interval){
DPRINT(7, "interval=%d\n", interval);
if (interval < GetUpdateTimeDelta()) return 0;
pthread_mutex_lock(&m_evthread);
smbitem_update_interval = interval;
pthread_mutex_unlock(&m_evthread);
return 1;
}
int SetSmbItemTime2Live(int ttl){
int result;
DPRINT(7, "ttl=%d\n", ttl);
pthread_mutex_lock(&m_evthread);
result = (ttl > smbitem_update_interval);
if (result) smbitem_time_to_live = ttl;
pthread_mutex_unlock(&m_evthread);
return result;
}
int SetConfigFileUpdateInterval(int interval){
if (interval < GetUpdateTimeDelta()) return 0;
pthread_mutex_lock(&m_evthread);
config_update_interval = interval;
pthread_mutex_unlock(&m_evthread);
return 1;
}
int isTimeForRereadConfig(){
int result = 0;
pthread_mutex_lock(&m_evthread);
result = (last_config_update + config_update_interval < time(NULL));
pthread_mutex_unlock(&m_evthread);
return result;
}
int isTimeForRescanSambaTree(){
int result = 0;
pthread_mutex_lock(&m_evthread);
result = (last_smbitem_scan_time + smbitem_update_interval < time(NULL));
pthread_mutex_unlock(&m_evthread);
return result;
}
void ReadSambaGroup(const char *group, char *buf1, size_t convert_buffer_size){
char *buf2;
file_handle *fh;
struct smbc_dirent *dirent;
DPRINT(5, "(group=%s)\n", group);
if (convert_buffer_size < strlen(group) + 7) return;
strcpy(buf1, "smb://");
if (! local2smb(buf1 + 6, group, convert_buffer_size - 6)) return;
if ((buf2 = (char*) malloc(convert_buffer_size)) == NULL) return;
if (get_connection_handle(buf1, &fh) != 0) goto error1;
buf1[0] = '/';
strcpy(buf1 + 1, group);
buf1[strlen(group) + 1] = '/';
strcpy(buf2, "../");
LockSmbCtx(fh->smb);
SetSmbCtxFlags(fh->smb, SMBCTX_DO_NOT_UPDATE | SMBCTX_NEEDS_CLEANUP);
if ((fh->fd = libsmb_opendir(fh->smb->ctx, fh->url)) == NULL){
SetSmbCtxFlags(fh->smb, SMBCTX_IS_BAD);
goto error2;
}
while ((dirent = libsmb_readdir(fh->smb->ctx, fh->fd)) != NULL){
if (strcmp(dirent->name, "") == 0) continue;
if (strcmp(dirent->name, ".") == 0) continue;
if (strcmp(dirent->name, "..") == 0) continue;
if (dirent->smbc_type != SMBC_SERVER) continue;
if (! smb2local(buf1 + strlen(group) + 2, dirent->name,
convert_buffer_size - strlen(group) - 2)) continue;
if (! smb2local(buf2 + 3, dirent->name,
convert_buffer_size - 3)) continue;
mklink(buf1, buf2, SAMBA_TREE);
}
libsmb_closedir(fh->smb->ctx, fh->fd);
fh->fd = NULL;
error2:
UnlockSmbCtx(fh->smb);
release_file_handle(fh);
error1:
free(buf2);
}
void ReadSambaTree(void){
char *buf;
file_handle *fh;
struct smbc_dirent *dirent;
SPLICE splice;
SPLICE_ITERATOR it;
if ((buf = (char*) malloc(convert_buffer_size)) == NULL) return;
if (!GetQueryBrowserFlag()) goto update_group;
DPRINT(5, "reading group list\n");
if (get_connection_handle("smb://", &fh) != 0) goto error0;
LockSmbCtx(fh->smb);
SetSmbCtxFlags(fh->smb, SMBCTX_DO_NOT_UPDATE | SMBCTX_NEEDS_CLEANUP);
if ((fh->fd = libsmb_opendir(fh->smb->ctx, fh->url)) == NULL){
SetSmbCtxFlags(fh->smb, SMBCTX_IS_BAD);
goto error1;
}
while ((dirent = libsmb_readdir(fh->smb->ctx, fh->fd)) != NULL){
if (strcmp(dirent->name, "") == 0) continue;
if (strcmp(dirent->name, ".") == 0) continue;
if (strcmp(dirent->name, "..") == 0) continue;
if (dirent->smbc_type != SMBC_WORKGROUP) continue;
if (! smb2local(buf, dirent->name, convert_buffer_size)) continue;
mkgroup(buf, SAMBA_TREE);
}
libsmb_closedir(fh->smb->ctx, fh->fd);
fh->fd = NULL;
error1:
UnlockSmbCtx(fh->smb);
release_file_handle(fh);
update_group:
getdir(&splice, "", SAMBA_TREE);
init_forward_splice_iterator(&splice, &it);
while(is_valid_splice_iterator(&splice, &it)){
smbitem *item = *((smbitem**)splice_itdata_ptr(&splice, &it));
set_next_splice_iterator(&splice, &it);
ReadSambaGroup(item->name.string, buf, convert_buffer_size);
}
DeleteSmbItemArray(&splice);
error0:
free(buf);
}
void UpdateSambaTree(void){
time_t die_time;
pthread_mutex_lock(&m_evthread);
last_smbitem_scan_time = time(NULL);
die_time = last_smbitem_scan_time - smbitem_time_to_live;
pthread_mutex_unlock(&m_evthread);
ReadSambaTree();
DeleteOldSmbItem(die_time, SAMBA_TREE);
}
int ReadConfig(void){
time_t threshold;
int result;
DPRINT(5, "\n");
pthread_mutex_lock(&m_evthread);
threshold = last_config_update = time(NULL);
pthread_mutex_unlock(&m_evthread);
result = (chdir(config_dir) == 0) ? ReadConfigFile(config_file) : 0;
DeleteOldSmbItem(threshold, USER_TREE);
DeleteOldSmbAuthData(threshold);
return result;
}
void* UpdateThread(void *data){
int sig_number;
sigset_t signal_set;
int delta;
struct itimerval value;
(void)data;
ReadSambaTree();
pthread_mutex_lock(&m_evthread);
last_smbitem_scan_time = last_config_update = last_event_time = time(NULL);
pthread_mutex_unlock(&m_evthread);
/* init system timer */
delta = GetUpdateTimeDelta();
memset(&value, 0, sizeof(struct itimerval));
setitimer(ITIMER_REAL, &value, NULL);
value.it_value.tv_sec = delta;
value.it_interval.tv_sec = delta;
setitimer(ITIMER_REAL, &value, NULL);
/* set signals to watch */
sigemptyset(&signal_set);
sigaddset(&signal_set, SIGHUP);
sigaddset(&signal_set, SIGALRM);
while(1){
sigwait(&signal_set, &sig_number);
pthread_mutex_lock(&m_evthread);
last_event_time = time(NULL);
pthread_mutex_unlock(&m_evthread);
if ((sig_number == SIGHUP) || isTimeForRereadConfig()) ReadConfig();
if (isTimeForRescanSambaTree()) UpdateSambaTree();
RefreshOldSmbCtxs(time(NULL) - GetSmbCtxUpdateInterval());
}
return NULL;
}
static void* samba_init(void){
if (pthread_create(&evthread, NULL, UpdateThread, NULL) != 0){
fprintf(stderr, "Could not create event thread\n");
exit(1);
}
return NULL;
}
static void samba_destroy(void *private_data){
(void)private_data;
DPRINT(1, "Destroy event thread\n");
pthread_cancel(evthread);
if (!GetFastShutdown()) pthread_join(evthread, NULL);
}
void SetEventHandlers(struct fuse_operations *file_oper){
file_oper->init = samba_init;
file_oper->destroy = samba_destroy;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks