#define DBOPTIONS (DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN)
int nocniddb = 0; /* Dont open CNID database, only scan filesystem */
- volatile sig_atomic_t alarmed;
+struct volinfo volinfo; /* needed by pack.c:idxname() */
+ volatile sig_atomic_t alarmed; /* flags for signals */
+ int db_locked; /* have we got the fcntl lock on lockfile ? */
static DBD *dbd;
static int verbose; /* Logging flag */
/* Check if -f is requested and wipe db if yes */
if ((flags & DBD_FLAGS_FORCE) && rebuild && (volinfo.v_flags & AFPVOL_CACHE)) {
char cmd[8 + MAXPATHLEN];
- close(lockfd);
++
+ if ((db_locked = get_lock(0, NULL)) != 0)
+ goto exit_failure;
- snprintf(cmd, 8 + MAXPATHLEN, "rm -f %s/*", dbpath);
++
+ snprintf(cmd, 8 + MAXPATHLEN, "rm -rf \"%s\"", dbpath);
dbd_log( LOGDEBUG, "Removing old database of volume: '%s'", volpath);
system(cmd);
+ if ((mkdir(dbpath, 0755)) != 0) {
+ dbd_log( LOGSTD, "Can't create dbpath \"%s\": %s", dbpath, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
dbd_log( LOGDEBUG, "Removed old database.");
- lockfd = get_lock(dbpath);
+ if ((db_locked = get_lock(1, dbpath)) == -1)
+ goto exit_failure;
}
/*
(strcmp(a,c) b 0)
extern int nocniddb; /* Dont open CNID database, only scan filesystem */
+ extern int db_locked; /* have we got the fcntl lock on lockfd ? */
extern volatile sig_atomic_t alarmed;
-extern struct volinfo *volinfo;
-extern char cwdbuf[MAXPATHLEN+1];
extern void dbd_log(enum logtype lt, char *fmt, ...);
extern int cmd_dbd_scanvol(DBD *dbd, struct volinfo *volinfo, dbd_flags_t flags);
Note: DB_INIT_LOCK is here so we can run the db_* utilities while netatalk is running.
It's a likey performance hit, but it might we worth it.
*/
- #define DBOPTIONS (DB_CREATE | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | DB_RECOVER)
+ #define DBOPTIONS (DB_CREATE | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN)
+/* Global, needed by pack.c:idxname() */
+struct volinfo volinfo;
+
static DBD *dbd;
static int exit_sig = 0;
+ static int db_locked;
static void sig_exit(int signo)
{
{
struct db_param *dbp;
int err = 0;
- int lockfd, ctrlfd, clntfd;
+ int ctrlfd, clntfd;
- char *dir, *logconfig;
+ char *logconfig;
set_processname("cnid_dbd");
/* Before we do anything else, check if there is an instance of cnid_dbd
running already and silently exit if yes. */
- lockfd = get_lock();
+ if ((db_locked = get_lock(1)) == -1) {
+ exit(1);
+ }
- LOG(log_info, logtype_cnid, "Startup, DB dir %s", dir);
-
set_signal();
/* SIGINT and SIGTERM are always off, unless we are in pselect */
exit(1);
LOG(log_maxdebug, logtype_cnid, "Finished parsing db_param config file");
- if (NULL == (dbd = dbif_init(".", "cnid2.db")))
+ if (NULL == (dbd = dbif_init(dbpath, "cnid2.db")))
exit(2);
- if (dbif_env_open(dbd, dbp, DBOPTIONS) < 0)
+ /* Only recover if we got the lock */
+ if (dbif_env_open(dbd,
+ dbp,
+ db_locked ? DBOPTIONS | DB_RECOVER : DBOPTIONS) < 0)
exit(2); /* FIXME: same exit code as failure for dbif_open() */
LOG(log_debug, logtype_cnid, "Finished initializing BerkeleyDB environment");
if (dbif_close(dbd) < 0)
err++;
- if (dbif_prep_upgrade(dir) < 0)
+ if (dbif_env_remove(dbpath) < 0)
err++;
- free_lock(lockfd);
+ (void)get_lock(0);
if (err)
exit(4);