2 $Id: dbd.c,v 1.3 2009-05-06 11:54:24 franklahm Exp $
4 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
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.
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.
19 #endif /* HAVE_CONFIG_H */
22 #include <sys/types.h>
31 #include <atalk/logger.h>
32 #include <atalk/cnid_dbd_private.h>
33 #include <atalk/volinfo.h>
38 #define LOCKFILENAME "lock"
39 #define DBOPTIONS (DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN)
43 static volatile sig_atomic_t alarmed;
44 static int verbose; /* Logging flag */
45 static int exclusive; /* Exclusive volume access */
46 static struct db_param db_param = {
47 NULL, /* Volume dirpath */
48 1, /* bdb logfile autoremove */
49 16384, /* bdb cachesize */
50 -1, /* not used ... */
61 void dbd_log(enum logtype lt, char *fmt, ...)
64 static char logbuffer[1024];
67 if ( (lt == LOGSTD) || (verbose == 1)) {
69 len = vsnprintf(logbuffer, 1023, fmt, args);
73 printf("%s\n", logbuffer);
79 ignore everything except SIGTERM which we catch and which causes
83 static void sig_handler(int signo)
93 sv.sa_handler = sig_handler;
94 sv.sa_flags = SA_RESTART;
95 sigemptyset(&sv.sa_mask);
96 sigaddset(&sv.sa_mask, SIGTERM);
97 if (sigaction(SIGTERM, &sv, NULL) < 0) {
98 dbd_log( LOGSTD, "error in sigaction(SIGTERM): %s", strerror(errno));
102 memset(&sv, 0, sizeof(struct sigaction));
103 sv.sa_handler = SIG_IGN;
104 sigemptyset(&sv.sa_mask);
106 if (sigaction(SIGINT, &sv, NULL) < 0) {
107 dbd_log( LOGSTD, "error in sigaction(SIGINT): %s", strerror(errno));
110 if (sigaction(SIGABRT, &sv, NULL) < 0) {
111 dbd_log( LOGSTD, "error in sigaction(SIGABRT): %s", strerror(errno));
114 if (sigaction(SIGHUP, &sv, NULL) < 0) {
115 dbd_log( LOGSTD, "error in sigaction(SIGHUP): %s", strerror(errno));
118 if (sigaction(SIGQUIT, &sv, NULL) < 0) {
119 dbd_log( LOGSTD, "error in sigaction(SIGQUIT): %s", strerror(errno));
125 static void block_sigs_onoff(int block)
130 sigaddset(&set, SIGINT);
131 sigaddset(&set, SIGTERM);
133 sigprocmask(SIG_BLOCK, &set, NULL);
135 sigprocmask(SIG_UNBLOCK, &set, NULL);
145 if ((lockfd = open(LOCKFILENAME, O_RDWR | O_CREAT, 0644)) < 0) {
146 dbd_log( LOGSTD, "Error opening lockfile: %s", strerror(errno));
151 lock.l_whence = SEEK_SET;
153 lock.l_type = F_WRLCK;
155 if (fcntl(lockfd, F_SETLK, &lock) < 0) {
156 if (errno == EACCES || errno == EAGAIN) {
158 dbd_log( LOGSTD, "Database is in use and exlusive was requested", strerror(errno));
162 dbd_log( LOGSTD, "Error getting fcntl F_WRLCK on lockfile: %s", strerror(errno));
170 void free_lock(int lockfd)
175 lock.l_whence = SEEK_SET;
177 lock.l_type = F_UNLCK;
178 fcntl(lockfd, F_SETLK, &lock);
184 printf("Usage: dbd [-e|-v|-x] -d [-i] | -s | -r [-f] <path to netatalk volume>\n"
185 "dbd can dump, scan, reindex and rebuild Netatalk dbd CNID databases.\n"
186 "dbd must be run with appropiate permissions i.e. as root.\n\n"
187 "Main commands are:\n"
188 " -d Dump CNID database\n"
189 " Option: -i dump indexes too\n"
191 " 1. Compare database with volume\n"
192 " 2. Check if .AppleDouble dirs exist\n"
193 " 3. Check if AppleDouble file exist\n"
194 " 4. Report orphaned AppleDouble files\n"
195 " 5. Check for directories inside AppleDouble directories\n"
196 " 6. Check name encoding by roundtripping, log on error\n"
197 " -r Rebuild volume:\n"
198 " 1. Sync database with volume\n"
199 " 2. Make sure .AppleDouble dir exist, create if missing\n"
200 " 3. Make sure AppleDouble file exists, create if missing\n"
201 " 4. Delete orphaned AppleDouble files\n"
202 " 5. Check for directories inside AppleDouble directories\n"
203 " 6. Check name encoding by roundtripping, log on error\n"
204 " Option: -f wipe database and rebuild from IDs stored in AppleDouble files,\n"
205 " only available for volumes with 'cachecnid' option\n\n"
207 " -e only work on inactive volumes and lock them (exclusive)\n"
208 " -x rebuild indexes\n"
211 "05-05-2009: -s and -r already check/repair the AppleDouble stuff and encoding,\n"
212 " no CNID database maintanance is done yet\n"
216 int main(int argc, char **argv)
219 int dump=0, scan=0, rebuild=0, rebuildindexes=0, dumpindexes=0, force=0;
220 dbd_flags_t flags = 0;
222 struct volinfo volinfo;
224 if (geteuid() != 0) {
229 while ((c = getopt(argc, argv, ":dsrvxife")) != -1) {
239 flags = DBD_FLAGS_SCAN;
255 flags = DBD_FLAGS_FORCE;
265 if ((dump + scan + rebuild) != 1) {
270 if ( (optind + 1) != argc ) {
274 volpath = argv[optind];
276 /* Put "/.AppleDB" at end of volpath */
277 if ( (strlen(volpath) + strlen("/.AppleDB")) > (PATH_MAX -1) ) {
278 dbd_log( LOGSTD, "Volume pathname too long");
281 char dbpath[PATH_MAX];
282 strncpy(dbpath, volpath, PATH_MAX - 1);
283 strcat(dbpath, "/.AppleDB");
285 /* cd to .AppleDB dir */
287 if ((cdir = open(".", O_RDONLY)) < 0) {
288 dbd_log( LOGSTD, "Can't open dir: %s", strerror(errno));
291 if (chdir(dbpath) < 0) {
292 dbd_log( LOGSTD, "chdir to %s failed: %s", dbpath, strerror(errno));
297 Before we do anything else, check if there is an instance of cnid_dbd
298 running already and silently exit if yes.
302 /* Ignore everything except SIGTERM */
305 /* Setup logging. Should be portable among *NIXes */
307 setuplog("default log_info /dev/tty");
309 setuplog("default log_debug /dev/tty");
311 /* Load .volinfo file */
312 if ( -1 == loadvolinfo(volpath, &volinfo)) {
313 dbd_log( LOGSTD, "Unkown volume options!");
316 if ( -1 == vol_load_charsets(&volinfo)) {
317 dbd_log( LOGSTD, "Error loading charsets!");
322 Lets start with the BerkeleyDB stuff
324 if (NULL == (dbd = dbif_init("cnid2.db")))
327 if (dbif_env_open(dbd, &db_param, DBOPTIONS) < 0) {
328 dbd_log( LOGSTD, "error opening database!");
332 dbd_log( LOGDEBUG, "Finished opening BerkeleyDB databases including recovery.");
334 if (dbif_open(dbd, &db_param, rebuildindexes) < 0) {
339 if ((fchdir(cdir)) < 0) {
340 dbd_log(LOGSTD, "fchdir: %s", strerror(errno));
345 if (dbif_dump(dbd, dumpindexes) < 0) {
346 dbd_log( LOGSTD, "Error dumping database");
348 } else if (rebuild || scan) {
349 if (cmd_dbd_scanvol(dbd, &volinfo, flags) < 0) {
350 dbd_log( LOGSTD, "Fatal error repairing database. Please rm -r it.");
355 if (dbif_close(dbd) < 0)