#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
-#include <sys/cdefs.h>
#include <unistd.h>
+#include <arpa/inet.h>
#include <db.h>
#include <atalk/logger.h>
#include <atalk/util.h>
+#include <atalk/errchk.h>
#include "db_param.h"
#include "dbif.h"
*/
static int dbif_stamp(DBD *dbd, void *buffer, int size)
{
+ EC_INIT;
struct stat st;
- int rc,cwd;
+ int cwd = -1;
if (size < 8)
- return -1;
+ EC_FAIL;
/* Remember cwd */
if ((cwd = open(".", O_RDONLY)) < 0) {
LOG(log_error, logtype_cnid, "error opening cwd: %s", strerror(errno));
- return -1;
+ EC_FAIL;
}
/* chdir to db_envhome */
if ((chdir(dbd->db_envhome)) != 0) {
LOG(log_error, logtype_cnid, "error chdiring to db_env '%s': %s", dbd->db_envhome, strerror(errno));
- return -1;
+ EC_FAIL;
}
- if ((rc = stat(dbd->db_table[DBIF_CNID].name, &st)) < 0) {
+ if (stat(dbd->db_table[DBIF_CNID].name, &st) < 0) {
LOG(log_error, logtype_cnid, "error stating database %s: %s", dbd->db_table[DBIF_CNID].name, db_strerror(errno));
- return -1;
+ EC_FAIL;
}
LOG(log_maxdebug, logtype_cnid,"stamp: %s", asctime(localtime(&st.st_ctime)));
memset(buffer, 0, size);
memcpy(buffer, &st.st_ctime, sizeof(st.st_ctime));
- if ((fchdir(cwd)) != 0) {
- LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));
- return -1;
+EC_CLEANUP:
+ if (cwd != -1) {
+ if (fchdir(cwd) != 0) {
+ LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));
+ EC_STATUS(-1);
+ }
+ close(cwd);
}
-
- return 0;
+ EC_EXIT;
}
/*!
return ret;
}
+/*!
+ * Get lock on db lock file
+ *
+ * @args cmd (r) lock command:
+ * LOCK_FREE: close lockfd
+ * LOCK_UNLOCK: unlock lockm keep lockfd open
+ * LOCK_EXCL: F_WRLCK on lockfd
+ * LOCK_SHRD: F_RDLCK on lockfd
+ * @args dbpath (r) path to lockfile, only used on first call,
+ * later the stored fd is used
+ * @returns LOCK_FREE/LOCK_UNLOCK return 0 on success, -1 on error
+ * LOCK_EXCL/LOCK_SHRD return LOCK_EXCL or LOCK_SHRD respectively on
+ * success, 0 if the lock couldn't be acquired, -1 on other errors
+ */
+int get_lock(int cmd, const char *dbpath)
+{
+ static int lockfd = -1;
+ int ret;
+ char lockpath[PATH_MAX];
+ struct stat st;
+
+ switch (cmd) {
+ case LOCK_FREE:
+ if (lockfd == -1)
+ return -1;
+ close(lockfd);
+ lockfd = -1;
+ return 0;
+
+ case LOCK_UNLOCK:
+ if (lockfd == -1)
+ return -1;
+ return unlock(lockfd, 0, SEEK_SET, 0);
+
+ case LOCK_EXCL:
+ case LOCK_SHRD:
+ if (lockfd == -1) {
+ if ( (strlen(dbpath) + strlen(LOCKFILENAME+1)) > (PATH_MAX - 1) ) {
+ LOG(log_error, logtype_cnid, ".AppleDB pathname too long");
+ return -1;
+ }
+ strncpy(lockpath, dbpath, PATH_MAX - 1);
+ strcat(lockpath, "/");
+ strcat(lockpath, LOCKFILENAME);
+
+ if ((lockfd = open(lockpath, O_RDWR | O_CREAT, 0644)) < 0) {
+ LOG(log_error, logtype_cnid, "Error opening lockfile: %s", strerror(errno));
+ return -1;
+ }
+
+ if ((stat(dbpath, &st)) != 0) {
+ LOG(log_error, logtype_cnid, "Error statting lockfile: %s", strerror(errno));
+ return -1;
+ }
+
+ if ((chown(lockpath, st.st_uid, st.st_gid)) != 0) {
+ LOG(log_error, logtype_cnid, "Error inheriting lockfile permissions: %s",
+ strerror(errno));
+ return -1;
+ }
+ }
+
+ if (cmd == LOCK_EXCL)
+ ret = write_lock(lockfd, 0, SEEK_SET, 0);
+ else
+ ret = read_lock(lockfd, 0, SEEK_SET, 0);
+
+ if (ret != 0) {
+ if (cmd == LOCK_SHRD)
+ LOG(log_error, logtype_cnid, "Volume CNID db is locked, try again...");
+ return 0;
+ }
+
+ LOG(log_debug, logtype_cnid, "get_lock: got %s lock",
+ cmd == LOCK_EXCL ? "LOCK_EXCL" : "LOCK_SHRD");
+ return cmd;
+
+ default:
+ return -1;
+ } /* switch(cmd) */
+
+ /* deadc0de, never get here */
+ return -1;
+}
+
/* --------------- */
DBD *dbif_init(const char *envhome, const char *filename)
{
int ret;
DBD *dbd;
- LOG(log_debug, logtype_cnid, "Reopening BerkeleyDB environment");
+ LOG(log_debug, logtype_cnid, "Trying to remove BerkeleyDB environment");
+
+ if (get_lock(LOCK_EXCL, path) != LOCK_EXCL) {
+ LOG(log_warning, logtype_cnid, "CNID db \"%s\" in use, can't remove BerkeleyDB environment", path);
+ return 0;
+ }
if (NULL == (dbd = dbif_init(path, "cnid2.db")))
return -1;
flags);
if (ret == DB_NOTFOUND) {
- LOG(log_info, logtype_cnid, "key not found");
+ LOG(log_debug, logtype_cnid, "key not found");
return 0;
}
if (ret) {
}
/*
- ret = 2 -> commit txn regardless of db_param.txn_frequency
ret = 1 -> commit txn if db_param.txn_frequency
ret = 0 -> abort txn db_param.txn_frequency -> exit!
anything else -> exit!
- db_param of the db environment might specify txn_frequency > 1 in which case
- we only close a txn every txn_frequency time. the `dbd` command uses this for the
- temp rebuild db, cnid_dbd keeps it at 0. For increasing cnid_dbd throughput this
- should be tuned and testes as well.
-
@returns 0 on success (abort or commit), -1 on error
*/
int dbif_txn_close(DBD *dbd, int ret)
LOG( log_error, logtype_cnid, "Fatal error aborting transaction. Exiting!");
return -1;
}
- } else if (ret == 1 || ret == 2) {
- static uint count;
- if (ret != 2 && dbd->db_param.txn_frequency > 1) {
- count++;
- if ((count % dbd->db_param.txn_frequency) != 0)
- return 0;
- }
+ } else if (ret == 1) {
ret = dbif_txn_commit(dbd);
if ( ret < 0) {
LOG( log_error, logtype_cnid, "Fatal error committing transaction. Exiting!");