From a83e6e2abcaf39654232bd353d6378bf2c696d0f Mon Sep 17 00:00:00 2001 From: franklahm Date: Mon, 18 May 2009 09:25:25 +0000 Subject: [PATCH] lock file was created in cwd --- etc/cnid_dbd/cmd_dbd.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/etc/cnid_dbd/cmd_dbd.c b/etc/cnid_dbd/cmd_dbd.c index 337fea65..9e2aef88 100644 --- a/etc/cnid_dbd/cmd_dbd.c +++ b/etc/cnid_dbd/cmd_dbd.c @@ -1,5 +1,5 @@ /* - $Id: cmd_dbd.c,v 1.1 2009-05-14 13:46:08 franklahm Exp $ + $Id: cmd_dbd.c,v 1.2 2009-05-18 09:25:25 franklahm Exp $ Copyright (c) 2009 Frank Lahm @@ -119,15 +119,35 @@ void set_signal(void) } } -int get_lock(void) +int get_lock(const char *dbpath) { int lockfd; + char lockpath[PATH_MAX]; struct flock lock; + struct stat st; - if ((lockfd = open(LOCKFILENAME, O_RDWR | O_CREAT, 0644)) < 0) { + if ( (strlen(dbpath) + strlen(LOCKFILENAME+1)) > (PATH_MAX - 1) ) { + dbd_log( LOGSTD, ".AppleDB pathname too long"); + exit(EXIT_FAILURE); + } + strncpy(lockpath, dbpath, PATH_MAX - 1); + strcat(lockpath, "/"); + strcat(lockpath, LOCKFILENAME); + + if ((lockfd = open(lockpath, O_RDWR | O_CREAT, 0644)) < 0) { dbd_log( LOGSTD, "Error opening lockfile: %s", strerror(errno)); exit(EXIT_FAILURE); } + + if ((stat(dbpath, &st)) != 0) { + dbd_log( LOGSTD, "Error statting lockfile: %s", strerror(errno)); + exit(EXIT_FAILURE); + } + + if ((chown(lockpath, st.st_uid, st.st_gid)) != 0) { + dbd_log( LOGSTD, "Error inheriting lockfile permissions: %s", strerror(errno)); + exit(EXIT_FAILURE); + } lock.l_start = 0; lock.l_whence = SEEK_SET; @@ -189,9 +209,6 @@ static void usage () " -e only work on inactive volumes and lock them (exclusive)\n" " -x rebuild indexes\n" " -v verbose\n" - "\n" - "05-05-2009: -s and -r already check/repair the AppleDouble stuff and encoding,\n" - " no CNID database maintanance is done yet\n" ); } @@ -275,7 +292,7 @@ int main(int argc, char **argv) Before we do anything else, check if there is an instance of cnid_dbd running already and silently exit if yes. */ - lockfd = get_lock(); + lockfd = get_lock(dbpath); /* Setup signal handling */ set_signal(); @@ -325,7 +342,6 @@ int main(int argc, char **argv) } } -cleanup: if (dbif_close(dbd) < 0) { dbd_log( LOGSTD, "Error closing database"); exit(EXIT_FAILURE); -- 2.39.2