| 1 | /* |
| 2 | * This file is part of DisOrder. |
| 3 | * Copyright (C) 2010 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | /** @file server/mount.c |
| 19 | * @brief Periodically check for devices being mounted and unmounted |
| 20 | */ |
| 21 | #include "disorder-server.h" |
| 22 | #if HAVE_GETFSSTAT |
| 23 | # include <sys/param.h> |
| 24 | # include <sys/ucred.h> |
| 25 | # include <sys/mount.h> |
| 26 | #endif |
| 27 | |
| 28 | #if HAVE_GETFSSTAT |
| 29 | static int compare_fsstat(const void *av, const void *bv) { |
| 30 | const struct statfs *a = av, *b = bv; |
| 31 | int c; |
| 32 | |
| 33 | c = memcmp(&a->f_fsid, &b->f_fsid, sizeof a->f_fsid); |
| 34 | if(c) |
| 35 | return c; |
| 36 | c = strcmp(a->f_mntonname, b->f_mntonname); |
| 37 | if(c) |
| 38 | return c; |
| 39 | return 0; |
| 40 | } |
| 41 | #endif |
| 42 | |
| 43 | void periodic_mount_check(ev_source *ev_) { |
| 44 | if(!config->mount_rescan) |
| 45 | return; |
| 46 | #if HAVE_GETFSSTAT |
| 47 | /* On OS X, we keep track of the hash of the kernel's mounted |
| 48 | * filesystem list */ |
| 49 | static int first = 1; |
| 50 | static unsigned char last[20]; |
| 51 | unsigned char *current; |
| 52 | int nfilesystems, space; |
| 53 | struct statfs *buf; |
| 54 | gcrypt_hash_handle h; |
| 55 | gcry_error_t e; |
| 56 | |
| 57 | space = getfsstat(NULL, 0, MNT_NOWAIT); |
| 58 | buf = xcalloc(space, sizeof *buf); |
| 59 | nfilesystems = getfsstat(buf, space * sizeof *buf, MNT_NOWAIT); |
| 60 | if(nfilesystems > space) |
| 61 | // The array grew between check and use! We just give up and try later. |
| 62 | return; |
| 63 | // Put into order so we get a bit of consistency |
| 64 | qsort(buf, nfilesystems, sizeof *buf, compare_fsstat); |
| 65 | if((e = gcry_md_open(&h, GCRY_MD_SHA1, 0))) { |
| 66 | disorder_error(0, "gcry_md_open: %s", gcry_strerror(e)); |
| 67 | return; |
| 68 | } |
| 69 | for(int n = 0; n < nfilesystems; ++n) { |
| 70 | gcry_md_write(h, &buf[n].f_fsid, sizeof buf[n].f_fsid); |
| 71 | gcry_md_write(h, buf[n].f_mntonname, 1 + strlen(buf[n].f_mntonname)); |
| 72 | } |
| 73 | current = gcry_md_read(h, GCRY_MD_SHA1); |
| 74 | if(!first && memcmp(current, last, sizeof last)) |
| 75 | trackdb_rescan(ev_, 1/*check*/, 0, 0); |
| 76 | memcpy(last, current, sizeof last); |
| 77 | first = 0; |
| 78 | gcry_md_close(h); |
| 79 | #elif defined PATH_MTAB |
| 80 | /* On Linux we keep track of the modification time of /etc/mtab */ |
| 81 | static time_t last_mount; |
| 82 | struct stat sb; |
| 83 | |
| 84 | if(stat(PATH_MTAB, &sb) >= 0) { |
| 85 | if(last_mount != 0 && last_mount != sb.st_mtime) |
| 86 | trackdb_rescan(ev_, 1/*check*/, 0, 0); |
| 87 | last_mount = sb.st_mtime; |
| 88 | } |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | Local Variables: |
| 94 | c-basic-offset:2 |
| 95 | comment-column:40 |
| 96 | fill-column:79 |
| 97 | indent-tabs-mode:nil |
| 98 | End: |
| 99 | */ |