2 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 #endif /* HAVE_CONFIG_H */
20 #include <sys/types.h>
29 #include <atalk/logger.h>
30 #include <atalk/cnid_dbd_private.h>
31 #include <atalk/globals.h>
32 #include <atalk/netatalk_conf.h>
33 #include <atalk/util.h>
41 #define DBOPTIONS (DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN)
43 int nocniddb = 0; /* Dont open CNID database, only scan filesystem */
44 volatile sig_atomic_t alarmed; /* flags for signals */
45 int db_locked; /* have we got the fcntl lock on lockfile ? */
48 static int verbose; /* Logging flag */
49 static int exclusive; /* Exclusive volume access */
50 static struct db_param db_param = {
51 NULL, /* Volume dirpath */
52 1, /* bdb logfile autoremove */
53 64 * 1024, /* bdb cachesize (64 MB) */
54 DEFAULT_MAXLOCKS, /* maxlocks */
55 DEFAULT_MAXLOCKOBJS, /* maxlockobjs */
56 0, /* flush_interval */
57 0, /* flush_frequency */
59 -1, /* fd_table_size */
60 -1, /* idle_timeout */
63 static char dbpath[MAXPATHLEN+1]; /* Path to the dbd database */
68 void dbd_log(enum logtype lt, char *fmt, ...)
71 static char logbuffer[1024];
74 if ( (lt == LOGSTD) || (verbose == 1)) {
76 len = vsnprintf(logbuffer, 1023, fmt, args);
80 printf("%s\n", logbuffer);
86 catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
89 static void sig_handler(int signo)
95 static void set_signal(void)
99 sv.sa_handler = sig_handler;
100 sv.sa_flags = SA_RESTART;
101 sigemptyset(&sv.sa_mask);
102 if (sigaction(SIGTERM, &sv, NULL) < 0) {
103 dbd_log( LOGSTD, "error in sigaction(SIGTERM): %s", strerror(errno));
106 if (sigaction(SIGINT, &sv, NULL) < 0) {
107 dbd_log( LOGSTD, "error in sigaction(SIGINT): %s", strerror(errno));
111 memset(&sv, 0, sizeof(struct sigaction));
112 sv.sa_handler = SIG_IGN;
113 sigemptyset(&sv.sa_mask);
115 if (sigaction(SIGABRT, &sv, NULL) < 0) {
116 dbd_log( LOGSTD, "error in sigaction(SIGABRT): %s", strerror(errno));
119 if (sigaction(SIGHUP, &sv, NULL) < 0) {
120 dbd_log( LOGSTD, "error in sigaction(SIGHUP): %s", strerror(errno));
123 if (sigaction(SIGQUIT, &sv, NULL) < 0) {
124 dbd_log( LOGSTD, "error in sigaction(SIGQUIT): %s", strerror(errno));
129 static void usage (void)
131 printf("dbd (Netatalk %s)\n"
132 "Usage: dbd [-CeFtvx] -d [-i] | -s [-c|-n]| -r [-c|-f] | -u <path to netatalk volume>\n"
133 "dbd can dump, scan, reindex and rebuild Netatalk dbd CNID databases.\n"
134 "dbd must be run with appropiate permissions i.e. as root.\n\n"
135 "Main commands are:\n"
136 " -d Dump CNID database\n"
137 " Option: -i dump indexes too\n\n"
139 " Options: -c Don't check .AppleDouble stuff, only ckeck orphaned.\n"
140 " -n Don't open CNID database, skip CNID checks.\n\n"
141 " -r Rebuild volume:\n"
142 " Options: -c Don't create .AppleDouble stuff, only cleanup orphaned.\n"
143 " -f wipe database and rebuild from IDs stored in AppleDouble\n"
144 " metadata file or EA. Implies -e.\n\n"
146 " Opens the database which triggers any necessary upgrades,\n"
147 " then closes and exits.\n\n"
149 " -C convert from adouble:v2 to adouble:ea (use with -r)\n"
150 " -F location of the afp.conf config file\n"
151 " -e only work on inactive volumes and lock them (exclusive)\n"
152 " -x rebuild indexes (just for completeness, mostly useless!)\n"
153 " -t show statistics while running\n"
159 int main(int argc, char **argv)
161 int c, lockfd, ret = -1;
162 int dump=0, scan=0, rebuild=0, prep_upgrade=0, rebuildindexes=0, dumpindexes=0, force=0;
163 dbd_flags_t flags = 0;
169 if (geteuid() != 0) {
173 /* Inhereting perms in ad_mkdir etc requires this */
176 while ((c = getopt(argc, argv, ":cCdefFinrstuvx")) != -1) {
179 flags |= DBD_FLAGS_CLEANUP;
182 flags |= DBD_FLAGS_V2TOEA;
192 flags |= DBD_FLAGS_SCAN;
195 nocniddb = 1; /* FIXME: this could/should be a flag too for consistency */
201 flags |= DBD_FLAGS_STATS;
211 flags |= DBD_FLAGS_EXCL;
219 flags |= DBD_FLAGS_FORCE | DBD_FLAGS_EXCL;
222 obj.cmdlineconfigfile = strdup(optarg);
232 if ((dump + scan + rebuild + prep_upgrade) != 1) {
237 if ( (optind + 1) != argc ) {
241 volpath = argv[optind];
243 setvbuf(stdout, (char *) NULL, _IONBF, 0);
246 if ((cdir = open(".", O_RDONLY)) < 0) {
247 dbd_log( LOGSTD, "Can't open dir: %s", strerror(errno));
251 /* Setup signal handling */
255 if (afp_config_parse(&obj, "dbd") != 0) {
256 dbd_log( LOGSTD, "Couldn't load afp.conf");
260 /* Setup logging. Should be portable among *NIXes */
262 setuplog("default:info", "/dev/tty");
264 setuplog("default:debug", "/dev/tty");
266 if (load_volumes(&obj) != 0) {
267 dbd_log( LOGSTD, "Couldn't load volumes");
271 if ((vol = getvolbypath(&obj, volpath)) == NULL) {
272 dbd_log( LOGSTD, "Couldn't find volume for '%s'", volpath);
276 if (load_charset(vol) != 0) {
277 dbd_log( LOGSTD, "Couldn't load charsets for '%s'", volpath);
283 if (vol->v_adouble == AD_VERSION_EA)
284 dbd_log( LOGDEBUG, "adouble:ea volume");
285 else if (vol->v_adouble == AD_VERSION2)
286 dbd_log( LOGDEBUG, "adouble:v2 volume");
288 dbd_log( LOGSTD, "unknown adouble volume");
292 /* -C v2 to ea conversion only on adouble:ea volumes */
293 if ((flags & DBD_FLAGS_V2TOEA) && (vol->v_adouble!= AD_VERSION_EA)) {
294 dbd_log( LOGSTD, "Can't run adouble:v2 to adouble:ea conversion because not an adouble:ea volume");
298 /* Sanity checks to ensure we can touch this volume */
299 if (vol->v_vfs_ea != AFPVOL_EA_AD && vol->v_vfs_ea != AFPVOL_EA_SYS) {
300 dbd_log( LOGSTD, "Unknown Extended Attributes option: %u", vol->v_vfs_ea);
304 /* Enuser dbpath is there, create if necessary */
306 if (stat(vol->v_dbpath, &st) != 0) {
307 if (errno != ENOENT) {
308 dbd_log( LOGSTD, "Can't stat dbpath \"%s\": %s", vol->v_dbpath, strerror(errno));
311 if ((mkdir(vol->v_dbpath, 0755)) != 0) {
312 dbd_log( LOGSTD, "Can't create dbpath \"%s\": %s", vol->v_dbpath, strerror(errno));
317 /* Put "/.AppleDB" at end of volpath, get path from volinfo file */
318 if ( (strlen(vol->v_dbpath) + strlen("/.AppleDB")) > MAXPATHLEN ) {
319 dbd_log( LOGSTD, "Volume pathname too long");
322 strncpy(dbpath, vol->v_dbpath, MAXPATHLEN - strlen("/.AppleDB"));
323 strcat(dbpath, "/.AppleDB");
325 /* Check or create dbpath */
326 int dbdirfd = open(dbpath, O_RDONLY);
327 if (dbdirfd == -1 && errno == ENOENT) {
328 if (errno == ENOENT) {
329 if ((mkdir(dbpath, 0755)) != 0) {
330 dbd_log( LOGSTD, "Can't create .AppleDB for \"%s\": %s", dbpath, strerror(errno));
334 dbd_log( LOGSTD, "Somethings wrong with .AppleDB for \"%s\", giving up: %s", dbpath, strerror(errno));
342 if ((db_locked = get_lock(LOCK_EXCL, dbpath)) == -1)
344 if (db_locked != LOCK_EXCL) {
345 dbd_log(LOGDEBUG, "Database is in use, acquiring shared lock");
346 /* Couldn't get exclusive lock, try shared lock if -e wasn't requested */
348 dbd_log(LOGSTD, "Database is in use and exlusive was requested");
351 if ((db_locked = get_lock(LOCK_SHRD, NULL)) != LOCK_SHRD)
355 /* Check if -f is requested and wipe db if yes */
356 if ((flags & DBD_FLAGS_FORCE) && rebuild) {
357 char cmd[8 + MAXPATHLEN];
358 if ((db_locked = get_lock(LOCK_FREE, NULL)) != 0)
361 snprintf(cmd, 8 + MAXPATHLEN, "rm -rf \"%s\"", dbpath);
362 dbd_log( LOGDEBUG, "Removing old database of volume: '%s'", volpath);
364 if ((mkdir(dbpath, 0755)) != 0) {
365 dbd_log( LOGSTD, "Can't create dbpath \"%s\": %s", dbpath, strerror(errno));
368 dbd_log( LOGDEBUG, "Removed old database.");
369 if ((db_locked = get_lock(LOCK_EXCL, dbpath)) == -1)
374 Lets start with the BerkeleyDB stuff
377 if ((dbd = dbif_init(dbpath, "cnid2.db")) == NULL)
380 if (dbif_env_open(dbd,
382 (db_locked == LOCK_EXCL) ? (DBOPTIONS | DB_RECOVER) : DBOPTIONS) < 0) {
383 dbd_log( LOGSTD, "error opening database!");
387 if (db_locked == LOCK_EXCL)
388 dbd_log( LOGDEBUG, "Finished recovery.");
390 if (dbif_open(dbd, NULL, rebuildindexes) < 0) {
395 /* Prepare upgrade ? We're done */
397 (void)dbif_txn_close(dbd, 1);
402 /* Downgrade db lock if not running exclusive */
403 if (!exclusive && (db_locked == LOCK_EXCL)) {
404 if (get_lock(LOCK_UNLOCK, NULL) != 0)
406 if (get_lock(LOCK_SHRD, NULL) != LOCK_SHRD)
410 /* Now execute given command scan|rebuild|dump */
411 if (dump && ! nocniddb) {
412 if (dbif_dump(dbd, dumpindexes) < 0) {
413 dbd_log( LOGSTD, "Error dumping database");
415 } else if ((rebuild && ! nocniddb) || scan) {
416 if (cmd_dbd_scanvol(dbd, vol, flags) < 0) {
417 dbd_log( LOGSTD, "Error repairing database.");
423 dbd_log(LOGDEBUG, "Closing db");
425 if (dbif_close(dbd) < 0) {
426 dbd_log( LOGSTD, "Error closing database");
435 if (dbif_env_remove(dbpath) < 0) {
436 dbd_log( LOGSTD, "Error removing BerkeleyDB database environment");
442 if ((fchdir(cdir)) < 0)
443 dbd_log(LOGSTD, "fchdir: %s", strerror(errno));