-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_fsync.c
359 lines (282 loc) · 8.76 KB
/
mod_fsync.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
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
359
/*
* ProFTPD: mod_fsync -- a module for using fsync to periodically force writes
* Copyright (c) 2004-2017 TJ Saunders
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* As a special exemption, TJ Saunders and other respective copyright holders
* give permission to link this program with OpenSSL, and distribute the
* resulting executable, without including the source code for OpenSSL in the
* source distribution.
*
* This is mod_fsync, contrib software for proftpd 1.3.x and above.
* For more information contact TJ Saunders <tj@castaglia.org>.
*/
#include "conf.h"
#include "privs.h"
#define MOD_FSYNC_VERSION "mod_fsync/0.3"
/* Make sure the version of proftpd is as necessary. */
#if PROFTPD_VERSION_NUMBER < 0x0001030001
# error "ProFTPD 1.3.0rc1 or later required"
#endif
module fsync_module;
static int fsync_logfd = -1;
static array_header *fsync_fds = NULL;
static off_t fsync_nwritten = 0;
static off_t fsync_threshold = 0;
/* FSIO handlers
*/
static int fsync_close(pr_fh_t *fh, int fd) {
/* Remove this fd from the tracked list, if present. We don't need
* to explicitly flush any disk buffer via fsync(), as that's handled
* by the kernel.
*/
if (fsync_fds != NULL &&
fsync_fds->nelts > 0) {
register unsigned int i;
int *fds = fsync_fds->elts;
for (i = 0; i < fsync_fds->nelts; i++) {
if (fds[i] == fd) {
(void) pr_log_writefile(fsync_logfd, MOD_FSYNC_VERSION,
"removing '%s' (%d) from list", fh->fh_path, fd);
fds[i] = -1;
break;
}
}
}
return close(fd);
}
static int fsync_open(pr_fh_t *fh, const char *path, int flags) {
int fd = open(path, flags, PR_OPEN_MODE);
/* Error out now if the file couldn't be opened. */
if (fd < 0)
return fd;
/* If the path is being opened for writing, once opened, add the
* fd to a tracking list.
*/
if ((flags & O_WRONLY) ||
(flags & O_RDWR)) {
register unsigned int i;
int *fds, added = FALSE;
if (!fsync_fds) {
pool *p = session.pool ? session.pool : permanent_pool;
fsync_fds = make_array(p, 1, sizeof(int));
}
/* Run through the list, checking for an empty slot first. No need
* to allocate more entries than necessary.
*/
fds = fsync_fds->elts;
for (i = 0; i < fsync_fds->nelts; i++) {
if (fds[i] == -1) {
/* Found an empty slot. */
fds[i] = fd;
added = TRUE;
break;
}
}
if (!added)
*((int *) push_array(fsync_fds)) = fd;
(void) pr_log_writefile(fsync_logfd, MOD_FSYNC_VERSION,
"added '%s' (%d) to the list", path, fd);
}
return fd;
}
static int fsync_write(pr_fh_t *fh, int fd, const char *buf, size_t size) {
int res;
/* Force a fsync() on all the tracked fds (which have been opened for
* writing) after a certain global amount of data has been written
* to the filesystem. (Note: do this even if the files are on separate
* disks?) After doing so, reset the global bytes-written counter.
*/
res = write(fd, buf, size);
if (res < 0)
return res;
fsync_nwritten += res;
if (fsync_fds != NULL &&
fsync_nwritten >= fsync_threshold) {
register unsigned int i;
int *fds = fsync_fds->elts;
(void) pr_log_writefile(fsync_logfd, MOD_FSYNC_VERSION,
"FsyncThreshold (%" PR_LU ") reached, syncing %d descriptors",
(pr_off_t) fsync_threshold, fsync_fds->nelts);
for (i = 0; i < fsync_fds->nelts; i++) {
if (fds[i] != -1) {
#ifdef HAVE_FDATASYNC
if (fdatasync(fds[i]) < 0) {
#else
if (fsync(fds[i]) < 0) {
#endif /* HAVE_FDATASYNC */
(void) pr_log_writefile(fsync_logfd, MOD_FSYNC_VERSION,
"error sync'ing data for %d: %s", fds[i], strerror(errno));
}
}
}
/* Reset the counter. */
fsync_nwritten = 0;
}
return res;
}
/* Configuration handlers
*/
/* usage: FsyncEngine on|off */
MODRET set_fsyncengine(cmd_rec *cmd) {
int engine = -1;
config_rec *c;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT);
engine = get_boolean(cmd, 1);
if (engine == -1) {
CONF_ERROR(cmd, "expected Boolean parameter");
}
c = add_config_param(cmd->argv[0], 1, NULL);
c->argv[0] = palloc(c->pool, sizeof(int));
*((int *) c->argv[0]) = engine;
return PR_HANDLED(cmd);
}
/* usage: FsyncLog path */
MODRET set_fsynclog(cmd_rec *cmd) {
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT);
if (pr_fs_valid_path(cmd->argv[1]) < 0) {
CONF_ERROR(cmd, "must be an absolute path");
}
add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
return PR_HANDLED(cmd);
}
/* usage: FsyncThreshold size */
MODRET set_fsyncthreshold(cmd_rec *cmd) {
char *tmp = NULL;
off_t threshold;
config_rec *c;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT);
#ifdef HAVE_STRTOULL
threshold = strtoull(cmd->argv[1], &tmp, 10);
#else
threshold = strtoul(cmd->argv[1], &tmp, 10);
#endif /* HAVE_STRTOULL */
if (tmp &&
*tmp) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "'", cmd->argv[1],
"' is not a valid threshold", NULL));
}
c = add_config_param(cmd->argv[0], 1, NULL);
c->argv[0] = palloc(c->pool, sizeof(off_t));
*((off_t *) c->argv[0]) = threshold;
return PR_HANDLED(cmd);
}
/* Event handlers
*/
#if defined(PR_SHARED_MODULE)
static void fsync_mod_unload_ev(const void *event_data, void *user_data) {
if (strcmp("mod_fsync.c", (const char *) event_data) == 0) {
(void) pr_unmount_fs("/", "fsync");
pr_event_unregister(&fsync_module, NULL, NULL);
}
}
#endif /* PR_SHARED_MODULE */
static void fsync_postparse_ev(const void *event_data, void *user_data) {
config_rec *c;
pr_fs_t *fs;
int engine = FALSE;
c = find_config(main_server->conf, CONF_PARAM, "FsyncEngine", FALSE);
if (c)
engine = *((int *) c->argv[0]);
if (!engine)
return;
c = find_config(main_server->conf, CONF_PARAM, "FsyncLog", FALSE);
if (c) {
int res;
char *path = c->argv[0];
PRIVS_ROOT
res = pr_log_openfile(path, &fsync_logfd, 0660);
PRIVS_RELINQUISH
switch (res) {
case 0:
break;
case -1:
pr_log_debug(DEBUG1, MOD_FSYNC_VERSION
": unable to open FsyncLog '%s': %s", path, strerror(errno));
break;
case PR_LOG_SYMLINK:
pr_log_debug(DEBUG1, MOD_FSYNC_VERSION
": unable to open FsyncLog '%s': %s", path, "is a symlink");
break;
case PR_LOG_WRITABLE_DIR:
pr_log_debug(DEBUG0, MOD_FSYNC_VERSION
": unable to open FsyncLog '%s': %s", path,
"parent directory is world-writable");
break;
}
}
c = find_config(main_server->conf, CONF_PARAM, "FsyncThreshold", FALSE);
if (c == NULL) {
/* This is a required directive. */
(void) pr_log_writefile(fsync_logfd, MOD_FSYNC_VERSION,
"missing required FsyncThreshold directive, disabling module");
return;
}
fsync_threshold = *((off_t *) c->argv[0]);
/* Register our custom filesystem. */
fs = pr_register_fs(permanent_pool, "fsync", "/");
if (fs == NULL) {
(void) pr_log_writefile(fsync_logfd, MOD_FSYNC_VERSION,
"error registering FS: %s", strerror(errno));
return;
}
/* Add our custom FSIO handlers. */
fs->close = fsync_close;
fs->open = fsync_open;
fs->write = fsync_write;
return;
}
/* Initialization functions
*/
static int fsync_init(void) {
#if defined(PR_SHARED_MODULE)
pr_event_register(&fsync_module, "core.module-unload", fsync_mod_unload_ev,
NULL);
#endif /* PR_SHARED_MODULE */
pr_event_register(&fsync_module, "core.postparse", fsync_postparse_ev,
NULL);
return 0;
}
/* Module API tables
*/
static conftable fsync_conftab[] = {
{ "FsyncEngine", set_fsyncengine, NULL },
{ "FsyncLog", set_fsynclog, NULL },
{ "FsyncThreshold", set_fsyncthreshold, NULL },
{ NULL }
};
module fsync_module = {
NULL, NULL,
/* Module API version 2.0 */
0x20,
/* Module name */
"fsync",
/* Module configuration handler table */
fsync_conftab,
/* Module command handler table */
NULL,
/* Module authentication handler table */
NULL,
/* Module initialization function */
fsync_init,
/* Session initialization function */
NULL,
/* Module version */
MOD_FSYNC_VERSION
};