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