summaryrefslogtreecommitdiffstats
path: root/lib/libalpm/handle.c
diff options
context:
space:
mode:
authorAndrew Gregory <andrew.gregory.8@gmail.com>2013-10-26 17:32:15 +0200
committerAllan McRae <allan@archlinux.org>2013-11-08 00:28:05 +0100
commit57090d8cba65988b822a215947a5fb44d55790d0 (patch)
treeebad03ec7c10cef372b4fe548c67a9dfc7f0f904 /lib/libalpm/handle.c
parentaf284d5fdb0ef15dbb7a2082380e2ec586599774 (diff)
downloadpacman-57090d8cba65988b822a215947a5fb44d55790d0.tar.gz
pacman-57090d8cba65988b822a215947a5fb44d55790d0.tar.xz
alpm_handle: store lock file descriptor
There was a brief window between opening the file descriptor and creating a stream to it. If the process was interrupted during that window the lock file would not be removed correctly. The pid is no longer printed to the lock file as this was virtually meaningless for lock files on NFS. Fixes FS#35603 Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> Signed-off-by: Allan McRae <allan@archlinux.org>
Diffstat (limited to 'lib/libalpm/handle.c')
-rw-r--r--lib/libalpm/handle.c25
1 files changed, 9 insertions, 16 deletions
diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c
index ea033b20..1d661a2d 100644
--- a/lib/libalpm/handle.c
+++ b/lib/libalpm/handle.c
@@ -44,6 +44,7 @@ alpm_handle_t *_alpm_handle_new(void)
CALLOC(handle, 1, sizeof(alpm_handle_t), return NULL);
handle->deltaratio = 0.0;
+ handle->lockfd = -1;
return handle;
}
@@ -91,11 +92,10 @@ void _alpm_handle_free(alpm_handle_t *handle)
/** Lock the database */
int _alpm_handle_lock(alpm_handle_t *handle)
{
- int fd;
char *dir, *ptr;
ASSERT(handle->lockfile != NULL, return -1);
- ASSERT(handle->lckstream == NULL, return 0);
+ ASSERT(handle->lockfd < 0, return 0);
/* create the dir of the lockfile first */
dir = strdup(handle->lockfile);
@@ -110,27 +110,20 @@ int _alpm_handle_lock(alpm_handle_t *handle)
FREE(dir);
do {
- fd = open(handle->lockfile, O_WRONLY | O_CREAT | O_EXCL, 0000);
- } while(fd == -1 && errno == EINTR);
- if(fd >= 0) {
- FILE *f = fdopen(fd, "w");
- fprintf(f, "%ld\n", (long)getpid());
- fflush(f);
- fsync(fd);
- handle->lckstream = f;
- return 0;
- }
- return -1;
+ handle->lockfd = open(handle->lockfile, O_WRONLY | O_CREAT | O_EXCL, 0000);
+ } while(handle->lockfd == -1 && errno == EINTR);
+
+ return (handle->lockfd >= 0 ? 0 : -1);
}
/** Remove a lock file */
int _alpm_handle_unlock(alpm_handle_t *handle)
{
ASSERT(handle->lockfile != NULL, return -1);
- ASSERT(handle->lckstream != NULL, return 0);
+ ASSERT(handle->lockfd >= 0, return 0);
- fclose(handle->lckstream);
- handle->lckstream = NULL;
+ close(handle->lockfd);
+ handle->lockfd = -1;
if(unlink(handle->lockfile) && errno != ENOENT) {
return -1;