summaryrefslogtreecommitdiff
path: root/lib/libalpm/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libalpm/util.c')
-rw-r--r--lib/libalpm/util.c33
1 files changed, 19 insertions, 14 deletions
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index 178de865..0e685d07 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -360,8 +360,9 @@ int _alpm_runscriptlet(char *root, char *installfn, char *script, char *ver, cha
char tmpdir[PATH_MAX] = "";
char *scriptpath;
struct stat buf;
- char cwd[PATH_MAX];
+ char cwd[PATH_MAX] = "";
pid_t pid;
+ int retval = 0;
if(stat(installfn, &buf)) {
/* not found */
@@ -382,7 +383,6 @@ int _alpm_runscriptlet(char *root, char *installfn, char *script, char *ver, cha
snprintf(scriptfn, PATH_MAX, "%s/.INSTALL", tmpdir);
/* chop off the root so we can find the tmpdir in the chroot */
scriptpath = scriptfn + strlen(root) - 1;
- return(0);
} else {
STRNCPY(scriptfn, installfn, PATH_MAX);
/* chop off the root so we can find the tmpdir in the chroot */
@@ -391,14 +391,16 @@ int _alpm_runscriptlet(char *root, char *installfn, char *script, char *ver, cha
if(!_alpm_grep(scriptfn, script)) {
/* script not found in scriptlet file */
- if(strlen(tmpdir) && _alpm_rmrf(tmpdir)) {
- _alpm_log(PM_LOG_WARNING, "could not remove tmpdir %s", tmpdir);
- }
- return(0);
+ goto cleanup;
}
/* save the cwd so we can restore it later */
- getcwd(cwd, PATH_MAX);
+ if(getcwd(cwd, PATH_MAX) == NULL) {
+ _alpm_log(PM_LOG_ERROR, "could not get current working directory");
+ /* in case of error, cwd content is undefined: so we set it to something */
+ cwd[0] = 0;
+ }
+
/* just in case our cwd was removed in the upgrade operation */
chdir("/");
@@ -416,15 +418,15 @@ int _alpm_runscriptlet(char *root, char *installfn, char *script, char *ver, cha
pid = fork();
if(pid == -1) {
_alpm_log(PM_LOG_ERROR, "could not fork a new process (%s)", strerror(errno));
- chdir(cwd);
- return(1);
+ retval = 1;
+ goto cleanup;
}
if(pid == 0) {
_alpm_log(PM_LOG_DEBUG, "chrooting in %s", root);
if(chroot(root) != 0) {
_alpm_log(PM_LOG_ERROR, "could not change the root directory (%s)", strerror(errno));
- return (1);
+ return(1);
}
umask(0022);
_alpm_log(PM_LOG_DEBUG, "executing \"%s\"", cmdline);
@@ -433,17 +435,20 @@ int _alpm_runscriptlet(char *root, char *installfn, char *script, char *ver, cha
} else {
if(waitpid(pid, 0, 0) == -1) {
_alpm_log(PM_LOG_ERROR, "call to waitpid failed (%s)", strerror(errno));
- chdir(cwd);
- return(1);
+ retval = 1;
+ goto cleanup;
}
}
+cleanup:
if(strlen(tmpdir) && _alpm_rmrf(tmpdir)) {
_alpm_log(PM_LOG_WARNING, "could not remove tmpdir %s", tmpdir);
}
+ if(strlen(cwd)) {
+ chdir(cwd);
+ }
- chdir(cwd);
- return(0);
+ return(retval);
}
/* vim: set ts=2 sw=2 noet: */