1 // -*- mode: cpp; mode: fold -*-
3 // $Id: dpkgpm.cc,v 1.28 2004/01/27 02:25:01 mdz Exp $
4 /* ######################################################################
6 DPKG Package Manager - Provide an interface to dpkg
8 ##################################################################### */
13 #include <apt-pkg/cachefile.h>
14 #include <apt-pkg/configuration.h>
15 #include <apt-pkg/depcache.h>
16 #include <apt-pkg/dpkgpm.h>
17 #include <apt-pkg/error.h>
18 #include <apt-pkg/fileutl.h>
19 #include <apt-pkg/install-progress.h>
20 #include <apt-pkg/packagemanager.h>
21 #include <apt-pkg/pkgrecords.h>
22 #include <apt-pkg/strutl.h>
23 #include <apt-pkg/cacheiterators.h>
24 #include <apt-pkg/macros.h>
25 #include <apt-pkg/pkgcache.h>
36 #include <sys/ioctl.h>
37 #include <sys/select.h>
58 APT_PURE
static unsigned int
61 unsigned int size
= 0;
62 char **envp
= environ
;
65 size
+= strlen (*envp
++) + 1;
70 class pkgDPkgPMPrivate
73 pkgDPkgPMPrivate() : stdin_is_dev_null(false), dpkgbuf_pos(0),
74 term_out(NULL
), history_out(NULL
),
75 progress(NULL
), tt_is_valid(false), master(-1),
76 slave(NULL
), protect_slave_from_dying(-1)
83 bool stdin_is_dev_null
;
84 // the buffer we use for the dpkg status-fd reading
90 APT::Progress::PackageManager
*progress
;
97 int protect_slave_from_dying
;
101 sigset_t original_sigmask
;
107 // Maps the dpkg "processing" info to human readable names. Entry 0
108 // of each array is the key, entry 1 is the value.
109 const std::pair
<const char *, const char *> PackageProcessingOps
[] = {
110 std::make_pair("install", N_("Installing %s")),
111 std::make_pair("configure", N_("Configuring %s")),
112 std::make_pair("remove", N_("Removing %s")),
113 std::make_pair("purge", N_("Completely removing %s")),
114 std::make_pair("disappear", N_("Noting disappearance of %s")),
115 std::make_pair("trigproc", N_("Running post-installation trigger %s"))
118 const std::pair
<const char *, const char *> * const PackageProcessingOpsBegin
= PackageProcessingOps
;
119 const std::pair
<const char *, const char *> * const PackageProcessingOpsEnd
= PackageProcessingOps
+ sizeof(PackageProcessingOps
) / sizeof(PackageProcessingOps
[0]);
121 // Predicate to test whether an entry in the PackageProcessingOps
122 // array matches a string.
123 class MatchProcessingOp
128 MatchProcessingOp(const char *the_target
)
133 bool operator()(const std::pair
<const char *, const char *> &pair
) const
135 return strcmp(pair
.first
, target
) == 0;
140 /* helper function to ionice the given PID
142 there is no C header for ionice yet - just the syscall interface
143 so we use the binary from util-linux
148 if (!FileExists("/usr/bin/ionice"))
150 pid_t Process
= ExecFork();
154 snprintf(buf
, sizeof(buf
), "-p%d", PID
);
156 Args
[0] = "/usr/bin/ionice";
160 execv(Args
[0], (char **)Args
);
162 return ExecWait(Process
, "ionice");
165 static std::string
getDpkgExecutable()
167 string Tmp
= _config
->Find("Dir::Bin::dpkg","dpkg");
168 string
const dpkgChrootDir
= _config
->FindDir("DPkg::Chroot-Directory", "/");
169 size_t dpkgChrootLen
= dpkgChrootDir
.length();
170 if (dpkgChrootDir
!= "/" && Tmp
.find(dpkgChrootDir
) == 0)
172 if (dpkgChrootDir
[dpkgChrootLen
- 1] == '/')
174 Tmp
= Tmp
.substr(dpkgChrootLen
);
179 // dpkgChrootDirectory - chrooting for dpkg if needed /*{{{*/
180 static void dpkgChrootDirectory()
182 std::string
const chrootDir
= _config
->FindDir("DPkg::Chroot-Directory");
183 if (chrootDir
== "/")
185 std::cerr
<< "Chrooting into " << chrootDir
<< std::endl
;
186 if (chroot(chrootDir
.c_str()) != 0)
194 // FindNowVersion - Helper to find a Version in "now" state /*{{{*/
195 // ---------------------------------------------------------------------
196 /* This is helpful when a package is no longer installed but has residual
200 pkgCache::VerIterator
FindNowVersion(const pkgCache::PkgIterator
&Pkg
)
202 pkgCache::VerIterator Ver
;
203 for (Ver
= Pkg
.VersionList(); Ver
.end() == false; ++Ver
)
204 for (pkgCache::VerFileIterator Vf
= Ver
.FileList(); Vf
.end() == false; ++Vf
)
205 for (pkgCache::PkgFileIterator F
= Vf
.File(); F
.end() == false; ++F
)
206 if (F
->Archive
!= 0 && strcmp(F
.Archive(), "now") == 0)
212 // DPkgPM::pkgDPkgPM - Constructor /*{{{*/
213 // ---------------------------------------------------------------------
215 pkgDPkgPM::pkgDPkgPM(pkgDepCache
*Cache
)
216 : pkgPackageManager(Cache
), pkgFailures(0), PackagesDone(0), PackagesTotal(0)
218 d
= new pkgDPkgPMPrivate();
221 // DPkgPM::pkgDPkgPM - Destructor /*{{{*/
222 // ---------------------------------------------------------------------
224 pkgDPkgPM::~pkgDPkgPM()
229 // DPkgPM::Install - Install a package /*{{{*/
230 // ---------------------------------------------------------------------
231 /* Add an install operation to the sequence list */
232 bool pkgDPkgPM::Install(PkgIterator Pkg
,string File
)
234 if (File
.empty() == true || Pkg
.end() == true)
235 return _error
->Error("Internal Error, No file name for %s",Pkg
.FullName().c_str());
237 // If the filename string begins with DPkg::Chroot-Directory, return the
238 // substr that is within the chroot so dpkg can access it.
239 string
const chrootdir
= _config
->FindDir("DPkg::Chroot-Directory","/");
240 if (chrootdir
!= "/" && File
.find(chrootdir
) == 0)
242 size_t len
= chrootdir
.length();
243 if (chrootdir
.at(len
- 1) == '/')
245 List
.push_back(Item(Item::Install
,Pkg
,File
.substr(len
)));
248 List
.push_back(Item(Item::Install
,Pkg
,File
));
253 // DPkgPM::Configure - Configure a package /*{{{*/
254 // ---------------------------------------------------------------------
255 /* Add a configure operation to the sequence list */
256 bool pkgDPkgPM::Configure(PkgIterator Pkg
)
258 if (Pkg
.end() == true)
261 List
.push_back(Item(Item::Configure
, Pkg
));
263 // Use triggers for config calls if we configure "smart"
264 // as otherwise Pre-Depends will not be satisfied, see #526774
265 if (_config
->FindB("DPkg::TriggersPending", false) == true)
266 List
.push_back(Item(Item::TriggersPending
, PkgIterator()));
271 // DPkgPM::Remove - Remove a package /*{{{*/
272 // ---------------------------------------------------------------------
273 /* Add a remove operation to the sequence list */
274 bool pkgDPkgPM::Remove(PkgIterator Pkg
,bool Purge
)
276 if (Pkg
.end() == true)
280 List
.push_back(Item(Item::Purge
,Pkg
));
282 List
.push_back(Item(Item::Remove
,Pkg
));
286 // DPkgPM::SendPkgInfo - Send info for install-pkgs hook /*{{{*/
287 // ---------------------------------------------------------------------
288 /* This is part of the helper script communication interface, it sends
289 very complete information down to the other end of the pipe.*/
290 bool pkgDPkgPM::SendV2Pkgs(FILE *F
)
292 return SendPkgsInfo(F
, 2);
294 bool pkgDPkgPM::SendPkgsInfo(FILE * const F
, unsigned int const &Version
)
296 // This version of APT supports only v3, so don't sent higher versions
298 fprintf(F
,"VERSION %u\n", Version
);
300 fprintf(F
,"VERSION 3\n");
302 /* Write out all of the configuration directives by walking the
303 configuration tree */
304 const Configuration::Item
*Top
= _config
->Tree(0);
307 if (Top
->Value
.empty() == false)
310 QuoteString(Top
->FullTag(),"=\"\n").c_str(),
311 QuoteString(Top
->Value
,"\n").c_str());
320 while (Top
!= 0 && Top
->Next
== 0)
327 // Write out the package actions in order.
328 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end(); ++I
)
330 if(I
->Pkg
.end() == true)
333 pkgDepCache::StateCache
&S
= Cache
[I
->Pkg
];
335 fprintf(F
,"%s ",I
->Pkg
.Name());
337 // Current version which we are going to replace
338 pkgCache::VerIterator CurVer
= I
->Pkg
.CurrentVer();
339 if (CurVer
.end() == true && (I
->Op
== Item::Remove
|| I
->Op
== Item::Purge
))
340 CurVer
= FindNowVersion(I
->Pkg
);
342 if (CurVer
.end() == true)
347 fprintf(F
, "- - none ");
351 fprintf(F
, "%s ", CurVer
.VerStr());
353 fprintf(F
, "%s %s ", CurVer
.Arch(), CurVer
.MultiArchType());
356 // Show the compare operator between current and install version
357 if (S
.InstallVer
!= 0)
359 pkgCache::VerIterator
const InstVer
= S
.InstVerIter(Cache
);
361 if (CurVer
.end() == false)
362 Comp
= InstVer
.CompareVer(CurVer
);
369 fprintf(F
, "%s ", InstVer
.VerStr());
371 fprintf(F
, "%s %s ", InstVer
.Arch(), InstVer
.MultiArchType());
378 fprintf(F
, "> - - none ");
381 // Show the filename/operation
382 if (I
->Op
== Item::Install
)
385 if (I
->File
[0] != '/')
386 fprintf(F
,"**ERROR**\n");
388 fprintf(F
,"%s\n",I
->File
.c_str());
390 else if (I
->Op
== Item::Configure
)
391 fprintf(F
,"**CONFIGURE**\n");
392 else if (I
->Op
== Item::Remove
||
393 I
->Op
== Item::Purge
)
394 fprintf(F
,"**REMOVE**\n");
402 // DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/
403 // ---------------------------------------------------------------------
404 /* This looks for a list of scripts to run from the configuration file
405 each one is run and is fed on standard input a list of all .deb files
406 that are due to be installed. */
407 bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf
)
411 Configuration::Item
const *Opts
= _config
->Tree(Cnf
);
412 if (Opts
== 0 || Opts
->Child
== 0)
416 sighandler_t old_sigpipe
= signal(SIGPIPE
, SIG_IGN
);
418 unsigned int Count
= 1;
419 for (; Opts
!= 0; Opts
= Opts
->Next
, Count
++)
421 if (Opts
->Value
.empty() == true)
424 if(_config
->FindB("Debug::RunScripts", false) == true)
425 std::clog
<< "Running external script with list of all .deb file: '"
426 << Opts
->Value
<< "'" << std::endl
;
428 // Determine the protocol version
429 string OptSec
= Opts
->Value
;
430 string::size_type Pos
;
431 if ((Pos
= OptSec
.find(' ')) == string::npos
|| Pos
== 0)
432 Pos
= OptSec
.length();
433 OptSec
= "DPkg::Tools::Options::" + string(Opts
->Value
.c_str(),Pos
);
435 unsigned int Version
= _config
->FindI(OptSec
+"::Version",1);
436 unsigned int InfoFD
= _config
->FindI(OptSec
+ "::InfoFD", STDIN_FILENO
);
439 std::set
<int> KeepFDs
;
440 MergeKeepFdsFromConfiguration(KeepFDs
);
442 if (pipe(Pipes
) != 0) {
443 result
= _error
->Errno("pipe","Failed to create IPC pipe to subprocess");
446 if (InfoFD
!= (unsigned)Pipes
[0])
447 SetCloseExec(Pipes
[0],true);
449 KeepFDs
.insert(Pipes
[0]);
452 SetCloseExec(Pipes
[1],true);
454 // Purified Fork for running the script
455 pid_t Process
= ExecFork(KeepFDs
);
459 dup2(Pipes
[0], InfoFD
);
460 SetCloseExec(STDOUT_FILENO
,false);
461 SetCloseExec(STDIN_FILENO
,false);
462 SetCloseExec(STDERR_FILENO
,false);
465 strprintf(hookfd
, "%d", InfoFD
);
466 setenv("APT_HOOK_INFO_FD", hookfd
.c_str(), 1);
468 dpkgChrootDirectory();
472 Args
[2] = Opts
->Value
.c_str();
474 execv(Args
[0],(char **)Args
);
478 FILE *F
= fdopen(Pipes
[1],"w");
480 result
= _error
->Errno("fdopen","Faild to open new FD");
484 // Feed it the filenames.
487 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end(); ++I
)
489 // Only deal with packages to be installed from .deb
490 if (I
->Op
!= Item::Install
)
494 if (I
->File
[0] != '/')
497 /* Feed the filename of each package that is pending install
499 fprintf(F
,"%s\n",I
->File
.c_str());
505 SendPkgsInfo(F
, Version
);
509 // Clean up the sub process
510 if (ExecWait(Process
,Opts
->Value
.c_str()) == false) {
511 result
= _error
->Error("Failure running script %s",Opts
->Value
.c_str());
515 signal(SIGPIPE
, old_sigpipe
);
520 // DPkgPM::DoStdin - Read stdin and pass to master pty /*{{{*/
521 // ---------------------------------------------------------------------
524 void pkgDPkgPM::DoStdin(int master
)
526 unsigned char input_buf
[256] = {0,};
527 ssize_t len
= read(STDIN_FILENO
, input_buf
, sizeof(input_buf
));
529 FileFd::Write(master
, input_buf
, len
);
531 d
->stdin_is_dev_null
= true;
534 // DPkgPM::DoTerminalPty - Read the terminal pty and write log /*{{{*/
535 // ---------------------------------------------------------------------
537 * read the terminal pty and write log
539 void pkgDPkgPM::DoTerminalPty(int master
)
541 unsigned char term_buf
[1024] = {0,0, };
543 ssize_t len
=read(master
, term_buf
, sizeof(term_buf
));
544 if(len
== -1 && errno
== EIO
)
546 // this happens when the child is about to exit, we
547 // give it time to actually exit, otherwise we run
548 // into a race so we sleep for half a second.
549 struct timespec sleepfor
= { 0, 500000000 };
550 nanosleep(&sleepfor
, NULL
);
555 FileFd::Write(1, term_buf
, len
);
557 fwrite(term_buf
, len
, sizeof(char), d
->term_out
);
560 // DPkgPM::ProcessDpkgStatusBuf /*{{{*/
561 // ---------------------------------------------------------------------
564 void pkgDPkgPM::ProcessDpkgStatusLine(char *line
)
566 bool const Debug
= _config
->FindB("Debug::pkgDPkgProgressReporting",false);
568 std::clog
<< "got from dpkg '" << line
<< "'" << std::endl
;
570 /* dpkg sends strings like this:
571 'status: <pkg>: <pkg qstate>'
572 'status: <pkg>:<arch>: <pkg qstate>'
574 'processing: {install,upgrade,configure,remove,purge,disappear,trigproc}: pkg'
575 'processing: {install,upgrade,configure,remove,purge,disappear,trigproc}: trigger'
578 // we need to split on ": " (note the appended space) as the ':' is
579 // part of the pkgname:arch information that dpkg sends
581 // A dpkg error message may contain additional ":" (like
582 // "failed in buffer_write(fd) (10, ret=-1): backend dpkg-deb ..."
583 // so we need to ensure to not split too much
584 std::vector
<std::string
> list
= StringSplit(line
, ": ", 4);
588 std::clog
<< "ignoring line: not enough ':'" << std::endl
;
592 // build the (prefix, pkgname, action) tuple, position of this
593 // is different for "processing" or "status" messages
594 std::string prefix
= APT::String::Strip(list
[0]);
598 // "processing" has the form "processing: action: pkg or trigger"
599 // with action = ["install", "upgrade", "configure", "remove", "purge",
600 // "disappear", "trigproc"]
601 if (prefix
== "processing")
603 pkgname
= APT::String::Strip(list
[2]);
604 action
= APT::String::Strip(list
[1]);
605 // we don't care for the difference (as dpkg doesn't really either)
606 if (action
== "upgrade")
609 // "status" has the form: "status: pkg: state"
610 // with state in ["half-installed", "unpacked", "half-configured",
611 // "installed", "config-files", "not-installed"]
612 else if (prefix
== "status")
614 pkgname
= APT::String::Strip(list
[1]);
615 action
= APT::String::Strip(list
[2]);
618 std::clog
<< "unknown prefix '" << prefix
<< "'" << std::endl
;
623 /* handle the special cases first:
625 errors look like this:
626 '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
627 and conffile-prompt like this
628 'status:/etc/compiz.conf/compiz.conf : conffile-prompt: 'current-conffile' 'new-conffile' useredited distedited
630 if (prefix
== "status")
632 if(action
== "error")
634 d
->progress
->Error(pkgname
, PackagesDone
, PackagesTotal
,
637 WriteApportReport(pkgname
.c_str(), list
[3].c_str());
640 else if(action
== "conffile-prompt")
642 d
->progress
->ConffilePrompt(pkgname
, PackagesDone
, PackagesTotal
,
648 // at this point we know that we should have a valid pkgname, so build all
651 // dpkg does not always send "pkgname:arch" so we add it here if needed
652 if (pkgname
.find(":") == std::string::npos
)
654 // find the package in the group that is touched by dpkg
655 // if there are multiple pkgs dpkg would send us a full pkgname:arch
656 pkgCache::GrpIterator Grp
= Cache
.FindGrp(pkgname
);
657 if (Grp
.end() == false)
659 pkgCache::PkgIterator P
= Grp
.PackageList();
660 for (; P
.end() != true; P
= Grp
.NextPkg(P
))
662 if(Cache
[P
].Keep() == false || Cache
[P
].ReInstall() == true)
664 pkgname
= P
.FullName();
671 const char* const pkg
= pkgname
.c_str();
672 std::string short_pkgname
= StringSplit(pkgname
, ":")[0];
673 std::string arch
= "";
674 if (pkgname
.find(":") != string::npos
)
675 arch
= StringSplit(pkgname
, ":")[1];
676 std::string i18n_pkgname
= pkgname
;
677 if (arch
.size() != 0)
678 strprintf(i18n_pkgname
, "%s (%s)", short_pkgname
.c_str(), arch
.c_str());
680 // 'processing' from dpkg looks like
681 // 'processing: action: pkg'
682 if(prefix
== "processing")
684 const std::pair
<const char *, const char *> * const iter
=
685 std::find_if(PackageProcessingOpsBegin
,
686 PackageProcessingOpsEnd
,
687 MatchProcessingOp(action
.c_str()));
688 if(iter
== PackageProcessingOpsEnd
)
691 std::clog
<< "ignoring unknown action: " << action
<< std::endl
;
695 strprintf(msg
, _(iter
->second
), i18n_pkgname
.c_str());
696 d
->progress
->StatusChanged(pkgname
, PackagesDone
, PackagesTotal
, msg
);
698 // FIXME: this needs a muliarch testcase
699 // FIXME2: is "pkgname" here reliable with dpkg only sending us
701 if (action
== "disappear")
702 handleDisappearAction(pkgname
);
706 if (prefix
== "status")
708 vector
<struct DpkgState
> const &states
= PackageOps
[pkg
];
709 if(PackageOpsDone
[pkg
] < states
.size())
711 char const * const next_action
= states
[PackageOpsDone
[pkg
]].state
;
712 if (next_action
&& Debug
== true)
713 std::clog
<< "(parsed from dpkg) pkg: " << short_pkgname
714 << " action: " << action
<< " (expected: '" << next_action
<< "' "
715 << PackageOpsDone
[pkg
] << " of " << states
.size() << ")" << endl
;
717 // check if the package moved to the next dpkg state
718 if(next_action
&& (action
== next_action
))
720 // only read the translation if there is actually a next action
721 char const * const translation
= _(states
[PackageOpsDone
[pkg
]].str
);
723 // we moved from one dpkg state to a new one, report that
724 ++PackageOpsDone
[pkg
];
728 strprintf(msg
, translation
, i18n_pkgname
.c_str());
729 d
->progress
->StatusChanged(pkgname
, PackagesDone
, PackagesTotal
, msg
);
735 // DPkgPM::handleDisappearAction /*{{{*/
736 void pkgDPkgPM::handleDisappearAction(string
const &pkgname
)
738 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(pkgname
);
739 if (unlikely(Pkg
.end() == true))
742 // record the package name for display and stuff later
743 disappearedPkgs
.insert(Pkg
.FullName(true));
745 // the disappeared package was auto-installed - nothing to do
746 if ((Cache
[Pkg
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
)
748 pkgCache::VerIterator PkgVer
= Cache
[Pkg
].InstVerIter(Cache
);
749 if (unlikely(PkgVer
.end() == true))
751 /* search in the list of dependencies for (Pre)Depends,
752 check if this dependency has a Replaces on our package
753 and if so transfer the manual installed flag to it */
754 for (pkgCache::DepIterator Dep
= PkgVer
.DependsList(); Dep
.end() != true; ++Dep
)
756 if (Dep
->Type
!= pkgCache::Dep::Depends
&&
757 Dep
->Type
!= pkgCache::Dep::PreDepends
)
759 pkgCache::PkgIterator Tar
= Dep
.TargetPkg();
760 if (unlikely(Tar
.end() == true))
762 // the package is already marked as manual
763 if ((Cache
[Tar
].Flags
& pkgCache::Flag::Auto
) != pkgCache::Flag::Auto
)
765 pkgCache::VerIterator TarVer
= Cache
[Tar
].InstVerIter(Cache
);
766 if (TarVer
.end() == true)
768 for (pkgCache::DepIterator Rep
= TarVer
.DependsList(); Rep
.end() != true; ++Rep
)
770 if (Rep
->Type
!= pkgCache::Dep::Replaces
)
772 if (Pkg
!= Rep
.TargetPkg())
774 // okay, they are strongly connected - transfer manual-bit
776 std::clog
<< "transfer manual-bit from disappeared »" << pkgname
<< "« to »" << Tar
.FullName() << "«" << std::endl
;
777 Cache
[Tar
].Flags
&= ~Flag::Auto
;
783 // DPkgPM::DoDpkgStatusFd /*{{{*/
784 // ---------------------------------------------------------------------
787 void pkgDPkgPM::DoDpkgStatusFd(int statusfd
)
792 len
=read(statusfd
, &d
->dpkgbuf
[d
->dpkgbuf_pos
], sizeof(d
->dpkgbuf
)-d
->dpkgbuf_pos
);
793 d
->dpkgbuf_pos
+= len
;
797 // process line by line if we have a buffer
799 while((q
=(char*)memchr(p
, '\n', d
->dpkgbuf
+d
->dpkgbuf_pos
-p
)) != NULL
)
802 ProcessDpkgStatusLine(p
);
803 p
=q
+1; // continue with next line
806 // now move the unprocessed bits (after the final \n that is now a 0x0)
807 // to the start and update d->dpkgbuf_pos
808 p
= (char*)memrchr(d
->dpkgbuf
, 0, d
->dpkgbuf_pos
);
812 // we are interessted in the first char *after* 0x0
815 // move the unprocessed tail to the start and update pos
816 memmove(d
->dpkgbuf
, p
, p
-d
->dpkgbuf
);
817 d
->dpkgbuf_pos
= d
->dpkgbuf
+d
->dpkgbuf_pos
-p
;
820 // DPkgPM::WriteHistoryTag /*{{{*/
821 void pkgDPkgPM::WriteHistoryTag(string
const &tag
, string value
)
823 size_t const length
= value
.length();
826 // poor mans rstrip(", ")
827 if (value
[length
-2] == ',' && value
[length
-1] == ' ')
828 value
.erase(length
- 2, 2);
829 fprintf(d
->history_out
, "%s: %s\n", tag
.c_str(), value
.c_str());
831 // DPkgPM::OpenLog /*{{{*/
832 bool pkgDPkgPM::OpenLog()
834 string
const logdir
= _config
->FindDir("Dir::Log");
835 if(CreateAPTDirectoryIfNeeded(logdir
, logdir
) == false)
836 // FIXME: use a better string after freeze
837 return _error
->Error(_("Directory '%s' missing"), logdir
.c_str());
841 time_t const t
= time(NULL
);
842 struct tm
const * const tmp
= localtime(&t
);
843 strftime(timestr
, sizeof(timestr
), "%F %T", tmp
);
846 string
const logfile_name
= flCombine(logdir
,
847 _config
->Find("Dir::Log::Terminal"));
848 if (!logfile_name
.empty())
850 d
->term_out
= fopen(logfile_name
.c_str(),"a");
851 if (d
->term_out
== NULL
)
852 return _error
->WarningE("OpenLog", _("Could not open file '%s'"), logfile_name
.c_str());
853 setvbuf(d
->term_out
, NULL
, _IONBF
, 0);
854 SetCloseExec(fileno(d
->term_out
), true);
855 if (getuid() == 0) // if we aren't root, we can't chown a file, so don't try it
857 struct passwd
*pw
= getpwnam("root");
858 struct group
*gr
= getgrnam("adm");
859 if (pw
!= NULL
&& gr
!= NULL
&& chown(logfile_name
.c_str(), pw
->pw_uid
, gr
->gr_gid
) != 0)
860 _error
->WarningE("OpenLog", "chown to root:adm of file %s failed", logfile_name
.c_str());
862 if (chmod(logfile_name
.c_str(), 0640) != 0)
863 _error
->WarningE("OpenLog", "chmod 0640 of file %s failed", logfile_name
.c_str());
864 fprintf(d
->term_out
, "\nLog started: %s\n", timestr
);
867 // write your history
868 string
const history_name
= flCombine(logdir
,
869 _config
->Find("Dir::Log::History"));
870 if (!history_name
.empty())
872 d
->history_out
= fopen(history_name
.c_str(),"a");
873 if (d
->history_out
== NULL
)
874 return _error
->WarningE("OpenLog", _("Could not open file '%s'"), history_name
.c_str());
875 SetCloseExec(fileno(d
->history_out
), true);
876 chmod(history_name
.c_str(), 0644);
877 fprintf(d
->history_out
, "\nStart-Date: %s\n", timestr
);
878 string remove
, purge
, install
, reinstall
, upgrade
, downgrade
;
879 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; ++I
)
881 enum { CANDIDATE
, CANDIDATE_AUTO
, CURRENT_CANDIDATE
, CURRENT
} infostring
;
883 #define HISTORYINFO(X, Y) { line = &X; infostring = Y; }
884 if (Cache
[I
].NewInstall() == true)
885 HISTORYINFO(install
, CANDIDATE_AUTO
)
886 else if (Cache
[I
].ReInstall() == true)
887 HISTORYINFO(reinstall
, CANDIDATE
)
888 else if (Cache
[I
].Upgrade() == true)
889 HISTORYINFO(upgrade
, CURRENT_CANDIDATE
)
890 else if (Cache
[I
].Downgrade() == true)
891 HISTORYINFO(downgrade
, CURRENT_CANDIDATE
)
892 else if (Cache
[I
].Delete() == true)
893 HISTORYINFO((Cache
[I
].Purge() ? purge
: remove
), CURRENT
)
897 line
->append(I
.FullName(false)).append(" (");
898 switch (infostring
) {
899 case CANDIDATE
: line
->append(Cache
[I
].CandVersion
); break;
901 line
->append(Cache
[I
].CandVersion
);
902 if ((Cache
[I
].Flags
& pkgCache::Flag::Auto
) == pkgCache::Flag::Auto
)
903 line
->append(", automatic");
905 case CURRENT_CANDIDATE
: line
->append(Cache
[I
].CurVersion
).append(", ").append(Cache
[I
].CandVersion
); break;
906 case CURRENT
: line
->append(Cache
[I
].CurVersion
); break;
910 if (_config
->Exists("Commandline::AsString") == true)
911 WriteHistoryTag("Commandline", _config
->Find("Commandline::AsString"));
912 WriteHistoryTag("Install", install
);
913 WriteHistoryTag("Reinstall", reinstall
);
914 WriteHistoryTag("Upgrade", upgrade
);
915 WriteHistoryTag("Downgrade",downgrade
);
916 WriteHistoryTag("Remove",remove
);
917 WriteHistoryTag("Purge",purge
);
918 fflush(d
->history_out
);
924 // DPkg::CloseLog /*{{{*/
925 bool pkgDPkgPM::CloseLog()
928 time_t t
= time(NULL
);
929 struct tm
*tmp
= localtime(&t
);
930 strftime(timestr
, sizeof(timestr
), "%F %T", tmp
);
934 fprintf(d
->term_out
, "Log ended: ");
935 fprintf(d
->term_out
, "%s", timestr
);
936 fprintf(d
->term_out
, "\n");
943 if (disappearedPkgs
.empty() == false)
946 for (std::set
<std::string
>::const_iterator d
= disappearedPkgs
.begin();
947 d
!= disappearedPkgs
.end(); ++d
)
949 pkgCache::PkgIterator P
= Cache
.FindPkg(*d
);
950 disappear
.append(*d
);
952 disappear
.append(", ");
954 disappear
.append(" (").append(Cache
[P
].CurVersion
).append("), ");
956 WriteHistoryTag("Disappeared", disappear
);
958 if (d
->dpkg_error
.empty() == false)
959 fprintf(d
->history_out
, "Error: %s\n", d
->dpkg_error
.c_str());
960 fprintf(d
->history_out
, "End-Date: %s\n", timestr
);
961 fclose(d
->history_out
);
963 d
->history_out
= NULL
;
970 // This implements a racy version of pselect for those architectures
971 // that don't have a working implementation.
972 // FIXME: Probably can be removed on Lenny+1
973 static int racy_pselect(int nfds
, fd_set
*readfds
, fd_set
*writefds
,
974 fd_set
*exceptfds
, const struct timespec
*timeout
,
975 const sigset_t
*sigmask
)
981 tv
.tv_sec
= timeout
->tv_sec
;
982 tv
.tv_usec
= timeout
->tv_nsec
/1000;
984 sigprocmask(SIG_SETMASK
, sigmask
, &origmask
);
985 retval
= select(nfds
, readfds
, writefds
, exceptfds
, &tv
);
986 sigprocmask(SIG_SETMASK
, &origmask
, 0);
991 // DPkgPM::BuildPackagesProgressMap /*{{{*/
992 void pkgDPkgPM::BuildPackagesProgressMap()
994 // map the dpkg states to the operations that are performed
995 // (this is sorted in the same way as Item::Ops)
996 static const struct DpkgState DpkgStatesOpMap
[][7] = {
999 {"half-installed", N_("Preparing %s")},
1000 {"unpacked", N_("Unpacking %s") },
1003 // Configure operation
1005 {"unpacked",N_("Preparing to configure %s") },
1006 {"half-configured", N_("Configuring %s") },
1007 { "installed", N_("Installed %s")},
1012 {"half-configured", N_("Preparing for removal of %s")},
1013 {"half-installed", N_("Removing %s")},
1014 {"config-files", N_("Removed %s")},
1019 {"config-files", N_("Preparing to completely remove %s")},
1020 {"not-installed", N_("Completely removed %s")},
1025 // init the PackageOps map, go over the list of packages that
1026 // that will be [installed|configured|removed|purged] and add
1027 // them to the PackageOps map (the dpkg states it goes through)
1028 // and the PackageOpsTranslations (human readable strings)
1029 for (vector
<Item
>::const_iterator I
= List
.begin(); I
!= List
.end(); ++I
)
1031 if((*I
).Pkg
.end() == true)
1034 string
const name
= (*I
).Pkg
.FullName();
1035 PackageOpsDone
[name
] = 0;
1036 for(int i
=0; (DpkgStatesOpMap
[(*I
).Op
][i
]).state
!= NULL
; ++i
)
1038 PackageOps
[name
].push_back(DpkgStatesOpMap
[(*I
).Op
][i
]);
1042 /* one extra: We don't want the progress bar to reach 100%, especially not
1043 if we call dpkg --configure --pending and process a bunch of triggers
1044 while showing 100%. Also, spindown takes a while, so never reaching 100%
1045 is way more correct than reaching 100% while still doing stuff even if
1046 doing it this way is slightly bending the rules */
1050 bool pkgDPkgPM::Go(int StatusFd
)
1052 APT::Progress::PackageManager
*progress
= NULL
;
1054 progress
= APT::Progress::PackageManagerProgressFactory();
1056 progress
= new APT::Progress::PackageManagerProgressFd(StatusFd
);
1058 return Go(progress
);
1061 void pkgDPkgPM::StartPtyMagic()
1063 if (_config
->FindB("Dpkg::Use-Pty", true) == false)
1066 if (d
->slave
!= NULL
)
1072 _error
->PushToStack();
1074 d
->master
= posix_openpt(O_RDWR
| O_NOCTTY
);
1075 if (d
->master
== -1)
1076 _error
->Errno("posix_openpt", _("Can not write log (%s)"), _("Is /dev/pts mounted?"));
1077 else if (unlockpt(d
->master
) == -1)
1078 _error
->Errno("unlockpt", "Unlocking the slave of master fd %d failed!", d
->master
);
1081 char const * const slave_name
= ptsname(d
->master
);
1082 if (slave_name
== NULL
)
1083 _error
->Errno("ptsname", "Getting name for slave of master fd %d failed!", d
->master
);
1086 d
->slave
= strdup(slave_name
);
1087 if (d
->slave
== NULL
)
1088 _error
->Errno("strdup", "Copying name %s for slave of master fd %d failed!", slave_name
, d
->master
);
1089 else if (grantpt(d
->master
) == -1)
1090 _error
->Errno("grantpt", "Granting access to slave %s based on master fd %d failed!", slave_name
, d
->master
);
1091 else if (tcgetattr(STDIN_FILENO
, &d
->tt
) == 0)
1093 d
->tt_is_valid
= true;
1094 struct termios raw_tt
;
1095 // copy window size of stdout if its a 'good' terminal
1096 if (tcgetattr(STDOUT_FILENO
, &raw_tt
) == 0)
1099 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &win
) < 0)
1100 _error
->Errno("ioctl", "Getting TIOCGWINSZ from stdout failed!");
1101 if (ioctl(d
->master
, TIOCSWINSZ
, &win
) < 0)
1102 _error
->Errno("ioctl", "Setting TIOCSWINSZ for master fd %d failed!", d
->master
);
1104 if (tcsetattr(d
->master
, TCSANOW
, &d
->tt
) == -1)
1105 _error
->Errno("tcsetattr", "Setting in Start via TCSANOW for master fd %d failed!", d
->master
);
1109 raw_tt
.c_lflag
&= ~ECHO
;
1110 raw_tt
.c_lflag
|= ISIG
;
1111 // block SIGTTOU during tcsetattr to prevent a hang if
1112 // the process is a member of the background process group
1113 // http://www.opengroup.org/onlinepubs/000095399/functions/tcsetattr.html
1114 sigemptyset(&d
->sigmask
);
1115 sigaddset(&d
->sigmask
, SIGTTOU
);
1116 sigprocmask(SIG_BLOCK
,&d
->sigmask
, &d
->original_sigmask
);
1117 if (tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &raw_tt
) == -1)
1118 _error
->Errno("tcsetattr", "Setting in Start via TCSAFLUSH for stdin failed!");
1119 sigprocmask(SIG_SETMASK
, &d
->original_sigmask
, NULL
);
1122 if (d
->slave
!= NULL
)
1124 /* on linux, closing (and later reopening) all references to the slave
1125 makes the slave a death end, so we open it here to have one open all
1126 the time. We could use this fd in SetupSlavePtyMagic() for linux, but
1127 on kfreebsd we get an incorrect ("step like") output then while it has
1128 no problem with closing all references… so to avoid platform specific
1129 code here we combine both and be happy once more */
1130 d
->protect_slave_from_dying
= open(d
->slave
, O_RDWR
| O_CLOEXEC
);
1135 if (_error
->PendingError() == true)
1137 if (d
->master
!= -1)
1142 if (d
->slave
!= NULL
)
1147 _error
->DumpErrors(std::cerr
);
1149 _error
->RevertToStack();
1151 void pkgDPkgPM::SetupSlavePtyMagic()
1153 if(d
->master
== -1 || d
->slave
== NULL
)
1156 if (close(d
->master
) == -1)
1157 _error
->FatalE("close", "Closing master %d in child failed!", d
->master
);
1160 _error
->FatalE("setsid", "Starting a new session for child failed!");
1162 int const slaveFd
= open(d
->slave
, O_RDWR
);
1164 _error
->FatalE("open", _("Can not write log (%s)"), _("Is /dev/pts mounted?"));
1165 else if (ioctl(slaveFd
, TIOCSCTTY
, 0) < 0)
1166 _error
->FatalE("ioctl", "Setting TIOCSCTTY for slave fd %d failed!", slaveFd
);
1169 for (unsigned short i
= 0; i
< 3; ++i
)
1170 if (dup2(slaveFd
, i
) == -1)
1171 _error
->FatalE("dup2", "Dupping %d to %d in child failed!", slaveFd
, i
);
1173 if (d
->tt_is_valid
== true && tcsetattr(STDIN_FILENO
, TCSANOW
, &d
->tt
) < 0)
1174 _error
->FatalE("tcsetattr", "Setting in Setup via TCSANOW for slave fd %d failed!", slaveFd
);
1180 void pkgDPkgPM::StopPtyMagic()
1182 if (d
->slave
!= NULL
)
1185 if (d
->protect_slave_from_dying
!= -1)
1187 close(d
->protect_slave_from_dying
);
1188 d
->protect_slave_from_dying
= -1;
1192 if (d
->tt_is_valid
== true && tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &d
->tt
) == -1)
1193 _error
->FatalE("tcsetattr", "Setting in Stop via TCSAFLUSH for stdin failed!");
1199 // DPkgPM::Go - Run the sequence /*{{{*/
1200 // ---------------------------------------------------------------------
1201 /* This globs the operations and calls dpkg
1203 * If it is called with a progress object apt will report the install
1204 * progress to this object. It maps the dpkg states a package goes
1205 * through to human readable (and i10n-able)
1206 * names and calculates a percentage for each step.
1208 bool pkgDPkgPM::Go(APT::Progress::PackageManager
*progress
)
1210 pkgPackageManager::SigINTStop
= false;
1211 d
->progress
= progress
;
1213 // Generate the base argument list for dpkg
1214 unsigned long StartSize
= 0;
1215 std::vector
<const char *> Args
;
1216 std::string DpkgExecutable
= getDpkgExecutable();
1217 Args
.push_back(DpkgExecutable
.c_str());
1218 StartSize
+= DpkgExecutable
.length();
1220 // Stick in any custom dpkg options
1221 Configuration::Item
const *Opts
= _config
->Tree("DPkg::Options");
1225 for (; Opts
!= 0; Opts
= Opts
->Next
)
1227 if (Opts
->Value
.empty() == true)
1229 Args
.push_back(Opts
->Value
.c_str());
1230 StartSize
+= Opts
->Value
.length();
1234 size_t const BaseArgs
= Args
.size();
1235 // we need to detect if we can qualify packages with the architecture or not
1236 Args
.push_back("--assert-multi-arch");
1237 Args
.push_back(NULL
);
1239 pid_t dpkgAssertMultiArch
= ExecFork();
1240 if (dpkgAssertMultiArch
== 0)
1242 dpkgChrootDirectory();
1243 // redirect everything to the ultimate sink as we only need the exit-status
1244 int const nullfd
= open("/dev/null", O_RDONLY
);
1245 dup2(nullfd
, STDIN_FILENO
);
1246 dup2(nullfd
, STDOUT_FILENO
);
1247 dup2(nullfd
, STDERR_FILENO
);
1248 execvp(Args
[0], (char**) &Args
[0]);
1249 _error
->WarningE("dpkgGo", "Can't detect if dpkg supports multi-arch!");
1256 // FIXME: do we really need this limit when we have MaxArgBytes?
1257 unsigned int const MaxArgs
= _config
->FindI("Dpkg::MaxArgs",32*1024);
1259 // try to figure out the max environment size
1260 int OSArgMax
= sysconf(_SC_ARG_MAX
);
1263 OSArgMax
-= EnvironmentSize() - 2*1024;
1264 unsigned int const MaxArgBytes
= _config
->FindI("Dpkg::MaxArgBytes", OSArgMax
);
1265 bool const NoTriggers
= _config
->FindB("DPkg::NoTriggers", false);
1267 if (RunScripts("DPkg::Pre-Invoke") == false)
1270 if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
1273 // support subpressing of triggers processing for special
1274 // cases like d-i that runs the triggers handling manually
1275 bool const TriggersPending
= _config
->FindB("DPkg::TriggersPending", false);
1276 if (_config
->FindB("DPkg::ConfigurePending", true) == true)
1277 List
.push_back(Item(Item::ConfigurePending
, PkgIterator()));
1280 BuildPackagesProgressMap();
1282 d
->stdin_is_dev_null
= false;
1287 bool dpkgMultiArch
= false;
1288 if (dpkgAssertMultiArch
> 0)
1291 while (waitpid(dpkgAssertMultiArch
, &Status
, 0) != dpkgAssertMultiArch
)
1295 _error
->WarningE("dpkgGo", _("Waited for %s but it wasn't there"), "dpkg --assert-multi-arch");
1298 if (WIFEXITED(Status
) == true && WEXITSTATUS(Status
) == 0)
1299 dpkgMultiArch
= true;
1302 // start pty magic before the loop
1305 // Tell the progress that its starting and fork dpkg
1306 d
->progress
->Start(d
->master
);
1308 // this loop is runs once per dpkg operation
1309 vector
<Item
>::const_iterator I
= List
.begin();
1310 while (I
!= List
.end())
1312 // Do all actions with the same Op in one run
1313 vector
<Item
>::const_iterator J
= I
;
1314 if (TriggersPending
== true)
1315 for (; J
!= List
.end(); ++J
)
1319 if (J
->Op
!= Item::TriggersPending
)
1321 vector
<Item
>::const_iterator T
= J
+ 1;
1322 if (T
!= List
.end() && T
->Op
== I
->Op
)
1327 for (; J
!= List
.end() && J
->Op
== I
->Op
; ++J
)
1330 // keep track of allocated strings for multiarch package names
1331 std::vector
<char *> Packages
;
1333 // start with the baseset of arguments
1334 unsigned long Size
= StartSize
;
1335 Args
.erase(Args
.begin() + BaseArgs
, Args
.end());
1337 // Now check if we are within the MaxArgs limit
1339 // this code below is problematic, because it may happen that
1340 // the argument list is split in a way that A depends on B
1341 // and they are in the same "--configure A B" run
1342 // - with the split they may now be configured in different
1343 // runs, using Immediate-Configure-All can help prevent this.
1344 if (J
- I
> (signed)MaxArgs
)
1347 unsigned long const size
= MaxArgs
+ 10;
1349 Packages
.reserve(size
);
1353 unsigned long const size
= (J
- I
) + 10;
1355 Packages
.reserve(size
);
1360 return _error
->Errno("pipe","Failed to create IPC pipe to dpkg");
1362 #define ADDARG(X) Args.push_back(X); Size += strlen(X)
1363 #define ADDARGC(X) Args.push_back(X); Size += sizeof(X) - 1
1365 ADDARGC("--status-fd");
1366 char status_fd_buf
[20];
1367 snprintf(status_fd_buf
,sizeof(status_fd_buf
),"%i", fd
[1]);
1368 ADDARG(status_fd_buf
);
1369 unsigned long const Op
= I
->Op
;
1374 ADDARGC("--force-depends");
1375 ADDARGC("--force-remove-essential");
1376 ADDARGC("--remove");
1380 ADDARGC("--force-depends");
1381 ADDARGC("--force-remove-essential");
1385 case Item::Configure
:
1386 ADDARGC("--configure");
1389 case Item::ConfigurePending
:
1390 ADDARGC("--configure");
1391 ADDARGC("--pending");
1394 case Item::TriggersPending
:
1395 ADDARGC("--triggers-only");
1396 ADDARGC("--pending");
1400 ADDARGC("--unpack");
1401 ADDARGC("--auto-deconfigure");
1405 if (NoTriggers
== true && I
->Op
!= Item::TriggersPending
&&
1406 I
->Op
!= Item::ConfigurePending
)
1408 ADDARGC("--no-triggers");
1412 // Write in the file or package names
1413 if (I
->Op
== Item::Install
)
1415 for (;I
!= J
&& Size
< MaxArgBytes
; ++I
)
1417 if (I
->File
[0] != '/')
1418 return _error
->Error("Internal Error, Pathname to install is not absolute '%s'",I
->File
.c_str());
1419 Args
.push_back(I
->File
.c_str());
1420 Size
+= I
->File
.length();
1425 string
const nativeArch
= _config
->Find("APT::Architecture");
1426 unsigned long const oldSize
= I
->Op
== Item::Configure
? Size
: 0;
1427 for (;I
!= J
&& Size
< MaxArgBytes
; ++I
)
1429 if((*I
).Pkg
.end() == true)
1431 if (I
->Op
== Item::Configure
&& disappearedPkgs
.find(I
->Pkg
.FullName(true)) != disappearedPkgs
.end())
1433 // We keep this here to allow "smooth" transitions from e.g. multiarch dpkg/ubuntu to dpkg/debian
1434 if (dpkgMultiArch
== false && (I
->Pkg
.Arch() == nativeArch
||
1435 strcmp(I
->Pkg
.Arch(), "all") == 0 ||
1436 strcmp(I
->Pkg
.Arch(), "none") == 0))
1438 char const * const name
= I
->Pkg
.Name();
1443 pkgCache::VerIterator PkgVer
;
1444 std::string name
= I
->Pkg
.Name();
1445 if (Op
== Item::Remove
|| Op
== Item::Purge
)
1447 PkgVer
= I
->Pkg
.CurrentVer();
1448 if(PkgVer
.end() == true)
1449 PkgVer
= FindNowVersion(I
->Pkg
);
1452 PkgVer
= Cache
[I
->Pkg
].InstVerIter(Cache
);
1453 if (strcmp(I
->Pkg
.Arch(), "none") == 0)
1454 ; // never arch-qualify a package without an arch
1455 else if (PkgVer
.end() == false)
1456 name
.append(":").append(PkgVer
.Arch());
1458 _error
->Warning("Can not find PkgVer for '%s'", name
.c_str());
1459 char * const fullname
= strdup(name
.c_str());
1460 Packages
.push_back(fullname
);
1464 // skip configure action if all sheduled packages disappeared
1465 if (oldSize
== Size
)
1472 if (_config
->FindB("Debug::pkgDPkgPM",false) == true)
1474 for (std::vector
<const char *>::const_iterator a
= Args
.begin();
1475 a
!= Args
.end(); ++a
)
1480 Args
.push_back(NULL
);
1486 /* Mask off sig int/quit. We do this because dpkg also does when
1487 it forks scripts. What happens is that when you hit ctrl-c it sends
1488 it to all processes in the group. Since dpkg ignores the signal
1489 it doesn't die but we do! So we must also ignore it */
1490 sighandler_t old_SIGQUIT
= signal(SIGQUIT
,SIG_IGN
);
1491 sighandler_t old_SIGINT
= signal(SIGINT
,SigINT
);
1493 // Check here for any SIGINT
1494 if (pkgPackageManager::SigINTStop
&& (Op
== Item::Remove
|| Op
== Item::Purge
|| Op
== Item::Install
))
1498 // ignore SIGHUP as well (debian #463030)
1499 sighandler_t old_SIGHUP
= signal(SIGHUP
,SIG_IGN
);
1502 d
->progress
->StartDpkg();
1503 std::set
<int> KeepFDs
;
1504 KeepFDs
.insert(fd
[1]);
1505 MergeKeepFdsFromConfiguration(KeepFDs
);
1506 pid_t Child
= ExecFork(KeepFDs
);
1509 // This is the child
1510 SetupSlavePtyMagic();
1511 close(fd
[0]); // close the read end of the pipe
1513 dpkgChrootDirectory();
1515 if (chdir(_config
->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
1518 if (_config
->FindB("DPkg::FlushSTDIN",true) == true && isatty(STDIN_FILENO
))
1522 if ((Flags
= fcntl(STDIN_FILENO
,F_GETFL
,dummy
)) < 0)
1525 // Discard everything in stdin before forking dpkg
1526 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
| O_NONBLOCK
) < 0)
1529 while (read(STDIN_FILENO
,&dummy
,1) == 1);
1531 if (fcntl(STDIN_FILENO
,F_SETFL
,Flags
& (~(long)O_NONBLOCK
)) < 0)
1535 /* No Job Control Stop Env is a magic dpkg var that prevents it
1536 from using sigstop */
1537 putenv((char *)"DPKG_NO_TSTP=yes");
1538 execvp(Args
[0], (char**) &Args
[0]);
1539 cerr
<< "Could not exec dpkg!" << endl
;
1544 if (_config
->FindB("DPkg::UseIoNice", false) == true)
1550 // we read from dpkg here
1551 int const _dpkgin
= fd
[0];
1552 close(fd
[1]); // close the write end of the pipe
1555 sigemptyset(&d
->sigmask
);
1556 sigprocmask(SIG_BLOCK
,&d
->sigmask
,&d
->original_sigmask
);
1558 /* free vectors (and therefore memory) as we don't need the included data anymore */
1559 for (std::vector
<char *>::const_iterator p
= Packages
.begin();
1560 p
!= Packages
.end(); ++p
)
1564 // the result of the waitpid call
1567 while ((res
=waitpid(Child
,&Status
, WNOHANG
)) != Child
) {
1569 // FIXME: move this to a function or something, looks ugly here
1570 // error handling, waitpid returned -1
1573 RunScripts("DPkg::Post-Invoke");
1575 // Restore sig int/quit
1576 signal(SIGQUIT
,old_SIGQUIT
);
1577 signal(SIGINT
,old_SIGINT
);
1579 signal(SIGHUP
,old_SIGHUP
);
1580 return _error
->Errno("waitpid","Couldn't wait for subprocess");
1583 // wait for input or output here
1585 if (d
->master
>= 0 && !d
->stdin_is_dev_null
)
1587 FD_SET(_dpkgin
, &rfds
);
1589 FD_SET(d
->master
, &rfds
);
1591 tv
.tv_nsec
= d
->progress
->GetPulseInterval();
1592 select_ret
= pselect(max(d
->master
, _dpkgin
)+1, &rfds
, NULL
, NULL
,
1593 &tv
, &d
->original_sigmask
);
1594 if (select_ret
< 0 && (errno
== EINVAL
|| errno
== ENOSYS
))
1595 select_ret
= racy_pselect(max(d
->master
, _dpkgin
)+1, &rfds
, NULL
,
1596 NULL
, &tv
, &d
->original_sigmask
);
1597 d
->progress
->Pulse();
1598 if (select_ret
== 0)
1600 else if (select_ret
< 0 && errno
== EINTR
)
1602 else if (select_ret
< 0)
1604 perror("select() returned error");
1608 if(d
->master
>= 0 && FD_ISSET(d
->master
, &rfds
))
1609 DoTerminalPty(d
->master
);
1610 if(d
->master
>= 0 && FD_ISSET(0, &rfds
))
1612 if(FD_ISSET(_dpkgin
, &rfds
))
1613 DoDpkgStatusFd(_dpkgin
);
1617 // Restore sig int/quit
1618 signal(SIGQUIT
,old_SIGQUIT
);
1619 signal(SIGINT
,old_SIGINT
);
1621 signal(SIGHUP
,old_SIGHUP
);
1622 // Check for an error code.
1623 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
1625 // if it was set to "keep-dpkg-runing" then we won't return
1626 // here but keep the loop going and just report it as a error
1628 bool const stopOnError
= _config
->FindB("Dpkg::StopOnError",true);
1630 if (WIFSIGNALED(Status
) != 0 && WTERMSIG(Status
) == SIGSEGV
)
1631 strprintf(d
->dpkg_error
, "Sub-process %s received a segmentation fault.",Args
[0]);
1632 else if (WIFEXITED(Status
) != 0)
1633 strprintf(d
->dpkg_error
, "Sub-process %s returned an error code (%u)",Args
[0],WEXITSTATUS(Status
));
1635 strprintf(d
->dpkg_error
, "Sub-process %s exited unexpectedly",Args
[0]);
1636 _error
->Error("%s", d
->dpkg_error
.c_str());
1642 // dpkg is done at this point
1643 d
->progress
->Stop();
1647 if (pkgPackageManager::SigINTStop
)
1648 _error
->Warning(_("Operation was interrupted before it could finish"));
1650 if (RunScripts("DPkg::Post-Invoke") == false)
1653 if (_config
->FindB("Debug::pkgDPkgPM",false) == false)
1655 std::string
const oldpkgcache
= _config
->FindFile("Dir::cache::pkgcache");
1656 if (oldpkgcache
.empty() == false && RealFileExists(oldpkgcache
) == true &&
1657 unlink(oldpkgcache
.c_str()) == 0)
1659 std::string
const srcpkgcache
= _config
->FindFile("Dir::cache::srcpkgcache");
1660 if (srcpkgcache
.empty() == false && RealFileExists(srcpkgcache
) == true)
1662 _error
->PushToStack();
1663 pkgCacheFile CacheFile
;
1664 CacheFile
.BuildCaches(NULL
, true);
1665 _error
->RevertToStack();
1670 Cache
.writeStateFile(NULL
);
1671 return d
->dpkg_error
.empty();
1674 void SigINT(int /*sig*/) {
1675 pkgPackageManager::SigINTStop
= true;
1678 // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
1679 // ---------------------------------------------------------------------
1681 void pkgDPkgPM::Reset()
1683 List
.erase(List
.begin(),List
.end());
1686 // pkgDpkgPM::WriteApportReport - write out error report pkg failure /*{{{*/
1687 // ---------------------------------------------------------------------
1689 void pkgDPkgPM::WriteApportReport(const char *pkgpath
, const char *errormsg
)
1691 // If apport doesn't exist or isn't installed do nothing
1692 // This e.g. prevents messages in 'universes' without apport
1693 pkgCache::PkgIterator apportPkg
= Cache
.FindPkg("apport");
1694 if (apportPkg
.end() == true || apportPkg
->CurrentVer
== 0)
1697 string pkgname
, reportfile
, pkgver
, arch
;
1698 string::size_type pos
;
1701 if (_config
->FindB("Dpkg::ApportFailureReport", true) == false)
1703 std::clog
<< "configured to not write apport reports" << std::endl
;
1707 // only report the first errors
1708 if(pkgFailures
> _config
->FindI("APT::Apport::MaxReports", 3))
1710 std::clog
<< _("No apport report written because MaxReports is reached already") << std::endl
;
1714 // check if its not a follow up error
1715 const char *needle
= dgettext("dpkg", "dependency problems - leaving unconfigured");
1716 if(strstr(errormsg
, needle
) != NULL
) {
1717 std::clog
<< _("No apport report written because the error message indicates its a followup error from a previous failure.") << std::endl
;
1721 // do not report disk-full failures
1722 if(strstr(errormsg
, strerror(ENOSPC
)) != NULL
) {
1723 std::clog
<< _("No apport report written because the error message indicates a disk full error") << std::endl
;
1727 // do not report out-of-memory failures
1728 if(strstr(errormsg
, strerror(ENOMEM
)) != NULL
||
1729 strstr(errormsg
, "failed to allocate memory") != NULL
) {
1730 std::clog
<< _("No apport report written because the error message indicates a out of memory error") << std::endl
;
1734 // do not report bugs regarding inaccessible local files
1735 if(strstr(errormsg
, strerror(ENOENT
)) != NULL
||
1736 strstr(errormsg
, "cannot access archive") != NULL
) {
1737 std::clog
<< _("No apport report written because the error message indicates an issue on the local system") << std::endl
;
1741 // do not report errors encountered when decompressing packages
1742 if(strstr(errormsg
, "--fsys-tarfile returned error exit status 2") != NULL
) {
1743 std::clog
<< _("No apport report written because the error message indicates an issue on the local system") << std::endl
;
1747 // do not report dpkg I/O errors, this is a format string, so we compare
1748 // the prefix and the suffix of the error with the dpkg error message
1749 vector
<string
> io_errors
;
1750 io_errors
.push_back(string("failed to read"));
1751 io_errors
.push_back(string("failed to write"));
1752 io_errors
.push_back(string("failed to seek"));
1753 io_errors
.push_back(string("unexpected end of file or stream"));
1755 for (vector
<string
>::iterator I
= io_errors
.begin(); I
!= io_errors
.end(); ++I
)
1757 vector
<string
> list
= VectorizeString(dgettext("dpkg", (*I
).c_str()), '%');
1758 if (list
.size() > 1) {
1759 // we need to split %s, VectorizeString only allows char so we need
1760 // to kill the "s" manually
1761 if (list
[1].size() > 1) {
1762 list
[1].erase(0, 1);
1763 if(strstr(errormsg
, list
[0].c_str()) &&
1764 strstr(errormsg
, list
[1].c_str())) {
1765 std::clog
<< _("No apport report written because the error message indicates a dpkg I/O error") << std::endl
;
1772 // get the pkgname and reportfile
1773 pkgname
= flNotDir(pkgpath
);
1774 pos
= pkgname
.find('_');
1775 if(pos
!= string::npos
)
1776 pkgname
= pkgname
.substr(0, pos
);
1778 // find the package versin and source package name
1779 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(pkgname
);
1780 if (Pkg
.end() == true)
1782 pkgCache::VerIterator Ver
= Cache
.GetCandidateVer(Pkg
);
1783 if (Ver
.end() == true)
1785 pkgver
= Ver
.VerStr() == NULL
? "unknown" : Ver
.VerStr();
1787 // if the file exists already, we check:
1788 // - if it was reported already (touched by apport).
1789 // If not, we do nothing, otherwise
1790 // we overwrite it. This is the same behaviour as apport
1791 // - if we have a report with the same pkgversion already
1793 reportfile
= flCombine("/var/crash",pkgname
+".0.crash");
1794 if(FileExists(reportfile
))
1799 // check atime/mtime
1800 stat(reportfile
.c_str(), &buf
);
1801 if(buf
.st_mtime
> buf
.st_atime
)
1804 // check if the existing report is the same version
1805 report
= fopen(reportfile
.c_str(),"r");
1806 while(fgets(strbuf
, sizeof(strbuf
), report
) != NULL
)
1808 if(strstr(strbuf
,"Package:") == strbuf
)
1810 char pkgname
[255], version
[255];
1811 if(sscanf(strbuf
, "Package: %254s %254s", pkgname
, version
) == 2)
1812 if(strcmp(pkgver
.c_str(), version
) == 0)
1822 // now write the report
1823 arch
= _config
->Find("APT::Architecture");
1824 report
= fopen(reportfile
.c_str(),"w");
1827 if(_config
->FindB("DPkgPM::InitialReportOnly",false) == true)
1828 chmod(reportfile
.c_str(), 0);
1830 chmod(reportfile
.c_str(), 0600);
1831 fprintf(report
, "ProblemType: Package\n");
1832 fprintf(report
, "Architecture: %s\n", arch
.c_str());
1833 time_t now
= time(NULL
);
1834 fprintf(report
, "Date: %s" , ctime(&now
));
1835 fprintf(report
, "Package: %s %s\n", pkgname
.c_str(), pkgver
.c_str());
1836 #if APT_PKG_ABI >= 413
1837 fprintf(report
, "SourcePackage: %s\n", Ver
.SourcePkgName());
1839 pkgRecords
Recs(Cache
);
1840 pkgRecords::Parser
&Parse
= Recs
.Lookup(Ver
.FileList());
1841 std::string srcpkgname
= Parse
.SourcePkg();
1842 if(srcpkgname
.empty())
1843 srcpkgname
= pkgname
;
1844 fprintf(report
, "SourcePackage: %s\n", srcpkgname
.c_str());
1846 fprintf(report
, "ErrorMessage:\n %s\n", errormsg
);
1848 // ensure that the log is flushed
1850 fflush(d
->term_out
);
1852 // attach terminal log it if we have it
1853 string logfile_name
= _config
->FindFile("Dir::Log::Terminal");
1854 if (!logfile_name
.empty())
1858 fprintf(report
, "DpkgTerminalLog:\n");
1859 log
= fopen(logfile_name
.c_str(),"r");
1863 while( fgets(buf
, sizeof(buf
), log
) != NULL
)
1864 fprintf(report
, " %s", buf
);
1865 fprintf(report
, " \n");
1870 // attach history log it if we have it
1871 string histfile_name
= _config
->FindFile("Dir::Log::History");
1872 if (!histfile_name
.empty())
1874 fprintf(report
, "DpkgHistoryLog:\n");
1875 FILE* log
= fopen(histfile_name
.c_str(),"r");
1879 while( fgets(buf
, sizeof(buf
), log
) != NULL
)
1880 fprintf(report
, " %s", buf
);
1886 const char *ops_str
[] = {"Install", "Configure","Remove","Purge"};
1887 fprintf(report
, "AptOrdering:\n");
1888 for (vector
<Item
>::iterator I
= List
.begin(); I
!= List
.end(); ++I
)
1889 if ((*I
).Pkg
!= NULL
)
1890 fprintf(report
, " %s: %s\n", (*I
).Pkg
.Name(), ops_str
[(*I
).Op
]);
1892 fprintf(report
, " %s: %s\n", "NULL", ops_str
[(*I
).Op
]);
1894 // attach dmesg log (to learn about segfaults)
1895 if (FileExists("/bin/dmesg"))
1897 fprintf(report
, "Dmesg:\n");
1898 FILE *log
= popen("/bin/dmesg","r");
1902 while( fgets(buf
, sizeof(buf
), log
) != NULL
)
1903 fprintf(report
, " %s", buf
);
1908 // attach df -l log (to learn about filesystem status)
1909 if (FileExists("/bin/df"))
1912 fprintf(report
, "Df:\n");
1913 FILE *log
= popen("/bin/df -l","r");
1917 while( fgets(buf
, sizeof(buf
), log
) != NULL
)
1918 fprintf(report
, " %s", buf
);