2 $Id: cmd_dbd.c,v 1.2 2009-05-18 09:25:25 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 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 catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
82 static void sig_handler(int signo)
92 sv.sa_handler = sig_handler;
93 sv.sa_flags = SA_RESTART;
94 sigemptyset(&sv.sa_mask);
95 if (sigaction(SIGTERM, &sv, NULL) < 0) {
96 dbd_log( LOGSTD, "error in sigaction(SIGTERM): %s", strerror(errno));
99 if (sigaction(SIGINT, &sv, NULL) < 0) {
100 dbd_log( LOGSTD, "error in sigaction(SIGINT): %s", strerror(errno));
104 memset(&sv, 0, sizeof(struct sigaction));
105 sv.sa_handler = SIG_IGN;
106 sigemptyset(&sv.sa_mask);
108 if (sigaction(SIGABRT, &sv, NULL) < 0) {
109 dbd_log( LOGSTD, "error in sigaction(SIGABRT): %s", strerror(errno));
112 if (sigaction(SIGHUP, &sv, NULL) < 0) {
113 dbd_log( LOGSTD, "error in sigaction(SIGHUP): %s", strerror(errno));
116 if (sigaction(SIGQUIT, &sv, NULL) < 0) {
117 dbd_log( LOGSTD, "error in sigaction(SIGQUIT): %s", strerror(errno));
122 int get_lock(const char *dbpath)
125 char lockpath[PATH_MAX];
129 if ( (strlen(dbpath) + strlen(LOCKFILENAME+1)) > (PATH_MAX - 1) ) {
130 dbd_log( LOGSTD, ".AppleDB pathname too long");
133 strncpy(lockpath, dbpath, PATH_MAX - 1);
134 strcat(lockpath, "/");
135 strcat(lockpath, LOCKFILENAME);
137 if ((lockfd = open(lockpath, O_RDWR | O_CREAT, 0644)) < 0) {
138 dbd_log( LOGSTD, "Error opening lockfile: %s", strerror(errno));
142 if ((stat(dbpath, &st)) != 0) {
143 dbd_log( LOGSTD, "Error statting lockfile: %s", strerror(errno));
147 if ((chown(lockpath, st.st_uid, st.st_gid)) != 0) {
148 dbd_log( LOGSTD, "Error inheriting lockfile permissions: %s", strerror(errno));
153 lock.l_whence = SEEK_SET;
155 lock.l_type = F_WRLCK;
157 if (fcntl(lockfd, F_SETLK, &lock) < 0) {
158 if (errno == EACCES || errno == EAGAIN) {
160 dbd_log( LOGSTD, "Database is in use and exlusive was requested", strerror(errno));
164 dbd_log( LOGSTD, "Error getting fcntl F_WRLCK on lockfile: %s", strerror(errno));
172 void free_lock(int lockfd)
177 lock.l_whence = SEEK_SET;
179 lock.l_type = F_UNLCK;
180 fcntl(lockfd, F_SETLK, &lock);
186 printf("Usage: dbd [-e|-v|-x] -d [-i] | -s | -r [-f] <path to netatalk volume>\n"
187 "dbd can dump, scan, reindex and rebuild Netatalk dbd CNID databases.\n"
188 "dbd must be run with appropiate permissions i.e. as root.\n\n"
189 "Main commands are:\n"
190 " -d Dump CNID database\n"
191 " Option: -i dump indexes too\n"
193 " 1. Compare database with volume\n"
194 " 2. Check if .AppleDouble dirs exist\n"
195 " 3. Check if AppleDouble file exist\n"
196 " 4. Report orphaned AppleDouble files\n"
197 " 5. Check for directories inside AppleDouble directories\n"
198 " 6. Check name encoding by roundtripping, log on error\n"
199 " -r Rebuild volume:\n"
200 " 1. Sync database with volume\n"
201 " 2. Make sure .AppleDouble dir exist, create if missing\n"
202 " 3. Make sure AppleDouble file exists, create if missing\n"
203 " 4. Delete orphaned AppleDouble files\n"
204 " 5. Check for directories inside AppleDouble directories\n"
205 " 6. Check name encoding by roundtripping, log on error\n"
206 " Option: -f wipe database and rebuild from IDs stored in AppleDouble files,\n"
207 " only available for volumes with 'cachecnid' option\n\n"
209 " -e only work on inactive volumes and lock them (exclusive)\n"
210 " -x rebuild indexes\n"
215 int main(int argc, char **argv)
218 int dump=0, scan=0, rebuild=0, rebuildindexes=0, dumpindexes=0, force=0;
219 dbd_flags_t flags = 0;
221 struct volinfo volinfo;
223 if (geteuid() != 0) {
228 while ((c = getopt(argc, argv, ":dsrvxife")) != -1) {
238 flags = DBD_FLAGS_SCAN;
254 flags = DBD_FLAGS_FORCE;
264 if ((dump + scan + rebuild) != 1) {
269 if ( (optind + 1) != argc ) {
273 volpath = argv[optind];
275 /* Put "/.AppleDB" at end of volpath */
276 if ( (strlen(volpath) + strlen("/.AppleDB")) > (PATH_MAX - 1) ) {
277 dbd_log( LOGSTD, "Volume pathname too long");
280 char dbpath[PATH_MAX];
281 strncpy(dbpath, volpath, PATH_MAX - 1);
282 strcat(dbpath, "/.AppleDB");
286 if ((cdir = open(".", O_RDONLY)) < 0) {
287 dbd_log( LOGSTD, "Can't open dir: %s", strerror(errno));
292 Before we do anything else, check if there is an instance of cnid_dbd
293 running already and silently exit if yes.
295 lockfd = get_lock(dbpath);
297 /* Setup signal handling */
300 /* Setup logging. Should be portable among *NIXes */
302 setuplog("default log_info /dev/tty");
304 setuplog("default log_debug /dev/tty");
306 /* Load .volinfo file */
307 if (loadvolinfo(volpath, &volinfo) == -1) {
308 dbd_log( LOGSTD, "Unkown volume options!");
311 if (vol_load_charsets(&volinfo) == -1) {
312 dbd_log( LOGSTD, "Error loading charsets!");
317 Lets start with the BerkeleyDB stuff
319 if ((dbd = dbif_init(dbpath, "cnid2.db")) == NULL)
322 if (dbif_env_open(dbd, &db_param, exclusive ? (DBOPTIONS | DB_RECOVER) : DBOPTIONS) < 0) {
323 dbd_log( LOGSTD, "error opening database!");
328 dbd_log( LOGDEBUG, "Finished recovery.");
330 if (dbif_open(dbd, &db_param, rebuildindexes) < 0) {
336 if (dbif_dump(dbd, dumpindexes) < 0) {
337 dbd_log( LOGSTD, "Error dumping database");
339 } else if (rebuild || scan) {
340 if (cmd_dbd_scanvol(dbd, &volinfo, flags) < 0) {
341 dbd_log( LOGSTD, "Error repairing database.");
345 if (dbif_close(dbd) < 0) {
346 dbd_log( LOGSTD, "Error closing database");
352 if ((fchdir(cdir)) < 0)
353 dbd_log(LOGSTD, "fchdir: %s", strerror(errno));