Fix double close of the lock file

According to FOPEN(3), using fclose on an fdopen'd file stream also
closes the underlying file descriptor. This happened in _alpm_lckmk
(util.c), which meant that when alpm_trans_release closed it again, the
log file (which reused the original file descriptor) was closed instead.

Signed-off-by: Jonathan Conder <jonno.conder@gmail.com>
Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Jonathan Conder 2011-02-05 13:39:37 +13:00 committed by Dan McGee
parent e8f799ba83
commit acd9269478
5 changed files with 10 additions and 15 deletions

View File

@ -49,7 +49,6 @@ pmhandle_t *_alpm_handle_new()
ALPM_LOG_FUNC; ALPM_LOG_FUNC;
CALLOC(handle, 1, sizeof(pmhandle_t), RET_ERR(PM_ERR_MEMORY, NULL)); CALLOC(handle, 1, sizeof(pmhandle_t), RET_ERR(PM_ERR_MEMORY, NULL));
handle->lckfd = -1;
return(handle); return(handle);
} }

View File

@ -34,7 +34,7 @@ typedef struct _pmhandle_t {
pmdb_t *db_local; /* local db pointer */ pmdb_t *db_local; /* local db pointer */
alpm_list_t *dbs_sync; /* List of (pmdb_t *) */ alpm_list_t *dbs_sync; /* List of (pmdb_t *) */
FILE *logstream; /* log file stream pointer */ FILE *logstream; /* log file stream pointer */
int lckfd; /* lock file descriptor if one exists */ FILE *lckstream; /* lock file stream pointer if one exists */
pmtrans_t *trans; pmtrans_t *trans;
/* callback functions */ /* callback functions */

View File

@ -73,8 +73,8 @@ int SYMEXPORT alpm_trans_init(pmtransflag_t flags,
/* lock db */ /* lock db */
if(!(flags & PM_TRANS_FLAG_NOLOCK)) { if(!(flags & PM_TRANS_FLAG_NOLOCK)) {
handle->lckfd = _alpm_lckmk(); handle->lckstream = _alpm_lckmk();
if(handle->lckfd == -1) { if(handle->lckstream == NULL) {
RET_ERR(PM_ERR_HANDLE_LOCK, -1); RET_ERR(PM_ERR_HANDLE_LOCK, -1);
} }
} }
@ -260,12 +260,9 @@ int SYMEXPORT alpm_trans_release()
/* unlock db */ /* unlock db */
if(!nolock_flag) { if(!nolock_flag) {
if(handle->lckfd != -1) { if(handle->lckstream != NULL) {
int fd; fclose(handle->lckstream);
do { handle->lckstream = NULL;
fd = close(handle->lckfd);
} while(fd == -1 && errno == EINTR);
handle->lckfd = -1;
} }
if(_alpm_lckrm()) { if(_alpm_lckrm()) {
_alpm_log(PM_LOG_WARNING, _("could not remove lock file %s\n"), _alpm_log(PM_LOG_WARNING, _("could not remove lock file %s\n"),

View File

@ -211,7 +211,7 @@ char *_alpm_strtrim(char *str)
} }
/* Create a lock file */ /* Create a lock file */
int _alpm_lckmk(void) FILE *_alpm_lckmk(void)
{ {
int fd; int fd;
char *dir, *ptr; char *dir, *ptr;
@ -234,10 +234,9 @@ int _alpm_lckmk(void)
fprintf(f, "%ld\n", (long)getpid()); fprintf(f, "%ld\n", (long)getpid());
fflush(f); fflush(f);
fsync(fd); fsync(fd);
fclose(f); return(f);
return(fd);
} }
return(-1); return(NULL);
} }
/* Remove a lock file */ /* Remove a lock file */

View File

@ -80,7 +80,7 @@ int _alpm_makepath(const char *path);
int _alpm_makepath_mode(const char *path, mode_t mode); int _alpm_makepath_mode(const char *path, mode_t mode);
int _alpm_copyfile(const char *src, const char *dest); int _alpm_copyfile(const char *src, const char *dest);
char *_alpm_strtrim(char *str); char *_alpm_strtrim(char *str);
int _alpm_lckmk(void); FILE *_alpm_lckmk(void);
int _alpm_lckrm(void); int _alpm_lckrm(void);
int _alpm_unpack_single(const char *archive, const char *prefix, const char *fn); int _alpm_unpack_single(const char *archive, const char *prefix, const char *fn);
int _alpm_unpack(const char *archive, const char *prefix, alpm_list_t *list, int breakfirst); int _alpm_unpack(const char *archive, const char *prefix, alpm_list_t *list, int breakfirst);