2 $Id: dbd.c,v 1.1 2009-04-28 13:01: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>
37 #define LOCKFILENAME "lock"
38 #define DBOPTIONS (DB_CREATE | DB_INIT_LOCK | DB_INIT_MPOOL | DB_INIT_TXN)
40 enum logtype {LOGSTD, LOGDEBUG};
42 static volatile sig_atomic_t alarmed;
43 static int verbose; /* Logging flag */
44 static int exclusive; /* How to open the bdb database */
45 static struct volinfo volinfo;
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 static void dblog(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 dblog( LOGSTD, "error in sigaction(SIGTERM): %s", strerror(errno));
102 sv.sa_handler = SIG_IGN;
103 memset(&sv, 0, sizeof(struct sigaction));
104 sigemptyset(&sv.sa_mask);
106 if (sigaction(SIGINT, &sv, NULL) < 0) {
107 dblog( LOGSTD, "error in sigaction(SIGINT): %s", strerror(errno));
110 if (sigaction(SIGABRT, &sv, NULL) < 0) {
111 dblog( LOGSTD, "error in sigaction(SIGABRT): %s", strerror(errno));
114 if (sigaction(SIGHUP, &sv, NULL) < 0) {
115 dblog( LOGSTD, "error in sigaction(SIGHUP): %s", strerror(errno));
118 if (sigaction(SIGQUIT, &sv, NULL) < 0) {
119 dblog( 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 dblog( 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 dblog( LOGDEBUG, "Database is in use and exlusive was requested", strerror(errno));
162 dblog( 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 run with appropiate permissions i.e. as root.\n\n"
187 "Main commands are:\n"
189 " Dump CNID database\n"
190 " Option: -i dump indexes too\n"
192 " Compare database with volume\n"
194 " Rebuild CNID database\n"
195 " Option: -f wipe database and rebuild from IDs stored in AppleDouble files\n\n"
197 " -e only work on inactive volumes and lock them (exclusive)\n"
198 " -x rebuild indexes\n"
201 "28-04-2009: -s and -r and not yet implemented\n"
205 int main(int argc, char **argv)
208 int dump=0, scan=0, rebuild=0, rebuildindexes=0, dumpindexes=0, force=0;
211 if (geteuid() != 0) {
216 while ((c = getopt(argc, argv, ":dsrvxif")) != -1) {
250 if ((dump + scan + rebuild) != 1) {
255 if ( (optind + 1) != argc ) {
259 volpath = argv[optind];
261 /* Put "/.AppleDB" at end of volpath */
262 if ( (strlen(volpath) + strlen("/.AppleDB")) > (PATH_MAX -1) ) {
263 dblog( LOGSTD, "Volume pathname too long");
266 char dbpath[PATH_MAX];
267 strncpy(dbpath, volpath, PATH_MAX - 1);
268 strcat(dbpath, "/.AppleDB");
270 if ( -1 == (ret = loadvolinfo(volpath, &volinfo)) ) {
271 dblog( LOGSTD, "Unkown volume options!");
275 if (chdir(dbpath) < 0) {
276 dblog( LOGSTD, "chdir to %s failed: %s", dbpath, strerror(errno));
281 Before we do anything else, check if there is an instance of cnid_dbd
282 running already and silently exit if yes.
286 /* Ignore everything except SIGTERM */
289 /* Setup logging. Should be portable among *NIXes */
291 setuplog("default log_info /dev/tty");
293 setuplog("default log_debug /dev/tty");
296 Lets start with the BerkeleyDB stuff
298 if (dbif_env_init(&db_param, DBOPTIONS) < 0) {
299 dblog( LOGSTD, "error opening database!");
303 dblog( LOGDEBUG, "Finished opening BerkeleyDB databases including recovery.");
305 if (dbif_open(&db_param, rebuildindexes) < 0) {
311 if (dbif_dump(dumpindexes) < 0) {
312 dblog( LOGSTD, "Error dumping database");
316 if (dbif_close() < 0)