]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/cmd_dbd.c
86ff5ec46e2f26c88d7977d0e6d3537e956f7b52
[netatalk.git] / etc / cnid_dbd / cmd_dbd.c
1 /* 
2    $Id: cmd_dbd.c,v 1.6 2009-09-03 08:35:15 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 /*
18   dbd specs and implementation progress
19   =====================================
20
21   St := Status
22
23   Force option
24   ------------
25   
26   St Spec
27   -- ----
28   OK If -f is requested, ensure -e is too.
29      Check if volumes is using AFPVOL_CACHE, then wipe db from disk. Rebuild from ad-files.
30
31   1st pass: Scan volume
32   --------------------
33
34   St Type Check
35   -- ---- -----
36   OK F/D  Make sure ad file exists
37   OK D    Make sure .AppleDouble dir exist, create if missing. Error creating
38           it is fatal as that shouldn't happen as root.
39   OK F/D  Delete orphaned ad-files, log dirs in ad-dir
40   OK F/D  Check name encoding by roundtripping, log on error
41   OK F/D  try: read CNID from ad file (if cnid caching is on)
42           try: fetch CNID from database
43           -> on mismatch: use CNID from file, update database (deleting both found CNIDs first)
44           -> if no CNID in ad file: write CNID from database to ad file
45           -> if no CNID in database: add CNID from ad file to database
46           -> on no CNID at all: create one and store in both places
47   OK F/D  Add found CNID, DID, filename, dev/inode, stamp to rebuild database
48   OK F/D  Check/update stamp (implicitly done while checking CNIDs)
49
50
51   2nd pass: Delete unused CNIDs
52   -----------------------------
53
54   St Spec
55   -- ----
56   OK Step through dbd (the one on disk) and rebuild-db from pass 1 and delete any CNID from
57      dbd not in rebuild db. This in only done in exclusive mode.
58 */
59
60 #ifdef HAVE_CONFIG_H
61 #include "config.h"
62 #endif /* HAVE_CONFIG_H */
63
64 #include <unistd.h>
65 #include <sys/types.h>
66 #include <stdlib.h>
67 #include <stdio.h>
68 #include <stdarg.h>
69 #include <limits.h>
70 #include <signal.h>
71 #include <string.h>
72 #include <errno.h>
73
74 #include <atalk/logger.h>
75 #include <atalk/cnid_dbd_private.h>
76 #include <atalk/volinfo.h>
77 #include "cmd_dbd.h"
78 #include "dbd.h"
79 #include "dbif.h"
80 #include "db_param.h"
81
82 #define LOCKFILENAME  "lock"
83 #define DBOPTIONS (DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN)
84
85 static DBD *dbd;
86
87 volatile sig_atomic_t alarmed;
88 static int verbose;             /* Logging flag */
89 static int exclusive;           /* Exclusive volume access */
90 static struct db_param db_param = {
91     NULL,                       /* Volume dirpath */
92     1,                          /* bdb logfile autoremove */
93     16384,                      /* bdb cachesize */
94     -1,                         /* not used ... */
95     -1,
96     "",
97     -1,
98     -1,
99     -1
100 };
101 static char dbpath[PATH_MAX];   /* Path to the dbd database */
102
103 /* 
104    Provide some logging
105  */
106 void dbd_log(enum logtype lt, char *fmt, ...)
107 {
108     int len;
109     static char logbuffer[1024];
110     va_list args;
111
112     if ( (lt == LOGSTD) || (verbose == 1)) {
113         va_start(args, fmt);
114         len = vsnprintf(logbuffer, 1023, fmt, args);
115         va_end(args);
116         logbuffer[1023] = 0;
117
118         printf("%s\n", logbuffer);
119     }
120 }
121
122 /* 
123    SIGNAL handling:
124    catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
125  */
126
127 static void sig_handler(int signo)
128 {
129     alarmed = 1;
130     return;
131 }
132
133 void set_signal(void)
134 {
135     struct sigaction sv;
136
137     sv.sa_handler = sig_handler;
138     sv.sa_flags = SA_RESTART;
139     sigemptyset(&sv.sa_mask);
140     if (sigaction(SIGTERM, &sv, NULL) < 0) {
141         dbd_log( LOGSTD, "error in sigaction(SIGTERM): %s", strerror(errno));
142         exit(EXIT_FAILURE);
143     }        
144     if (sigaction(SIGINT, &sv, NULL) < 0) {
145         dbd_log( LOGSTD, "error in sigaction(SIGINT): %s", strerror(errno));
146         exit(EXIT_FAILURE);
147     }        
148
149     memset(&sv, 0, sizeof(struct sigaction));
150     sv.sa_handler = SIG_IGN;
151     sigemptyset(&sv.sa_mask);
152
153     if (sigaction(SIGABRT, &sv, NULL) < 0) {
154         dbd_log( LOGSTD, "error in sigaction(SIGABRT): %s", strerror(errno));
155         exit(EXIT_FAILURE);
156     }        
157     if (sigaction(SIGHUP, &sv, NULL) < 0) {
158         dbd_log( LOGSTD, "error in sigaction(SIGHUP): %s", strerror(errno));
159         exit(EXIT_FAILURE);
160     }        
161     if (sigaction(SIGQUIT, &sv, NULL) < 0) {
162         dbd_log( LOGSTD, "error in sigaction(SIGQUIT): %s", strerror(errno));
163         exit(EXIT_FAILURE);
164     }        
165 }
166
167 int get_lock(const char *dbpath)
168 {
169     int lockfd;
170     char lockpath[PATH_MAX];
171     struct flock lock;
172     struct stat st;
173
174     if ( (strlen(dbpath) + strlen(LOCKFILENAME+1)) > (PATH_MAX - 1) ) {
175         dbd_log( LOGSTD, ".AppleDB pathname too long");
176         exit(EXIT_FAILURE);
177     }
178     strncpy(lockpath, dbpath, PATH_MAX - 1);
179     strcat(lockpath, "/");
180     strcat(lockpath, LOCKFILENAME);
181
182     if ((lockfd = open(lockpath, O_RDWR | O_CREAT, 0644)) < 0) {
183         dbd_log( LOGSTD, "Error opening lockfile: %s", strerror(errno));
184         exit(EXIT_FAILURE);
185     }
186
187     if ((stat(dbpath, &st)) != 0) {
188         dbd_log( LOGSTD, "Error statting lockfile: %s", strerror(errno));
189         exit(EXIT_FAILURE);
190     }
191
192     if ((chown(lockpath, st.st_uid, st.st_gid)) != 0) {
193         dbd_log( LOGSTD, "Error inheriting lockfile permissions: %s", strerror(errno));
194         exit(EXIT_FAILURE);
195     }
196     
197     lock.l_start  = 0;
198     lock.l_whence = SEEK_SET;
199     lock.l_len    = 0;
200     lock.l_type   = F_WRLCK;
201
202     if (fcntl(lockfd, F_SETLK, &lock) < 0) {
203         if (errno == EACCES || errno == EAGAIN) {
204             if (exclusive) {
205                 dbd_log( LOGSTD, "Database is in use and exlusive was requested", strerror(errno));        
206                 exit(EXIT_FAILURE);
207             };
208         } else {
209             dbd_log( LOGSTD, "Error getting fcntl F_WRLCK on lockfile: %s", strerror(errno));
210             exit(EXIT_FAILURE);
211        }
212     }
213     
214     return lockfd;
215 }
216
217 void free_lock(int lockfd)
218 {
219     struct flock lock;
220
221     lock.l_start  = 0;
222     lock.l_whence = SEEK_SET;
223     lock.l_len    = 0;
224     lock.l_type = F_UNLCK;
225     fcntl(lockfd, F_SETLK, &lock);
226     close(lockfd);
227 }
228
229 static void usage ()
230 {
231     printf("Usage: dbd [-e|-v|-x|-u] -d [-i] | -s | -r [-f] <path to netatalk volume>\n"
232            "dbd can dump, scan, reindex and rebuild Netatalk dbd CNID databases.\n"
233            "dbd must be run with appropiate permissions i.e. as root.\n\n"
234            "Main commands are:\n"
235            "   -d Dump CNID database\n"
236            "      Option: -i dump indexes too\n"
237            "   -s Scan volume:\n"
238            "      1. Compare CNIDs in database with volume\n"
239            "      2. Check if .AppleDouble dirs exist\n"
240            "      3. Check if  AppleDouble file exist\n"
241            "      4. Report orphaned AppleDouble files\n"
242            "      5. Check for directories inside AppleDouble directories\n"
243            "      6. Check name encoding by roundtripping, log on error\n"
244            "      7. Check for orphaned CNIDs in database (requires -e)\n"
245            "   -r Rebuild volume:\n"
246            "      1. Sync CNIDSs in database with volume\n"
247            "      2. Make sure .AppleDouble dir exist, create if missing\n"
248            "      3. Make sure AppleDouble file exists, create if missing\n"
249            "      4. Delete orphaned AppleDouble files\n"
250            "      5. Check for directories inside AppleDouble directories\n"
251            "      6. Check name encoding by roundtripping, log on error\n"
252            "      7. Check for orphaned CNIDs in database (requires -e)\n"
253            "      Option: -f wipe database and rebuild from IDs stored in AppleDouble files,\n"
254            "                 only available for volumes with 'cachecnid' option. Implies -e.\n"
255            "   -u Prepare upgrade:\n"
256            "      Before installing an upgraded version of Netatalk that is linked against\n"
257            "      a newer BerkeleyDB lib, run `dbd -u ...` from the OLD Netatalk pior to\n"
258            "      upgrading on all volumes. This removes the BerkleyDB environment.\n"
259            "      On exit cnid_dbd does this automatically, so normally calling dbd -u should not be necessary.\n\n"
260            "General options:\n"
261            "   -e only work on inactive volumes and lock them (exclusive)\n"
262            "   -x rebuild indexes (just for completeness, mostly useless!)\n"
263            "   -v verbose\n\n"
264            "WARNING:\n"
265            "If you want/need to run an -r -f rebuild after adding 'cachecnid' to a volume configuration,\n"
266            "you must run a rebuild with -r alone at first in order to sync all existing CNIDs from the db\n"
267            "to the AppleDouble files!\n"
268         );
269 }
270
271 int main(int argc, char **argv)
272 {
273     int c, lockfd, ret = -1;
274     int dump=0, scan=0, rebuild=0, prep_upgrade=0, rebuildindexes=0, dumpindexes=0, force=0;
275     dbd_flags_t flags = 0;
276     char *volpath;
277     struct volinfo volinfo;
278
279     if (geteuid() != 0) {
280         usage();
281         exit(EXIT_FAILURE);
282     }
283
284     while ((c = getopt(argc, argv, ":dsruvxife")) != -1) {
285         switch(c) {
286         case 'd':
287             dump = 1;
288             break;
289         case 'i':
290             dumpindexes = 1;
291             break;
292         case 's':
293             scan = 1;
294             flags |= DBD_FLAGS_SCAN;
295             break;
296         case 'r':
297             rebuild = 1;
298             break;
299         case 'u':
300             prep_upgrade = 1;
301             break;
302         case 'v':
303             verbose = 1;
304             break;
305         case 'e':
306             exclusive = 1;
307             flags |= DBD_FLAGS_EXCL;
308             break;
309         case 'x':
310             rebuildindexes = 1;
311             break;
312         case 'f':
313             force = 1;
314             exclusive = 1;
315             flags |= DBD_FLAGS_FORCE | DBD_FLAGS_EXCL;
316             break;
317         case ':':
318         case '?':
319             usage();
320             exit(EXIT_FAILURE);
321             break;
322         }
323     }
324
325     if ((dump + scan + rebuild + prep_upgrade) != 1) {
326         usage();
327         exit(EXIT_FAILURE);
328     }
329
330     if ( (optind + 1) != argc ) {
331         usage();
332         exit(EXIT_FAILURE);
333     }
334     volpath = argv[optind];
335
336     /* Remember cwd */
337     int cdir;
338     if ((cdir = open(".", O_RDONLY)) < 0) {
339         dbd_log( LOGSTD, "Can't open dir: %s", strerror(errno));
340         exit(EXIT_FAILURE);
341     }
342         
343     /* Setup signal handling */
344     set_signal();
345
346     /* Setup logging. Should be portable among *NIXes */
347     if (!verbose)
348         setuplog("default log_info /dev/tty");
349     else
350         setuplog("default log_debug /dev/tty");
351
352     /* Load .volinfo file */
353     if (loadvolinfo(volpath, &volinfo) == -1) {
354         dbd_log( LOGSTD, "Unkown volume options!");
355         exit(EXIT_FAILURE);
356     }
357     if (vol_load_charsets(&volinfo) == -1) {
358         dbd_log( LOGSTD, "Error loading charsets!");
359         exit(EXIT_FAILURE);
360     }
361
362     /* Put "/.AppleDB" at end of volpath, get path from volinfo file */
363     if ( (strlen(volinfo.v_dbpath) + strlen("/.AppleDB")) > (PATH_MAX - 1) ) {
364         dbd_log( LOGSTD, "Volume pathname too long");
365         exit(EXIT_FAILURE);        
366     }
367     strncpy(dbpath, volinfo.v_dbpath, PATH_MAX - 9 - 1);
368     strcat(dbpath, "/.AppleDB");
369
370     /* 
371        Before we do anything else, check if there is an instance of cnid_dbd
372        running already and silently exit if yes.
373     */
374     lockfd = get_lock(dbpath);
375
376     /* Prepare upgrade ? */
377     if (prep_upgrade) {
378         if (dbif_prep_upgrade(dbpath))
379             goto exit_failure;
380         goto exit_success;
381     }        
382
383     /* Check if -f is requested and wipe db if yes */
384     if ((flags & DBD_FLAGS_FORCE) && rebuild && (volinfo.v_flags & AFPVOL_CACHE)) {
385         char cmd[8 + MAXPATHLEN];
386         snprintf(cmd, 8 + MAXPATHLEN, "rm -f %s/*", dbpath);
387         dbd_log( LOGDEBUG, "Removing old database of volume: '%s'", volpath);
388         system(cmd);
389         dbd_log( LOGDEBUG, "Removed old database.");
390     }
391
392     /* 
393        Lets start with the BerkeleyDB stuff
394     */
395     if ((dbd = dbif_init(dbpath, "cnid2.db")) == NULL)
396         goto exit_failure;
397
398     if (dbif_env_open(dbd, &db_param, exclusive ? (DBOPTIONS | DB_RECOVER) : DBOPTIONS) < 0) {
399         dbd_log( LOGSTD, "error opening database!");
400         goto exit_failure;
401     }
402
403     if (exclusive)
404         dbd_log( LOGDEBUG, "Finished recovery.");
405
406     if (dbif_open(dbd, &db_param, rebuildindexes) < 0) {
407         dbif_close(dbd);
408         goto exit_failure;
409     }
410
411     if (dbd_stamp(dbd) < 0) {
412         dbif_close(dbd);
413         goto exit_failure;
414     }
415
416     if (dump) {
417         if (dbif_dump(dbd, dumpindexes) < 0) {
418             dbd_log( LOGSTD, "Error dumping database");
419         }
420     } else if (rebuild || scan) {
421         if (cmd_dbd_scanvol(dbd, &volinfo, flags) < 0) {
422             dbd_log( LOGSTD, "Error repairing database.");
423         }
424     }
425
426     if (dbif_close(dbd) < 0) {
427         dbd_log( LOGSTD, "Error closing database");
428         goto exit_failure;
429     }
430
431 exit_success:
432     ret = 0;
433
434 exit_failure:
435     free_lock(lockfd);
436     
437     if ((fchdir(cdir)) < 0)
438         dbd_log(LOGSTD, "fchdir: %s", strerror(errno));
439
440     if (ret == 0)
441         exit(EXIT_SUCCESS);
442     else
443         exit(EXIT_FAILURE);
444 }