Debian Codegraph @

contrib/mod_statcache.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_statcache -- a module implementing caching of stat(2),
3 * fstat(2), and lstat(2) calls
4 * Copyright (c) 2013-2023 TJ Saunders
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19 *
20 * As a special exemption, TJ Saunders and other respective copyright holders
21 * give permission to link this program with OpenSSL, and distribute the
22 * resulting executable, without including the source code for OpenSSL in the
23 * source distribution.
24 *
25 * This is mod_statcache, contrib software for proftpd 1.3.x.
26 * For more information contact TJ Saunders <[email protected]>.
27 */
28
29 #include "conf.h"
30 #include "privs.h"
31 #if defined(PR_USE_CTRLS)
32 # include "mod_ctrls.h"
33 #endif /* PR_USE_CTRLS */
34
35 #include <signal.h>
36 #include <sys/ipc.h>
37 #include <sys/shm.h>
38
39 #if HAVE_SYS_MMAN_H
40 # include <sys/mman.h>
41 #endif
42
43 #if HAVE_SYS_UIO_H
44 # include <sys/uio.h>
45 #endif
46
47 #define MOD_STATCACHE_VERSION "mod_statcache/0.2"
48
49 /* Make sure the version of proftpd is as necessary. */
50 #if PROFTPD_VERSION_NUMBER < 0x0001030402
51 # error "ProFTPD 1.3.4rc2 or later required"
52 #endif
53
54 /* On some platforms, this may not be defined. On AIX, for example, this
55 * symbol is only defined when _NO_PROTO is defined, and _XOPEN_SOURCE is 500.
56 * How annoying.
57 */
58 #ifndef MAP_FAILED
59 # define MAP_FAILED ((void *) -1)
60 #endif
61
62 #define STATCACHE_DEFAULT_CAPACITY 5000
63 #define STATCACHE_DEFAULT_MAX_AGE 5
64
65 /* A path is hashed, and that hash % ncols indicates the row index. For
66 * each row, there can be N columns. This value indicates the number of
67 * columns for a row; it controls how many collisions can be handled.
68 */
69 #define STATCACHE_COLS_PER_ROW 10
70
71 /* Max number of lock attempts */
72 #define STATCACHE_MAX_LOCK_ATTEMPTS 10
73
74 /* Subpool size */
75 #define STATCACHE_POOL_SIZE 256
76
77 /* From src/main.c */
78 extern pid_t mpid;
79
80 module statcache_module;
81
82 #if defined(PR_USE_CTRLS)
83 static ctrls_acttab_t statcache_acttab[];
84 #endif
85
86 /* Pool for this module's use */
87 static pool *statcache_pool = NULL;
88
89 /* Copied from src/fsio.c. */
90 struct statcache_entry {
91 uint32_t sce_hash;
92 char sce_path[PR_TUNABLE_PATH_MAX+1];
93 size_t sce_pathlen;
94 struct stat sce_stat;
95 int sce_errno;
96 unsigned char sce_op;
97 time_t sce_ts;
98 };
99
100 /* Storage structure:
101 *
102 * Header (stats):
103 * uint32_t count
104 * uint32_t highest
105 * uint32_t hits
106 * uint32_t misses
107 * uint32_t expires
108 * uint32_t rejects
109 *
110 * Data (entries):
111 * nrows = capacity / STATCACHE_COLS_PER_ROW
112 * row_len = sizeof(struct statcache_entry) * STATCACHE_COLS_PER_ROW
113 * row_start = ((hash % nrows) * row_len) + data_start
114 */
115
116 static int statcache_engine = FALSE;
117 static unsigned int statcache_max_positive_age = STATCACHE_DEFAULT_MAX_AGE;
118 static unsigned int statcache_max_negative_age = 1;
119 static unsigned int statcache_capacity = STATCACHE_DEFAULT_CAPACITY;
120 static unsigned int statcache_nrows = 0;
121 static size_t statcache_rowlen = 0;
122
123 static char *statcache_table_path = NULL;
124 static pr_fh_t *statcache_tabfh = NULL;
125
126 static void *statcache_table = NULL;
127 static size_t statcache_tablesz = 0;
128 static uint32_t *statcache_table_stats = NULL;
129 static void *statcache_table_data = NULL;
130
131 static const char *trace_channel = "statcache";
132
133 static int statcache_wlock_row(int fd, uint32_t hash);
134 static int statcache_unlock_row(int fd, uint32_t hash);
135
136 #if defined(PR_USE_CTRLS)
137 static int statcache_rlock_stats(int fd);
138 static int statcache_rlock_table(int fd);
139 static int statcache_unlock_table(int fd);
140 #endif /* PR_USE_CTRLS */
141 static int statcache_wlock_stats(int fd);
142 static int statcache_unlock_stats(int fd);
143
144 static void statcache_fs_statcache_clear_ev(const void *event_data,
145 void *user_data);
146 static int statcache_sess_init(void);
147
148 /* Functions for marshalling key/value data to/from local cache (SysV shm). */
149 static void *statcache_get_shm(pr_fh_t *tabfh, size_t datasz) {
150 void *data;
151 int fd, mmap_flags, res, xerrno;
152 #if defined(MADV_RANDOM) || defined(MADV_ACCESS_MANY)
153 int advice = 0;
154 #endif
155
156 fd = tabfh->fh_fd;
157
158 /* Truncate the table first; any existing data should be deleted. */
159 res = ftruncate(fd, 0);
160 if (res < 0) {
161 xerrno = errno;
162
163 pr_log_debug(DEBUG0, MOD_STATCACHE_VERSION
164 ": error truncating StatCacheTable '%s' to size 0: %s", tabfh->fh_path,
165 strerror(xerrno));
166
167 errno = xerrno;
168 return NULL;
169 }
170
171 /* Seek to the desired table size (actually, one byte less than the desired
172 * size) and write a single byte, so that there's enough allocated backing
173 * store on the filesystem to support the ensuing mmap() call.
174 */
175 if (lseek(fd, datasz, SEEK_SET) == (off_t) -1) {
176 xerrno = errno;
177
178 pr_log_debug(DEBUG0, MOD_STATCACHE_VERSION
179 ": error seeking to offset %lu in StatCacheTable '%s': %s",
180 (unsigned long) datasz-1, tabfh->fh_path, strerror(xerrno));
181
182 errno = xerrno;
183 return NULL;
184 }
185
186 res = write(fd, "", 1);
187 if (res != 1) {
188 xerrno = errno;
189
190 pr_log_debug(DEBUG0, MOD_STATCACHE_VERSION
191 ": error writing single byte to StatCacheTable '%s': %s",
192 tabfh->fh_path, strerror(xerrno));
193
194 errno = xerrno;
195 return NULL;
196 }
197
198 mmap_flags = MAP_SHARED;
199
200 /* Make sure to set the fd to -1 if MAP_ANON(YMOUS) is used. By definition,
201 * anonymous mapped memory does not need (or want) a valid file backing
202 * store; some implementations will not do what is expected when anonymous
203 * memory is requested AND a valid fd is passed in.
204 *
205 * However, we want to keep a valid fd open anyway, for later use by
206 * fcntl(2) for byte range locking; we simply don't use the valid fd for
207 * the mmap(2) call.
208 */
209
210 #if defined(MAP_ANONYMOUS)
211 /* Linux */
212 mmap_flags |= MAP_ANONYMOUS;
213 fd = -1;
214
215 #elif defined(MAP_ANON)
216 /* FreeBSD, MacOSX, Solaris, others? */
217 mmap_flags |= MAP_ANON;
218 fd = -1;
219
220 #else
221 pr_log_debug(DEBUG8, MOD_STATCACHE_VERSION
222 ": mmap(2) MAP_ANONYMOUS and MAP_ANON flags not defined");
223 #endif
224
225 data = mmap(NULL, datasz, PROT_READ|PROT_WRITE, mmap_flags, fd, 0);
226 if (data == MAP_FAILED) {
227 xerrno = errno;
228
229 pr_log_debug(DEBUG0, MOD_STATCACHE_VERSION
230 ": error mapping StatCacheTable '%s' fd %d size %lu into memory: %s",
231 tabfh->fh_path, fd, (unsigned long) datasz, strerror(xerrno));
232
233 errno = xerrno;
234 return NULL;
235 }
236
237 /* Make sure the data are zeroed. */
238 memset(data, 0, datasz);
239
240 #if defined(MADV_RANDOM) || defined(MADV_ACCESS_MANY)
241 /* Provide some hints to the kernel, for hopefully better handling of
242 * this buffer.
243 */
244 # if defined(MADV_RANDOM)
245 advice = MADV_RANDOM;
246 # elif defined(MADV_ACCESS_MANY)
247 /* Oracle-ism? */
248 advice = MADV_ACCESS_MANY;
249 # endif /* Random access pattern memory advice */
250
251 res = madvise(data, datasz, advice);
252 if (res < 0) {
253 pr_log_debug(DEBUG5, MOD_STATCACHE_VERSION
254 ": madvise(2) error with MADV_RANDOM: %s", strerror(errno));
255 }
256 #endif
257
258 return data;
259 }
260
261 static const char *get_lock_type(struct flock *lock) {
262 const char *lock_type;
263
264 switch (lock->l_type) {
265 case F_RDLCK:
266 lock_type = "read";
267 break;
268
269 case F_WRLCK:
270 lock_type = "write";
271 break;
272
273 case F_UNLCK:
274 lock_type = "unlock";
275 break;
276
277 default:
278 lock_type = "[UNKNOWN]";
279 }
280
281 return lock_type;
282 }
283
284 /* Header locking routines */
285 static int lock_table(int fd, int lock_type, off_t lock_len) {
286 struct flock lock;
287 unsigned int nattempts = 1;
288
289 lock.l_type = lock_type;
290 lock.l_whence = 0;
291 lock.l_start = 0;
292 lock.l_len = (6 * sizeof(uint32_t));
293
294 pr_trace_msg(trace_channel, 15,
295 "attempt #%u to acquire %s lock on StatCacheTable fd %d (off %lu, len %lu)",
296 nattempts, get_lock_type(&lock), fd, (unsigned long) lock.l_start,
297 (unsigned long) lock.l_len);
298
299 while (fcntl(fd, F_SETLK, &lock) < 0) {
300 int xerrno = errno;
301
302 if (xerrno == EINTR) {
303 pr_signals_handle();
304 continue;
305 }
306
307 pr_trace_msg(trace_channel, 3,
308 "%s lock (attempt #%u) of StatCacheTable fd %d failed: %s",
309 get_lock_type(&lock), nattempts, fd, strerror(xerrno));
310 if (xerrno == EACCES) {
311 struct flock locker;
312
313 /* Get the PID of the process blocking this lock. */
314 if (fcntl(fd, F_GETLK, &locker) == 0) {
315 pr_trace_msg(trace_channel, 3, "process ID %lu has blocking %s lock on "
316 "StatCacheTable fd %d", (unsigned long) locker.l_pid,
317 get_lock_type(&locker), fd);
318 }
319 }
320
321 if (xerrno == EAGAIN ||
322 xerrno == EACCES) {
323 /* Treat this as an interrupted call, call pr_signals_handle() (which
324 * will delay for a few msecs because of EINTR), and try again.
325 * After MAX_LOCK_ATTEMPTS attempts, give up altogether.
326 */
327
328 nattempts++;
329 if (nattempts <= STATCACHE_MAX_LOCK_ATTEMPTS) {
330 errno = EINTR;
331
332 pr_signals_handle();
333
334 errno = 0;
335 pr_trace_msg(trace_channel, 15,
336 "attempt #%u to acquire %s lock on StatCacheTable fd %d", nattempts,
337 get_lock_type(&lock), fd);
338 continue;
339 }
340
341 pr_trace_msg(trace_channel, 15, "unable to acquire %s lock on "
342 "StatCacheTable fd %d after %u attempts: %s", get_lock_type(&lock),
343 nattempts, fd, strerror(xerrno));
344 }
345
346 errno = xerrno;
347 return -1;
348 }
349
350 pr_trace_msg(trace_channel, 15,
351 "acquired %s lock of StatCacheTable fd %d successfully",
352 get_lock_type(&lock), fd);
353 return 0;
354 }
355
356 #if defined(PR_USE_CTRLS)
357 static int statcache_rlock_stats(int fd) {
358 return lock_table(fd, F_RDLCK, (6 * sizeof(uint32_t)));
359 }
360
361 static int statcache_rlock_table(int fd) {
362 return lock_table(fd, F_RDLCK, 0);
363 }
364
365 static int statcache_unlock_table(int fd) {
366 return lock_table(fd, F_RDLCK, 0);
367 }
368 #endif /* PR_USE_CTRLS */
369
370 static int statcache_wlock_stats(int fd) {
371 return lock_table(fd, F_WRLCK, (6 * sizeof(uint32_t)));
372 }
373
374 static int statcache_unlock_stats(int fd) {
375 return lock_table(fd, F_UNLCK, (6 * sizeof(uint32_t)));
376 }
377
378 #if defined(PR_USE_CTRLS)
379 static uint32_t statcache_stats_get_count(void) {
380 uint32_t *count;
381
382 count = statcache_table_stats;
383 return *count;
384 }
385
386 static uint32_t statcache_stats_get_highest(void) {
387 uint32_t *highest;
388
389 highest = statcache_table_stats + 1;
390 return *highest;
391 }
392
393 static uint32_t statcache_stats_get_hits(void) {
394 uint32_t *hits;
395
396 hits = statcache_table_stats + 2;
397 return *hits;
398 }
399
400 static uint32_t statcache_stats_get_misses(void) {
401 uint32_t *misses;
402
403 /* misses = statcache_table_stats + (3 * sizeof(uint32_t)) */
404 misses = statcache_table_stats + 3;
405 return *misses;
406 }
407
408 static uint32_t statcache_stats_get_expires(void) {
409 uint32_t *expires;
410
411 expires = statcache_table_stats + 4;
412 return *expires;
413 }
414
415 static uint32_t statcache_stats_get_rejects(void) {
416 uint32_t *rejects;
417
418 rejects = statcache_table_stats + 5;
419 return *rejects;
420 }
421 #endif /* PR_USE_CTRLS */
422
423 static int statcache_stats_decr_count(uint32_t decr) {
424 uint32_t *count = NULL;
425
426 if (decr == 0) {
427 return 0;
428 }
429
430 count = statcache_table_stats;
431
432 /* Prevent underflow. */
433 if (*count < decr) {
434 *count = 0;
435
436 } else {
437 *count -= decr;
438 }
439
440 return 0;
441 }
442
443 static int statcache_stats_incr_count(uint32_t incr) {
444 uint32_t *count = NULL, *highest = NULL;
445
446 if (incr == 0) {
447 return 0;
448 }
449
450 count = statcache_table_stats;
451 highest = statcache_table_stats + 1;
452
453 /* Prevent overflow. */
454 if (UINT32_MAX - *count > incr) {
455 *count += incr;
456
457 if (*count > *highest) {
458 *highest = *count;
459 }
460 }
461
462 return 0;
463 }
464
465 static int statcache_stats_incr_hits(uint32_t incr) {
466 uint32_t *hits = NULL;
467
468 if (incr == 0) {
469 return 0;
470 }
471
472 hits = statcache_table_stats + 2;
473
474 /* Prevent overflow. */
475 if (UINT32_MAX - *hits > incr) {
476 *hits += incr;
477 }
478
479 return 0;
480 }
481
482 static int statcache_stats_incr_misses(uint32_t incr) {
483 uint32_t *misses = NULL;
484
485 if (incr == 0) {
486 return 0;
487 }
488
489 misses = statcache_table_stats + 3;
490
491 /* Prevent overflow. */
492 if (UINT32_MAX - *misses > incr) {
493 *misses += incr;
494 }
495
496 return 0;
497 }
498
499 static int statcache_stats_incr_expires(uint32_t incr) {
500 uint32_t *expires = NULL;
501
502 if (incr == 0) {
503 return 0;
504 }
505
506 expires = statcache_table_stats + 4;
507
508 /* Prevent overflow. */
509 if (UINT32_MAX - *expires > incr) {
510 *expires += incr;
511 }
512
513 return 0;
514 }
515
516 static int statcache_stats_incr_rejects(uint32_t incr) {
517 uint32_t *rejects = NULL;
518
519 if (incr == 0) {
520 return 0;
521 }
522
523 rejects = statcache_table_stats + 5;
524
525 /* Prevent overflow. */
526 if (UINT32_MAX - *rejects > incr) {
527 *rejects += incr;
528 }
529
530 return 0;
531 }
532
533 /* Data locking routines */
534
535 static int get_row_range(uint32_t hash, off_t *row_start, off_t *row_len) {
536 uint32_t row_idx;
537
538 row_idx = hash % statcache_nrows;
539 *row_start = (row_idx * statcache_rowlen);
540 *row_len = statcache_rowlen;
541
542 return 0;
543 }
544
545 static int lock_row(int fd, int lock_type, uint32_t hash) {
546 struct flock lock;
547 unsigned int nattempts = 1;
548
549 lock.l_type = lock_type;
550 lock.l_whence = 0;
551 get_row_range(hash, &lock.l_start, &lock.l_len);
552
553 pr_trace_msg(trace_channel, 15,
554 "attempt #%u to acquire row %s lock on StatCacheTable fd %d "
555 "(off %lu, len %lu)", nattempts, get_lock_type(&lock), fd,
556 (unsigned long) lock.l_start,
557 (unsigned long) lock.l_len);
558
559 while (fcntl(fd, F_SETLK, &lock) < 0) {
560 int xerrno = errno;
561
562 if (xerrno == EINTR) {
563 pr_signals_handle();
564 continue;
565 }
566
567 pr_trace_msg(trace_channel, 3,
568 "%s lock (attempt #%u) of StatCacheTable fd %d failed: %s",
569 get_lock_type(&lock), nattempts, fd, strerror(xerrno));
570 if (xerrno == EACCES) {
571 struct flock locker;
572
573 /* Get the PID of the process blocking this lock. */
574 if (fcntl(fd, F_GETLK, &locker) == 0) {
575 pr_trace_msg(trace_channel, 3, "process ID %lu has blocking %s lock on "
576 "StatCacheTable fd %d", (unsigned long) locker.l_pid,
577 get_lock_type(&locker), fd);
578 }
579 }
580
581 if (xerrno == EAGAIN ||
582 xerrno == EACCES) {
583 /* Treat this as an interrupted call, call pr_signals_handle() (which
584 * will delay for a few msecs because of EINTR), and try again.
585 * After MAX_LOCK_ATTEMPTS attempts, give up altogether.
586 */
587
588 nattempts++;
589 if (nattempts <= STATCACHE_MAX_LOCK_ATTEMPTS) {
590 errno = EINTR;
591
592 pr_signals_handle();
593
594 errno = 0;
595 pr_trace_msg(trace_channel, 15,
596 "attempt #%u to acquire %s row lock on StatCacheTable fd %d",
597 nattempts, get_lock_type(&lock), fd);
598 continue;
599 }
600
601 pr_trace_msg(trace_channel, 15, "unable to acquire %s row lock on "
602 "StatCacheTable fd %d after %u attempts: %s", get_lock_type(&lock),
603 nattempts, fd, strerror(xerrno));
604 }
605
606 errno = xerrno;
607 return -1;
608 }
609
610 pr_trace_msg(trace_channel, 15,
611 "acquired %s row lock of StatCacheTable fd %d successfully",
612 get_lock_type(&lock), fd);
613 return 0;
614 }
615
616 static int statcache_wlock_row(int fd, uint32_t hash) {
617 return lock_row(fd, F_WRLCK, hash);
618 }
619
620 static int statcache_unlock_row(int fd, uint32_t hash) {
621 return lock_row(fd, F_UNLCK, hash);
622 }
623
624 /* Table manipulation routines */
625
626 /* See http://www.cse.yorku.ca/~oz/hash.html */
627 static uint32_t statcache_hash(const char *path, size_t pathlen) {
628 register unsigned int i;
629 uint32_t h = 5381;
630
631 for (i = 0; i < pathlen; i++) {
632 h = ((h << 5) + h) + path[i];
633 }
634
635 /* Strip off the high bit. */
636 h &= ~(1 << 31);
637
638 return h;
639 }
640
641 /* Add an entry to the table. */
642 static int statcache_table_add(int fd, const char *path, size_t pathlen,
643 struct stat *st, int xerrno, uint32_t hash, unsigned char op) {
644 register unsigned int i;
645 uint32_t row_idx;
646 int found_slot = FALSE, expired_entries = 0;
647 time_t now;
648 struct statcache_entry *sce = NULL;
649
650 if (statcache_table == NULL) {
651 errno = EPERM;
652 return -1;
653 }
654
655 /* Find an open slot in the list for this new entry. */
656 now = time(NULL);
657
658 row_idx = hash % statcache_nrows;
659
660 for (i = 0; i < STATCACHE_COLS_PER_ROW; i++) {
661 uint32_t col_start;
662
663 pr_signals_handle();
664
665 col_start = row_idx + i;
666 sce = ((struct statcache_entry *) statcache_table_data) + col_start;
667 if (sce->sce_ts == 0) {
668 /* Empty slot */
669 found_slot = TRUE;
670 break;
671 }
672
673 /* If existing item is too old, use this slot. Note that there
674 * are different expiry rules for negative cache entries (i.e.
675 * errors) than for positive cache entries.
676 */
677 if (sce->sce_errno == 0) {
678 if (now > (sce->sce_ts + statcache_max_positive_age)) {
679 found_slot = TRUE;
680 expired_entries++;
681 break;
682 }
683
684 } else {
685 if (now > (sce->sce_ts + statcache_max_negative_age)) {
686 found_slot = TRUE;
687 expired_entries++;
688 break;
689 }
690 }
691 }
692
693 if (found_slot == FALSE) {
694 if (statcache_wlock_stats(fd) < 0) {
695 pr_trace_msg(trace_channel, 3,
696 "error write-locking shared memory: %s", strerror(errno));
697 }
698
699 statcache_stats_incr_rejects(1);
700
701 if (statcache_unlock_stats(fd) < 0) {
702 pr_trace_msg(trace_channel, 3,
703 "error un-locking shared memory: %s", strerror(errno));
704 }
705
706 errno = ENOSPC;
707 return -1;
708 }
709
710 if (st != NULL) {
711 pr_trace_msg(trace_channel, 9,
712 "adding entry for path '%s' (hash %lu) at row %lu, col %u "
713 "(op %s, type %s)", path,
714 (unsigned long) hash, (unsigned long) row_idx + 1, i + 1,
715 op == FSIO_FILE_LSTAT ? "LSTAT" : "STAT",
716 S_ISLNK(st->st_mode) ? "symlink" :
717 S_ISDIR(st->st_mode) ? "dir" : "file");
718
719 } else {
720 pr_trace_msg(trace_channel, 9,
721 "adding entry for path '%s' (hash %lu) at row %lu, col %u "
722 "(op %s, errno %d)", path,
723 (unsigned long) hash, (unsigned long) row_idx + 1, i + 1,
724 op == FSIO_FILE_LSTAT ? "LSTAT" : "STAT", xerrno);
725 }
726
727 sce->sce_hash = hash;
728 sce->sce_pathlen = pathlen;
729
730 /* Include trailing NUL. */
731 memcpy(sce->sce_path, path, pathlen + 1);
732 if (st != NULL) {
733 memcpy(&(sce->sce_stat), st, sizeof(struct stat));
734 }
735 sce->sce_errno = xerrno;
736 sce->sce_ts = now;
737 sce->sce_op = op;
738
739 if (statcache_wlock_stats(fd) < 0) {
740 pr_trace_msg(trace_channel, 3,
741 "error write-locking shared memory: %s", strerror(errno));
742 }
743
744 statcache_stats_incr_count(1);
745 if (expired_entries > 0) {
746 statcache_stats_decr_count(expired_entries);
747 statcache_stats_incr_expires(expired_entries);
748 }
749
750 if (statcache_unlock_stats(fd) < 0) {
751 pr_trace_msg(trace_channel, 3,
752 "error un-locking shared memory: %s", strerror(errno));
753 }
754
755 return 0;
756 }
757
758 static int statcache_table_get(int fd, const char *path, size_t pathlen,
759 struct stat *st, int *xerrno, uint32_t hash, unsigned char op) {
760 register unsigned int i;
761 int expired_entries = 0, res = -1;
762 uint32_t row_idx;
763
764 if (statcache_table == NULL) {
765 errno = EPERM;
766 return -1;
767 }
768
769 row_idx = hash % statcache_nrows;
770 /* Find the matching entry for this path. */
771 for (i = 0; i < STATCACHE_COLS_PER_ROW; i++) {
772 uint32_t col_start;
773 struct statcache_entry *sce;
774
775 pr_signals_handle();
776
777 col_start = row_idx + i;
778 sce = ((struct statcache_entry *) statcache_table_data) + col_start;
779 if (sce->sce_ts > 0) {
780 if (sce->sce_hash == hash) {
781 /* Possible collision; check paths. */
782 if (sce->sce_pathlen == pathlen) {
783
784 /* Include the trailing NUL in the comparison... */
785 if (strncmp(sce->sce_path, path, pathlen + 1) == 0) {
786 time_t now;
787
788 now = time(NULL);
789
790 /* Check the age. If it's aged out, clear it now, for later use. */
791 if (sce->sce_errno == 0) {
792 if (now > (sce->sce_ts + statcache_max_positive_age)) {
793 pr_trace_msg(trace_channel, 17,
794 "clearing expired cache entry for path '%s' (hash %lu) "
795 "at row %lu, col %u: aged %lu secs",
796 sce->sce_path, (unsigned long) hash,
797 (unsigned long) row_idx + 1, i + 1,
798 (unsigned long) (now - sce->sce_ts));
799 sce->sce_ts = 0;
800 expired_entries++;
801 continue;
802 }
803
804 } else {
805 if (now > (sce->sce_ts + statcache_max_negative_age)) {
806 pr_trace_msg(trace_channel, 17,
807 "clearing expired negative cache entry for path '%s' "
808 "(hash %lu) at row %lu, col %u: aged %lu secs",
809 sce->sce_path, (unsigned long) hash,
810 (unsigned long) row_idx + 1, i + 1,
811 (unsigned long) (now - sce->sce_ts));
812 sce->sce_ts = 0;
813 expired_entries++;
814 continue;
815 }
816 }
817
818 /* If the ops match, OR if the entry is from a LSTAT AND the entry
819 * is NOT a symlink, we can use it.
820 */
821 if (sce->sce_op == op ||
822 (sce->sce_op == FSIO_FILE_LSTAT &&
823 S_ISLNK(sce->sce_stat.st_mode) == FALSE)) {
824 /* Found matching entry. */
825 pr_trace_msg(trace_channel, 9,
826 "found entry for path '%s' (hash %lu) at row %lu, col %u",
827 path, (unsigned long) hash, (unsigned long) row_idx + 1, i + 1);
828
829 *xerrno = sce->sce_errno;
830 if (sce->sce_errno == 0) {
831 memcpy(st, &(sce->sce_stat), sizeof(struct stat));
832 }
833
834 res = 0;
835 break;
836 }
837 }
838 }
839 }
840 }
841 }
842
843 if (statcache_wlock_stats(fd) < 0) {
844 pr_trace_msg(trace_channel, 3,
845 "error write-locking shared memory: %s", strerror(errno));
846 }
847
848 if (res == 0) {
849 statcache_stats_incr_hits(1);
850
851 } else {
852 statcache_stats_incr_misses(1);
853 }
854
855 if (expired_entries > 0) {
856 statcache_stats_decr_count(expired_entries);
857 statcache_stats_incr_expires(expired_entries);
858 }
859
860 if (statcache_unlock_stats(fd) < 0) {
861 pr_trace_msg(trace_channel, 3,
862 "error un-locking shared memory: %s", strerror(errno));
863 }
864
865 if (res < 0) {
866 errno = ENOENT;
867 }
868
869 return res;
870 }
871
872 static int statcache_table_remove(int fd, const char *path, size_t pathlen,
873 uint32_t hash) {
874 register unsigned int i;
875 uint32_t row_idx;
876 int removed_entries = 0, res = -1;
877
878 if (statcache_table == NULL) {
879 errno = EPERM;
880 return -1;
881 }
882
883 row_idx = hash % statcache_nrows;
884
885 /* Find the matching entry for this path. */
886 for (i = 0; i < STATCACHE_COLS_PER_ROW; i++) {
887 uint32_t col_start;
888 struct statcache_entry *sce;
889
890 pr_signals_handle();
891
892 col_start = row_idx + i;
893 sce = ((struct statcache_entry *) statcache_table_data) + col_start;
894 if (sce->sce_ts > 0) {
895 if (sce->sce_hash == hash) {
896 /* Possible collision; check paths. */
897 if (sce->sce_pathlen == pathlen) {
898
899 /* Include the trailing NUL in the comparison... */
900 if (strncmp(sce->sce_path, path, pathlen + 1) == 0) {
901 /* Found matching entry. Clear it by zeroing timestamp field. */
902
903 pr_trace_msg(trace_channel, 9,
904 "removing entry for path '%s' (hash %lu) at row %lu, col %u",
905 path, (unsigned long) hash, (unsigned long) row_idx + 1, i + 1);
906
907 sce->sce_ts = 0;
908 removed_entries++;
909 res = 0;
910
911 /* Rather than returning now, we finish iterating through
912 * the bucket, in order to clear out multiple entries for
913 * the same path (e.g. one for LSTAT, and another for STAT).
914 */
915 }
916 }
917 }
918 }
919 }
920
921 if (res == 0) {
922 if (statcache_wlock_stats(fd) < 0) {
923 pr_trace_msg(trace_channel, 3,
924 "error write-locking shared memory: %s", strerror(errno));
925 }
926
927 if (removed_entries > 0) {
928 statcache_stats_decr_count(removed_entries);
929 }
930
931 if (statcache_unlock_stats(fd) < 0) {
932 pr_trace_msg(trace_channel, 3,
933 "error un-locking shared memory: %s", strerror(errno));
934 }
935
936 } else {
937 errno = ENOENT;
938 }
939
940 return res;
941 }
942
943 static const char *statcache_get_canon_path(pool *p, const char *path,
944 size_t *pathlen) {
945 int res;
946 char *canon_path = NULL, *interp_path = NULL;
947 size_t canon_pathlen = PR_TUNABLE_PATH_MAX + 1;
948
949 /* Handle any '~' interpolation needed. */
950 interp_path = dir_interpolate(p, path);
951 if (interp_path == NULL) {
952 /* This happens when the '~' was just that, and did NOT refer to
953 * any known user.
954 */
955 interp_path = (char *) path;
956 }
957
958 canon_path = palloc(p, canon_pathlen);
959 res = pr_fs_dircat(canon_path, canon_pathlen, pr_fs_getcwd(), interp_path);
960 if (res < 0) {
961 errno = ENOMEM;
962 return NULL;
963 }
964
965 *pathlen = strlen(canon_path);
966 return canon_path;
967 }
968
969 /* FSIO callbacks
970 */
971
972 static int statcache_fsio_stat(pr_fs_t *fs, const char *path,
973 struct stat *st) {
974 int res, tab_fd, xerrno = 0;
975 const char *canon_path = NULL;
976 size_t canon_pathlen = 0;
977 pool *p;
978 uint32_t hash;
979
980 p = make_sub_pool(statcache_pool);
981 pr_pool_tag(p, "statcache_fsio_stat sub-pool");
982 canon_path = statcache_get_canon_path(p, path, &canon_pathlen);
983 if (canon_path == NULL) {
984 xerrno = errno;
985
986 destroy_pool(p);
987 errno = xerrno;
988 return -1;
989 }
990
991 hash = statcache_hash(canon_path, canon_pathlen);
992 tab_fd = statcache_tabfh->fh_fd;
993
994 if (statcache_wlock_row(tab_fd, hash) < 0) {
995 pr_trace_msg(trace_channel, 3,
996 "error write-locking shared memory: %s", strerror(errno));
997 }
998
999 res = statcache_table_get(tab_fd, canon_path, canon_pathlen, st, &xerrno,
1000 hash, FSIO_FILE_STAT);
1001
1002 if (statcache_unlock_row(tab_fd, hash) < 0) {
1003 pr_trace_msg(trace_channel, 3,
1004 "error unlocking shared memory: %s", strerror(errno));
1005 }
1006
1007 if (res == 0) {
1008 if (xerrno != 0) {
1009 res = -1;
1010
1011 } else {
1012 pr_trace_msg(trace_channel, 11,
1013 "using cached stat for path '%s'", canon_path);
1014 }
1015
1016 destroy_pool(p);
1017 errno = xerrno;
1018 return res;
1019 }
1020
1021 res = stat(path, st);
1022 xerrno = errno;
1023
1024 if (statcache_wlock_row(tab_fd, hash) < 0) {
1025 pr_trace_msg(trace_channel, 3,
1026 "error write-locking shared memory: %s", strerror(errno));
1027 }
1028
1029 if (statcache_wlock_row(tab_fd, hash) < 0) {
1030 pr_trace_msg(trace_channel, 3,
1031 "error write-locking shared memory: %s", strerror(errno));
1032 }
1033
1034 if (res < 0) {
1035 if (statcache_max_negative_age > 0) {
1036 /* Negatively cache the failed stat(2). */
1037 if (statcache_table_add(tab_fd, canon_path, canon_pathlen, NULL, xerrno,
1038 hash, FSIO_FILE_STAT) < 0) {
1039 pr_trace_msg(trace_channel, 3, "error adding entry for path '%s': %s",
1040 canon_path, strerror(errno));
1041 }
1042 }
1043
1044 } else {
1045 if (statcache_table_add(tab_fd, canon_path, canon_pathlen, st, 0, hash,
1046 FSIO_FILE_STAT) < 0) {
1047 pr_trace_msg(trace_channel, 3, "error adding entry for path '%s': %s",
1048 canon_path, strerror(errno));
1049 }
1050 }
1051
1052 if (statcache_unlock_row(tab_fd, hash) < 0) {
1053 pr_trace_msg(trace_channel, 3,
1054 "error unlocking shared memory: %s", strerror(errno));
1055 }
1056
1057 destroy_pool(p);
1058 errno = xerrno;
1059 return res;
1060 }
1061
1062 static int statcache_fsio_fstat(pr_fh_t *fh, int fd, struct stat *st) {
1063 int res, tab_fd, xerrno = 0;
1064 size_t pathlen = 0;
1065 uint32_t hash;
1066
1067 /* XXX Core FSIO API should have an fh_pathlen member.
1068 *
1069 * XXX Core FSIO API should have an fh_notes table, so that e.g.
1070 * mod_statcache could generate its hash for this handle only once, and
1071 * stash it in the table.
1072 */
1073
1074 pathlen = strlen(fh->fh_path);
1075 hash = statcache_hash(fh->fh_path, pathlen);
1076 tab_fd = statcache_tabfh->fh_fd;
1077
1078 if (statcache_wlock_row(tab_fd, hash) < 0) {
1079 pr_trace_msg(trace_channel, 3,
1080 "error write-locking shared memory: %s", strerror(errno));
1081 }
1082
1083 res = statcache_table_get(tab_fd, fh->fh_path, pathlen, st, &xerrno, hash,
1084 FSIO_FILE_STAT);
1085
1086 if (statcache_unlock_row(tab_fd, hash) < 0) {
1087 pr_trace_msg(trace_channel, 3,
1088 "error unlocking shared memory: %s", strerror(errno));
1089 }
1090
1091 if (res == 0) {
1092 if (xerrno != 0) {
1093 res = -1;
1094
1095 } else {
1096 pr_trace_msg(trace_channel, 11,
1097 "using cached stat for path '%s'", fh->fh_path);
1098 }
1099
1100 errno = xerrno;
1101 return res;
1102 }
1103
1104 res = fstat(fd, st);
1105 xerrno = errno;
1106
1107 if (statcache_wlock_row(tab_fd, hash) < 0) {
1108 pr_trace_msg(trace_channel, 3,
1109 "error write-locking shared memory: %s", strerror(errno));
1110 }
1111
1112 if (res < 0) {
1113 if (statcache_max_negative_age > 0) {
1114 /* Negatively cache the failed fstat(2). */
1115 if (statcache_table_add(tab_fd, fh->fh_path, pathlen, NULL, xerrno,
1116 hash, FSIO_FILE_STAT) < 0) {
1117 pr_trace_msg(trace_channel, 3, "error adding entry for path '%s': %s",
1118 fh->fh_path, strerror(errno));
1119 }
1120 }
1121
1122 } else {
1123 if (statcache_table_add(tab_fd, fh->fh_path, pathlen, st, 0, hash,
1124 FSIO_FILE_STAT) < 0) {
1125 pr_trace_msg(trace_channel, 3, "error adding entry for path '%s': %s",
1126 fh->fh_path, strerror(errno));
1127 }
1128 }
1129
1130 if (statcache_unlock_row(tab_fd, hash) < 0) {
1131 pr_trace_msg(trace_channel, 3,
1132 "error unlocking shared memory: %s", strerror(errno));
1133 }
1134
1135 errno = xerrno;
1136 return res;
1137 }
1138
1139 static int statcache_fsio_lstat(pr_fs_t *fs, const char *path,
1140 struct stat *st) {
1141 int res, tab_fd, xerrno = 0;
1142 const char *canon_path = NULL;
1143 size_t canon_pathlen = 0;
1144 pool *p;
1145 uint32_t hash;
1146
1147 p = make_sub_pool(statcache_pool);
1148 pr_pool_tag(p, "statcache_fsio_lstat sub-pool");
1149 canon_path = statcache_get_canon_path(p, path, &canon_pathlen);
1150 if (canon_path == NULL) {
1151 xerrno = errno;
1152
1153 destroy_pool(p);
1154 errno = xerrno;
1155 return -1;
1156 }
1157
1158 hash = statcache_hash(canon_path, canon_pathlen);
1159 tab_fd = statcache_tabfh->fh_fd;
1160
1161 if (statcache_wlock_row(tab_fd, hash) < 0) {
1162 pr_trace_msg(trace_channel, 3,
1163 "error write-locking shared memory: %s", strerror(errno));
1164 }
1165
1166 res = statcache_table_get(tab_fd, canon_path, canon_pathlen, st, &xerrno,
1167 hash, FSIO_FILE_LSTAT);
1168
1169 if (statcache_unlock_row(tab_fd, hash) < 0) {
1170 pr_trace_msg(trace_channel, 3,
1171 "error unlocking shared memory: %s", strerror(errno));
1172 }
1173
1174 if (res == 0) {
1175 if (xerrno != 0) {
1176 res = -1;
1177
1178 } else {
1179 pr_trace_msg(trace_channel, 11,
1180 "using cached lstat for path '%s'", canon_path);
1181 }
1182
1183 destroy_pool(p);
1184 errno = xerrno;
1185 return res;
1186 }
1187
1188 res = lstat(path, st);
1189 xerrno = errno;
1190
1191 if (statcache_wlock_row(tab_fd, hash) < 0) {
1192 pr_trace_msg(trace_channel, 3,
1193 "error write-locking shared memory: %s", strerror(errno));
1194 }
1195
1196 if (res < 0) {
1197 if (statcache_max_negative_age > 0) {
1198 /* Negatively cache the failed lstat(2). */
1199 if (statcache_table_add(tab_fd, canon_path, canon_pathlen, NULL, xerrno,
1200 hash, FSIO_FILE_LSTAT) < 0) {
1201 pr_trace_msg(trace_channel, 3, "error adding entry for path '%s': %s",
1202 canon_path, strerror(errno));
1203 }
1204 }
1205
1206 } else {
1207 if (statcache_table_add(tab_fd, canon_path, canon_pathlen, st, 0, hash,
1208 FSIO_FILE_LSTAT) < 0) {
1209 pr_trace_msg(trace_channel, 3, "error adding entry for path '%s': %s",
1210 canon_path, strerror(errno));
1211 }
1212 }
1213
1214 if (statcache_unlock_row(tab_fd, hash) < 0) {
1215 pr_trace_msg(trace_channel, 3,
1216 "error unlocking shared memory: %s", strerror(errno));
1217 }
1218
1219 destroy_pool(p);
1220 errno = xerrno;
1221 return res;
1222 }
1223
1224 static int statcache_fsio_rename(pr_fs_t *fs, const char *rnfm,
1225 const char *rnto) {
1226 int res, xerrno;
1227
1228 res = rename(rnfm, rnto);
1229 xerrno = errno;
1230
1231 if (res == 0) {
1232 int tab_fd;
1233 const char *canon_rnfm = NULL, *canon_rnto = NULL;
1234 size_t canon_rnfmlen = 0, canon_rntolen = 0;
1235 pool *p;
1236 uint32_t hash_rnfm, hash_rnto;
1237
1238 p = make_sub_pool(statcache_pool);
1239 pr_pool_tag(p, "statcache_fsio_rename sub-pool");
1240
1241 canon_rnfm = statcache_get_canon_path(p, rnfm, &canon_rnfmlen);
1242 if (canon_rnfm == NULL) {
1243 xerrno = errno;
1244
1245 destroy_pool(p);
1246 errno = xerrno;
1247 return res;
1248 }
1249
1250 canon_rnto = statcache_get_canon_path(p, rnto, &canon_rntolen);
1251 if (canon_rnto == NULL) {
1252 xerrno = errno;
1253
1254 destroy_pool(p);
1255 errno = xerrno;
1256 return res;
1257 }
1258
1259 hash_rnfm = statcache_hash(canon_rnfm, canon_rnfmlen);
1260 hash_rnto = statcache_hash(canon_rnto, canon_rntolen);
1261 tab_fd = statcache_tabfh->fh_fd;
1262
1263 if (statcache_wlock_row(tab_fd, hash_rnfm) < 0) {
1264 pr_trace_msg(trace_channel, 3,
1265 "error write-locking shared memory: %s", strerror(errno));
1266 }
1267
1268 (void) statcache_table_remove(tab_fd, canon_rnfm, canon_rnfmlen, hash_rnfm);
1269
1270 if (statcache_unlock_row(tab_fd, hash_rnfm) < 0) {
1271 pr_trace_msg(trace_channel, 3,
1272 "error unlocking shared memory: %s", strerror(errno));
1273 }
1274
1275 if (statcache_wlock_row(tab_fd, hash_rnto) < 0) {
1276 pr_trace_msg(trace_channel, 3,
1277 "error write-locking shared memory: %s", strerror(errno));
1278 }
1279
1280 (void) statcache_table_remove(tab_fd, canon_rnto, canon_rntolen, hash_rnto);
1281
1282 if (statcache_unlock_row(tab_fd, hash_rnto) < 0) {
1283 pr_trace_msg(trace_channel, 3,
1284 "error unlocking shared memory: %s", strerror(errno));
1285 }
1286
1287 destroy_pool(p);
1288 }
1289
1290 errno = xerrno;
1291 return res;
1292 }
1293
1294 static int statcache_fsio_unlink(pr_fs_t *fs, const char *path) {
1295 int res, xerrno;
1296
1297 res = unlink(path);
1298 xerrno = errno;
1299
1300 if (res == 0) {
1301 int tab_fd;
1302 const char *canon_path = NULL;
1303 size_t canon_pathlen = 0;
1304 pool *p;
1305 uint32_t hash;
1306
1307 p = make_sub_pool(statcache_pool);
1308 pr_pool_tag(p, "statcache_fsio_unlink sub-pool");
1309 canon_path = statcache_get_canon_path(p, path, &canon_pathlen);
1310 if (canon_path == NULL) {
1311 xerrno = errno;
1312
1313 destroy_pool(p);
1314 errno = xerrno;
1315 return res;
1316 }
1317
1318 hash = statcache_hash(canon_path, canon_pathlen);
1319 tab_fd = statcache_tabfh->fh_fd;
1320
1321 if (statcache_wlock_row(tab_fd, hash) < 0) {
1322 pr_trace_msg(trace_channel, 3,
1323 "error write-locking shared memory: %s", strerror(errno));
1324 }
1325
1326 (void) statcache_table_remove(tab_fd, canon_path, canon_pathlen, hash);
1327
1328 if (statcache_unlock_row(tab_fd, hash) < 0) {
1329 pr_trace_msg(trace_channel, 3,
1330 "error unlocking shared memory: %s", strerror(errno));
1331 }
1332
1333 destroy_pool(p);
1334 }
1335
1336 errno = xerrno;
1337 return res;
1338 }
1339
1340 static int statcache_fsio_open(pr_fh_t *fh, const char *path, int flags) {
1341 int res, xerrno;
1342
1343 res = open(path, flags, PR_OPEN_MODE);
1344 xerrno = errno;
1345
1346 if (res >= 0) {
1347 /* Clear the cache for this patch, but only if O_CREAT or O_TRUNC are
1348 * present.
1349 */
1350 if ((flags & O_CREAT) ||
1351 (flags & O_TRUNC)) {
1352 int tab_fd;
1353 const char *canon_path = NULL;
1354 size_t canon_pathlen = 0;
1355 pool *p;
1356 uint32_t hash;
1357
1358 p = make_sub_pool(statcache_pool);
1359 pr_pool_tag(p, "statcache_fsio_open sub-pool");
1360 canon_path = statcache_get_canon_path(p, path, &canon_pathlen);
1361 if (canon_path == NULL) {
1362 xerrno = errno;
1363
1364 destroy_pool(p);
1365 errno = xerrno;
1366 return res;
1367 }
1368
1369 hash = statcache_hash(canon_path, canon_pathlen);
1370 tab_fd = statcache_tabfh->fh_fd;
1371
1372 if (statcache_wlock_row(tab_fd, hash) < 0) {
1373 pr_trace_msg(trace_channel, 3,
1374 "error write-locking shared memory: %s", strerror(errno));
1375 }
1376
1377 pr_trace_msg(trace_channel, 14,
1378 "removing entry for path '%s' due to open(2) flags", canon_path);
1379 (void) statcache_table_remove(tab_fd, canon_path, canon_pathlen, hash);
1380
1381 if (statcache_unlock_row(tab_fd, hash) < 0) {
1382 pr_trace_msg(trace_channel, 3,
1383 "error unlocking shared memory: %s", strerror(errno));
1384 }
1385
1386 destroy_pool(p);
1387 }
1388 }
1389
1390 errno = xerrno;
1391 return res;
1392 }
1393
1394 static int statcache_fsio_write(pr_fh_t *fh, int fd, const char *buf,
1395 size_t buflen) {
1396 int res, xerrno;
1397
1398 res = write(fd, buf, buflen);
1399 xerrno = errno;
1400
1401 if (res > 0) {
1402 int tab_fd;
1403 size_t pathlen = 0;
1404 uint32_t hash;
1405
1406 pathlen = strlen(fh->fh_path);
1407 hash = statcache_hash(fh->fh_path, pathlen);
1408 tab_fd = statcache_tabfh->fh_fd;
1409
1410 if (statcache_wlock_row(tab_fd, hash) < 0) {
1411 pr_trace_msg(trace_channel, 3,
1412 "error write-locking shared memory: %s", strerror(errno));
1413 }
1414
1415 (void) statcache_table_remove(tab_fd, fh->fh_path, pathlen, hash);
1416
1417 if (statcache_unlock_row(tab_fd, hash) < 0) {
1418 pr_trace_msg(trace_channel, 3,
1419 "error unlocking shared memory: %s", strerror(errno));
1420 }
1421 }
1422
1423 errno = xerrno;
1424 return res;
1425 }
1426
1427 static int statcache_fsio_truncate(pr_fs_t *fs, const char *path, off_t len) {
1428 int res, xerrno;
1429
1430 res = truncate(path, len);
1431 xerrno = errno;
1432
1433 if (res == 0) {
1434 int tab_fd;
1435 const char *canon_path = NULL;
1436 size_t canon_pathlen = 0;
1437 pool *p;
1438 uint32_t hash;
1439
1440 p = make_sub_pool(statcache_pool);
1441 pr_pool_tag(p, "statcache_fsio_truncate sub-pool");
1442 canon_path = statcache_get_canon_path(p, path, &canon_pathlen);
1443 if (canon_path == NULL) {
1444 xerrno = errno;
1445
1446 destroy_pool(p);
1447 errno = xerrno;
1448 return res;
1449 }
1450
1451 hash = statcache_hash(canon_path, canon_pathlen);
1452 tab_fd = statcache_tabfh->fh_fd;
1453
1454 if (statcache_wlock_row(tab_fd, hash) < 0) {
1455 pr_trace_msg(trace_channel, 3,
1456 "error write-locking shared memory: %s", strerror(errno));
1457 }
1458
1459 (void) statcache_table_remove(tab_fd, canon_path, canon_pathlen, hash);
1460
1461 if (statcache_unlock_row(tab_fd, hash) < 0) {
1462 pr_trace_msg(trace_channel, 3,
1463 "error unlocking shared memory: %s", strerror(errno));
1464 }
1465
1466 destroy_pool(p);
1467 }
1468
1469 errno = xerrno;
1470 return res;
1471 }
1472
1473 static int statcache_fsio_ftruncate(pr_fh_t *fh, int fd, off_t len) {
1474 int res, xerrno;
1475
1476 res = ftruncate(fd, len);
1477 xerrno = errno;
1478
1479 if (res == 0) {
1480 int tab_fd;
1481 size_t pathlen = 0;
1482 uint32_t hash;
1483
1484 pathlen = strlen(fh->fh_path);
1485 hash = statcache_hash(fh->fh_path, pathlen);
1486 tab_fd = statcache_tabfh->fh_fd;
1487
1488 if (statcache_wlock_row(tab_fd, hash) < 0) {
1489 pr_trace_msg(trace_channel, 3,
1490 "error write-locking shared memory: %s", strerror(errno));
1491 }
1492
1493 (void) statcache_table_remove(tab_fd, fh->fh_path, pathlen, hash);
1494
1495 if (statcache_unlock_row(tab_fd, hash) < 0) {
1496 pr_trace_msg(trace_channel, 3,
1497 "error unlocking shared memory: %s", strerror(errno));
1498 }
1499 }
1500
1501 errno = xerrno;
1502 return res;
1503 }
1504
1505 static int statcache_fsio_chmod(pr_fs_t *fs, const char *path, mode_t mode) {
1506 int res, xerrno;
1507
1508 res = chmod(path, mode);
1509 xerrno = errno;
1510
1511 if (res == 0) {
1512 int tab_fd;
1513 const char *canon_path = NULL;
1514 size_t canon_pathlen = 0;
1515 pool *p;
1516 uint32_t hash;
1517
1518 p = make_sub_pool(statcache_pool);
1519 pr_pool_tag(p, "statcache_fsio_chmod sub-pool");
1520 canon_path = statcache_get_canon_path(p, path, &canon_pathlen);
1521 if (canon_path == NULL) {
1522 xerrno = errno;
1523
1524 destroy_pool(p);
1525 errno = xerrno;
1526 return res;
1527 }
1528
1529 hash = statcache_hash(canon_path, canon_pathlen);
1530 tab_fd = statcache_tabfh->fh_fd;
1531
1532 if (statcache_wlock_row(tab_fd, hash) < 0) {
1533 pr_trace_msg(trace_channel, 3,
1534 "error write-locking shared memory: %s", strerror(errno));
1535 }
1536
1537 (void) statcache_table_remove(tab_fd, canon_path, canon_pathlen, hash);
1538
1539 if (statcache_unlock_row(tab_fd, hash) < 0) {
1540 pr_trace_msg(trace_channel, 3,
1541 "error unlocking shared memory: %s", strerror(errno));
1542 }
1543
1544 destroy_pool(p);
1545 }
1546
1547 errno = xerrno;
1548 return res;
1549 }
1550
1551 static int statcache_fsio_fchmod(pr_fh_t *fh, int fd, mode_t mode) {
1552 int res, xerrno;
1553
1554 res = fchmod(fd, mode);
1555 xerrno = errno;
1556
1557 if (res == 0) {
1558 int tab_fd;
1559 size_t pathlen = 0;
1560 uint32_t hash;
1561
1562 pathlen = strlen(fh->fh_path);
1563 hash = statcache_hash(fh->fh_path, pathlen);
1564 tab_fd = statcache_tabfh->fh_fd;
1565
1566 if (statcache_wlock_row(tab_fd, hash) < 0) {
1567 pr_trace_msg(trace_channel, 3,
1568 "error write-locking shared memory: %s", strerror(errno));
1569 }
1570
1571 (void) statcache_table_remove(tab_fd, fh->fh_path, pathlen, hash);
1572
1573 if (statcache_unlock_row(tab_fd, hash) < 0) {
1574 pr_trace_msg(trace_channel, 3,
1575 "error unlocking shared memory: %s", strerror(errno));
1576 }
1577 }
1578
1579 errno = xerrno;
1580 return res;
1581 }
1582
1583 static int statcache_fsio_chown(pr_fs_t *fs, const char *path, uid_t uid,
1584 gid_t gid) {
1585 int res, xerrno;
1586
1587 res = chown(path, uid, gid);
1588 xerrno = errno;
1589
1590 if (res == 0) {
1591 int tab_fd;
1592 const char *canon_path = NULL;
1593 size_t canon_pathlen = 0;
1594 pool *p;
1595 uint32_t hash;
1596
1597 p = make_sub_pool(statcache_pool);
1598 pr_pool_tag(p, "statcache_fsio_chown sub-pool");
1599 canon_path = statcache_get_canon_path(p, path, &canon_pathlen);
1600 if (canon_path == NULL) {
1601 xerrno = errno;
1602
1603 destroy_pool(p);
1604 errno = xerrno;
1605 return res;
1606 }
1607
1608 hash = statcache_hash(canon_path, canon_pathlen);
1609 tab_fd = statcache_tabfh->fh_fd;
1610
1611 if (statcache_wlock_row(tab_fd, hash) < 0) {
1612 pr_trace_msg(trace_channel, 3,
1613 "error write-locking shared memory: %s", strerror(errno));
1614 }
1615
1616 (void) statcache_table_remove(tab_fd, canon_path, canon_pathlen, hash);
1617
1618 if (statcache_unlock_row(tab_fd, hash) < 0) {
1619 pr_trace_msg(trace_channel, 3,
1620 "error unlocking shared memory: %s", strerror(errno));
1621 }
1622
1623 destroy_pool(p);
1624 }
1625
1626 errno = xerrno;
1627 return res;
1628 }
1629
1630 static int statcache_fsio_fchown(pr_fh_t *fh, int fd, uid_t uid, gid_t gid) {
1631 int res, xerrno;
1632
1633 res = fchown(fd, uid, gid);
1634 xerrno = errno;
1635
1636 if (res == 0) {
1637 int tab_fd;
1638 size_t pathlen = 0;
1639 uint32_t hash;
1640
1641 pathlen = strlen(fh->fh_path);
1642 hash = statcache_hash(fh->fh_path, pathlen);
1643 tab_fd = statcache_tabfh->fh_fd;
1644
1645 if (statcache_wlock_row(tab_fd, hash) < 0) {
1646 pr_trace_msg(trace_channel, 3,
1647 "error write-locking shared memory: %s", strerror(errno));
1648 }
1649
1650 (void) statcache_table_remove(tab_fd, fh->fh_path, pathlen, hash);
1651
1652 if (statcache_unlock_row(tab_fd, hash) < 0) {
1653 pr_trace_msg(trace_channel, 3,
1654 "error unlocking shared memory: %s", strerror(errno));
1655 }
1656 }
1657
1658 errno = xerrno;
1659 return res;
1660 }
1661
1662 #if PROFTPD_VERSION_NUMBER >= 0x0001030407
1663 static int statcache_fsio_lchown(pr_fs_t *fs, const char *path, uid_t uid,
1664 gid_t gid) {
1665 int res, xerrno;
1666
1667 res = lchown(path, uid, gid);
1668 xerrno = errno;
1669
1670 if (res == 0) {
1671 int tab_fd;
1672 const char *canon_path = NULL;
1673 size_t canon_pathlen = 0;
1674 pool *p;
1675 uint32_t hash;
1676
1677 p = make_sub_pool(statcache_pool);
1678 pr_pool_tag(p, "statcache_fsio_lchown sub-pool");
1679 canon_path = statcache_get_canon_path(p, path, &canon_pathlen);
1680 if (canon_path == NULL) {
1681 xerrno = errno;
1682
1683 destroy_pool(p);
1684 errno = xerrno;
1685 return res;
1686 }
1687
1688 hash = statcache_hash(canon_path, canon_pathlen);
1689 tab_fd = statcache_tabfh->fh_fd;
1690
1691 if (statcache_wlock_row(tab_fd, hash) < 0) {
1692 pr_trace_msg(trace_channel, 3,
1693 "error write-locking shared memory: %s", strerror(errno));
1694 }
1695
1696 (void) statcache_table_remove(tab_fd, canon_path, canon_pathlen, hash);
1697
1698 if (statcache_unlock_row(tab_fd, hash) < 0) {
1699 pr_trace_msg(trace_channel, 3,
1700 "error unlocking shared memory: %s", strerror(errno));
1701 }
1702
1703 destroy_pool(p);
1704 }
1705
1706 errno = xerrno;
1707 return res;
1708 }
1709 #endif /* ProFTPD 1.3.4c or later */
1710
1711 static int statcache_fsio_utimes(pr_fs_t *fs, const char *path,
1712 struct timeval *tvs) {
1713 int res, xerrno;
1714
1715 res = utimes(path, tvs);
1716 xerrno = errno;
1717
1718 if (res == 0) {
1719 int tab_fd;
1720 const char *canon_path = NULL;
1721 size_t canon_pathlen = 0;
1722 pool *p;
1723 uint32_t hash;
1724
1725 p = make_sub_pool(statcache_pool);
1726 pr_pool_tag(p, "statcache_fsio_utimes sub-pool");
1727 canon_path = statcache_get_canon_path(p, path, &canon_pathlen);
1728 if (canon_path == NULL) {
1729 xerrno = errno;
1730
1731 destroy_pool(p);
1732 errno = xerrno;
1733 return res;
1734 }
1735
1736 hash = statcache_hash(canon_path, canon_pathlen);
1737 tab_fd = statcache_tabfh->fh_fd;
1738
1739 if (statcache_wlock_row(tab_fd, hash) < 0) {
1740 pr_trace_msg(trace_channel, 3,
1741 "error write-locking shared memory: %s", strerror(errno));
1742 }
1743
1744 (void) statcache_table_remove(tab_fd, canon_path, canon_pathlen, hash);
1745
1746 if (statcache_unlock_row(tab_fd, hash) < 0) {
1747 pr_trace_msg(trace_channel, 3,
1748 "error unlocking shared memory: %s", strerror(errno));
1749 }
1750
1751 destroy_pool(p);
1752 }
1753
1754 errno = xerrno;
1755 return res;
1756 }
1757
1758 static int statcache_fsio_futimes(pr_fh_t *fh, int fd, struct timeval *tvs) {
1759 #if defined(HAVE_FUTIMES)
1760 int res, xerrno;
1761
1762 /* Check for an ENOSYS errno; if so, fallback to using fsio_utimes. Some
1763 * platforms will provide a futimes(2) stub which does not actually do
1764 * anything.
1765 */
1766 res = futimes(fd, tvs);
1767 xerrno = errno;
1768
1769 if (res < 0 &&
1770 xerrno == ENOSYS) {
1771 return statcache_fsio_utimes(fh->fh_fs, fh->fh_path, tvs);
1772 }
1773
1774 if (res == 0) {
1775 int tab_fd;
1776 size_t pathlen = 0;
1777 uint32_t hash;
1778
1779 pathlen = strlen(fh->fh_path);
1780 hash = statcache_hash(fh->fh_path, pathlen);
1781 tab_fd = statcache_tabfh->fh_fd;
1782
1783 if (statcache_wlock_row(tab_fd, hash) < 0) {
1784 pr_trace_msg(trace_channel, 3,
1785 "error write-locking shared memory: %s", strerror(errno));
1786 }
1787
1788 (void) statcache_table_remove(tab_fd, fh->fh_path, pathlen, hash);
1789
1790 if (statcache_unlock_row(tab_fd, hash) < 0) {
1791 pr_trace_msg(trace_channel, 3,
1792 "error unlocking shared memory: %s", strerror(errno));
1793 }
1794 }
1795
1796 errno = xerrno;
1797 return res;
1798 #else
1799 return statcache_fsio_utimes(fh->fh_fs, fh->fh_path, tvs);
1800 #endif /* HAVE_FUTIMES */
1801 }
1802
1803 #if defined(PR_USE_CTRLS)
1804 /* Controls handlers
1805 */
1806
1807 static int statcache_handle_statcache(pr_ctrls_t *ctrl, int reqargc,
1808 char **reqargv) {
1809 /* Check the ban ACL */
1810 if (pr_ctrls_check_acl(ctrl, statcache_acttab, "statcache") != TRUE) {
1811
1812 /* Access denied */
1813 pr_ctrls_add_response(ctrl, "access denied");
1814 return PR_CTRLS_STATUS_ACCESS_DENIED;
1815 }
1816
1817 /* Sanity check */
1818 if (reqargv == NULL) {
1819 pr_ctrls_add_response(ctrl, "missing parameters");
1820 return PR_CTRLS_STATUS_WRONG_PARAMETERS;
1821 }
1822
1823 if (statcache_engine != TRUE) {
1824 pr_ctrls_add_response(ctrl, MOD_STATCACHE_VERSION " not enabled");
1825 return PR_CTRLS_STATUS_UNSUPPORTED_OPERATION;
1826 }
1827
1828 /* Check for options. */
1829 pr_getopt_reset();
1830
1831 if (strcmp(reqargv[0], "info") == 0) {
1832 uint32_t count, highest, hits, misses, expires, rejects;
1833 float current_usage = 0.0, highest_usage = 0.0, hit_rate = 0.0;
1834
1835 if (statcache_rlock_stats(statcache_tabfh->fh_fd) < 0) {
1836 pr_ctrls_add_response(ctrl, "error locking shared memory: %s",
1837 strerror(errno));
1838 return PR_CTRLS_STATUS_INTERNAL_ERROR;
1839 }
1840
1841 count = statcache_stats_get_count();
1842 highest = statcache_stats_get_highest();
1843 hits = statcache_stats_get_hits();
1844 misses = statcache_stats_get_misses();
1845 expires = statcache_stats_get_expires();
1846 rejects = statcache_stats_get_rejects();
1847
1848 if (statcache_unlock_stats(statcache_tabfh->fh_fd) < 0) {
1849 pr_trace_msg(trace_channel, 3,
1850 "error un-locking shared memory: %s", strerror(errno));
1851 }
1852
1853 current_usage = (((float) count / (float) statcache_capacity) * 100.0);
1854 highest_usage = (((float) highest / (float) statcache_capacity) * 100.0);
1855 if ((hits + misses) > 0) {
1856 hit_rate = (((float) hits / (float) (hits + misses)) * 100.0);
1857 }
1858
1859 pr_log_debug(DEBUG7, MOD_STATCACHE_VERSION
1860 ": showing statcache statistics");
1861
1862 pr_ctrls_add_response(ctrl,
1863 " hits %lu, misses %lu: %02.1f%% hit rate",
1864 (unsigned long) hits, (unsigned long) misses, hit_rate);
1865 pr_ctrls_add_response(ctrl,
1866 " expires %lu, rejects %lu", (unsigned long) expires,
1867 (unsigned long) rejects);
1868 pr_ctrls_add_response(ctrl, " current count: %lu (of %lu) (%02.1f%% usage)",
1869 (unsigned long) count, (unsigned long) statcache_capacity, current_usage);
1870 pr_ctrls_add_response(ctrl, " highest count: %lu (of %lu) (%02.1f%% usage)",
1871 (unsigned long) highest, (unsigned long) statcache_capacity,
1872 highest_usage);
1873
1874 } else if (strcmp(reqargv[0], "dump") == 0) {
1875 register unsigned int i;
1876 time_t now;
1877
1878 if (statcache_rlock_table(statcache_tabfh->fh_fd) < 0) {
1879 pr_ctrls_add_response(ctrl, "error locking shared memory: %s",
1880 strerror(errno));
1881 return PR_CTRLS_STATUS_INTERNAL_ERROR;
1882 }
1883
1884 pr_log_debug(DEBUG7, MOD_STATCACHE_VERSION ": dumping statcache");
1885
1886 pr_ctrls_add_response(ctrl, "StatCache Contents:");
1887 now = time(NULL);
1888
1889 for (i = 0; i < statcache_nrows; i++) {
1890 register unsigned int j;
1891 unsigned long row_start;
1892
1893 pr_ctrls_add_response(ctrl, " Row %u:", i + 1);
1894 row_start = i;
1895
1896 for (j = 0; j < STATCACHE_COLS_PER_ROW; j++) {
1897 unsigned long col_start;
1898 struct statcache_entry *sce;
1899
1900 pr_signals_handle();
1901
1902 col_start = row_start + j;
1903 sce = ((struct statcache_entry *) statcache_table_data) + col_start;
1904 if (sce->sce_ts > 0) {
1905 if (sce->sce_errno == 0) {
1906 pr_ctrls_add_response(ctrl, " Col %u: '%s' (%u secs old)",
1907 j + 1, sce->sce_path, (unsigned int) (now - sce->sce_ts));
1908
1909 } else {
1910 pr_ctrls_add_response(ctrl, " Col %u: '%s' (error: %s)",
1911 j + 1, sce->sce_path, strerror(sce->sce_errno));
1912 }
1913
1914 } else {
1915 pr_ctrls_add_response(ctrl, " Col %u: <empty>", j + 1);
1916 }
1917 }
1918 }
1919
1920 statcache_unlock_table(statcache_tabfh->fh_fd);
1921
1922 } else {
1923 pr_ctrls_add_response(ctrl, "unknown statcache action requested: '%s'",
1924 reqargv[0]);
1925 return PR_CTRLS_STATUS_UNSUPPORTED_OPERATION;
1926 }
1927
1928 return PR_CTRLS_STATUS_OK;
1929 }
1930 #endif /* PR_USE_CTRLS */
1931
1932 /* Configuration handlers
1933 */
1934
1935 /* usage: StatCacheCapacity count */
1936 MODRET set_statcachecapacity(cmd_rec *cmd) {
1937 int capacity;
1938
1939 CHECK_ARGS(cmd, 1);
1940 CHECK_CONF(cmd, CONF_ROOT);
1941
1942 capacity = atoi(cmd->argv[1]);
1943 if (capacity < STATCACHE_COLS_PER_ROW) {
1944 char str[32];
1945
1946 memset(str, '\0', sizeof(str));
1947 pr_snprintf(str, sizeof(str), "%d", (int) STATCACHE_COLS_PER_ROW);
1948 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "parameter must be ", str,
1949 " or greater", NULL));
1950 }
1951
1952 /* Always round UP to the nearest multiple of STATCACHE_COLS_PER_ROW. */
1953 if (capacity % STATCACHE_COLS_PER_ROW != 0) {
1954 int factor;
1955
1956 factor = (capacity / (int) STATCACHE_COLS_PER_ROW);
1957 capacity = ((factor * (int) STATCACHE_COLS_PER_ROW) +
1958 (int) STATCACHE_COLS_PER_ROW);
1959 }
1960
1961 statcache_capacity = capacity;
1962 return PR_HANDLED(cmd);
1963 }
1964
1965 /* usage: StatCacheControlsACLs actions|all allow|deny user|group list */
1966 MODRET set_statcachectrlsacls(cmd_rec *cmd) {
1967 #if defined(PR_USE_CTRLS)
1968 char *bad_action = NULL, **actions = NULL;
1969
1970 CHECK_ARGS(cmd, 4);
1971 CHECK_CONF(cmd, CONF_ROOT);
1972
1973 actions = pr_ctrls_parse_acl(cmd->tmp_pool, cmd->argv[1]);
1974
1975 /* Check the second parameter to make sure it is "allow" or "deny" */
1976 if (strcmp(cmd->argv[2], "allow") != 0 &&
1977 strcmp(cmd->argv[2], "deny") != 0) {
1978 CONF_ERROR(cmd, "second parameter must be 'allow' or 'deny'");
1979 }
1980
1981 /* Check the third parameter to make sure it is "user" or "group" */
1982 if (strcmp(cmd->argv[3], "user") != 0 &&
1983 strcmp(cmd->argv[3], "group") != 0) {
1984 CONF_ERROR(cmd, "third parameter must be 'user' or 'group'");
1985 }
1986
1987 bad_action = pr_ctrls_set_module_acls(statcache_acttab, statcache_pool,
1988 actions, cmd->argv[2], cmd->argv[3], cmd->argv[4]);
1989 if (bad_action != NULL) {
1990 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown action: '",
1991 bad_action, "'", NULL));
1992 }
1993
1994 return PR_HANDLED(cmd);
1995 #else
1996 CONF_ERROR(cmd, "requires Controls support (use --enable-ctrls)");
1997 #endif /* PR_USE_CTRLS */
1998 }
1999
2000 /* usage: StatCacheEngine on|off */
2001 MODRET set_statcacheengine(cmd_rec *cmd) {
2002 int engine = -1;
2003 config_rec *c;
2004
2005 CHECK_ARGS(cmd, 1);
2006 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2007
2008 engine = get_boolean(cmd, 1);
2009 if (engine == -1) {
2010 CONF_ERROR(cmd, "expected Boolean parameter");
2011 }
2012
2013 statcache_engine = engine;
2014
2015 c = add_config_param(cmd->argv[0], 1, NULL);
2016 c->argv[0] = palloc(c->pool, sizeof(int));
2017 *((int *) c->argv[0]) = engine;
2018
2019 return PR_HANDLED(cmd);
2020 }
2021
2022 /* usage: StatCacheMaxAge secs */
2023 MODRET set_statcachemaxage(cmd_rec *cmd) {
2024 int positive_age;
2025
2026 if (cmd->argc < 2 ||
2027 cmd->argc > 3) {
2028 CONF_ERROR(cmd, "wrong number of parameters");
2029 }
2030
2031 CHECK_CONF(cmd, CONF_ROOT);
2032
2033 positive_age = atoi(cmd->argv[1]);
2034 if (positive_age <= 0) {
2035 CONF_ERROR(cmd, "positive-age parameter must be 1 or greater");
2036 }
2037
2038 if (cmd->argc == 2) {
2039 statcache_max_positive_age = statcache_max_negative_age = positive_age;
2040
2041 } else {
2042 int negative_age;
2043
2044 negative_age = atoi(cmd->argv[2]);
2045 if (negative_age < 0) {
2046 negative_age = 0;
2047 }
2048
2049 statcache_max_positive_age = positive_age;
2050 statcache_max_negative_age = negative_age;
2051 }
2052
2053 return PR_HANDLED(cmd);
2054 }
2055
2056 /* usage: StatCacheTable path */
2057 MODRET set_statcachetable(cmd_rec *cmd) {
2058 CHECK_ARGS(cmd, 1);
2059 CHECK_CONF(cmd, CONF_ROOT);
2060
2061 if (pr_fs_valid_path(cmd->argv[1]) < 0) {
2062 CONF_ERROR(cmd, "must be an absolute path");
2063 }
2064
2065 statcache_table_path = pstrdup(statcache_pool, cmd->argv[1]);
2066 return PR_HANDLED(cmd);
2067 }
2068
2069 /* Command handlers
2070 */
2071
2072 MODRET statcache_post_pass(cmd_rec *cmd) {
2073 pr_fs_t *fs;
2074 const char *proto;
2075
2076 if (statcache_engine == FALSE) {
2077 return PR_DECLINED(cmd);
2078 }
2079
2080 /* Unmount the default/system FS, so that our FS is used for relative
2081 * paths, too.
2082 */
2083 (void) pr_unmount_fs("/", NULL);
2084
2085 fs = pr_register_fs(statcache_pool, "statcache", "/");
2086 if (fs == NULL) {
2087 pr_log_debug(DEBUG3, MOD_STATCACHE_VERSION
2088 ": error registering 'statcache' fs: %s", strerror(errno));
2089 statcache_engine = FALSE;
2090 return PR_DECLINED(cmd);
2091 }
2092
2093 /* Add the module's custom FS callbacks here. */
2094 fs->stat = statcache_fsio_stat;
2095 fs->fstat = statcache_fsio_fstat;
2096 fs->lstat = statcache_fsio_lstat;
2097 fs->rename = statcache_fsio_rename;
2098 fs->unlink = statcache_fsio_unlink;
2099 fs->open = statcache_fsio_open;;
2100 fs->truncate = statcache_fsio_truncate;
2101 fs->ftruncate = statcache_fsio_ftruncate;
2102 fs->write = statcache_fsio_write;
2103 fs->chmod = statcache_fsio_chmod;
2104 fs->fchmod = statcache_fsio_fchmod;
2105 fs->chown = statcache_fsio_chown;
2106 fs->fchown = statcache_fsio_fchown;
2107 #if PROFTPD_VERSION_NUMBER >= 0x0001030407
2108 fs->lchown = statcache_fsio_lchown;
2109 #endif /* ProFTPD 1.3.4c or later */
2110 fs->utimes = statcache_fsio_utimes;
2111 fs->futimes = statcache_fsio_futimes;
2112
2113 pr_fs_setcwd(pr_fs_getvwd());
2114 pr_fs_clear_cache();
2115
2116 pr_event_register(&statcache_module, "fs.statcache.clear",
2117 statcache_fs_statcache_clear_ev, NULL);
2118
2119 /* If we are handling an SSH2 session, then we need to disable all
2120 * negative caching; something about ProFTPD's stat caching interacting
2121 * with mod_statcache's caching, AND mod_sftp's dispatching through
2122 * the main FTP handlers, causes unexpected behavior.
2123 */
2124
2125 proto = pr_session_get_protocol(0);
2126 if (strncmp(proto, "ssh2", 5) == 0 ||
2127 strncmp(proto, "sftp", 5) == 0 ||
2128 strncmp(proto, "scp", 4) == 0) {
2129 pr_trace_msg(trace_channel, 9,
2130 "disabling negative caching for %s protocol", proto);
2131 statcache_max_negative_age = 0;
2132 }
2133
2134 return PR_DECLINED(cmd);
2135 }
2136
2137 #if defined(MADV_WILLNEED)
2138 MODRET statcache_pre_list(cmd_rec *cmd) {
2139 int res;
2140
2141 if (statcache_engine == FALSE) {
2142 return PR_DECLINED(cmd);
2143 }
2144
2145 res = madvise(statcache_table, statcache_tablesz, MADV_WILLNEED);
2146 if (res < 0) {
2147 pr_log_debug(DEBUG5, MOD_STATCACHE_VERSION
2148 ": madvise(2) error with MADV_WILLNEED: %s", strerror(errno));
2149 }
2150
2151 return PR_DECLINED(cmd);
2152 }
2153 #endif /* MADV_WILLNEED */
2154
2155 /* Event handlers
2156 */
2157
2158 static void statcache_fs_statcache_clear_ev(const void *event_data,
2159 void *user_data) {
2160 int tab_fd;
2161 const char *canon_path = NULL, *path;
2162 size_t canon_pathlen = 0;
2163 pool *p;
2164 uint32_t hash;
2165
2166 path = event_data;
2167 if (path == NULL) {
2168 return;
2169 }
2170
2171 p = make_sub_pool(statcache_pool);
2172 pr_pool_tag(p, "statcache_clear_ev sub-pool");
2173 canon_path = statcache_get_canon_path(p, path, &canon_pathlen);
2174 if (canon_path == NULL) {
2175 destroy_pool(p);
2176 return;
2177 }
2178
2179 hash = statcache_hash(canon_path, canon_pathlen);
2180 tab_fd = statcache_tabfh->fh_fd;
2181
2182 if (statcache_wlock_row(tab_fd, hash) < 0) {
2183 pr_trace_msg(trace_channel, 3,
2184 "error write-locking shared memory: %s", strerror(errno));
2185 }
2186
2187 pr_trace_msg(trace_channel, 14,
2188 "removing entry for path '%s' due to event", canon_path);
2189 (void) statcache_table_remove(tab_fd, canon_path, canon_pathlen, hash);
2190
2191 if (statcache_unlock_row(tab_fd, hash) < 0) {
2192 pr_trace_msg(trace_channel, 3,
2193 "error unlocking shared memory: %s", strerror(errno));
2194 }
2195
2196 destroy_pool(p);
2197 }
2198
2199 static void statcache_sess_reinit_ev(const void *event_data, void *user_data) {
2200 int res;
2201
2202 /* A HOST command changed the main_server pointer; reinitialize ourselves. */
2203
2204 pr_event_unregister(&statcache_module, "core.session-reinit",
2205 statcache_sess_reinit_ev);
2206
2207 /* Restore defaults */
2208 statcache_engine = FALSE;
2209
2210 res = statcache_sess_init();
2211 if (res < 0) {
2212 pr_session_disconnect(&statcache_module,
2213 PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL);
2214 }
2215 }
2216
2217 static void statcache_shutdown_ev(const void *event_data, void *user_data) {
2218
2219 /* Remove the mmap from the system. We can only do this reliably
2220 * when the standalone daemon process exits; if it's an inetd process,
2221 * there many be other proftpd processes still running.
2222 */
2223
2224 if (getpid() == mpid &&
2225 ServerType == SERVER_STANDALONE &&
2226 (statcache_table != NULL && statcache_tabfh->fh_fd >= 0)) {
2227 int res;
2228
2229 res = munmap(statcache_table, statcache_tablesz);
2230 if (res < 0) {
2231 pr_log_debug(DEBUG1, MOD_STATCACHE_VERSION
2232 ": error detaching shared memory: %s", strerror(errno));
2233
2234 } else {
2235 pr_log_debug(DEBUG7, MOD_STATCACHE_VERSION
2236 ": detached %lu bytes of shared memory for StatCacheTable '%s'",
2237 (unsigned long) statcache_tablesz, statcache_table_path);
2238 }
2239
2240 res = pr_fsio_close(statcache_tabfh);
2241 if (res < 0) {
2242 pr_log_debug(DEBUG1, MOD_STATCACHE_VERSION
2243 ": error closing StatCacheTable '%s': %s", statcache_table_path,
2244 strerror(errno));
2245 }
2246 }
2247 }
2248
2249 #if defined(PR_SHARED_MODULE)
2250 static void statcache_mod_unload_ev(const void *event_data, void *user_data) {
2251 if (strcmp("mod_statcache.c", (const char *) event_data) == 0) {
2252 #if defined(PR_USE_CTRLS)
2253 register unsigned int i;
2254
2255 for (i = 0; statcache_acttab[i].act_action; i++) {
2256 (void) pr_ctrls_unregister(&statcache_module,
2257 statcache_acttab[i].act_action);
2258 }
2259 #endif /* PR_USE_CTRLS */
2260
2261 pr_event_unregister(&statcache_module, NULL, NULL);
2262
2263 if (statcache_tabfh) {
2264 (void) pr_fsio_close(statcache_tabfh);
2265 statcache_tabfh = NULL;
2266 }
2267
2268 if (statcache_pool) {
2269 destroy_pool(statcache_pool);
2270 statcache_pool = NULL;
2271 }
2272
2273 statcache_engine = FALSE;
2274 }
2275 }
2276 #endif /* PR_SHARED_MODULE */
2277
2278 static void statcache_postparse_ev(const void *event_data, void *user_data) {
2279 size_t tablesz;
2280 void *table;
2281 int xerrno;
2282 struct stat st;
2283
2284 if (statcache_engine == FALSE) {
2285 return;
2286 }
2287
2288 /* Make sure the StatCacheTable exists. */
2289 if (statcache_table_path == NULL) {
2290 pr_log_pri(PR_LOG_NOTICE, MOD_STATCACHE_VERSION
2291 ": missing required StatCacheTable configuration");
2292 pr_session_disconnect(&statcache_module, PR_SESS_DISCONNECT_BAD_CONFIG,
2293 NULL);
2294 }
2295
2296 PRIVS_ROOT
2297 statcache_tabfh = pr_fsio_open(statcache_table_path, O_RDWR|O_CREAT);
2298 xerrno = errno;
2299 PRIVS_RELINQUISH
2300
2301 if (statcache_tabfh == NULL) {
2302 pr_log_pri(PR_LOG_NOTICE, MOD_STATCACHE_VERSION
2303 ": unable to open StatCacheTable '%s': %s", statcache_table_path,
2304 strerror(xerrno));
2305 pr_session_disconnect(&statcache_module, PR_SESS_DISCONNECT_BAD_CONFIG,
2306 NULL);
2307 }
2308
2309 if (pr_fsio_fstat(statcache_tabfh, &st) < 0) {
2310 xerrno = errno;
2311
2312 pr_log_pri(PR_LOG_NOTICE, MOD_STATCACHE_VERSION
2313 ": unable to stat StatCacheTable '%s': %s", statcache_table_path,
2314 strerror(xerrno));
2315 pr_fsio_close(statcache_tabfh);
2316 statcache_tabfh = NULL;
2317 pr_session_disconnect(&statcache_module, PR_SESS_DISCONNECT_BAD_CONFIG,
2318 NULL);
2319 }
2320
2321 if (S_ISDIR(st.st_mode)) {
2322 xerrno = EISDIR;
2323
2324 pr_log_pri(PR_LOG_NOTICE, MOD_STATCACHE_VERSION
2325 ": unable to stat StatCacheTable '%s': %s", statcache_table_path,
2326 strerror(xerrno));
2327 pr_fsio_close(statcache_tabfh);
2328 statcache_tabfh = NULL;
2329 pr_session_disconnect(&statcache_module, PR_SESS_DISCONNECT_BAD_CONFIG,
2330 NULL);
2331 }
2332
2333 if (statcache_tabfh->fh_fd <= STDERR_FILENO) {
2334 int usable_fd;
2335
2336 usable_fd = pr_fs_get_usable_fd(statcache_tabfh->fh_fd);
2337 if (usable_fd < 0) {
2338 pr_log_debug(DEBUG0, MOD_STATCACHE_VERSION
2339 "warning: unable to find good fd for StatCacheTable %s: %s",
2340 statcache_table_path, strerror(errno));
2341
2342 } else {
2343 close(statcache_tabfh->fh_fd);
2344 statcache_tabfh->fh_fd = usable_fd;
2345 }
2346 }
2347
2348 /* The size of the table, in bytes, is:
2349 *
2350 * sizeof(header) + sizeof(data)
2351 *
2352 * thus:
2353 *
2354 * header = 6 * sizeof(uint32_t)
2355 * data = capacity * sizeof(struct statcache_entry)
2356 */
2357
2358 tablesz = (6 * sizeof(uint32_t)) +
2359 (statcache_capacity * sizeof(struct statcache_entry));
2360
2361 /* Get the shm for storing all of our stat info. */
2362 table = statcache_get_shm(statcache_tabfh, tablesz);
2363 if (table == NULL) {
2364 pr_log_pri(PR_LOG_NOTICE, MOD_STATCACHE_VERSION
2365 ": unable to get shared memory for StatCacheTable '%s': %s",
2366 statcache_table_path, strerror(errno));
2367 pr_session_disconnect(&statcache_module, PR_SESS_DISCONNECT_BAD_CONFIG,
2368 NULL);
2369 }
2370
2371 pr_trace_msg(trace_channel, 9,
2372 "allocated %lu bytes of shared memory for %u cache entries",
2373 (unsigned long) tablesz, statcache_capacity);
2374
2375 statcache_table = table;
2376 statcache_tablesz = tablesz;
2377 statcache_table_stats = statcache_table;
2378 statcache_table_data = ((uint32_t *) statcache_table + 6);
2379
2380 statcache_nrows = (statcache_capacity / STATCACHE_COLS_PER_ROW);
2381 statcache_rowlen = (STATCACHE_COLS_PER_ROW * sizeof(struct statcache_entry));
2382
2383 return;
2384 }
2385
2386 static void statcache_restart_ev(const void *event_data, void *user_data) {
2387 #if defined(PR_USE_CTRLS)
2388 register unsigned int i;
2389 #endif /* PR_USE_CTRLS */
2390
2391 if (statcache_pool) {
2392 destroy_pool(statcache_pool);
2393 statcache_pool = NULL;
2394 }
2395
2396 statcache_pool = make_sub_pool(permanent_pool);
2397 pr_pool_tag(statcache_pool, MOD_STATCACHE_VERSION);
2398
2399 #if defined(PR_USE_CTRLS)
2400 /* Register the control handlers */
2401 for (i = 0; statcache_acttab[i].act_action; i++) {
2402
2403 /* Allocate and initialize the ACL for this control. */
2404 statcache_acttab[i].act_acl = pcalloc(statcache_pool, sizeof(ctrls_acl_t));
2405 pr_ctrls_init_acl(statcache_acttab[i].act_acl);
2406 }
2407 #endif /* PR_USE_CTRLS */
2408
2409 /* Close the StatCacheTable file descriptor; it will be reopened by the
2410 * postparse event listener.
2411 */
2412 if (statcache_tabfh != NULL) {
2413 pr_fsio_close(statcache_tabfh);
2414 statcache_tabfh = NULL;
2415 }
2416
2417 return;
2418 }
2419
2420 /* Initialization routines
2421 */
2422
2423 static int statcache_init(void) {
2424 #if defined(PR_USE_CTRLS)
2425 register unsigned int i = 0;
2426 #endif /* PR_USE_CTRLS */
2427
2428 /* Allocate the pool for this module's use. */
2429 statcache_pool = make_sub_pool(permanent_pool);
2430 pr_pool_tag(statcache_pool, MOD_STATCACHE_VERSION);
2431
2432 #if defined(PR_USE_CTRLS)
2433 /* Register the control handlers */
2434 for (i = 0; statcache_acttab[i].act_action; i++) {
2435
2436 /* Allocate and initialize the ACL for this control. */
2437 statcache_acttab[i].act_acl = pcalloc(statcache_pool, sizeof(ctrls_acl_t));
2438 pr_ctrls_init_acl(statcache_acttab[i].act_acl);
2439
2440 if (pr_ctrls_register(&statcache_module, statcache_acttab[i].act_action,
2441 statcache_acttab[i].act_desc, statcache_acttab[i].act_cb) < 0) {
2442 pr_log_pri(PR_LOG_INFO, MOD_STATCACHE_VERSION
2443 ": error registering '%s' control: %s",
2444 statcache_acttab[i].act_action, strerror(errno));
2445 }
2446 }
2447 #endif /* PR_USE_CTRLS */
2448
2449 #if defined(PR_SHARED_MODULE)
2450 pr_event_register(&statcache_module, "core.module-unload",
2451 statcache_mod_unload_ev, NULL);
2452 #endif /* PR_SHARED_MODULE */
2453 pr_event_register(&statcache_module, "core.postparse",
2454 statcache_postparse_ev, NULL);
2455 pr_event_register(&statcache_module, "core.restart",
2456 statcache_restart_ev, NULL);
2457 pr_event_register(&statcache_module, "core.shutdown",
2458 statcache_shutdown_ev, NULL);
2459
2460 return 0;
2461 }
2462
2463 static int statcache_sess_init(void) {
2464 config_rec *c;
2465
2466 pr_event_register(&statcache_module, "core.session-reinit",
2467 statcache_sess_reinit_ev, NULL);
2468
2469 /* Check to see if the BanEngine directive is set to 'off'. */
2470 c = find_config(main_server->conf, CONF_PARAM, "StatCacheEngine", FALSE);
2471 if (c != NULL) {
2472 statcache_engine = *((int *) c->argv[0]);
2473 }
2474
2475 return 0;
2476 }
2477
2478 #if defined(PR_USE_CTRLS)
2479 /* Controls table
2480 */
2481 static ctrls_acttab_t statcache_acttab[] = {
2482 { "statcache", "display cache stats", NULL,
2483 statcache_handle_statcache },
2484
2485 { NULL, NULL, NULL, NULL }
2486 };
2487 #endif /* PR_USE_CTRLS */
2488
2489 /* Module API tables
2490 */
2491
2492 static conftable statcache_conftab[] = {
2493 { "StatCacheCapacity", set_statcachecapacity, NULL },
2494 { "StatCacheControlsACLs", set_statcachectrlsacls, NULL },
2495 { "StatCacheEngine", set_statcacheengine, NULL },
2496 { "StatCacheMaxAge", set_statcachemaxage, NULL },
2497 { "StatCacheTable", set_statcachetable, NULL },
2498 { NULL }
2499 };
2500
2501 static cmdtable statcache_cmdtab[] = {
2502 { POST_CMD, C_PASS, G_NONE, statcache_post_pass, FALSE, FALSE },
2503
2504 #if defined(MADV_WILLNEED)
2505 /* If the necessary madvise(2) flag is present, register a PRE_CMD
2506 * handler for directory listings, to suggest to the kernel that
2507 * it read in some pages of the mmap()'d region.
2508 */
2509 { PRE_CMD, C_LIST, G_NONE, statcache_pre_list, FALSE, FALSE },
2510 { PRE_CMD, C_MLSD, G_NONE, statcache_pre_list, FALSE, FALSE },
2511 { PRE_CMD, C_NLST, G_NONE, statcache_pre_list, FALSE, FALSE },
2512 #endif /* MADV_WILLNEED */
2513
2514 { 0, NULL }
2515 };
2516
2517 module statcache_module = {
2518 NULL, NULL,
2519
2520 /* Module API version 2.0 */
2521 0x20,
2522
2523 /* Module name */
2524 "statcache",
2525
2526 /* Module configuration handler table */
2527 statcache_conftab,
2528
2529 /* Module command handler table */
2530 statcache_cmdtab,
2531
2532 /* Module authentication handler table */
2533 NULL,
2534
2535 /* Module initialization function */
2536 statcache_init,
2537
2538 /* Session initialization function */
2539 statcache_sess_init,
2540
2541 /* Module version */
2542 MOD_STATCACHE_VERSION
2543 };