trans.c : reworking of transaction interruptions
My two previous hacks related to this part (8038190c7c
andb15a5194d1
) were caused by the lack of understanding of a feature introduced a while ago: Better control over CTRL-C interruptions -- do not leave the DB in an inconsistent state (54008798ef
). Now I have been looking at this commit, and the added feature is indeed interesting. The main problem I had with it is that it does a rather unusual use of alpm_trans_release, which caused a few problems that I tried to fix in a weird way. I think these problems were caused by the fact that there weren't any difference between "interrupt transaction" and "release a transaction which failed" actions from the alpm_trans_release POV. So I decided to add a new function instead, alpm_trans_interrupt, which is called on Ctrl+C, and which only sets trans->state to STATE_INTERRUPTED so that remove_commit and add_commit can exit cleanly at a safe moment. This allowed me to revert my two previous hacks as well. Also ensure we handle SIGINT correctly in all cases- if a transaction is not ongoing, then we can free the transaction and exit quickly. Signed-off-by: Chantry Xavier <shiningxc@gmail.com> Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
parent
046c8a6819
commit
b0aa510592
6 changed files with 39 additions and 26 deletions
|
@ -905,7 +905,7 @@ int _alpm_add_commit(pmtrans_t *trans, pmdb_t *db)
|
||||||
/* loop through our package list adding/upgrading one at a time */
|
/* loop through our package list adding/upgrading one at a time */
|
||||||
for(targ = trans->packages; targ; targ = targ->next) {
|
for(targ = trans->packages; targ; targ = targ->next) {
|
||||||
if(handle->trans->state == STATE_INTERRUPTED) {
|
if(handle->trans->state == STATE_INTERRUPTED) {
|
||||||
break;
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
pmpkg_t *newpkg = (pmpkg_t *)targ->data;
|
pmpkg_t *newpkg = (pmpkg_t *)targ->data;
|
||||||
|
@ -914,10 +914,8 @@ int _alpm_add_commit(pmtrans_t *trans, pmdb_t *db)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* run ldconfig if it exists */
|
/* run ldconfig if it exists */
|
||||||
if(handle->trans->state != STATE_INTERRUPTED) {
|
_alpm_log(PM_LOG_DEBUG, "running \"ldconfig -r %s\"\n", handle->root);
|
||||||
_alpm_log(PM_LOG_DEBUG, "running \"ldconfig -r %s\"\n", handle->root);
|
_alpm_ldconfig(handle->root);
|
||||||
_alpm_ldconfig(handle->root);
|
|
||||||
}
|
|
||||||
|
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -335,6 +335,7 @@ int alpm_trans_sysupgrade(void);
|
||||||
int alpm_trans_addtarget(char *target);
|
int alpm_trans_addtarget(char *target);
|
||||||
int alpm_trans_prepare(alpm_list_t **data);
|
int alpm_trans_prepare(alpm_list_t **data);
|
||||||
int alpm_trans_commit(alpm_list_t **data);
|
int alpm_trans_commit(alpm_list_t **data);
|
||||||
|
int alpm_trans_interrupt(void);
|
||||||
int alpm_trans_release(void);
|
int alpm_trans_release(void);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -111,8 +111,7 @@ int SYMEXPORT alpm_db_unregister_all(void)
|
||||||
/* Sanity checks */
|
/* Sanity checks */
|
||||||
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
|
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
|
||||||
/* Do not unregister a database if a transaction is on-going */
|
/* Do not unregister a database if a transaction is on-going */
|
||||||
ASSERT(handle->trans == NULL || handle->trans->state == STATE_INTERRUPTED,
|
ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL, -1));
|
||||||
RET_ERR(PM_ERR_TRANS_NOT_NULL, -1));
|
|
||||||
|
|
||||||
/* close local database */
|
/* close local database */
|
||||||
_alpm_db_unregister(handle->db_local);
|
_alpm_db_unregister(handle->db_local);
|
||||||
|
@ -142,8 +141,7 @@ int SYMEXPORT alpm_db_unregister(pmdb_t *db)
|
||||||
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
|
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
|
||||||
ASSERT(db != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
|
ASSERT(db != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1));
|
||||||
/* Do not unregister a database if a transaction is on-going */
|
/* Do not unregister a database if a transaction is on-going */
|
||||||
ASSERT(handle->trans == NULL || handle->trans->state == STATE_INTERRUPTED,
|
ASSERT(handle->trans == NULL, RET_ERR(PM_ERR_TRANS_NOT_NULL, -1));
|
||||||
RET_ERR(PM_ERR_TRANS_NOT_NULL, -1));
|
|
||||||
|
|
||||||
if(db == handle->db_local) {
|
if(db == handle->db_local) {
|
||||||
handle->db_local = NULL;
|
handle->db_local = NULL;
|
||||||
|
|
|
@ -280,7 +280,7 @@ int _alpm_remove_commit(pmtrans_t *trans, pmdb_t *db)
|
||||||
const char *pkgname = NULL;
|
const char *pkgname = NULL;
|
||||||
|
|
||||||
if(handle->trans->state == STATE_INTERRUPTED) {
|
if(handle->trans->state == STATE_INTERRUPTED) {
|
||||||
break;
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* get the name now so we can use it after package is removed */
|
/* get the name now so we can use it after package is removed */
|
||||||
|
@ -360,8 +360,7 @@ int _alpm_remove_commit(pmtrans_t *trans, pmdb_t *db)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* run ldconfig if it exists */
|
/* run ldconfig if it exists */
|
||||||
if((trans->type != PM_TRANS_TYPE_REMOVEUPGRADE)
|
if(trans->type != PM_TRANS_TYPE_REMOVEUPGRADE) {
|
||||||
&& (handle->trans->state != STATE_INTERRUPTED)) {
|
|
||||||
_alpm_log(PM_LOG_DEBUG, "running \"ldconfig -r %s\"\n", handle->root);
|
_alpm_log(PM_LOG_DEBUG, "running \"ldconfig -r %s\"\n", handle->root);
|
||||||
_alpm_ldconfig(handle->root);
|
_alpm_ldconfig(handle->root);
|
||||||
}
|
}
|
||||||
|
|
|
@ -165,13 +165,34 @@ int SYMEXPORT alpm_trans_commit(alpm_list_t **data)
|
||||||
return(_alpm_trans_commit(handle->trans, data));
|
return(_alpm_trans_commit(handle->trans, data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Interrupt a transaction.
|
||||||
|
* @return 0 on success, -1 on error (pm_errno is set accordingly)
|
||||||
|
*/
|
||||||
|
int SYMEXPORT alpm_trans_interrupt()
|
||||||
|
{
|
||||||
|
pmtrans_t *trans;
|
||||||
|
|
||||||
|
ALPM_LOG_FUNC;
|
||||||
|
|
||||||
|
/* Sanity checks */
|
||||||
|
ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
|
||||||
|
|
||||||
|
trans = handle->trans;
|
||||||
|
ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
|
||||||
|
ASSERT(trans->state == STATE_COMMITING || trans->state == STATE_INTERRUPTED,
|
||||||
|
RET_ERR(PM_ERR_TRANS_TYPE, -1));
|
||||||
|
|
||||||
|
trans->state = STATE_INTERRUPTED;
|
||||||
|
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
|
||||||
/** Release a transaction.
|
/** Release a transaction.
|
||||||
* @return 0 on success, -1 on error (pm_errno is set accordingly)
|
* @return 0 on success, -1 on error (pm_errno is set accordingly)
|
||||||
*/
|
*/
|
||||||
int SYMEXPORT alpm_trans_release()
|
int SYMEXPORT alpm_trans_release()
|
||||||
{
|
{
|
||||||
pmtrans_t *trans;
|
pmtrans_t *trans;
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
ALPM_LOG_FUNC;
|
ALPM_LOG_FUNC;
|
||||||
|
|
||||||
|
@ -182,15 +203,6 @@ int SYMEXPORT alpm_trans_release()
|
||||||
ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
|
ASSERT(trans != NULL, RET_ERR(PM_ERR_TRANS_NULL, -1));
|
||||||
ASSERT(trans->state != STATE_IDLE, RET_ERR(PM_ERR_TRANS_NULL, -1));
|
ASSERT(trans->state != STATE_IDLE, RET_ERR(PM_ERR_TRANS_NULL, -1));
|
||||||
|
|
||||||
/* during a commit do not interrupt immediately, just after a target */
|
|
||||||
if(trans->state == STATE_COMMITING || trans->state == STATE_INTERRUPTED) {
|
|
||||||
if(trans->state == STATE_COMMITING) {
|
|
||||||
trans->state = STATE_INTERRUPTED;
|
|
||||||
}
|
|
||||||
pm_errno = PM_ERR_TRANS_COMMITING;
|
|
||||||
ret = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
_alpm_trans_free(trans);
|
_alpm_trans_free(trans);
|
||||||
handle->trans = NULL;
|
handle->trans = NULL;
|
||||||
|
|
||||||
|
@ -206,7 +218,7 @@ int SYMEXPORT alpm_trans_release()
|
||||||
alpm_option_get_lockfile());
|
alpm_option_get_lockfile());
|
||||||
}
|
}
|
||||||
|
|
||||||
return(ret);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
|
@ -207,9 +207,14 @@ static void cleanup(int signum)
|
||||||
pm_fprintf(stderr, PM_LOG_ERROR, "Internal pacman error: Segmentation fault.\n"
|
pm_fprintf(stderr, PM_LOG_ERROR, "Internal pacman error: Segmentation fault.\n"
|
||||||
"Please submit a full bug report with --debug if appropriate.\n");
|
"Please submit a full bug report with --debug if appropriate.\n");
|
||||||
exit(signum);
|
exit(signum);
|
||||||
} else if((signum == SIGINT) && (alpm_trans_release() == -1)
|
} else if((signum == SIGINT)) {
|
||||||
&& (pm_errno == PM_ERR_TRANS_COMMITING)) {
|
if(alpm_trans_interrupt() == 0) {
|
||||||
return;
|
/* a transaction is being interrupted, don't exit pacman yet. */
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
/* no commiting transaction, we can release it now and then exit pacman */
|
||||||
|
alpm_trans_release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* free alpm library resources */
|
/* free alpm library resources */
|
||||||
|
|
Loading…
Add table
Reference in a new issue