Fix concurrency issues in pkgdb: only allow 1 write transaction at the same time.

This implementation relies on a POSIX named semaphore, which is also
required by xbps-rindex(8).
This commit is contained in:
Juan RP
2014-02-23 08:23:14 +01:00
parent bc2bada045
commit 4d1cdcac0c
9 changed files with 115 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
/*-
* Copyright (c) 2008-2013 Juan Romero Pardines.
* Copyright (c) 2008-2014 Juan Romero Pardines.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -216,6 +216,11 @@ main(int argc, char **argv)
if (sync && !update && (argc == optind))
exit(EXIT_SUCCESS);
if ((rv = xbps_pkgdb_lock(&xh)) != 0) {
fprintf(stderr, "Failed to lock the pkgdb: %s\n", strerror(rv));
exit(rv);
}
if (update && (argc == optind)) {
/* Update all installed packages */
rv = dist_upgrade(&xh, maxcols, yes, drun);
@@ -223,19 +228,24 @@ main(int argc, char **argv)
/* Update target packages */
for (i = optind; i < argc; i++) {
rv = update_pkg(&xh, argv[i]);
if (rv != 0)
if (rv != 0) {
xbps_pkgdb_unlock(&xh);
exit(rv);
}
}
rv = exec_transaction(&xh, maxcols, yes, drun);
} else if (!update) {
/* Install target packages */
for (i = optind; i < argc; i++) {
rv = install_new_pkg(&xh, argv[i], reinstall);
if (rv != 0)
if (rv != 0) {
xbps_pkgdb_unlock(&xh);
exit(rv);
}
}
rv = exec_transaction(&xh, maxcols, yes, drun);
}
xbps_pkgdb_unlock(&xh);
exit(rv);
}

View File

@@ -1,5 +1,5 @@
/*-
* Copyright (c) 2013 Juan Romero Pardines.
* Copyright (c) 2013-2014 Juan Romero Pardines.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -146,12 +146,17 @@ main(int argc, char **argv)
exit(EXIT_FAILURE);
}
if ((rv = xbps_pkgdb_lock(&xh)) != 0) {
fprintf(stderr, "failed to lock pkgdb: %s\n", strerror(rv));
exit(EXIT_FAILURE);
}
if (update_format)
convert_pkgdb_format(&xh);
else if (instmode) {
if (argc == optind) {
fprintf(stderr,
"xbps-pkgdb: missing PKGNAME argument\n");
xbps_pkgdb_unlock(&xh);
exit(EXIT_FAILURE);
}
for (i = optind; i < argc; i++) {
@@ -160,6 +165,7 @@ main(int argc, char **argv)
fprintf(stderr, "xbps-pkgdb: failed to "
"change to %s mode to %s: %s\n",
instmode, argv[i], strerror(rv));
xbps_pkgdb_unlock(&xh);
exit(EXIT_FAILURE);
}
}
@@ -174,5 +180,6 @@ main(int argc, char **argv)
}
}
xbps_pkgdb_unlock(&xh);
exit(rv ? EXIT_FAILURE : EXIT_SUCCESS);
}

View File

@@ -350,8 +350,14 @@ main(int argc, char **argv)
exit(rv);;
}
if ((rv = xbps_pkgdb_lock(&xh)) != 0) {
fprintf(stderr, "failed to lock pkgdb: %s\n", strerror(rv));
exit(rv);
}
if (orphans) {
if ((rv = xbps_transaction_autoremove_pkgs(&xh)) != 0) {
xbps_pkgdb_unlock(&xh);
if (rv != ENOENT) {
fprintf(stderr, "Failed to queue package "
"orphans: %s\n", strerror(rv));
@@ -370,11 +376,13 @@ main(int argc, char **argv)
else
reqby_force = true;
}
if (reqby_force && !ignore_revdeps)
if (reqby_force && !ignore_revdeps) {
xbps_pkgdb_unlock(&xh);
exit(EXIT_FAILURE);
if (orphans || (argc > optind))
}
if (orphans || (argc > optind)) {
rv = exec_transaction(&xh, maxcols, yes, drun);
}
xbps_pkgdb_unlock(&xh);
exit(rv);
}