summaryrefslogtreecommitdiff
path: root/apt-pkg/deb/dpkgpm.cc
diff options
context:
space:
mode:
Diffstat (limited to 'apt-pkg/deb/dpkgpm.cc')
-rw-r--r--apt-pkg/deb/dpkgpm.cc725
1 files changed, 433 insertions, 292 deletions
diff --git a/apt-pkg/deb/dpkgpm.cc b/apt-pkg/deb/dpkgpm.cc
index b069f22f7..b975754b0 100644
--- a/apt-pkg/deb/dpkgpm.cc
+++ b/apt-pkg/deb/dpkgpm.cc
@@ -19,6 +19,7 @@
#include <apt-pkg/fileutl.h>
#include <apt-pkg/cachefile.h>
#include <apt-pkg/packagemanager.h>
+#include <apt-pkg/install-progress.h>
#include <unistd.h>
#include <stdlib.h>
@@ -37,11 +38,13 @@
#include <map>
#include <pwd.h>
#include <grp.h>
+#include <iomanip>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <pty.h>
+#include <stdio.h>
#include <apti18n.h>
/*}}}*/
@@ -52,10 +55,14 @@ class pkgDPkgPMPrivate
{
public:
pkgDPkgPMPrivate() : stdin_is_dev_null(false), dpkgbuf_pos(0),
- term_out(NULL), history_out(NULL)
+ term_out(NULL), history_out(NULL),
+ progress(NULL), master(-1), slave(-1)
{
dpkgbuf[0] = '\0';
}
+ ~pkgDPkgPMPrivate()
+ {
+ }
bool stdin_is_dev_null;
// the buffer we use for the dpkg status-fd reading
char dpkgbuf[1024];
@@ -63,6 +70,17 @@ public:
FILE *term_out;
FILE *history_out;
string dpkg_error;
+ APT::Progress::PackageManager *progress;
+
+ // pty stuff
+ struct termios tt;
+ int master;
+ int slave;
+
+ // signals
+ sigset_t sigmask;
+ sigset_t original_sigmask;
+
};
namespace
@@ -125,6 +143,20 @@ ionice(int PID)
return ExecWait(Process, "ionice");
}
+static std::string getDpkgExecutable()
+{
+ string Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
+ string const dpkgChrootDir = _config->FindDir("DPkg::Chroot-Directory", "/");
+ size_t dpkgChrootLen = dpkgChrootDir.length();
+ if (dpkgChrootDir != "/" && Tmp.find(dpkgChrootDir) == 0)
+ {
+ if (dpkgChrootDir[dpkgChrootLen - 1] == '/')
+ --dpkgChrootLen;
+ Tmp = Tmp.substr(dpkgChrootLen);
+ }
+ return Tmp;
+}
+
// dpkgChrootDirectory - chrooting for dpkg if needed /*{{{*/
static void dpkgChrootDirectory()
{
@@ -170,7 +202,7 @@ pkgCache::VerIterator FindNowVersion(const pkgCache::PkgIterator &Pkg)
// ---------------------------------------------------------------------
/* */
pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache)
- : pkgPackageManager(Cache), PackagesDone(0), PackagesTotal(0)
+ : pkgPackageManager(Cache), pkgFailures(0), PackagesDone(0), PackagesTotal(0)
{
d = new pkgDPkgPMPrivate();
}
@@ -385,17 +417,21 @@ bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf)
unsigned int InfoFD = _config->FindI(OptSec + "::InfoFD", STDIN_FILENO);
// Create the pipes
+ std::set<int> KeepFDs;
+ MergeKeepFdsFromConfiguration(KeepFDs);
int Pipes[2];
if (pipe(Pipes) != 0)
return _error->Errno("pipe","Failed to create IPC pipe to subprocess");
if (InfoFD != (unsigned)Pipes[0])
SetCloseExec(Pipes[0],true);
else
- _config->Set("APT::Keep-Fds::", Pipes[0]);
+ KeepFDs.insert(Pipes[0]);
+
+
SetCloseExec(Pipes[1],true);
// Purified Fork for running the script
- pid_t Process = ExecFork();
+ pid_t Process = ExecFork(KeepFDs);
if (Process == 0)
{
// Setup the FDs
@@ -417,8 +453,6 @@ bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf)
execv(Args[0],(char **)Args);
_exit(100);
}
- if (InfoFD == (unsigned)Pipes[0])
- _config->Clear("APT::Keep-Fds", Pipes[0]);
close(Pipes[0]);
FILE *F = fdopen(Pipes[1],"w");
if (F == 0)
@@ -501,156 +535,184 @@ void pkgDPkgPM::DoTerminalPty(int master)
// ---------------------------------------------------------------------
/*
*/
-void pkgDPkgPM::ProcessDpkgStatusLine(int OutStatusFd, char *line)
+void pkgDPkgPM::ProcessDpkgStatusLine(char *line)
{
bool const Debug = _config->FindB("Debug::pkgDPkgProgressReporting",false);
- // the status we output
- ostringstream status;
-
if (Debug == true)
std::clog << "got from dpkg '" << line << "'" << std::endl;
-
/* dpkg sends strings like this:
- 'status: <pkg>: <pkg qstate>'
- errors look like this:
- 'status: /var/cache/apt/archives/krecipes_0.8.1-0ubuntu1_i386.deb : error : trying to overwrite `/usr/share/doc/kde/HTML/en/krecipes/krectip.png', which is also in package krecipes-data
- and conffile-prompt like this
- 'status: conffile-prompt: conffile : 'current-conffile' 'new-conffile' useredited distedited
+ 'status: <pkg>: <pkg qstate>'
+ 'status: <pkg>:<arch>: <pkg qstate>'
- Newer versions of dpkg sent also:
- 'processing: install: pkg'
- 'processing: configure: pkg'
- 'processing: remove: pkg'
- 'processing: purge: pkg'
- 'processing: disappear: pkg'
- 'processing: trigproc: trigger'
-
+ 'processing: {install,configure,remove,purge,disappear,trigproc}: pkg'
+ 'processing: {install,configure,remove,purge,disappear,trigproc}: trigger'
*/
- char* list[6];
- // dpkg sends multiline error messages sometimes (see
- // #374195 for a example. we should support this by
- // either patching dpkg to not send multiline over the
- // statusfd or by rewriting the code here to deal with
- // it. for now we just ignore it and not crash
- TokSplitString(':', line, list, sizeof(list)/sizeof(list[0]));
- if( list[0] == NULL || list[1] == NULL || list[2] == NULL)
+
+ // we need to split on ": " (note the appended space) as the ':' is
+ // part of the pkgname:arch information that dpkg sends
+ //
+ // A dpkg error message may contain additional ":" (like
+ // "failed in buffer_write(fd) (10, ret=-1): backend dpkg-deb ..."
+ // so we need to ensure to not split too much
+ std::vector<std::string> list = StringSplit(line, ": ", 4);
+ if(list.size() < 3)
{
if (Debug == true)
std::clog << "ignoring line: not enough ':'" << std::endl;
return;
}
- const char* const pkg = list[1];
- const char* action = _strstrip(list[2]);
+
+ // build the (prefix, pkgname, action) tuple, position of this
+ // is different for "processing" or "status" messages
+ std::string prefix = APT::String::Strip(list[0]);
+ std::string pkgname;
+ std::string action;
+
+ // "processing" has the form "processing: action: pkg or trigger"
+ // with action = ["install", "configure", "remove", "purge", "disappear",
+ // "trigproc"]
+ if (prefix == "processing")
+ {
+ pkgname = APT::String::Strip(list[2]);
+ action = APT::String::Strip(list[1]);
+ }
+ // "status" has the form: "status: pkg: state"
+ // with state in ["half-installed", "unpacked", "half-configured",
+ // "installed", "config-files", "not-installed"]
+ else if (prefix == "status")
+ {
+ pkgname = APT::String::Strip(list[1]);
+ action = APT::String::Strip(list[2]);
+ } else {
+ if (Debug == true)
+ std::clog << "unknown prefix '" << prefix << "'" << std::endl;
+ return;
+ }
+
+
+ /* handle the special cases first:
+
+ errors look like this:
+ 'status: /var/cache/apt/archives/krecipes_0.8.1-0ubuntu1_i386.deb : error : trying to overwrite `/usr/share/doc/kde/HTML/en/krecipes/krectip.png', which is also in package krecipes-data
+ and conffile-prompt like this
+ 'status:/etc/compiz.conf/compiz.conf : conffile-prompt: 'current-conffile' 'new-conffile' useredited distedited
+ */
+ if (prefix == "status")
+ {
+ if(action == "error")
+ {
+ d->progress->Error(list[1], PackagesDone, PackagesTotal,
+ list[3]);
+ pkgFailures++;
+ WriteApportReport(list[1].c_str(), list[3].c_str());
+ return;
+ }
+ else if(action == "conffile-prompt")
+ {
+ d->progress->ConffilePrompt(list[1], PackagesDone, PackagesTotal,
+ list[3]);
+ return;
+ }
+ }
+
+ // at this point we know that we should have a valid pkgname, so build all
+ // the info from it
+
+ // dpkg does not send always send "pkgname:arch" so we add it here
+ // if needed
+ if (pkgname.find(":") == std::string::npos)
+ {
+ // find the package in the group that is in a touched by dpkg
+ // if there are multiple dpkg will send us a full pkgname:arch
+ pkgCache::GrpIterator Grp = Cache.FindGrp(pkgname);
+ if (Grp.end() == false)
+ {
+ pkgCache::PkgIterator P = Grp.PackageList();
+ for (; P.end() != true; P = Grp.NextPkg(P))
+ {
+ if(Cache[P].Mode != pkgDepCache::ModeKeep)
+ {
+ pkgname = P.FullName();
+ break;
+ }
+ }
+ }
+ }
+
+ const char* const pkg = pkgname.c_str();
+ std::string short_pkgname = StringSplit(pkgname, ":")[0];
+ std::string arch = "";
+ if (pkgname.find(":") != string::npos)
+ arch = StringSplit(pkgname, ":")[1];
+ std::string i18n_pkgname = pkgname;
+ if (arch.size() != 0)
+ strprintf(i18n_pkgname, "%s (%s)", short_pkgname.c_str(), arch.c_str());
// 'processing' from dpkg looks like
// 'processing: action: pkg'
- if(strncmp(list[0], "processing", strlen("processing")) == 0)
+ if(prefix == "processing")
{
- char s[200];
- const char* const pkg_or_trigger = _strstrip(list[2]);
- action = _strstrip( list[1]);
const std::pair<const char *, const char *> * const iter =
std::find_if(PackageProcessingOpsBegin,
PackageProcessingOpsEnd,
- MatchProcessingOp(action));
+ MatchProcessingOp(action.c_str()));
if(iter == PackageProcessingOpsEnd)
{
if (Debug == true)
std::clog << "ignoring unknown action: " << action << std::endl;
return;
}
- snprintf(s, sizeof(s), _(iter->second), pkg_or_trigger);
-
- status << "pmstatus:" << pkg_or_trigger
- << ":" << (PackagesDone/float(PackagesTotal)*100.0)
- << ":" << s
- << endl;
- if(OutStatusFd > 0)
- FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
- if (Debug == true)
- std::clog << "send: '" << status.str() << "'" << endl;
-
- if (strncmp(action, "disappear", strlen("disappear")) == 0)
- handleDisappearAction(pkg_or_trigger);
- return;
- }
-
- if(strncmp(action,"error",strlen("error")) == 0)
- {
- // urgs, sometime has ":" in its error string so that we
- // end up with the error message split between list[3]
- // and list[4], e.g. the message:
- // "failed in buffer_write(fd) (10, ret=-1): backend dpkg-deb ..."
- // concat them again
- if( list[4] != NULL )
- list[3][strlen(list[3])] = ':';
-
- status << "pmerror:" << list[1]
- << ":" << (PackagesDone/float(PackagesTotal)*100.0)
- << ":" << list[3]
- << endl;
- if(OutStatusFd > 0)
- FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
- if (Debug == true)
- std::clog << "send: '" << status.str() << "'" << endl;
- pkgFailures++;
- WriteApportReport(list[1], list[3]);
- return;
- }
- else if(strncmp(action,"conffile",strlen("conffile")) == 0)
- {
- status << "pmconffile:" << list[1]
- << ":" << (PackagesDone/float(PackagesTotal)*100.0)
- << ":" << list[3]
- << endl;
- if(OutStatusFd > 0)
- FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
- if (Debug == true)
- std::clog << "send: '" << status.str() << "'" << endl;
+ std::string msg;
+ strprintf(msg, _(iter->second), i18n_pkgname.c_str());
+ d->progress->StatusChanged(pkgname, PackagesDone, PackagesTotal, msg);
+
+ // FIXME: this needs a muliarch testcase
+ // FIXME2: is "pkgname" here reliable with dpkg only sending us
+ // short pkgnames?
+ if (action == "disappear")
+ handleDisappearAction(pkgname);
return;
- }
+ }
- vector<struct DpkgState> const &states = PackageOps[pkg];
- const char *next_action = NULL;
- if(PackageOpsDone[pkg] < states.size())
- next_action = states[PackageOpsDone[pkg]].state;
- // check if the package moved to the next dpkg state
- if(next_action && (strcmp(action, next_action) == 0))
+ if (prefix == "status")
{
- // only read the translation if there is actually a next
- // action
- const char *translation = _(states[PackageOpsDone[pkg]].str);
- char s[200];
- snprintf(s, sizeof(s), translation, pkg);
-
- // we moved from one dpkg state to a new one, report that
- PackageOpsDone[pkg]++;
- PackagesDone++;
- // build the status str
- status << "pmstatus:" << pkg
- << ":" << (PackagesDone/float(PackagesTotal)*100.0)
- << ":" << s
- << endl;
- if(OutStatusFd > 0)
- FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
- if (Debug == true)
- std::clog << "send: '" << status.str() << "'" << endl;
+ vector<struct DpkgState> const &states = PackageOps[pkg];
+ const char *next_action = NULL;
+ if(PackageOpsDone[pkg] < states.size())
+ next_action = states[PackageOpsDone[pkg]].state;
+ // check if the package moved to the next dpkg state
+ if(next_action && (action == next_action))
+ {
+ // only read the translation if there is actually a next
+ // action
+ const char *translation = _(states[PackageOpsDone[pkg]].str);
+ std::string msg;
+
+ // we moved from one dpkg state to a new one, report that
+ PackageOpsDone[pkg]++;
+ PackagesDone++;
+
+ strprintf(msg, translation, i18n_pkgname.c_str());
+ d->progress->StatusChanged(pkgname, PackagesDone, PackagesTotal, msg);
+
+ }
+ if (Debug == true)
+ std::clog << "(parsed from dpkg) pkg: " << short_pkgname
+ << " action: " << action << endl;
}
- if (Debug == true)
- std::clog << "(parsed from dpkg) pkg: " << pkg
- << " action: " << action << endl;
}
/*}}}*/
// DPkgPM::handleDisappearAction /*{{{*/
void pkgDPkgPM::handleDisappearAction(string const &pkgname)
{
- // record the package name for display and stuff later
- disappearedPkgs.insert(pkgname);
-
pkgCache::PkgIterator Pkg = Cache.FindPkg(pkgname);
if (unlikely(Pkg.end() == true))
return;
+
+ // record the package name for display and stuff later
+ disappearedPkgs.insert(Pkg.FullName(true));
+
// the disappeared package was auto-installed - nothing to do
if ((Cache[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto)
return;
@@ -693,7 +755,7 @@ void pkgDPkgPM::handleDisappearAction(string const &pkgname)
// ---------------------------------------------------------------------
/*
*/
-void pkgDPkgPM::DoDpkgStatusFd(int statusfd, int OutStatusFd)
+void pkgDPkgPM::DoDpkgStatusFd(int statusfd)
{
char *p, *q;
int len;
@@ -708,7 +770,7 @@ void pkgDPkgPM::DoDpkgStatusFd(int statusfd, int OutStatusFd)
while((q=(char*)memchr(p, '\n', d->dpkgbuf+d->dpkgbuf_pos-p)) != NULL)
{
*q = 0;
- ProcessDpkgStatusLine(OutStatusFd, p);
+ ProcessDpkgStatusLine(p);
p=q+1; // continue with next line
}
@@ -874,6 +936,7 @@ bool pkgDPkgPM::CloseLog()
return true;
}
/*}}}*/
+ /*}}}*/
/*{{{*/
// This implements a racy version of pselect for those architectures
// that don't have a working implementation.
@@ -894,36 +957,158 @@ static int racy_pselect(int nfds, fd_set *readfds, fd_set *writefds,
sigprocmask(SIG_SETMASK, &origmask, 0);
return retval;
}
-/*}}}*/
+ /*}}}*/
+
+// DPkgPM::BuildPackagesProgressMap /*{{{*/
+void pkgDPkgPM::BuildPackagesProgressMap()
+{
+ // map the dpkg states to the operations that are performed
+ // (this is sorted in the same way as Item::Ops)
+ static const struct DpkgState DpkgStatesOpMap[][7] = {
+ // Install operation
+ {
+ {"half-installed", N_("Preparing %s")},
+ {"unpacked", N_("Unpacking %s") },
+ {NULL, NULL}
+ },
+ // Configure operation
+ {
+ {"unpacked",N_("Preparing to configure %s") },
+ {"half-configured", N_("Configuring %s") },
+ { "installed", N_("Installed %s")},
+ {NULL, NULL}
+ },
+ // Remove operation
+ {
+ {"half-configured", N_("Preparing for removal of %s")},
+ {"half-installed", N_("Removing %s")},
+ {"config-files", N_("Removed %s")},
+ {NULL, NULL}
+ },
+ // Purge operation
+ {
+ {"config-files", N_("Preparing to completely remove %s")},
+ {"not-installed", N_("Completely removed %s")},
+ {NULL, NULL}
+ },
+ };
+
+ // init the PackageOps map, go over the list of packages that
+ // that will be [installed|configured|removed|purged] and add
+ // them to the PackageOps map (the dpkg states it goes through)
+ // and the PackageOpsTranslations (human readable strings)
+ for (vector<Item>::const_iterator I = List.begin(); I != List.end(); ++I)
+ {
+ if((*I).Pkg.end() == true)
+ continue;
+
+ string const name = (*I).Pkg.FullName();
+ PackageOpsDone[name] = 0;
+ for(int i=0; (DpkgStatesOpMap[(*I).Op][i]).state != NULL; ++i)
+ {
+ PackageOps[name].push_back(DpkgStatesOpMap[(*I).Op][i]);
+ PackagesTotal++;
+ }
+ }
+}
+ /*}}}*/
+#if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR < 13)
+bool pkgDPkgPM::Go(int StatusFd)
+{
+ APT::Progress::PackageManager *progress = NULL;
+ if (StatusFd == -1)
+ progress = APT::Progress::PackageManagerProgressFactory();
+ else
+ progress = new APT::Progress::PackageManagerProgressFd(StatusFd);
+
+ return GoNoABIBreak(progress);
+}
+#endif
+
+void pkgDPkgPM::StartPtyMagic()
+{
+ if (_config->FindB("Dpkg::Use-Pty", true) == false)
+ {
+ d->master = d->slave = -1;
+ return;
+ }
+
+ // setup the pty and stuff
+ struct winsize win;
+
+ // if tcgetattr does not return zero there was a error
+ // and we do not do any pty magic
+ _error->PushToStack();
+ if (tcgetattr(STDOUT_FILENO, &d->tt) == 0)
+ {
+ if (ioctl(1, TIOCGWINSZ, (char *)&win) < 0)
+ {
+ _error->Errno("ioctl", _("ioctl(TIOCGWINSZ) failed"));
+ } else if (openpty(&d->master, &d->slave, NULL, &d->tt, &win) < 0)
+ {
+ _error->Errno("openpty", _("Can not write log (%s)"), _("Is /dev/pts mounted?"));
+ d->master = d->slave = -1;
+ } else {
+ struct termios rtt;
+ rtt = d->tt;
+ cfmakeraw(&rtt);
+ rtt.c_lflag &= ~ECHO;
+ rtt.c_lflag |= ISIG;
+ // block SIGTTOU during tcsetattr to prevent a hang if
+ // the process is a member of the background process group
+ // http://www.opengroup.org/onlinepubs/000095399/functions/tcsetattr.html
+ sigemptyset(&d->sigmask);
+ sigaddset(&d->sigmask, SIGTTOU);
+ sigprocmask(SIG_BLOCK,&d->sigmask, &d->original_sigmask);
+ tcsetattr(0, TCSAFLUSH, &rtt);
+ sigprocmask(SIG_SETMASK, &d->original_sigmask, 0);
+ }
+ }
+ // complain only if stdout is either a terminal (but still failed) or is an invalid
+ // descriptor otherwise we would complain about redirection to e.g. /dev/null as well.
+ else if (isatty(STDOUT_FILENO) == 1 || errno == EBADF)
+ _error->Errno("tcgetattr", _("Can not write log (%s)"), _("Is stdout a terminal?"));
+
+ if (_error->PendingError() == true)
+ _error->DumpErrors(std::cerr);
+ _error->RevertToStack();
+}
+
+void pkgDPkgPM::StopPtyMagic()
+{
+ if(d->slave > 0)
+ close(d->slave);
+ if(d->master >= 0)
+ {
+ tcsetattr(0, TCSAFLUSH, &d->tt);
+ close(d->master);
+ }
+}
+
// DPkgPM::Go - Run the sequence /*{{{*/
// ---------------------------------------------------------------------
/* This globs the operations and calls dpkg
- *
- * If it is called with "OutStatusFd" set to a valid file descriptor
- * apt will report the install progress over this fd. It maps the
- * dpkg states a package goes through to human readable (and i10n-able)
+ *
+ * If it is called with a progress object apt will report the install
+ * progress to this object. It maps the dpkg states a package goes
+ * through to human readable (and i10n-able)
* names and calculates a percentage for each step.
-*/
-bool pkgDPkgPM::Go(int OutStatusFd)
+ */
+#if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
+bool pkgDPkgPM::Go(APT::Progress::PackageManager *progress)
+#else
+bool pkgDPkgPM::GoNoABIBreak(APT::Progress::PackageManager *progress)
+#endif
{
pkgPackageManager::SigINTStop = false;
+ d->progress = progress;
// Generate the base argument list for dpkg
- std::vector<const char *> Args;
unsigned long StartSize = 0;
- string Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
- {
- string const dpkgChrootDir = _config->FindDir("DPkg::Chroot-Directory", "/");
- size_t dpkgChrootLen = dpkgChrootDir.length();
- if (dpkgChrootDir != "/" && Tmp.find(dpkgChrootDir) == 0)
- {
- if (dpkgChrootDir[dpkgChrootLen - 1] == '/')
- --dpkgChrootLen;
- Tmp = Tmp.substr(dpkgChrootLen);
- }
- }
- Args.push_back(Tmp.c_str());
- StartSize += Tmp.length();
+ std::vector<const char *> Args;
+ std::string DpkgExecutable = getDpkgExecutable();
+ Args.push_back(DpkgExecutable.c_str());
+ StartSize += DpkgExecutable.length();
// Stick in any custom dpkg options
Configuration::Item const *Opts = _config->Tree("DPkg::Options");
@@ -960,8 +1145,6 @@ bool pkgDPkgPM::Go(int OutStatusFd)
fd_set rfds;
struct timespec tv;
- sigset_t sigmask;
- sigset_t original_sigmask;
unsigned int const MaxArgs = _config->FindI("Dpkg::MaxArgs",8*1024);
unsigned int const MaxArgBytes = _config->FindI("Dpkg::MaxArgBytes",32*1024);
@@ -980,54 +1163,8 @@ bool pkgDPkgPM::Go(int OutStatusFd)
if (_config->FindB("DPkg::ConfigurePending", SmartConf) == true)
List.push_back(Item(Item::ConfigurePending, PkgIterator()));
- // map the dpkg states to the operations that are performed
- // (this is sorted in the same way as Item::Ops)
- static const struct DpkgState DpkgStatesOpMap[][7] = {
- // Install operation
- {
- {"half-installed", N_("Preparing %s")},
- {"unpacked", N_("Unpacking %s") },
- {NULL, NULL}
- },
- // Configure operation
- {
- {"unpacked",N_("Preparing to configure %s") },
- {"half-configured", N_("Configuring %s") },
- { "installed", N_("Installed %s")},
- {NULL, NULL}
- },
- // Remove operation
- {
- {"half-configured", N_("Preparing for removal of %s")},
- {"half-installed", N_("Removing %s")},
- {"config-files", N_("Removed %s")},
- {NULL, NULL}
- },
- // Purge operation
- {
- {"config-files", N_("Preparing to completely remove %s")},
- {"not-installed", N_("Completely removed %s")},
- {NULL, NULL}
- },
- };
-
- // init the PackageOps map, go over the list of packages that
- // that will be [installed|configured|removed|purged] and add
- // them to the PackageOps map (the dpkg states it goes through)
- // and the PackageOpsTranslations (human readable strings)
- for (vector<Item>::const_iterator I = List.begin(); I != List.end(); ++I)
- {
- if((*I).Pkg.end() == true)
- continue;
-
- string const name = (*I).Pkg.Name();
- PackageOpsDone[name] = 0;
- for(int i=0; (DpkgStatesOpMap[(*I).Op][i]).state != NULL; ++i)
- {
- PackageOps[name].push_back(DpkgStatesOpMap[(*I).Op][i]);
- PackagesTotal++;
- }
- }
+ // for the progress
+ BuildPackagesProgressMap();
d->stdin_is_dev_null = false;
@@ -1049,8 +1186,15 @@ bool pkgDPkgPM::Go(int OutStatusFd)
dpkgMultiArch = true;
}
- // this loop is runs once per operation
- for (vector<Item>::const_iterator I = List.begin(); I != List.end();)
+ // start pty magic before the loop
+ StartPtyMagic();
+
+ // Tell the progress that its starting and fork dpkg
+ d->progress->Start(d->master);
+
+ // this loop is runs once per dpkg operation
+ vector<Item>::const_iterator I = List.begin();
+ while (I != List.end())
{
// Do all actions with the same Op in one run
vector<Item>::const_iterator J = I;
@@ -1171,7 +1315,7 @@ bool pkgDPkgPM::Go(int OutStatusFd)
{
if((*I).Pkg.end() == true)
continue;
- if (I->Op == Item::Configure && disappearedPkgs.find(I->Pkg.Name()) != disappearedPkgs.end())
+ if (I->Op == Item::Configure && disappearedPkgs.find(I->Pkg.FullName(true)) != disappearedPkgs.end())
continue;
// We keep this here to allow "smooth" transitions from e.g. multiarch dpkg/ubuntu to dpkg/debian
if (dpkgMultiArch == false && (I->Pkg.Arch() == nativeArch ||
@@ -1241,72 +1385,29 @@ bool pkgDPkgPM::Go(int OutStatusFd)
// ignore SIGHUP as well (debian #463030)
sighandler_t old_SIGHUP = signal(SIGHUP,SIG_IGN);
- struct termios tt;
- struct winsize win;
- int master = -1;
- int slave = -1;
-
- // if tcgetattr does not return zero there was a error
- // and we do not do any pty magic
- _error->PushToStack();
- if (tcgetattr(STDOUT_FILENO, &tt) == 0)
- {
- ioctl(0, TIOCGWINSZ, (char *)&win);
- if (openpty(&master, &slave, NULL, &tt, &win) < 0)
- {
- _error->Errno("openpty", _("Can not write log (%s)"), _("Is /dev/pts mounted?"));
- master = slave = -1;
- } else {
- struct termios rtt;
- rtt = tt;
- cfmakeraw(&rtt);
- rtt.c_lflag &= ~ECHO;
- rtt.c_lflag |= ISIG;
- // block SIGTTOU during tcsetattr to prevent a hang if
- // the process is a member of the background process group
- // http://www.opengroup.org/onlinepubs/000095399/functions/tcsetattr.html
- sigemptyset(&sigmask);
- sigaddset(&sigmask, SIGTTOU);
- sigprocmask(SIG_BLOCK,&sigmask, &original_sigmask);
- tcsetattr(0, TCSAFLUSH, &rtt);
- sigprocmask(SIG_SETMASK, &original_sigmask, 0);
- }
- }
- // complain only if stdout is either a terminal (but still failed) or is an invalid
- // descriptor otherwise we would complain about redirection to e.g. /dev/null as well.
- else if (isatty(STDOUT_FILENO) == 1 || errno == EBADF)
- _error->Errno("tcgetattr", _("Can not write log (%s)"), _("Is stdout a terminal?"));
-
- if (_error->PendingError() == true)
- _error->DumpErrors(std::cerr);
- _error->RevertToStack();
-
- // Fork dpkg
- pid_t Child;
- _config->Set("APT::Keep-Fds::",fd[1]);
- // send status information that we are about to fork dpkg
- if(OutStatusFd > 0) {
- ostringstream status;
- status << "pmstatus:dpkg-exec:"
- << (PackagesDone/float(PackagesTotal)*100.0)
- << ":" << _("Running dpkg")
- << endl;
- FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
- }
- Child = ExecFork();
-
- // This is the child
+ // now run dpkg
+ d->progress->StartDpkg();
+ std::set<int> KeepFDs;
+ KeepFDs.insert(fd[1]);
+ MergeKeepFdsFromConfiguration(KeepFDs);
+ pid_t Child = ExecFork(KeepFDs);
if (Child == 0)
{
- if(slave >= 0 && master >= 0)
+ // This is the child
+ if(d->slave >= 0 && d->master >= 0)
{
setsid();
- ioctl(slave, TIOCSCTTY, 0);
- close(master);
- dup2(slave, 0);
- dup2(slave, 1);
- dup2(slave, 2);
- close(slave);
+ int res = ioctl(d->slave, TIOCSCTTY, 0);
+ if (res < 0) {
+ std::cerr << "ioctl(TIOCSCTTY) failed for fd: "
+ << d->slave << std::endl;
+ } else {
+ close(d->master);
+ dup2(d->slave, 0);
+ dup2(d->slave, 1);
+ dup2(d->slave, 2);
+ close(d->slave);
+ }
}
close(fd[0]); // close the read end of the pipe
@@ -1314,7 +1415,7 @@ bool pkgDPkgPM::Go(int OutStatusFd)
if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
_exit(100);
-
+
if (_config->FindB("DPkg::FlushSTDIN",true) == true && isatty(STDIN_FILENO))
{
int Flags,dummy;
@@ -1343,9 +1444,6 @@ bool pkgDPkgPM::Go(int OutStatusFd)
if (_config->FindB("DPkg::UseIoNice", false) == true)
ionice(Child);
- // clear the Keep-Fd again
- _config->Clear("APT::Keep-Fds",fd[1]);
-
// Wait for dpkg
int Status = 0;
@@ -1353,12 +1451,9 @@ bool pkgDPkgPM::Go(int OutStatusFd)
int const _dpkgin = fd[0];
close(fd[1]); // close the write end of the pipe
- if(slave > 0)
- close(slave);
-
// setups fds
- sigemptyset(&sigmask);
- sigprocmask(SIG_BLOCK,&sigmask,&original_sigmask);
+ sigemptyset(&d->sigmask);
+ sigprocmask(SIG_BLOCK,&d->sigmask,&d->original_sigmask);
/* free vectors (and therefore memory) as we don't need the included data anymore */
for (std::vector<char *>::const_iterator p = Packages.begin();
@@ -1387,18 +1482,19 @@ bool pkgDPkgPM::Go(int OutStatusFd)
// wait for input or output here
FD_ZERO(&rfds);
- if (master >= 0 && !d->stdin_is_dev_null)
+ if (d->master >= 0 && !d->stdin_is_dev_null)
FD_SET(0, &rfds);
FD_SET(_dpkgin, &rfds);
- if(master >= 0)
- FD_SET(master, &rfds);
- tv.tv_sec = 1;
- tv.tv_nsec = 0;
- select_ret = pselect(max(master, _dpkgin)+1, &rfds, NULL, NULL,
- &tv, &original_sigmask);
+ if(d->master >= 0)
+ FD_SET(d->master, &rfds);
+ tv.tv_sec = 0;
+ tv.tv_nsec = d->progress->GetPulseInterval();
+ select_ret = pselect(max(d->master, _dpkgin)+1, &rfds, NULL, NULL,
+ &tv, &d->original_sigmask);
if (select_ret < 0 && (errno == EINVAL || errno == ENOSYS))
- select_ret = racy_pselect(max(master, _dpkgin)+1, &rfds, NULL,
- NULL, &tv, &original_sigmask);
+ select_ret = racy_pselect(max(d->master, _dpkgin)+1, &rfds, NULL,
+ NULL, &tv, &d->original_sigmask);
+ d->progress->Pulse();
if (select_ret == 0)
continue;
else if (select_ret < 0 && errno == EINTR)
@@ -1409,12 +1505,12 @@ bool pkgDPkgPM::Go(int OutStatusFd)
continue;
}
- if(master >= 0 && FD_ISSET(master, &rfds))
- DoTerminalPty(master);
- if(master >= 0 && FD_ISSET(0, &rfds))
- DoStdin(master);
+ if(d->master >= 0 && FD_ISSET(d->master, &rfds))
+ DoTerminalPty(d->master);
+ if(d->master >= 0 && FD_ISSET(0, &rfds))
+ DoStdin(d->master);
if(FD_ISSET(_dpkgin, &rfds))
- DoDpkgStatusFd(_dpkgin, OutStatusFd);
+ DoDpkgStatusFd(_dpkgin);
}
close(_dpkgin);
@@ -1423,13 +1519,6 @@ bool pkgDPkgPM::Go(int OutStatusFd)
signal(SIGINT,old_SIGINT);
signal(SIGHUP,old_SIGHUP);
-
- if(master >= 0)
- {
- tcsetattr(0, TCSAFLUSH, &tt);
- close(master);
- }
-
// Check for an error code.
if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
{
@@ -1454,12 +1543,17 @@ bool pkgDPkgPM::Go(int OutStatusFd)
if(stopOnError)
{
CloseLog();
+ StopPtyMagic();
+ d->progress->Stop();
return false;
}
}
}
+ // dpkg is done at this point
+ d->progress->Stop();
+ StopPtyMagic();
CloseLog();
-
+
if (pkgPackageManager::SigINTStop)
_error->Warning(_("Operation was interrupted before it could finish"));
@@ -1541,18 +1635,49 @@ void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg)
}
// do not report out-of-memory failures
- if(strstr(errormsg, strerror(ENOMEM)) != NULL) {
+ if(strstr(errormsg, strerror(ENOMEM)) != NULL ||
+ strstr(errormsg, "failed to allocate memory") != NULL) {
std::clog << _("No apport report written because the error message indicates a out of memory error") << std::endl;
return;
}
- // do not report dpkg I/O errors
- // XXX - this message is localized, but this only matches the English version. This is better than nothing.
- if(strstr(errormsg, "short read in buffer_copy (")) {
- std::clog << _("No apport report written because the error message indicates a dpkg I/O error") << std::endl;
+ // do not report bugs regarding inaccessible local files
+ if(strstr(errormsg, strerror(ENOENT)) != NULL ||
+ strstr(errormsg, "cannot access archive") != NULL) {
+ std::clog << _("No apport report written because the error message indicates an issue on the local system") << std::endl;
+ return;
+ }
+
+ // do not report errors encountered when decompressing packages
+ if(strstr(errormsg, "--fsys-tarfile returned error exit status 2") != NULL) {
+ std::clog << _("No apport report written because the error message indicates an issue on the local system") << std::endl;
return;
}
+ // do not report dpkg I/O errors, this is a format string, so we compare
+ // the prefix and the suffix of the error with the dpkg error message
+ vector<string> io_errors;
+ io_errors.push_back(string("failed to read on buffer copy for %s"));
+ io_errors.push_back(string("failed in write on buffer copy for %s"));
+ io_errors.push_back(string("short read on buffer copy for %s"));
+
+ for (vector<string>::iterator I = io_errors.begin(); I != io_errors.end(); ++I)
+ {
+ vector<string> list = VectorizeString(dgettext("dpkg", (*I).c_str()), '%');
+ if (list.size() > 1) {
+ // we need to split %s, VectorizeString only allows char so we need
+ // to kill the "s" manually
+ if (list[1].size() > 1) {
+ list[1].erase(0, 1);
+ if(strstr(errormsg, list[0].c_str()) &&
+ strstr(errormsg, list[1].c_str())) {
+ std::clog << _("No apport report written because the error message indicates a dpkg I/O error") << std::endl;
+ return;
+ }
+ }
+ }
+ }
+
// get the pkgname and reportfile
pkgname = flNotDir(pkgpath);
pos = pkgname.find('_');
@@ -1642,6 +1767,22 @@ void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg)
char buf[1024];
while( fgets(buf, sizeof(buf), log) != NULL)
fprintf(report, " %s", buf);
+ fprintf(report, " \n");
+ fclose(log);
+ }
+ }
+
+ // attach history log it if we have it
+ string histfile_name = _config->FindFile("Dir::Log::History");
+ if (!histfile_name.empty())
+ {
+ fprintf(report, "DpkgHistoryLog:\n");
+ FILE* log = fopen(histfile_name.c_str(),"r");
+ if(log != NULL)
+ {
+ char buf[1024];
+ while( fgets(buf, sizeof(buf), log) != NULL)
+ fprintf(report, " %s", buf);
fclose(log);
}
}