mirror of
https://github.com/moparisthebest/pacman
synced 2024-11-12 04:15:06 -05:00
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>
This commit is contained in:
parent
af284d5fdb
commit
57090d8cba
@ -44,6 +44,7 @@ alpm_handle_t *_alpm_handle_new(void)
|
|||||||
|
|
||||||
CALLOC(handle, 1, sizeof(alpm_handle_t), return NULL);
|
CALLOC(handle, 1, sizeof(alpm_handle_t), return NULL);
|
||||||
handle->deltaratio = 0.0;
|
handle->deltaratio = 0.0;
|
||||||
|
handle->lockfd = -1;
|
||||||
|
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
@ -91,11 +92,10 @@ void _alpm_handle_free(alpm_handle_t *handle)
|
|||||||
/** Lock the database */
|
/** Lock the database */
|
||||||
int _alpm_handle_lock(alpm_handle_t *handle)
|
int _alpm_handle_lock(alpm_handle_t *handle)
|
||||||
{
|
{
|
||||||
int fd;
|
|
||||||
char *dir, *ptr;
|
char *dir, *ptr;
|
||||||
|
|
||||||
ASSERT(handle->lockfile != NULL, return -1);
|
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 */
|
/* create the dir of the lockfile first */
|
||||||
dir = strdup(handle->lockfile);
|
dir = strdup(handle->lockfile);
|
||||||
@ -110,27 +110,20 @@ int _alpm_handle_lock(alpm_handle_t *handle)
|
|||||||
FREE(dir);
|
FREE(dir);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
fd = open(handle->lockfile, O_WRONLY | O_CREAT | O_EXCL, 0000);
|
handle->lockfd = open(handle->lockfile, O_WRONLY | O_CREAT | O_EXCL, 0000);
|
||||||
} while(fd == -1 && errno == EINTR);
|
} while(handle->lockfd == -1 && errno == EINTR);
|
||||||
if(fd >= 0) {
|
|
||||||
FILE *f = fdopen(fd, "w");
|
return (handle->lockfd >= 0 ? 0 : -1);
|
||||||
fprintf(f, "%ld\n", (long)getpid());
|
|
||||||
fflush(f);
|
|
||||||
fsync(fd);
|
|
||||||
handle->lckstream = f;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Remove a lock file */
|
/** Remove a lock file */
|
||||||
int _alpm_handle_unlock(alpm_handle_t *handle)
|
int _alpm_handle_unlock(alpm_handle_t *handle)
|
||||||
{
|
{
|
||||||
ASSERT(handle->lockfile != NULL, return -1);
|
ASSERT(handle->lockfile != NULL, return -1);
|
||||||
ASSERT(handle->lckstream != NULL, return 0);
|
ASSERT(handle->lockfd >= 0, return 0);
|
||||||
|
|
||||||
fclose(handle->lckstream);
|
close(handle->lockfd);
|
||||||
handle->lckstream = NULL;
|
handle->lockfd = -1;
|
||||||
|
|
||||||
if(unlink(handle->lockfile) && errno != ENOENT) {
|
if(unlink(handle->lockfile) && errno != ENOENT) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -55,7 +55,7 @@ struct __alpm_handle_t {
|
|||||||
alpm_db_t *db_local; /* local db pointer */
|
alpm_db_t *db_local; /* local db pointer */
|
||||||
alpm_list_t *dbs_sync; /* List of (alpm_db_t *) */
|
alpm_list_t *dbs_sync; /* List of (alpm_db_t *) */
|
||||||
FILE *logstream; /* log file stream pointer */
|
FILE *logstream; /* log file stream pointer */
|
||||||
FILE *lckstream; /* lock file stream pointer if one exists */
|
int lockfd; /* lock file descriptor */
|
||||||
alpm_trans_t *trans;
|
alpm_trans_t *trans;
|
||||||
|
|
||||||
#ifdef HAVE_LIBCURL
|
#ifdef HAVE_LIBCURL
|
||||||
|
Loading…
Reference in New Issue
Block a user