]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_open.c
f1cf2c6f40f66001d61b41c07a2403eb59cc02be
[netatalk.git] / libatalk / cnid / cnid_open.c
1 /*
2  * $Id: cnid_open.c,v 1.52 2003-06-06 20:25:32 srittau 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 #ifdef CNID_DB_CDB
94 #define DBOPTIONS    (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL)
95 #else /* !CNID_DB_CDB */
96 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
97 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
98 DB_INIT_LOG | DB_INIT_TXN)
99 #else /* DB_VERSION_MINOR < 1 */
100 /*#define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
101 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)*/
102 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
103 DB_INIT_LOG | DB_INIT_TXN)
104 #endif /* DB_VERSION_MINOR */
105 #endif /* CNID_DB_CDB */
106
107 #ifndef CNID_DB_CDB
108 /* Let's try and use the youngest lock detector if present.
109  * If we can't do that, then let BDB use its default deadlock detector. */
110 #if defined DB_LOCK_YOUNGEST
111 #define DEAD_LOCK_DETECT DB_LOCK_YOUNGEST
112 #else /* DB_LOCK_YOUNGEST */
113 #define DEAD_LOCK_DETECT DB_LOCK_DEFAULT
114 #endif /* DB_LOCK_YOUNGEST */
115 #endif /* CNID_DB_CDB */
116
117 #define MAXITER     0xFFFF /* maximum number of simultaneously open CNID
118 * databases. */
119
120 #if 0
121 /* -----------------------
122  * bandaid for LanTest performance pb. for now not used, cf. ifdef 0 below
123 */
124 static int my_yield(void) 
125 {
126     struct timeval t;
127     int ret;
128
129     t.tv_sec = 0;
130     t.tv_usec = 1000;
131     ret = select(0, NULL, NULL, NULL, &t);
132     return 0;
133 }
134 #endif
135
136 /* --------------- */
137 static int  my_open(DB *p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
138 {
139 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
140     return p->open(p, NULL, f, d, t, flags, mode);
141 #else
142     return p->open(p,       f, d, t, flags, mode);
143 #endif
144 }
145
146 /* --------------- */
147 #ifdef EXTENDED_DB
148 /* the first compare that's always done. */
149 static __inline__ int compare_did(const DBT *a, const DBT *b)
150 {
151     u_int32_t dida, didb;
152
153     memcpy(&dida, a->data, sizeof(dida));
154     memcpy(&didb, b->data, sizeof(didb));
155     return dida - didb;
156 }
157
158 #if 0
159 /* sort did's and then names. this is for unix paths.
160  * i.e., did/unixname lookups. */
161 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
162 static int compare_unix(DB *db, const DBT *a, const DBT *b)
163 #else /* DB_VERSION_MINOR < 1 */
164 static int compare_unix(const DBT *a, const DBT *b)
165 #endif /* DB_VERSION_MINOR */
166 {
167     u_int8_t *sa, *sb;
168     int len, ret;
169
170     /* sort by did */
171     if ((ret = compare_did(a, b)))
172         return ret;
173
174     sa = (u_int8_t *) a->data + 4; /* shift past did */
175     sb = (u_int8_t *) b->data + 4;
176     for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
177         if ((ret = (*sa - *sb)))
178             return ret; /* sort by lexical ordering */
179
180     return a->size - b->size; /* sort by length */
181 }
182 #endif
183
184 /* sort did's and then names. this is for macified paths (i.e.,
185  * did/macname, and did/shortname. i think did/longname needs a
186  * unicode table to work. also, we can't use strdiacasecmp as that
187  * returns a match if a < b. */
188 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
189 static int compare_mac(DB *db, const DBT *a, const DBT *b)
190 #else /* DB_VERSION_MINOR < 1 */
191 static int compare_mac(const DBT *a, const DBT *b)
192 #endif /* DB_VERSION_MINOR */
193 {
194     u_int8_t *sa, *sb;
195     int len, ret;
196
197     /* sort by did */
198     if ((ret = compare_did(a, b)))
199         return ret;
200
201     sa = (u_int8_t *) a->data + 4;
202     sb = (u_int8_t *) b->data + 4;
203     for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
204         if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
205             return ret; /* sort by lexical ordering */
206
207     return a->size - b->size; /* sort by length */
208 }
209
210
211 /* for unicode names -- right now it's the same as compare_mac. */
212 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
213 static int compare_unicode(DB *db, const DBT *a, const DBT *b)
214 #else /* DB_VERSION_MINOR < 1 */
215 static int compare_unicode(const DBT *a, const DBT *b)
216 #endif /* DB_VERSION_MINOR */
217 {
218 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
219     return compare_mac(db,a,b);
220 #else /* DB_VERSION_MINOR < 1 */
221     return compare_mac(a,b);
222 #endif /* DB_VERSION_MINOR */
223 }
224 #endif /* EXTENDED_DB */
225
226 void *cnid_open(const char *dir, mode_t mask) {
227     struct stat st;
228 #ifndef CNID_DB_CDB
229     struct flock lock;
230 #endif /* CNID_DB_CDB */
231 char path[MAXPATHLEN + 1];
232     CNID_private *db;
233     DBT key, data;
234     int open_flag, len;
235     int rc;
236
237     if (!dir) {
238         return NULL;
239     }
240
241     /* this checks .AppleDB */
242     if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
243         LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", dir);
244         return NULL;
245     }
246
247     if ((db = (CNID_private *)calloc(1, sizeof(CNID_private))) == NULL) {
248         LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
249         return NULL;
250     }
251
252     db->magic = CNID_DB_MAGIC;
253
254     strcpy(path, dir);
255     if (path[len - 1] != '/') {
256         strcat(path, "/");
257         len++;
258     }
259
260     strcpy(path + len, DBHOME);
261     if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777 & ~mask) < 0)) {
262         LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
263         goto fail_adouble;
264     }
265
266 #ifndef CNID_DB_CDB
267     lock.l_type = F_WRLCK;
268     lock.l_whence = SEEK_SET;
269     /* Make sure cnid.lock goes in .AppleDB. */
270     strcat(path, "/");
271     len++;
272
273     /* Search for a byte lock.  This allows us to cleanup the log files
274      * at cnid_close() in a clean fashion.
275      *
276      * NOTE: This won't work if multiple volumes for the same user refer
277      * to the sahe directory. */
278     strcat(path, DBLOCKFILE);
279     strcpy(db->lock_file, path);
280     if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666 & ~mask)) > -1) {
281         lock.l_start = 0;
282         lock.l_len = 1;
283         while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
284             if (++lock.l_start > MAXITER) {
285                 LOG(log_error, logtype_default, "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
286                 close(db->lockfd);
287                 db->lockfd = -1;
288                 break;
289             }
290         }
291     }
292     else {
293         LOG(log_error, logtype_default, "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
294     }
295 #endif /* CNID_DB_CDB */
296
297     path[len + DBHOMELEN] = '\0';
298     open_flag = DB_CREATE;
299
300     /* We need to be able to open the database environment with full
301      * transaction, logging, and locking support if we ever hope to 
302      * be a true multi-acess file server. */
303     if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
304         LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
305         goto fail_lock;
306     }
307
308 #ifndef CNID_DB_CDB
309     /* Setup internal deadlock detection. */
310     if ((rc = db->dbenv->set_lk_detect(db->dbenv, DEAD_LOCK_DETECT)) != 0) {
311         LOG(log_error, logtype_default, "cnid_open: set_lk_detect: %s", db_strerror(rc));
312         goto fail_lock;
313     }
314 #endif /* CNID_DB_CDB */
315
316 #ifndef CNID_DB_CDB
317 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
318 #if 0
319     /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
320     if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
321         LOG(log_error, logtype_default, "cnid_open: set_flags: %s", db_strerror(rc));
322         goto fail_lock;
323     }
324 #endif
325 #endif /* DB_VERSION_MINOR > 1 */
326 #endif /* CNID_DB_CDB */
327
328     /* Open the database environment. */
329     if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666 & ~mask)) != 0) {
330         if (rc == DB_RUNRECOVERY) {
331             /* This is the mother of all errors.  We _must_ fail here. */
332             LOG(log_error, logtype_default, "cnid_open: CATASTROPHIC ERROR opening database environment %s.  Run db_recovery -c immediately", path);
333             goto fail_lock;
334         }
335
336         /* We can't get a full transactional environment, so multi-access
337          * is out of the question.  Let's assume a read-only environment,
338          * and try to at least get a shared memory pool. */
339         if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666 & ~mask)) != 0) {
340             /* Nope, not a MPOOL, either.  Last-ditch effort: we'll try to
341              * open the environment with no flags. */
342             if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666 & ~mask)) != 0) {
343                 LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s",
344                     path, db_strerror(rc));
345                 goto fail_lock;
346             }
347         }
348         db->flags |= CNIDFLAG_DB_RO;
349         open_flag = DB_RDONLY;
350         LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
351     }
352
353     /* did/name reverse mapping.  We use a BTree for this one. */
354     if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
355         LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s",
356             db_strerror(rc));
357         goto fail_appinit;
358     }
359
360     /*db->db_didname->set_bt_compare(db->db_didname, &compare_unix);*/
361     if ((rc = my_open(db->db_didname, DBDIDNAME, NULL, DB_HASH, open_flag, 0666 & ~mask))) {
362         LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
363             db_strerror(rc));
364         goto fail_appinit;
365     }
366
367     /* Check for version.  This way we can update the database if we need
368      * to change the format in any way. */
369     memset(&key, 0, sizeof(key));
370     memset(&data, 0, sizeof(data));
371     key.data = DBVERSION_KEY;
372     key.size = DBVERSION_KEYLEN;
373
374 #ifdef CNID_DB_CDB
375     if ((rc = db->db_didname->get(db->db_didname, NULL, &key, &data, 0)) != 0) {
376         int ret;
377         {
378             u_int32_t version = htonl(DBVERSION);
379
380             data.data = &version;
381             data.size = sizeof(version);
382         }
383         if ((ret = db->db_didname->put(db->db_didname, NULL, &key, &data,
384                                        DB_NOOVERWRITE))) {
385             LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s",
386                 db_strerror(ret));
387             goto fail_appinit;
388         }
389     }
390 #else /* CNID_DB_CDB */
391 dbversion_retry:
392     if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
393         LOG(log_error, logtype_default, "cnid_open: txn_begin: failed to check db version: %s",
394             db_strerror(rc));
395         goto fail_appinit;
396     }
397
398     while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
399         int ret;
400         switch (rc) {
401         case DB_LOCK_DEADLOCK:
402             if ((ret = txn_abort(tid)) != 0) {
403                 LOG(log_error, logtype_default, "cnid_open: txn_abort: %s", db_strerror(ret));
404                 goto fail_appinit;
405             }
406             goto dbversion_retry;
407         case DB_NOTFOUND:
408             {
409                 u_int32_t version = htonl(DBVERSION);
410
411                 data.data = &version;
412                 data.size = sizeof(version);
413             }
414
415             if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data,
416                                            DB_NOOVERWRITE))) {
417                 if (ret == DB_LOCK_DEADLOCK) {
418                     if ((ret = txn_abort(tid)) != 0) {
419                         LOG(log_error, logtype_default, "cnid_open: txn_abort: %s",
420                             db_strerror(ret));
421                         goto fail_appinit;
422                     }
423                     goto dbversion_retry;
424                 }
425                 else if (ret == DB_RUNRECOVERY) {
426                     /* At this point, we don't care if the transaction aborts
427                     * successfully or not. */
428                     txn_abort(tid);
429                     LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s",
430                         db_strerror(ret));
431                     goto fail_appinit;
432                 }
433             }
434             break; /* while loop */
435         default:
436             txn_abort(tid);
437             LOG(log_error, logtype_default, "cnid_open: Failed to check db version: %s",
438                 db_strerror(rc));
439             goto fail_appinit;
440         }
441     }
442
443     if ((rc = txn_commit(tid, 0)) != 0) {
444         LOG(log_error, logtype_default, "cnid_open: Failed to commit db version: %s",
445             db_strerror(rc));
446         goto fail_appinit;
447     }
448 #endif /* CNID_DB_CDB */
449
450     /* TODO In the future we might check for version number here. */
451 #if 0
452     memcpy(&version, data.data, sizeof(version));
453     if (version != ntohl(DBVERSION)) {
454         /* Do stuff here. */
455     }
456 #endif /* 0 */
457
458 #ifdef EXTENDED_DB
459     /* did/macname (31 character) mapping.  Use a BTree for this one. */
460     if ((rc = db_create(&db->db_macname, db->dbenv, 0)) != 0) {
461         LOG(log_error, logtype_default, "cnid_open: Failed to create did/macname database: %s",
462             db_strerror(rc));
463         goto fail_appinit;
464     }
465
466     db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
467     if ((rc = my_open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666 & ~mask))) {
468         LOG(log_error, logtype_default, "cnid_open: Failed to open did/macname database: %s",
469             db_strerror(rc));
470         goto fail_appinit;
471     }
472
473     /* did/shortname (DOS 8.3) mapping.  Use a BTree for this one. */
474     if ((rc = db_create(&db->db_shortname, db->dbenv, 0)) != 0) {
475         LOG(log_error, logtype_default, "cnid_open: Failed to create did/shortname database: %s",
476             db_strerror(rc));
477         goto fail_appinit;
478     }
479
480     db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
481     if ((rc = my_open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666 & ~mask))) {
482         LOG(log_error, logtype_default, "cnid_open: Failed to open did/shortname database: %s",
483             db_strerror(rc));
484         goto fail_appinit;
485     }
486
487     /* did/longname (Unicode) mapping.  Use a BTree for this one. */
488     if ((rc = db_create(&db->db_longname, db->dbenv, 0)) != 0) {
489         LOG(log_error, logtype_default, "cnid_open: Failed to create did/longname database: %s",
490             db_strerror(rc));
491         goto fail_appinit;
492     }
493
494     db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
495     if ((rc = my_open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666 & ~mask))) {
496         LOG(log_error, logtype_default, "cnid_open: Failed to open did/longname database: %s",
497             db_strerror(rc));
498         goto fail_appinit;
499     }
500 #endif /* EXTENDED_DB */
501
502     /* dev/ino reverse mapping.  Use a hash for this one. */
503     if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
504         LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s",
505             db_strerror(rc));
506         goto fail_appinit;
507     }
508
509     if ((rc = my_open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666 & ~mask))) {
510         LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
511             db_strerror(rc));
512         goto fail_appinit;
513     }
514
515     /* Main CNID database.  Use a hash for this one. */
516     if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
517         LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s",
518             db_strerror(rc));
519         goto fail_appinit;
520     }
521
522     if ((rc = my_open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666 & ~mask))) {
523         LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
524             db_strerror(rc));
525         goto fail_appinit;
526     }
527
528 #ifdef FILE_MANGLING
529     /* filename mangling database.  Use a hash for this one. */
530     if ((rc = db_create(&db->db_mangle, db->dbenv, 0)) != 0) {
531         LOG(log_error, logtype_default, "cnid_open: Failed to create mangle database: %s", db_strerror(rc));
532         goto fail_appinit;
533     }
534
535     if ((rc = my_open(db->db_mangle, DBMANGLE, NULL, DB_HASH, open_flag, 0666 & ~mask))) {
536         LOG(log_error, logtype_default, "cnid_open: Failed to open mangle database: %s", db_strerror(rc));
537         goto fail_appinit;
538     }
539 #endif /* FILE_MANGLING */
540
541     /* Print out the version of BDB we're linked against. */
542     LOG(log_info, logtype_default, "CNID DB initialized using %s", db_version(NULL, NULL, NULL));
543 #if 0
544     db_env_set_func_yield(my_yield);
545 #endif
546     return db;
547
548 fail_appinit:
549     if (db->db_didname) db->db_didname->close(db->db_didname, 0);
550     if (db->db_devino)  db->db_devino->close(db->db_devino, 0);
551     if (db->db_cnid)    db->db_cnid->close(db->db_cnid, 0);
552 #ifdef EXTENDED_DB
553     if (db->db_macname)   db->db_macname->close(db->db_macname, 0);
554     if (db->db_shortname) db->db_shortname->close(db->db_shortname, 0);
555     if (db->db_longname)  db->db_longname->close(db->db_longname, 0);
556 #endif /* EXTENDED_DB */
557     LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
558     db->dbenv->close(db->dbenv, 0);
559
560 fail_lock:
561 #ifndef CNID_DB_CDB
562     if (db->lockfd > -1) {
563         close(db->lockfd);
564         (void)remove(db->lock_file);
565     }
566 #endif /* CNID_DB_CDB */
567
568 fail_adouble:
569
570     free(db);
571     return NULL;
572 }
573 #endif /* CNID_DB */
574
575