]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_open.c
3d8b5fee8d14f6b38f92c623c4a3ab2de1ff658e
[netatalk.git] / libatalk / cnid / cnid_open.c
1 /*
2  * $Id: cnid_open.c,v 1.41 2002-06-03 22:58:10 jmarcus Exp $
3  *
4  * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
5  * All Rights Reserved. See COPYRIGHT.
6  *
7  * CNID database support. 
8  *
9  * here's the deal:
10  *  1) afpd already caches did's. 
11  *  2) the database stores cnid's as both did/name and dev/ino pairs. 
12  *  3) RootInfo holds the value of the NextID.
13  *  4) the cnid database gets called in the following manner --
14  *     start a database:
15  *     cnid = cnid_open(root_dir);
16  *
17  *     allocate a new id: 
18  *     newid = cnid_add(cnid, dev, ino, parent did,
19  *     name, id); id is a hint for a specific id. pass 0 if you don't
20  *     care. if the id is already assigned, you won't get what you
21  *     requested.
22  *
23  *     given an id, get a did/name and dev/ino pair.
24  *     name = cnid_get(cnid, &id); given an id, return the corresponding
25  *     info.
26  *     return code = cnid_delete(cnid, id); delete an entry. 
27  *
28  * with AFP, CNIDs 0-2 have special meanings. here they are:
29  * 0 -- invalid cnid
30  * 1 -- parent of root directory (handled by afpd) 
31  * 2 -- root directory (handled by afpd)
32  *
33  * CNIDs 4-16 are reserved according to page 31 of the AFP 3.0 spec so, 
34  * CNID_START begins at 17.
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif /* HAVE_CONFIG_H */
40
41 #ifdef CNID_DB
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif /* HAVE_UNISTD_H */
48 #ifdef HAVE_FCNTL_H
49 #include <fcntl.h>
50 #endif /* HAVE_FCNTL_H */
51 #include <sys/param.h>
52 #include <sys/stat.h>
53 #include <atalk/logger.h>
54 #ifdef HAVE_SYS_TIME_H
55 #include <sys/time.h>
56 #endif /* HAVE_SYS_TIME_H */
57
58 #include <db.h>
59
60 #include <atalk/adouble.h>
61 #include <atalk/cnid.h>
62 #include <atalk/util.h>
63
64 #include "cnid_private.h"
65
66 #ifndef MIN
67 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
68 #endif /* ! MIN */
69
70 #define DBHOME        ".AppleDB"
71 #define DBCNID        "cnid.db"
72 #define DBDEVINO      "devino.db"
73 #define DBDIDNAME     "didname.db"   /* did/full name mapping */
74 #define DBSHORTNAME   "shortname.db" /* did/8+3 mapping */
75 #define DBMACNAME     "macname.db"   /* did/31 mapping */
76 #define DBMANGLE      "mangle.db"    /* filename mangling */
77 #define DBLONGNAME    "longname.db"  /* did/unicode mapping */
78 #define DBLOCKFILE    "cnid.lock"
79 #define DBRECOVERFILE "cnid.dbrecover"
80 #define DBCLOSEFILE   "cnid.close"
81
82 #define DBHOMELEN    8
83 #define DBLEN        10
84
85 /* we version the did/name database so that we can change the format
86  * if necessary. the key is in the form of a did/name pair. in this case,
87  * we use 0/0. */
88 #define DBVERSION_KEY    "\0\0\0\0\0"
89 #define DBVERSION_KEYLEN 5
90 #define DBVERSION1       0x00000001U
91 #define DBVERSION        DBVERSION1
92
93 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
94 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
95 DB_INIT_LOG | DB_INIT_TXN)
96 #else /* DB_VERSION_MINOR < 1 */
97 /*#define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
98 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)*/
99 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
100 DB_INIT_LOG | DB_INIT_TXN)
101 #endif /* DB_VERSION_MINOR */
102
103 /* Let's try and use the youngest lock detector if present.
104  * If we can't do that, then let DB3 use its default deadlock detector. */
105 #if defined DB_LOCK_YOUNGEST
106 #define DEAD_LOCK_DETECT DB_LOCK_YOUNGEST
107 #else /* DB_LOCK_YOUNGEST */
108 #define DEAD_LOCK_DETECT DB_LOCK_DEFAULT
109 #endif /* DB_LOCK_YOUNGEST */
110
111 #define MAXITER     0xFFFF /* maximum number of simultaneously open CNID
112 * databases. */
113
114 /* the first compare that's always done. */
115 static __inline__ int compare_did(const DBT *a, const DBT *b)
116 {
117     u_int32_t dida, didb;
118
119     memcpy(&dida, a->data, sizeof(dida));
120     memcpy(&didb, b->data, sizeof(didb));
121     return dida - didb;
122 }
123
124 /* sort did's and then names. this is for unix paths.
125  * i.e., did/unixname lookups. */
126 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
127 static int compare_unix(DB *db, const DBT *a, const DBT *b)
128 #else /* DB_VERSION_MINOR < 1 */
129 static int compare_unix(const DBT *a, const DBT *b)
130 #endif /* DB_VERSION_MINOR */
131 {
132     u_int8_t *sa, *sb;
133     int len, ret;
134
135     /* sort by did */
136     if ((ret = compare_did(a, b)))
137         return ret;
138
139     sa = (u_int8_t *) a->data + 4; /* shift past did */
140     sb = (u_int8_t *) b->data + 4;
141     for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
142         if ((ret = (*sa - *sb)))
143             return ret; /* sort by lexical ordering */
144
145     return a->size - b->size; /* sort by length */
146 }
147
148 /* sort did's and then names. this is for macified paths (i.e.,
149  * did/macname, and did/shortname. i think did/longname needs a
150  * unicode table to work. also, we can't use strdiacasecmp as that
151  * returns a match if a < b. */
152 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
153 static int compare_mac(DB *db, const DBT *a, const DBT *b)
154 #else /* DB_VERSION_MINOR < 1 */
155 static int compare_mac(const DBT *a, const DBT *b)
156 #endif /* DB_VERSION_MINOR */
157 {
158     u_int8_t *sa, *sb;
159     int len, ret;
160
161     /* sort by did */
162     if ((ret = compare_did(a, b)))
163         return ret;
164
165     sa = (u_int8_t *) a->data + 4;
166     sb = (u_int8_t *) b->data + 4;
167     for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
168         if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
169             return ret; /* sort by lexical ordering */
170
171     return a->size - b->size; /* sort by length */
172 }
173
174
175 /* for unicode names -- right now it's the same as compare_mac. */
176 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
177 static int compare_unicode(DB *db, const DBT *a, const DBT *b)
178 #else /* DB_VERSION_MINOR < 1 */
179 static int compare_unicode(const DBT *a, const DBT *b)
180 #endif /* DB_VERSION_MINOR */
181 {
182 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
183     return compare_mac(db,a,b);
184 #else /* DB_VERSION_MINOR < 1 */
185     return compare_mac(a,b);
186 #endif /* DB_VERSION_MINOR */
187 }
188
189 void *cnid_open(const char *dir) {
190     struct stat st, rsb, lsb, csb;
191     struct flock lock;
192     char path[MAXPATHLEN + 1];
193     CNID_private *db;
194     DBT key, data;
195     DB_TXN *tid;
196     int open_flag, len;
197     int rc;
198
199     if (!dir) {
200         return NULL;
201     }
202
203     /* this checks .AppleDB */
204     if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
205         LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", dir);
206         return NULL;
207     }
208
209     if ((db = (CNID_private *)calloc(1, sizeof(CNID_private))) == NULL) {
210         LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
211         return NULL;
212     }
213
214     db->magic = CNID_DB_MAGIC;
215
216     strcpy(path, dir);
217     if (path[len - 1] != '/') {
218         strcat(path, "/");
219         len++;
220     }
221
222     lock.l_type = F_WRLCK;
223     lock.l_whence = SEEK_SET;
224
225     strcpy(path + len, DBHOME);
226     if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
227         LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
228         goto fail_adouble;
229     }
230
231     /* Make sure cnid.lock goes in .AppleDB. */
232     strcat(path, "/");
233     len++;
234
235     /* Search for a byte lock.  This allows us to cleanup the log files
236      * at cnid_close() in a clean fashion.
237      *
238      * NOTE: This won't work if multiple volumes for the same user refer
239      * to the sahe directory. */
240     strcat(path, DBLOCKFILE);
241     strcpy(db->lock_file, path);
242     if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
243         lock.l_start = 0;
244         lock.l_len = 1;
245         while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
246             if (++lock.l_start > MAXITER) {
247                 LOG(log_error, logtype_default, "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
248                 close(db->lockfd);
249                 db->lockfd = -1;
250                 break;
251             }
252         }
253     }
254     else {
255         LOG(log_error, logtype_default, "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
256     }
257
258     path[len + DBHOMELEN] = '\0';
259     open_flag = DB_CREATE;
260
261     /* We need to be able to open the database environment with full
262      * transaction, logging, and locking support if we ever hope to 
263      * be a true multi-acess file server. */
264     if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
265         LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
266         goto fail_lock;
267     }
268
269     /* Setup internal deadlock detection. */
270     if ((rc = db->dbenv->set_lk_detect(db->dbenv, DEAD_LOCK_DETECT)) != 0) {
271         LOG(log_error, logtype_default, "cnid_open: set_lk_detect: %s", db_strerror(rc));
272         goto fail_lock;
273     }
274
275 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
276 #if 0
277     /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
278     if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
279         LOG(log_error, logtype_default, "cnid_open: set_flags: %s", db_strerror(rc));
280         goto fail_lock;
281     }
282 #endif
283 #endif /* DB_VERSION_MINOR > 1 */
284
285     /* Open the database environment. */
286     if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666)) != 0) {
287         if (rc == DB_RUNRECOVERY) {
288             /* This is the mother of all errors.  We _must_ fail here. */
289             LOG(log_error, logtype_default, "cnid_open: CATASTROPHIC ERROR opening database environment %s.  Run db_recovery -c immediately", path);
290             goto fail_lock;
291         }
292
293         /* We can't get a full transactional environment, so multi-access
294          * is out of the question.  Let's assume a read-only environment,
295          * and try to at least get a shared memory pool. */
296         if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666)) != 0) {
297             /* Nope, not a MPOOL, either.  Last-ditch effort: we'll try to
298              * open the environment with no flags. */
299             if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666)) != 0) {
300                 LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s",
301                     path, db_strerror(rc));
302                 goto fail_lock;
303             }
304         }
305         db->flags |= CNIDFLAG_DB_RO;
306         open_flag = DB_RDONLY;
307         LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
308     }
309
310     /* did/name reverse mapping.  We use a BTree for this one. */
311     if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
312         LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s",
313             db_strerror(rc));
314         goto fail_appinit;
315     }
316
317     /*db->db_didname->set_bt_compare(db->db_didname, &compare_unix);*/
318     if ((rc = db->db_didname->open(db->db_didname, DBDIDNAME, NULL,
319                                    DB_HASH, open_flag, 0666))) {
320         LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
321             db_strerror(rc));
322         goto fail_appinit;
323     }
324
325     /* Check for version.  This way we can update the database if we need
326      * to change the format in any way. */
327     memset(&key, 0, sizeof(key));
328     memset(&data, 0, sizeof(data));
329     key.data = DBVERSION_KEY;
330     key.size = DBVERSION_KEYLEN;
331
332 dbversion_retry:
333     if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
334         LOG(log_error, logtype_default, "cnid_open: txn_begin: failed to check db version: %s",
335             db_strerror(rc));
336         db->db_didname->close(db->db_didname, 0);
337         goto fail_appinit;
338     }
339
340     while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
341         int ret;
342         switch (rc) {
343         case DB_LOCK_DEADLOCK:
344             if ((ret = txn_abort(tid)) != 0) {
345                 LOG(log_error, logtype_default, "cnid_open: txn_abort: %s", db_strerror(ret));
346                 db->db_didname->close(db->db_didname, 0);
347                 goto fail_appinit;
348             }
349             goto dbversion_retry;
350         case DB_NOTFOUND:
351             {
352                 u_int32_t version = htonl(DBVERSION);
353
354                 data.data = &version;
355                 data.size = sizeof(version);
356             }
357
358             if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data,
359                                            DB_NOOVERWRITE))) {
360                 if (ret == DB_LOCK_DEADLOCK) {
361                     if ((ret = txn_abort(tid)) != 0) {
362                         LOG(log_error, logtype_default, "cnid_open: txn_abort: %s",
363                             db_strerror(ret));
364                         db->db_didname->close(db->db_didname, 0);
365                         goto fail_appinit;
366                     }
367                     goto dbversion_retry;
368                 }
369                 else if (ret == DB_RUNRECOVERY) {
370                     /* At this point, we don't care if the transaction aborts
371                      * successfully or not. */
372                     txn_abort(tid);
373                     LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s",
374                         db_strerror(ret));
375                     db->db_didname->close(db->db_didname, 0);
376                     goto fail_appinit;
377                 }
378             }
379             break; /* while loop */
380         default:
381             txn_abort(tid);
382             LOG(log_error, logtype_default, "cnid_open: Failed to check db version: %s",
383                 db_strerror(rc));
384             db->db_didname->close(db->db_didname, 0);
385             goto fail_appinit;
386         }
387     }
388
389     if ((rc = txn_commit(tid, 0)) != 0) {
390         LOG(log_error, logtype_default, "cnid_open: Failed to commit db version: %s",
391             db_strerror(rc));
392         db->db_didname->close(db->db_didname, 0);
393         goto fail_appinit;
394     }
395
396     /* TODO In the future we might check for version number here. */
397 #if 0
398     memcpy(&version, data.data, sizeof(version));
399     if (version != ntohl(DBVERSION)) {
400         /* Do stuff here. */
401     }
402 #endif /* 0 */
403
404 #ifdef EXTENDED_DB
405     /* did/macname (31 character) mapping.  Use a BTree for this one. */
406     if ((rc = db_create(&db->db_macname, db->dbenv, 0)) != 0) {
407         LOG(log_error, logtype_default, "cnid_open: Failed to create did/macname database: %s",
408             db_strerror(rc));
409         db->db_didname->close(db->db_didname, 0);
410         goto fail_appinit;
411     }
412
413     db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
414     if ((rc = db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
415         LOG(log_error, logtype_default, "cnid_open: Failed to open did/macname database: %s",
416             db_strerror(rc));
417         db->db_didname->close(db->db_didname, 0);
418         goto fail_appinit;
419     }
420
421     /* did/shortname (DOS 8.3) mapping.  Use a BTree for this one. */
422     if ((rc = db_create(&db->db_shortname, db->dbenv, 0)) != 0) {
423         LOG(log_error, logtype_default, "cnid_open: Failed to create did/shortname database: %s",
424             db_strerror(rc));
425         db->db_didname->close(db->db_didname, 0);
426         db->db_macname->close(db->db_macname, 0);
427         goto fail_appinit;
428     }
429
430     db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
431     if ((rc = db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
432         LOG(log_error, logtype_default, "cnid_open: Failed to open did/shortname database: %s",
433             db_strerror(rc));
434         db->db_didname->close(db->db_didname, 0);
435         db->db_macname->close(db->db_macname, 0);
436         goto fail_appinit;
437     }
438
439     /* did/longname (Unicode) mapping.  Use a BTree for this one. */
440     if ((rc = db_create(&db->db_longname, db->dbenv, 0)) != 0) {
441         LOG(log_error, logtype_default, "cnid_open: Failed to create did/longname database: %s",
442             db_strerror(rc));
443         db->db_didname->close(db->db_didname, 0);
444         db->db_macname->close(db->db_macname, 0);
445         db->db_shortname->close(db->db_shortname, 0);
446         goto fail_appinit;
447     }
448
449     db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
450     if ((rc = db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
451         LOG(log_error, logtype_default, "cnid_open: Failed to open did/longname database: %s",
452             db_strerror(rc));
453         db->db_didname->close(db->db_didname, 0);
454         db->db_macname->close(db->db_macname, 0);
455         db->db_shortname->close(db->db_shortname, 0);
456         goto fail_appinit;
457     }
458 #endif /* EXTENDED_DB */
459
460     /* dev/ino reverse mapping.  Use a hash for this one. */
461     if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
462         LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s",
463             db_strerror(rc));
464         db->db_didname->close(db->db_didname, 0);
465 #ifdef EXTENDED_DB
466         db->db_macname->close(db->db_macname, 0);
467         db->db_shortname->close(db->db_shortname, 0);
468         db->db_longname->close(db->db_longname, 0);
469 #endif /* EXTENDED_DB */
470         goto fail_appinit;
471     }
472
473     if ((rc = db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666)) != 0) {
474         LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
475             db_strerror(rc));
476         db->db_didname->close(db->db_didname, 0);
477 #ifdef EXTENDED_DB
478         db->db_macname->close(db->db_macname, 0);
479         db->db_shortname->close(db->db_shortname, 0);
480         db->db_longname->close(db->db_longname, 0);
481 #endif /* EXTENDED_DB */
482         goto fail_appinit;
483     }
484
485     /* Main CNID database.  Use a hash for this one. */
486     if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
487         LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s",
488             db_strerror(rc));
489         db->db_didname->close(db->db_didname, 0);
490 #ifdef EXTENDED_DB
491         db->db_macname->close(db->db_macname, 0);
492         db->db_shortname->close(db->db_shortname, 0);
493         db->db_longname->close(db->db_longname, 0);
494 #endif /* EXTENDED_DB */
495         db->db_devino->close(db->db_devino, 0);
496         goto fail_appinit;
497     }
498
499
500     if ((rc = db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666)) != 0) {
501         LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
502             db_strerror(rc));
503         db->db_didname->close(db->db_didname, 0);
504 #ifdef EXTENDED_DB
505         db->db_macname->close(db->db_macname, 0);
506         db->db_shortname->close(db->db_shortname, 0);
507         db->db_longname->close(db->db_longname, 0);
508 #endif /* EXTENDED_DB */
509         db->db_devino->close(db->db_devino, 0);
510         goto fail_appinit;
511     }
512
513 #ifdef FILE_MANGLING
514     /* filename mangling database.  Use a hash for this one. */
515     if ((rc = db_create(&db->db_mangle, db->dbenv, 0)) != 0) {
516         LOG(log_error, logtype_default, "cnid_open: Failed to create mangle database: %s", db_strerror(rc));
517         db->db_didname->close(db->db_didname, 0);
518         db->db_devino->close(db->db_devino, 0);
519         db->db_cnid->close(db->db_cnid, 0);
520 #ifdef EXTENDED_DB
521         db->db_macname->close(db->db_macname, 0);
522         db->db_shortname->close(db->db_shortname, 0);
523         db->db_longname->close(db->db_longname, 0);
524 #endif /* EXTENDED_DB */
525         goto fail_appinit;
526     }
527
528     if ((rc = db->db_mangle->open(db->db_mangle, DBMANGLE, NULL, DB_HASH, open_flag, 0666)) != 0) {
529         LOG(log_error, logtype_default, "cnid_open: Failed to open mangle database: %s", db_strerror(rc));
530         db->db_didname->close(db->db_didname, 0);
531         db->db_devino->close(db->db_devino, 0);
532         db->db_cnid->close(db->db_cnid, 0);
533 #ifdef EXTENDED_DB
534         db->db_macname->close(db->db_macname, 0);
535         db->db_shortname->close(db->db_shortname, 0);
536         db->db_longname->close(db->db_longname, 0);
537 #endif /* EXTENDED_DB */
538         goto fail_appinit;
539     }
540 #endif /* FILE_MANGLING */
541
542     /* Print out the version of DB3 we're linked against. */
543     LOG(log_info, logtype_default, "CNID DB initialized using %s",
544         db_version(NULL, NULL, NULL));
545
546     return db;
547
548 fail_appinit:
549     LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
550     db->dbenv->close(db->dbenv, 0);
551
552 fail_lock:
553     if (db->lockfd > -1) {
554         close(db->lockfd);
555         (void)remove(db->lock_file);
556     }
557
558 fail_adouble:
559
560 fail_db:
561     free(db);
562     return NULL;
563 }
564 #endif /* CNID_DB */
565
566