forked from pcloudcom/pfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.c
319 lines (287 loc) · 7.17 KB
/
settings.c
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
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include "common.h"
#include "settings.h"
#define SETTINGSCNT (sizeof(settings)/sizeof(setting))
#define EVENT_EXPIRE 3600
typedef void (*get_setting_func)(char *, size_t *);
typedef int (*set_setting_func)(const char *, size_t);
typedef struct {
const char *name;
get_setting_func get;
set_setting_func set;
char support_stat;
} setting;
typedef struct _event{
struct _event *next;
void *ptr;
size_t sz;
time_t tm;
} event;
static event *events=NULL;
static event **lastevent=&events;
static pthread_mutex_t events_mutex=PTHREAD_MUTEX_INITIALIZER;
static void setting_get_size_t(char *data, size_t *sz, size_t num){
*sz=snprintf(data, *sz, "%lu\n", (unsigned long)num);
}
static void setting_get_bool(char *data, size_t *sz, int b){
*sz=2;
if (b)
data[0]='1';
else
data[0]='0';
data[1]='\n';
data[2]=0;
}
static void get_page_size(char *data, size_t *sz){
setting_get_size_t(data, sz, fs_settings.pagesize);
}
static int set_page_size(const char *str, size_t len){
size_t sz=atol(str);
if (sz<1024 || sz>4*1024*1024 || (sz&(sz-1))!=0)
return -EINVAL;
fs_settings.pagesize=sz;
reset_cache();
return 0;
}
static void get_cache_size(char *data, size_t *sz){
setting_get_size_t(data, sz, fs_settings.cachesize);
}
static int set_cache_size(const char *str, size_t len){
size_t sz=atol(str);
if (sz<fs_settings.pagesize*4)
return -EINVAL;
if (sz>MAX_CACHE_SIZE)
return -EINVAL;
fs_settings.cachesize=sz;
reset_cache();
return 0;
}
static void get_readahead_min(char *data, size_t *sz){
setting_get_size_t(data, sz, fs_settings.readaheadmin);
}
static int set_readahead_min(const char *str, size_t len){
size_t sz=atol(str);
if (sz>fs_settings.readaheadmax)
return -EINVAL;
fs_settings.readaheadmin=sz;
return 0;
}
static void get_readahead_max(char *data, size_t *sz){
setting_get_size_t(data, sz, fs_settings.readaheadmax);
}
static int set_readahead_max(const char *str, size_t len){
size_t sz=atol(str);
if (sz<fs_settings.readaheadmin)
return -EINVAL;
fs_settings.readaheadmax=sz;
return 0;
}
static void get_readahead_max_sec(char *data, size_t *sz){
setting_get_size_t(data, sz, fs_settings.readaheadmaxsec);
}
static int set_readahead_max_sec(const char *str, size_t len){
size_t sz=atol(str);
fs_settings.readaheadmaxsec=sz;
return 0;
}
static void get_use_ssl(char *data, size_t *sz){
setting_get_bool(data, sz, fs_settings.usessl);
}
static int set_use_ssl(const char *str, size_t len){
fs_settings.usessl=atoi(str)?1:0;
return 0;
}
static void update_events_stat();
static void get_events(char *data, size_t *sz){
event *e;
pthread_mutex_lock(&events_mutex);
if (events){
if (events->sz<=*sz){
e=events;
events=e->next;
if (!events)
lastevent=&events;
update_events_stat();
pthread_mutex_unlock(&events_mutex);
memcpy(data, e->ptr, e->sz);
*sz=e->sz;
free(e);
return;
}
else{
memcpy(data, events->ptr, *sz);
events->ptr+=*sz;
events->sz-=*sz;
}
}
else
*sz=0;
update_events_stat();
pthread_mutex_unlock(&events_mutex);
}
static int set_events(const char *str, size_t len){
return -EINVAL;
}
static setting settings[]={
{"page_size", get_page_size, set_page_size, 1},
{"cache_size", get_cache_size, set_cache_size, 1},
{"readahead_min", get_readahead_min, set_readahead_min, 1},
{"readahead_max", get_readahead_max, set_readahead_max, 1},
{"readahead_max_sec", get_readahead_max_sec, set_readahead_max_sec, 1},
{"use_ssl", get_use_ssl, set_use_ssl, 1},
{"events", get_events, set_events, 0}
};
static const char *setting_names[SETTINGSCNT+1];
struct stat setting_stat[SETTINGSCNT];
struct stat settings_stat;
static int names_init=0;
static void init_settings(){
char buff[1024];
time_t tm;
size_t sz;
int i;
time(&tm);
memset(setting_stat, 0, sizeof(setting_stat));
memset(&settings_stat, 0, sizeof(settings_stat));
for (i=0; i<SETTINGSCNT; i++){
setting_names[i]=settings[i].name;
sz=sizeof(buff);
if (settings[i].support_stat)
settings[i].get(buff, &sz);
else
sz=0;
setting_stat[i].st_ctime=tm;
setting_stat[i].st_mtime=tm;
setting_stat[i].st_mode=S_IFREG | 0644;
setting_stat[i].st_nlink=1;
setting_stat[i].st_size=sz;
#if !defined(MINGW) && !defined(_WIN32)
setting_stat[i].st_blocks=(sz+511)/512;
setting_stat[i].st_blksize=FS_BLOCK_SIZE;
#endif
setting_stat[i].st_uid=myuid;
setting_stat[i].st_gid=mygid;
}
setting_names[SETTINGSCNT]=NULL;
settings_stat.st_ctime=tm;
settings_stat.st_mtime=tm;
settings_stat.st_mode=S_IFDIR | 0755;
settings_stat.st_nlink=2;
settings_stat.st_size=SETTINGSCNT;
#if !defined(MINGW) && !defined(_WIN32)
settings_stat.st_blocks=(settings_stat.st_size+511)/512;
settings_stat.st_blksize=FS_BLOCK_SIZE;
#endif
settings_stat.st_uid=myuid;
settings_stat.st_gid=mygid;
pthread_mutex_lock(&events_mutex);
update_events_stat();
pthread_mutex_unlock(&events_mutex);
names_init=1;
}
const char **list_settings(){
if (!names_init)
init_settings();
return setting_names;
}
static int get_setting_id(const char *name){
int i;
for (i=0; i<SETTINGSCNT; i++)
if (!strcmp(name, settings[i].name))
return i;
return -1;
}
const struct stat *get_setting_stat(const char *name){
if (!names_init)
init_settings();
if (name[0]){
int id=get_setting_id(name+1);
if (id!=-1)
return &setting_stat[id];
else
return NULL;
}
else
return &settings_stat;
}
int set_setting(const char *name, const char *val, size_t vallen){
int id=get_setting_id(name);
if (id!=-1){
int ret=settings[id].set(val, vallen);
if (!ret){
char buff[4096];
size_t sz;
sz=sizeof(buff);
settings[id].get(buff, &sz);
setting_stat[id].st_size=sz;
#if !defined(MINGW) && !defined(_WIN32)
setting_stat[id].st_blocks=(sz+511)/512;
#endif
}
return ret;
}
else
return -ENOENT;
}
int get_setting(const char *name, char *val, size_t *vallen){
int id=get_setting_id(name);
if (id!=-1){
settings[id].get(val, vallen);
return 0;
}
else
return -ENOENT;
}
void event_writev(const struct iovec *iov, int iovcnt){
size_t ssize;
event *e;
void *p, *cp;
time_t tm;
int i;
ssize=0;
for (i=0; i<iovcnt; i++)
ssize+=iov[i].iov_len;
e=malloc(sizeof(event)+ssize);
p=e+1;
e->next=NULL;
e->ptr=p;
e->sz=ssize;
cp=p;
for (i=0; i<iovcnt; i++){
memcpy(cp, iov[i].iov_base, iov[i].iov_len);
cp+=iov[i].iov_len;
}
time(&tm);
e->tm=tm;
pthread_mutex_lock(&events_mutex);
*lastevent=e;
lastevent=&e->next;
while (events->tm<tm-EVENT_EXPIRE){
e=events;
events=e->next;
free(e);
}
update_events_stat();
pthread_mutex_unlock(&events_mutex);
}
static void update_events_stat(){
size_t sz;
int id=get_setting_id("events");
if (id!=-1){
if (events)
sz=events->sz;
else
sz=0;
setting_stat[id].st_size=sz;
#if !defined(MINGW) && !defined(_WIN32)
setting_stat[id].st_blocks=(sz+511)/512;
#endif
}
}