[pacman-dev] [PATCH] libalpm: add PID to db.lck
This is the first step in being able to automatically remove phantom lock files. Signed-off-by: Allan McRae <allan@archlinux.org> --- lib/libalpm/util.c | 15 +++++++++++++-- 1 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index da3463b..811572a 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -242,7 +242,9 @@ char *_alpm_strreplace(const char *str, const char *needle, const char *replace) int _alpm_lckmk() { int fd; - char *dir, *ptr; + pid_t pid; + size_t len; + char *dir, *ptr, *spid; const char *file = alpm_option_get_lockfile(); /* create the dir of the lockfile first */ @@ -256,7 +258,16 @@ int _alpm_lckmk() while((fd = open(file, O_WRONLY | O_CREAT | O_EXCL, 0000)) == -1 && errno == EINTR); - return(fd > 0 ? fd : -1); + if(fd > 0) { + pid = getpid(); + size_t len = snprintf(spid, 0, "%d", pid) + 1; + spid = malloc(len); + snprintf(spid, len, "%d", pid) + 1; + while(write(fd, (void *)spid, len) == -1 && errno == EINTR); + fsync(fd); + return(fd); + } + return(-1); } /* Remove a lock file */ -- 1.6.0.3
Allan McRae wrote:
This is the first step in being able to automatically remove phantom lock files.
Signed-off-by: Allan McRae <allan@archlinux.org> <snip>
I should point out that this is my first attempt at writing to a file at such a low level (I'm a C++ streams kkinda guy). I'm not sure if I need further caution on the write line. Specifically, I think write may not always push all bytes to the file. But given the small amount of data being written, I am not sure if that is an issue here. Cheers Allan
On Sun, Nov 2, 2008 at 8:22 PM, Allan McRae <allan@archlinux.org> wrote:
This is the first step in being able to automatically remove phantom lock files.
Signed-off-by: Allan McRae <allan@archlinux.org> --- lib/libalpm/util.c | 15 +++++++++++++-- 1 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index da3463b..811572a 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -242,7 +242,9 @@ char *_alpm_strreplace(const char *str, const char *needle, const char *replace) int _alpm_lckmk() { int fd; - char *dir, *ptr; + pid_t pid; + size_t len; + char *dir, *ptr, *spid; const char *file = alpm_option_get_lockfile();
/* create the dir of the lockfile first */ @@ -256,7 +258,16 @@ int _alpm_lckmk()
while((fd = open(file, O_WRONLY | O_CREAT | O_EXCL, 0000)) == -1 && errno == EINTR); - return(fd > 0 ? fd : -1); + if(fd > 0) { + pid = getpid(); + size_t len = snprintf(spid, 0, "%d", pid) + 1; I think we need to use %ld here as a pid_t can be a signed type, up to length long. Thus the whole thing: snprintf(spid, 0, "%ld", (long)pid) Obviously do this below too. And do you want to add a newline char in here to make your life easier when reading this in?
+ spid = malloc(len); + snprintf(spid, len, "%d", pid) + 1; Think you left a +1 here by accident.
+ while(write(fd, (void *)spid, len) == -1 && errno == EINTR); + fsync(fd); + return(fd); + } + return(-1); }
And re: your second message, I think your write() call is just fine here.
Dan McGee wrote:
On Sun, Nov 2, 2008 at 8:22 PM, Allan McRae <allan@archlinux.org> wrote:
This is the first step in being able to automatically remove phantom lock files.
Signed-off-by: Allan McRae <allan@archlinux.org> --- lib/libalpm/util.c | 15 +++++++++++++-- 1 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index da3463b..811572a 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -242,7 +242,9 @@ char *_alpm_strreplace(const char *str, const char *needle, const char *replace) int _alpm_lckmk() { int fd; - char *dir, *ptr; + pid_t pid; + size_t len; + char *dir, *ptr, *spid; const char *file = alpm_option_get_lockfile();
/* create the dir of the lockfile first */ @@ -256,7 +258,16 @@ int _alpm_lckmk()
while((fd = open(file, O_WRONLY | O_CREAT | O_EXCL, 0000)) == -1 && errno == EINTR); - return(fd > 0 ? fd : -1); + if(fd > 0) { + pid = getpid(); + size_t len = snprintf(spid, 0, "%d", pid) + 1;
I think we need to use %ld here as a pid_t can be a signed type, up to length long. Thus the whole thing: snprintf(spid, 0, "%ld", (long)pid) Obviously do this below too. And do you want to add a newline char in here to make your life easier when reading this in?
+ spid = malloc(len); + snprintf(spid, len, "%d", pid) + 1;
Think you left a +1 here by accident.
+ while(write(fd, (void *)spid, len) == -1 && errno == EINTR); + fsync(fd); + return(fd); + } + return(-1); }
And re: your second message, I think your write() call is just fine here.
There is an updated version with all these comments fixed on my working branch (http://dev.archlinux.org/~allan/gitweb/gitweb.cgi?p=pacman.git;a=commitdiff;...). As a bonus, I also fixed the memory leak... Allan
participants (2)
-
Allan McRae
-
Dan McGee