X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/d568ed2d3ba5d7d05e4381ee59468a124309f152..bebdca4bf63ae661c60bc1e0f4e03e6bbb7a7cc5:/apt-pkg/deb/dpkgpm.cc diff --git a/apt-pkg/deb/dpkgpm.cc b/apt-pkg/deb/dpkgpm.cc index 228e45c48..f8c59d844 100644 --- a/apt-pkg/deb/dpkgpm.cc +++ b/apt-pkg/deb/dpkgpm.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: dpkgpm.cc,v 1.4 1998/11/23 07:32:21 jgg Exp $ +// $Id: dpkgpm.cc,v 1.8 1999/03/05 19:36:49 jgg Exp $ /* ###################################################################### DPKG Package Manager - Provide an interface to dpkg @@ -71,6 +71,81 @@ bool pkgDPkgPM::Remove(PkgIterator Pkg) return false; List.push_back(Item(Item::Remove,Pkg)); + return true; +} + /*}}}*/ +// DPkgPM::RunScripts - Run a set of scripts /*{{{*/ +// --------------------------------------------------------------------- +/* This looks for a list of script sto run from the configuration file, + each one is run with system from a forked child. */ +bool pkgDPkgPM::RunScripts(const char *Cnf) +{ + Configuration::Item const *Opts = _config->Tree(Cnf); + if (Opts == 0 || Opts->Child == 0) + return true; + Opts = Opts->Child; + + // Fork for running the system calls + pid_t Child = fork(); + if (Child < 0) + return _error->Errno("fork","Could't fork"); + + // This is the child + if (Child == 0) + { + signal(SIGPIPE,SIG_DFL); + signal(SIGQUIT,SIG_DFL); + signal(SIGINT,SIG_DFL); + signal(SIGWINCH,SIG_DFL); + signal(SIGCONT,SIG_DFL); + signal(SIGTSTP,SIG_DFL); + + if (chdir("/tmp/") != 0) + _exit(100); + + // Close all of our FDs - just in case + for (int K = 3; K != 40; K++) + fcntl(K,F_SETFD,FD_CLOEXEC); + + unsigned int Count = 1; + for (; Opts != 0; Opts = Opts->Next, Count++) + { + if (Opts->Value.empty() == true) + continue; + + if (system(Opts->Value.c_str()) != 0) + _exit(100+Count); + } + _exit(0); + } + + // Wait for the child + int Status = 0; + while (waitpid(Child,&Status,0) != Child) + { + if (errno == EINTR) + continue; + return _error->Errno("waitpid","Couldn't wait for subprocess"); + } + + // Restore sig int/quit + signal(SIGQUIT,SIG_DFL); + signal(SIGINT,SIG_DFL); + + // Check for an error code. + if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) + { + unsigned int Count = WEXITSTATUS(Status); + if (Count > 100) + { + Count -= 100; + for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--); + _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str()); + } + + return _error->Error("Sub-process returned an error code"); + } + return true; } /*}}}*/ @@ -79,6 +154,9 @@ bool pkgDPkgPM::Remove(PkgIterator Pkg) /* This globs the operations and calls dpkg */ bool pkgDPkgPM::Go() { + if (RunScripts("DPkg::Pre-Invoke") == false) + return false; + for (vector::iterator I = List.begin(); I != List.end();) { vector::iterator J = I; @@ -94,6 +172,20 @@ bool pkgDPkgPM::Go() Args[n++] = _config->Find("Dir::Bin::dpkg","dpkg").c_str(); Size += strlen(Args[n-1]); + // Stick in any custom dpkg options + Configuration::Item const *Opts = _config->Tree("DPkg::Options"); + if (Opts != 0) + { + Opts = Opts->Child; + for (; Opts != 0; Opts = Opts->Next) + { + if (Opts->Value.empty() == true) + continue; + Args[n++] = Opts->Value.c_str(); + Size += Opts->Value.length(); + } + } + switch (I->Op) { case Item::Remove: @@ -121,6 +213,8 @@ bool pkgDPkgPM::Go() { for (;I != J && Size < 1024; I++) { + if (I->File[0] != '/') + return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str()); Args[n++] = I->File.c_str(); Size += strlen(Args[n-1]); } @@ -154,12 +248,12 @@ bool pkgDPkgPM::Go() it doesn't die but we do! So we must also ignore it */ signal(SIGQUIT,SIG_IGN); signal(SIGINT,SIG_IGN); - + // Fork dpkg pid_t Child = fork(); if (Child < 0) return _error->Errno("fork","Could't fork"); - + // This is the child if (Child == 0) { @@ -169,9 +263,9 @@ bool pkgDPkgPM::Go() signal(SIGWINCH,SIG_DFL); signal(SIGCONT,SIG_DFL); signal(SIGTSTP,SIG_DFL); - - if (chdir(_config->FindDir("Dir::Cache::Archives").c_str()) != 0) - exit(100); + + if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0) + _exit(100); // Close all of our FDs - just in case for (int K = 3; K != 40; K++) @@ -179,23 +273,23 @@ bool pkgDPkgPM::Go() int Flags,dummy; if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0) - exit(100); + _exit(100); // Discard everything in stdin before forking dpkg if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0) - exit(100); + _exit(100); while (read(STDIN_FILENO,&dummy,1) == 1); if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0) - exit(100); + _exit(100); /* No Job Control Stop Env is a magic dpkg var that prevents it from using sigstop */ setenv("DPKG_NO_TSTP","yes",1); execvp(Args[0],(char **)Args); cerr << "Could not exec dpkg!" << endl; - exit(100); + _exit(100); } // Wait for dpkg @@ -204,17 +298,30 @@ bool pkgDPkgPM::Go() { if (errno == EINTR) continue; + RunScripts("DPkg::Post-Invoke"); return _error->Errno("waitpid","Couldn't wait for subprocess"); } - - // Check for an error code. - if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) - return _error->Error("Sub-process returned an error code"); // Restore sig int/quit signal(SIGQUIT,SIG_DFL); signal(SIGINT,SIG_DFL); + + // Check for an error code. + if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) + { + RunScripts("DPkg::Post-Invoke"); + if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV) + return _error->Error("Sub-process recieved a segmentation fault."); + + if (WIFEXITED(Status) != 0) + return _error->Error("Sub-process returned an error code (%u)",WEXITSTATUS(Status)); + + return _error->Error("Sub-process exited unexpectedly"); + } } + + if (RunScripts("DPkg::Post-Invoke") == false) + return false; return true; } /*}}}*/