Fix return from chroot() on verify (RhBug:590588)
authorPanu Matilainen <pmatilai@redhat.com>
Tue, 18 May 2010 07:39:22 +0000 (10:39 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Tue, 18 May 2010 07:43:05 +0000 (10:43 +0300)
- a couple of important steps in chroot() in and out sequence missing,
  causing "No such file or directory" whining on return from chroot()
  unless cwd happened to be /

lib/verify.c

index fc581f5..30938b9 100644 (file)
@@ -448,6 +448,7 @@ int rpmcliVerify(rpmts ts, QVA_t qva, char * const * argv)
 {
     rpmVSFlags vsflags, ovsflags;
     int ec = 0, xx;
+    int dirfd = -1;
     const char * rootDir = rpmtsRootDir(ts);
     FD_t scriptFd = fdDup(STDOUT_FILENO);
 
@@ -458,7 +459,8 @@ int rpmcliVerify(rpmts ts, QVA_t qva, char * const * argv)
     rpmtsOpenDB(ts, O_RDONLY);
     rpmdbOpenAll(rpmtsGetRdb(ts));
     if (rootDir && !rstreq(rootDir, "/")) {
-       if (chroot(rootDir) == -1) {
+       dirfd = open(".", O_RDONLY);
+       if (dirfd == -1 || chdir("/") == -1 || chroot(rootDir) == -1) {
            rpmlog(RPMLOG_ERR, _("Unable to change root directory: %m\n"));
            ec = 1;
            goto exit;
@@ -494,11 +496,13 @@ int rpmcliVerify(rpmts ts, QVA_t qva, char * const * argv)
     if (rpmtsChrootDone(ts)) {
        /* only done if previous chroot succeeded, assume success */
        xx = chroot(".");
+       xx = fchdir(dirfd);
        rpmtsSetChrootDone(ts, 0);
     }
 
 exit:
     Fclose(scriptFd);
+    if (dirfd >= 0) close(dirfd);
 
     return ec;
 }