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