]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/dbd.c
New utility to maintain dbd databases: dbd. Also replaces cnid_index. Still incomplete.
[netatalk.git] / etc / cnid_dbd / dbd.c
1 /* 
2    $Id: dbd.c,v 1.1 2009-04-28 13:01:24 franklahm Exp $
3
4    Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
5    
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.
10  
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.
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif /* HAVE_CONFIG_H */
20
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <limits.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <errno.h>
30
31 #include <atalk/logger.h>
32 #include <atalk/cnid_dbd_private.h>
33 #include <atalk/volinfo.h>
34 #include "dbif.h"
35 #include "db_param.h"
36
37 #define LOCKFILENAME  "lock"
38 #define DBOPTIONS (DB_CREATE | DB_INIT_LOCK | DB_INIT_MPOOL | DB_INIT_TXN)
39
40 enum logtype {LOGSTD, LOGDEBUG};
41
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 ... */
51     -1,
52     "",
53     -1,
54     -1,
55     -1
56 };
57
58 /* 
59    Provide some logging
60  */
61 static void dblog(enum logtype lt, char *fmt, ...)
62 {
63     int len;
64     static char logbuffer[1024];
65     va_list args;
66
67     if ( (lt == LOGSTD) || (verbose == 1)) {
68         va_start(args, fmt);
69         len = vsnprintf(logbuffer, 1023, fmt, args);
70         va_end(args);
71         logbuffer[1023] = 0;
72
73         printf("%s\n", logbuffer);
74     }
75 }
76
77 /* 
78    SIGNAL handling:
79    ignore everything except SIGTERM which we catch and which causes
80    a clean exit.
81  */
82
83 static void sig_handler(int signo)
84 {
85     alarmed = 1;
86     return;
87 }
88
89 void set_signal(void)
90 {
91     struct sigaction sv;
92
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));
99         exit(EXIT_FAILURE);
100     }        
101
102     sv.sa_handler = SIG_IGN;
103     memset(&sv, 0, sizeof(struct sigaction));
104     sigemptyset(&sv.sa_mask);
105
106     if (sigaction(SIGINT, &sv, NULL) < 0) {
107         dblog( LOGSTD, "error in sigaction(SIGINT): %s", strerror(errno));
108         exit(EXIT_FAILURE);
109     }        
110     if (sigaction(SIGABRT, &sv, NULL) < 0) {
111         dblog( LOGSTD, "error in sigaction(SIGABRT): %s", strerror(errno));
112         exit(EXIT_FAILURE);
113     }        
114     if (sigaction(SIGHUP, &sv, NULL) < 0) {
115         dblog( LOGSTD, "error in sigaction(SIGHUP): %s", strerror(errno));
116         exit(EXIT_FAILURE);
117     }        
118     if (sigaction(SIGQUIT, &sv, NULL) < 0) {
119         dblog( LOGSTD, "error in sigaction(SIGQUIT): %s", strerror(errno));
120         exit(EXIT_FAILURE);
121     }        
122 }
123
124 #if 0
125 static void block_sigs_onoff(int block)
126 {
127     sigset_t set;
128     
129     sigemptyset(&set);
130     sigaddset(&set, SIGINT);
131     sigaddset(&set, SIGTERM);
132     if (block)
133         sigprocmask(SIG_BLOCK, &set, NULL);
134     else
135         sigprocmask(SIG_UNBLOCK, &set, NULL);
136     return;
137 }
138 #endif
139
140 int get_lock(void)
141 {
142     int lockfd;
143     struct flock lock;
144
145     if ((lockfd = open(LOCKFILENAME, O_RDWR | O_CREAT, 0644)) < 0) {
146         dblog( LOGSTD, "Error opening lockfile: %s", strerror(errno));
147         exit(EXIT_FAILURE);
148     }
149     
150     lock.l_start  = 0;
151     lock.l_whence = SEEK_SET;
152     lock.l_len    = 0;
153     lock.l_type   = F_WRLCK;
154
155     if (fcntl(lockfd, F_SETLK, &lock) < 0) {
156         if (errno == EACCES || errno == EAGAIN) {
157             if (exclusive) {
158                 dblog( LOGDEBUG, "Database is in use and exlusive was requested", strerror(errno));        
159                 exit(EXIT_FAILURE);
160             };
161         } else {
162             dblog( LOGSTD, "Error getting fcntl F_WRLCK on lockfile: %s", strerror(errno));
163             exit(EXIT_FAILURE);
164        }
165     }
166     
167     return lockfd;
168 }
169
170 void free_lock(int lockfd)
171 {
172     struct flock lock;
173
174     lock.l_start  = 0;
175     lock.l_whence = SEEK_SET;
176     lock.l_len    = 0;
177     lock.l_type = F_UNLCK;
178     fcntl(lockfd, F_SETLK, &lock);
179     close(lockfd);
180 }
181
182 static void usage ()
183 {
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"
188            "   -d dump\n"
189            "      Dump CNID database\n"
190            "      Option: -i dump indexes too\n"
191            "   -s scan\n"
192            "      Compare database with volume\n"
193            "   -r rebuild\n"
194            "      Rebuild CNID database\n"
195            "      Option: -f wipe database and rebuild from IDs stored in AppleDouble files\n\n"
196            "General options:\n"
197            "   -e only work on inactive volumes and lock them (exclusive)\n"
198            "   -x rebuild indexes\n"
199            "   -v verbose\n"
200            "\n"
201            "28-04-2009: -s and -r and not yet implemented\n"
202         );
203 }
204
205 int main(int argc, char **argv)
206 {
207     int c, ret, lockfd;
208     int dump=0, scan=0, rebuild=0, rebuildindexes=0, dumpindexes=0, force=0;
209     char *volpath;
210
211     if (geteuid() != 0) {
212         usage();
213         exit(EXIT_FAILURE);
214     }
215
216     while ((c = getopt(argc, argv, ":dsrvxif")) != -1) {
217         switch(c) {
218         case 'd':
219             dump = 1;
220             break;
221         case 'i':
222             dumpindexes = 1;
223             break;
224         case 's':
225             scan = 1;
226             break;
227         case 'r':
228             rebuild = 1;
229             break;
230         case 'v':
231             verbose = 1;
232             break;
233         case 'e':
234             exclusive = 1;
235             break;
236         case 'x':
237             rebuildindexes = 1;
238             break;
239         case 'f':
240             force = 1;
241             break;
242         case ':':
243         case '?':
244             usage();
245             exit(EXIT_FAILURE);
246             break;
247         }
248     }
249
250     if ((dump + scan + rebuild) != 1) {
251         usage();
252         exit(EXIT_FAILURE);
253     }
254
255     if ( (optind + 1) != argc ) {
256         usage();
257         exit(EXIT_FAILURE);
258     }
259     volpath = argv[optind];
260
261     /* Put "/.AppleDB" at end of volpath */
262     if ( (strlen(volpath) + strlen("/.AppleDB")) > (PATH_MAX -1) ) {
263         dblog( LOGSTD, "Volume pathname too long");
264         exit(EXIT_FAILURE);        
265     }
266     char dbpath[PATH_MAX];
267     strncpy(dbpath, volpath, PATH_MAX - 1);
268     strcat(dbpath, "/.AppleDB");
269
270     if ( -1 == (ret = loadvolinfo(volpath, &volinfo)) ) {
271         dblog( LOGSTD, "Unkown volume options!");
272         exit(EXIT_FAILURE);
273     }
274     
275     if (chdir(dbpath) < 0) {
276         dblog( LOGSTD, "chdir to %s failed: %s", dbpath, strerror(errno));
277         exit(EXIT_FAILURE);
278     }
279     
280     /* 
281        Before we do anything else, check if there is an instance of cnid_dbd
282        running already and silently exit if yes.
283     */
284     lockfd = get_lock();
285     
286     /* Ignore everything except SIGTERM */
287     set_signal();
288
289     /* Setup logging. Should be portable among *NIXes */
290     if (!verbose)
291         setuplog("default log_info /dev/tty");
292     else
293         setuplog("default log_debug /dev/tty");
294
295     /* 
296        Lets start with the BerkeleyDB stuff
297     */
298     if (dbif_env_init(&db_param, DBOPTIONS) < 0) {
299         dblog( LOGSTD, "error opening database!");
300         exit(EXIT_FAILURE);
301     }
302
303     dblog( LOGDEBUG, "Finished opening BerkeleyDB databases including recovery.");
304
305     if (dbif_open(&db_param, rebuildindexes) < 0) {
306         dbif_close();
307         exit(EXIT_FAILURE);
308     }
309
310     if (dump) {
311         if (dbif_dump(dumpindexes) < 0) {
312             dblog( LOGSTD, "Error dumping database");
313         }
314     }
315
316     if (dbif_close() < 0)
317         exit(EXIT_FAILURE);
318
319     free_lock(lockfd);
320
321     return 0;
322 }