]>
Commit | Line | Data |
---|---|---|
c0c0b100 | 1 | // -*- mode: cpp; mode: fold -*- |
03e39e59 | 2 | // Description /*{{{*/ |
7f9a6360 | 3 | // $Id: dpkgpm.cc,v 1.28 2004/01/27 02:25:01 mdz Exp $ |
03e39e59 AL |
4 | /* ###################################################################### |
5 | ||
6 | DPKG Package Manager - Provide an interface to dpkg | |
7 | ||
8 | ##################################################################### */ | |
9 | /*}}}*/ | |
10 | // Includes /*{{{*/ | |
03e39e59 AL |
11 | #include <apt-pkg/dpkgpm.h> |
12 | #include <apt-pkg/error.h> | |
13 | #include <apt-pkg/configuration.h> | |
b2e465d6 | 14 | #include <apt-pkg/depcache.h> |
5e457a93 | 15 | #include <apt-pkg/pkgrecords.h> |
b2e465d6 | 16 | #include <apt-pkg/strutl.h> |
a4cf3665 | 17 | #include <apti18n.h> |
614adaa0 | 18 | #include <apt-pkg/fileutl.h> |
233b185f | 19 | |
03e39e59 AL |
20 | #include <unistd.h> |
21 | #include <stdlib.h> | |
22 | #include <fcntl.h> | |
090c6566 | 23 | #include <sys/select.h> |
96db74ce | 24 | #include <sys/stat.h> |
03e39e59 AL |
25 | #include <sys/types.h> |
26 | #include <sys/wait.h> | |
27 | #include <signal.h> | |
28 | #include <errno.h> | |
2f0d5dea | 29 | #include <string.h> |
db0c350f | 30 | #include <stdio.h> |
f7dec19f DB |
31 | #include <string.h> |
32 | #include <algorithm> | |
75ef8f14 MV |
33 | #include <sstream> |
34 | #include <map> | |
35 | ||
d8cb4aa4 MV |
36 | #include <termios.h> |
37 | #include <unistd.h> | |
38 | #include <sys/ioctl.h> | |
39 | #include <pty.h> | |
40 | ||
75ef8f14 MV |
41 | #include <config.h> |
42 | #include <apti18n.h> | |
b0ebdef5 | 43 | /*}}}*/ |
233b185f AL |
44 | |
45 | using namespace std; | |
03e39e59 | 46 | |
f7dec19f DB |
47 | namespace |
48 | { | |
49 | // Maps the dpkg "processing" info to human readable names. Entry 0 | |
50 | // of each array is the key, entry 1 is the value. | |
51 | const std::pair<const char *, const char *> PackageProcessingOps[] = { | |
52 | std::make_pair("install", N_("Installing %s")), | |
53 | std::make_pair("configure", N_("Configuring %s")), | |
54 | std::make_pair("remove", N_("Removing %s")), | |
ac81ae9c | 55 | std::make_pair("purge", N_("Completely removing %s")), |
b3514c56 | 56 | std::make_pair("disappear", N_("Noting disappearance of %s")), |
f7dec19f DB |
57 | std::make_pair("trigproc", N_("Running post-installation trigger %s")) |
58 | }; | |
59 | ||
60 | const std::pair<const char *, const char *> * const PackageProcessingOpsBegin = PackageProcessingOps; | |
61 | const std::pair<const char *, const char *> * const PackageProcessingOpsEnd = PackageProcessingOps + sizeof(PackageProcessingOps) / sizeof(PackageProcessingOps[0]); | |
62 | ||
63 | // Predicate to test whether an entry in the PackageProcessingOps | |
64 | // array matches a string. | |
65 | class MatchProcessingOp | |
66 | { | |
67 | const char *target; | |
68 | ||
69 | public: | |
70 | MatchProcessingOp(const char *the_target) | |
71 | : target(the_target) | |
72 | { | |
73 | } | |
74 | ||
75 | bool operator()(const std::pair<const char *, const char *> &pair) const | |
76 | { | |
77 | return strcmp(pair.first, target) == 0; | |
78 | } | |
79 | }; | |
80 | } | |
09fa2df2 | 81 | |
cebe0287 MV |
82 | /* helper function to ionice the given PID |
83 | ||
84 | there is no C header for ionice yet - just the syscall interface | |
85 | so we use the binary from util-linux | |
86 | */ | |
87 | static bool | |
88 | ionice(int PID) | |
89 | { | |
90 | if (!FileExists("/usr/bin/ionice")) | |
91 | return false; | |
92 | pid_t Process = ExecFork(); | |
93 | if (Process == 0) | |
94 | { | |
95 | char buf[32]; | |
96 | snprintf(buf, sizeof(buf), "-p%d", PID); | |
97 | const char *Args[4]; | |
98 | Args[0] = "/usr/bin/ionice"; | |
99 | Args[1] = "-c3"; | |
100 | Args[2] = buf; | |
101 | Args[3] = 0; | |
102 | execv(Args[0], (char **)Args); | |
103 | } | |
104 | return ExecWait(Process, "ionice"); | |
105 | } | |
106 | ||
03e39e59 AL |
107 | // DPkgPM::pkgDPkgPM - Constructor /*{{{*/ |
108 | // --------------------------------------------------------------------- | |
109 | /* */ | |
09fa2df2 | 110 | pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache) |
71afbdb5 | 111 | : pkgPackageManager(Cache), dpkgbuf_pos(0), |
5e8b2b74 | 112 | term_out(NULL), history_out(NULL), PackagesDone(0), PackagesTotal(0) |
03e39e59 AL |
113 | { |
114 | } | |
115 | /*}}}*/ | |
116 | // DPkgPM::pkgDPkgPM - Destructor /*{{{*/ | |
117 | // --------------------------------------------------------------------- | |
118 | /* */ | |
119 | pkgDPkgPM::~pkgDPkgPM() | |
120 | { | |
121 | } | |
122 | /*}}}*/ | |
123 | // DPkgPM::Install - Install a package /*{{{*/ | |
124 | // --------------------------------------------------------------------- | |
125 | /* Add an install operation to the sequence list */ | |
126 | bool pkgDPkgPM::Install(PkgIterator Pkg,string File) | |
127 | { | |
128 | if (File.empty() == true || Pkg.end() == true) | |
129 | return _error->Error("Internal Error, No file name for %s",Pkg.Name()); | |
130 | ||
05bae55f DK |
131 | // If the filename string begins with DPkg::Chroot-Directory, return the |
132 | // substr that is within the chroot so dpkg can access it. | |
133 | string const chrootdir = _config->FindDir("DPkg::Chroot-Directory","/"); | |
134 | if (chrootdir != "/" && File.find(chrootdir) == 0) | |
135 | { | |
136 | size_t len = chrootdir.length(); | |
137 | if (chrootdir.at(len - 1) == '/') | |
138 | len--; | |
139 | List.push_back(Item(Item::Install,Pkg,File.substr(len))); | |
140 | } | |
141 | else | |
142 | List.push_back(Item(Item::Install,Pkg,File)); | |
143 | ||
03e39e59 AL |
144 | return true; |
145 | } | |
146 | /*}}}*/ | |
147 | // DPkgPM::Configure - Configure a package /*{{{*/ | |
148 | // --------------------------------------------------------------------- | |
149 | /* Add a configure operation to the sequence list */ | |
150 | bool pkgDPkgPM::Configure(PkgIterator Pkg) | |
151 | { | |
152 | if (Pkg.end() == true) | |
153 | return false; | |
3e9c4f70 | 154 | |
5e312de7 DK |
155 | List.push_back(Item(Item::Configure, Pkg)); |
156 | ||
157 | // Use triggers for config calls if we configure "smart" | |
158 | // as otherwise Pre-Depends will not be satisfied, see #526774 | |
159 | if (_config->FindB("DPkg::TriggersPending", false) == true) | |
160 | List.push_back(Item(Item::TriggersPending, PkgIterator())); | |
3e9c4f70 | 161 | |
03e39e59 AL |
162 | return true; |
163 | } | |
164 | /*}}}*/ | |
165 | // DPkgPM::Remove - Remove a package /*{{{*/ | |
166 | // --------------------------------------------------------------------- | |
167 | /* Add a remove operation to the sequence list */ | |
fc4b5c9f | 168 | bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge) |
03e39e59 AL |
169 | { |
170 | if (Pkg.end() == true) | |
171 | return false; | |
172 | ||
fc4b5c9f AL |
173 | if (Purge == true) |
174 | List.push_back(Item(Item::Purge,Pkg)); | |
175 | else | |
176 | List.push_back(Item(Item::Remove,Pkg)); | |
6dd55be7 AL |
177 | return true; |
178 | } | |
179 | /*}}}*/ | |
b2e465d6 AL |
180 | // DPkgPM::SendV2Pkgs - Send version 2 package info /*{{{*/ |
181 | // --------------------------------------------------------------------- | |
182 | /* This is part of the helper script communication interface, it sends | |
183 | very complete information down to the other end of the pipe.*/ | |
184 | bool pkgDPkgPM::SendV2Pkgs(FILE *F) | |
185 | { | |
186 | fprintf(F,"VERSION 2\n"); | |
187 | ||
188 | /* Write out all of the configuration directives by walking the | |
189 | configuration tree */ | |
190 | const Configuration::Item *Top = _config->Tree(0); | |
191 | for (; Top != 0;) | |
192 | { | |
193 | if (Top->Value.empty() == false) | |
194 | { | |
195 | fprintf(F,"%s=%s\n", | |
196 | QuoteString(Top->FullTag(),"=\"\n").c_str(), | |
197 | QuoteString(Top->Value,"\n").c_str()); | |
198 | } | |
199 | ||
200 | if (Top->Child != 0) | |
201 | { | |
202 | Top = Top->Child; | |
203 | continue; | |
204 | } | |
205 | ||
206 | while (Top != 0 && Top->Next == 0) | |
207 | Top = Top->Parent; | |
208 | if (Top != 0) | |
209 | Top = Top->Next; | |
210 | } | |
211 | fprintf(F,"\n"); | |
212 | ||
213 | // Write out the package actions in order. | |
214 | for (vector<Item>::iterator I = List.begin(); I != List.end(); I++) | |
215 | { | |
3e9c4f70 DK |
216 | if(I->Pkg.end() == true) |
217 | continue; | |
218 | ||
b2e465d6 AL |
219 | pkgDepCache::StateCache &S = Cache[I->Pkg]; |
220 | ||
221 | fprintf(F,"%s ",I->Pkg.Name()); | |
222 | // Current version | |
223 | if (I->Pkg->CurrentVer == 0) | |
224 | fprintf(F,"- "); | |
225 | else | |
226 | fprintf(F,"%s ",I->Pkg.CurrentVer().VerStr()); | |
227 | ||
228 | // Show the compare operator | |
229 | // Target version | |
230 | if (S.InstallVer != 0) | |
231 | { | |
232 | int Comp = 2; | |
233 | if (I->Pkg->CurrentVer != 0) | |
234 | Comp = S.InstVerIter(Cache).CompareVer(I->Pkg.CurrentVer()); | |
235 | if (Comp < 0) | |
236 | fprintf(F,"> "); | |
237 | if (Comp == 0) | |
238 | fprintf(F,"= "); | |
239 | if (Comp > 0) | |
240 | fprintf(F,"< "); | |
241 | fprintf(F,"%s ",S.InstVerIter(Cache).VerStr()); | |
242 | } | |
243 | else | |
244 | fprintf(F,"> - "); | |
245 | ||
246 | // Show the filename/operation | |
247 | if (I->Op == Item::Install) | |
248 | { | |
249 | // No errors here.. | |
250 | if (I->File[0] != '/') | |
251 | fprintf(F,"**ERROR**\n"); | |
252 | else | |
253 | fprintf(F,"%s\n",I->File.c_str()); | |
254 | } | |
255 | if (I->Op == Item::Configure) | |
256 | fprintf(F,"**CONFIGURE**\n"); | |
257 | if (I->Op == Item::Remove || | |
258 | I->Op == Item::Purge) | |
259 | fprintf(F,"**REMOVE**\n"); | |
260 | ||
261 | if (ferror(F) != 0) | |
262 | return false; | |
263 | } | |
264 | return true; | |
265 | } | |
266 | /*}}}*/ | |
db0c350f AL |
267 | // DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/ |
268 | // --------------------------------------------------------------------- | |
269 | /* This looks for a list of scripts to run from the configuration file | |
270 | each one is run and is fed on standard input a list of all .deb files | |
271 | that are due to be installed. */ | |
272 | bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf) | |
273 | { | |
274 | Configuration::Item const *Opts = _config->Tree(Cnf); | |
275 | if (Opts == 0 || Opts->Child == 0) | |
276 | return true; | |
277 | Opts = Opts->Child; | |
278 | ||
279 | unsigned int Count = 1; | |
280 | for (; Opts != 0; Opts = Opts->Next, Count++) | |
281 | { | |
282 | if (Opts->Value.empty() == true) | |
283 | continue; | |
b2e465d6 AL |
284 | |
285 | // Determine the protocol version | |
286 | string OptSec = Opts->Value; | |
287 | string::size_type Pos; | |
288 | if ((Pos = OptSec.find(' ')) == string::npos || Pos == 0) | |
289 | Pos = OptSec.length(); | |
b2e465d6 AL |
290 | OptSec = "DPkg::Tools::Options::" + string(Opts->Value.c_str(),Pos); |
291 | ||
292 | unsigned int Version = _config->FindI(OptSec+"::Version",1); | |
293 | ||
db0c350f AL |
294 | // Create the pipes |
295 | int Pipes[2]; | |
296 | if (pipe(Pipes) != 0) | |
297 | return _error->Errno("pipe","Failed to create IPC pipe to subprocess"); | |
298 | SetCloseExec(Pipes[0],true); | |
299 | SetCloseExec(Pipes[1],true); | |
300 | ||
301 | // Purified Fork for running the script | |
302 | pid_t Process = ExecFork(); | |
303 | if (Process == 0) | |
304 | { | |
305 | // Setup the FDs | |
306 | dup2(Pipes[0],STDIN_FILENO); | |
307 | SetCloseExec(STDOUT_FILENO,false); | |
308 | SetCloseExec(STDIN_FILENO,false); | |
309 | SetCloseExec(STDERR_FILENO,false); | |
90ecbd7d AL |
310 | |
311 | const char *Args[4]; | |
db0c350f | 312 | Args[0] = "/bin/sh"; |
90ecbd7d AL |
313 | Args[1] = "-c"; |
314 | Args[2] = Opts->Value.c_str(); | |
315 | Args[3] = 0; | |
db0c350f AL |
316 | execv(Args[0],(char **)Args); |
317 | _exit(100); | |
318 | } | |
319 | close(Pipes[0]); | |
b2e465d6 AL |
320 | FILE *F = fdopen(Pipes[1],"w"); |
321 | if (F == 0) | |
322 | return _error->Errno("fdopen","Faild to open new FD"); | |
323 | ||
db0c350f | 324 | // Feed it the filenames. |
b2e465d6 AL |
325 | bool Die = false; |
326 | if (Version <= 1) | |
db0c350f | 327 | { |
b2e465d6 | 328 | for (vector<Item>::iterator I = List.begin(); I != List.end(); I++) |
db0c350f | 329 | { |
b2e465d6 AL |
330 | // Only deal with packages to be installed from .deb |
331 | if (I->Op != Item::Install) | |
332 | continue; | |
333 | ||
334 | // No errors here.. | |
335 | if (I->File[0] != '/') | |
336 | continue; | |
337 | ||
338 | /* Feed the filename of each package that is pending install | |
339 | into the pipe. */ | |
340 | fprintf(F,"%s\n",I->File.c_str()); | |
341 | if (ferror(F) != 0) | |
342 | { | |
343 | Die = true; | |
344 | break; | |
345 | } | |
90ecbd7d | 346 | } |
db0c350f | 347 | } |
b2e465d6 AL |
348 | else |
349 | Die = !SendV2Pkgs(F); | |
350 | ||
351 | fclose(F); | |
db0c350f AL |
352 | |
353 | // Clean up the sub process | |
354 | if (ExecWait(Process,Opts->Value.c_str()) == false) | |
90ecbd7d | 355 | return _error->Error("Failure running script %s",Opts->Value.c_str()); |
db0c350f AL |
356 | } |
357 | ||
358 | return true; | |
359 | } | |
ceabc520 MV |
360 | /*}}}*/ |
361 | // DPkgPM::DoStdin - Read stdin and pass to slave pty /*{{{*/ | |
362 | // --------------------------------------------------------------------- | |
363 | /* | |
364 | */ | |
365 | void pkgDPkgPM::DoStdin(int master) | |
366 | { | |
aff87a76 MV |
367 | unsigned char input_buf[256] = {0,}; |
368 | ssize_t len = read(0, input_buf, sizeof(input_buf)); | |
9983591d OS |
369 | if (len) |
370 | write(master, input_buf, len); | |
371 | else | |
372 | stdin_is_dev_null = true; | |
ceabc520 | 373 | } |
03e39e59 | 374 | /*}}}*/ |
ceabc520 MV |
375 | // DPkgPM::DoTerminalPty - Read the terminal pty and write log /*{{{*/ |
376 | // --------------------------------------------------------------------- | |
377 | /* | |
378 | * read the terminal pty and write log | |
379 | */ | |
1ba38171 | 380 | void pkgDPkgPM::DoTerminalPty(int master) |
ceabc520 | 381 | { |
aff87a76 | 382 | unsigned char term_buf[1024] = {0,0, }; |
ceabc520 | 383 | |
aff87a76 | 384 | ssize_t len=read(master, term_buf, sizeof(term_buf)); |
7052511e MV |
385 | if(len == -1 && errno == EIO) |
386 | { | |
387 | // this happens when the child is about to exit, we | |
388 | // give it time to actually exit, otherwise we run | |
389 | // into a race | |
390 | usleep(500000); | |
391 | return; | |
392 | } | |
393 | if(len <= 0) | |
955a6ddb | 394 | return; |
955a6ddb | 395 | write(1, term_buf, len); |
8da1f029 MV |
396 | if(term_out) |
397 | fwrite(term_buf, len, sizeof(char), term_out); | |
ceabc520 | 398 | } |
03e39e59 | 399 | /*}}}*/ |
6191b008 MV |
400 | // DPkgPM::ProcessDpkgStatusBuf /*{{{*/ |
401 | // --------------------------------------------------------------------- | |
402 | /* | |
403 | */ | |
09fa2df2 | 404 | void pkgDPkgPM::ProcessDpkgStatusLine(int OutStatusFd, char *line) |
6191b008 | 405 | { |
887f5036 | 406 | bool const Debug = _config->FindB("Debug::pkgDPkgProgressReporting",false); |
09fa2df2 MV |
407 | // the status we output |
408 | ostringstream status; | |
409 | ||
887f5036 | 410 | if (Debug == true) |
09fa2df2 MV |
411 | std::clog << "got from dpkg '" << line << "'" << std::endl; |
412 | ||
413 | ||
414 | /* dpkg sends strings like this: | |
415 | 'status: <pkg>: <pkg qstate>' | |
416 | errors look like this: | |
417 | '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 | |
418 | and conffile-prompt like this | |
419 | 'status: conffile-prompt: conffile : 'current-conffile' 'new-conffile' useredited distedited | |
fc2d32c0 MV |
420 | |
421 | Newer versions of dpkg sent also: | |
422 | 'processing: install: pkg' | |
423 | 'processing: configure: pkg' | |
424 | 'processing: remove: pkg' | |
b3514c56 DK |
425 | 'processing: purge: pkg' |
426 | 'processing: disappear: pkg' | |
fc2d32c0 | 427 | 'processing: trigproc: trigger' |
09fa2df2 MV |
428 | |
429 | */ | |
5279f566 | 430 | char* list[6]; |
09fa2df2 MV |
431 | // dpkg sends multiline error messages sometimes (see |
432 | // #374195 for a example. we should support this by | |
433 | // either patching dpkg to not send multiline over the | |
434 | // statusfd or by rewriting the code here to deal with | |
435 | // it. for now we just ignore it and not crash | |
436 | TokSplitString(':', line, list, sizeof(list)/sizeof(list[0])); | |
f26fcbc7 | 437 | if( list[0] == NULL || list[1] == NULL || list[2] == NULL) |
09fa2df2 | 438 | { |
887f5036 | 439 | if (Debug == true) |
09fa2df2 MV |
440 | std::clog << "ignoring line: not enough ':'" << std::endl; |
441 | return; | |
442 | } | |
887f5036 DK |
443 | const char* const pkg = list[1]; |
444 | const char* action = _strstrip(list[2]); | |
09fa2df2 | 445 | |
fc2d32c0 MV |
446 | // 'processing' from dpkg looks like |
447 | // 'processing: action: pkg' | |
448 | if(strncmp(list[0], "processing", strlen("processing")) == 0) | |
449 | { | |
450 | char s[200]; | |
887f5036 DK |
451 | const char* const pkg_or_trigger = _strstrip(list[2]); |
452 | action = _strstrip( list[1]); | |
f7dec19f DB |
453 | const std::pair<const char *, const char *> * const iter = |
454 | std::find_if(PackageProcessingOpsBegin, | |
455 | PackageProcessingOpsEnd, | |
456 | MatchProcessingOp(action)); | |
457 | if(iter == PackageProcessingOpsEnd) | |
fc2d32c0 | 458 | { |
887f5036 DK |
459 | if (Debug == true) |
460 | std::clog << "ignoring unknown action: " << action << std::endl; | |
fc2d32c0 MV |
461 | return; |
462 | } | |
f7dec19f | 463 | snprintf(s, sizeof(s), _(iter->second), pkg_or_trigger); |
fc2d32c0 MV |
464 | |
465 | status << "pmstatus:" << pkg_or_trigger | |
466 | << ":" << (PackagesDone/float(PackagesTotal)*100.0) | |
467 | << ":" << s | |
468 | << endl; | |
469 | if(OutStatusFd > 0) | |
470 | write(OutStatusFd, status.str().c_str(), status.str().size()); | |
887f5036 | 471 | if (Debug == true) |
fc2d32c0 | 472 | std::clog << "send: '" << status.str() << "'" << endl; |
642ebc1a DK |
473 | |
474 | if (strncmp(action, "disappear", strlen("disappear")) == 0) | |
eb6f9bac | 475 | handleDisappearAction(pkg_or_trigger); |
fc2d32c0 MV |
476 | return; |
477 | } | |
478 | ||
09fa2df2 MV |
479 | if(strncmp(action,"error",strlen("error")) == 0) |
480 | { | |
d6a4afcb MV |
481 | // urgs, sometime has ":" in its error string so that we |
482 | // end up with the error message split between list[3] | |
483 | // and list[4], e.g. the message: | |
5279f566 | 484 | // "failed in buffer_write(fd) (10, ret=-1): backend dpkg-deb ..." |
d6a4afcb | 485 | // concat them again |
5279f566 MV |
486 | if( list[4] != NULL ) |
487 | list[3][strlen(list[3])] = ':'; | |
d6a4afcb | 488 | |
09fa2df2 | 489 | status << "pmerror:" << list[1] |
ff56e980 | 490 | << ":" << (PackagesDone/float(PackagesTotal)*100.0) |
09fa2df2 MV |
491 | << ":" << list[3] |
492 | << endl; | |
493 | if(OutStatusFd > 0) | |
494 | write(OutStatusFd, status.str().c_str(), status.str().size()); | |
887f5036 | 495 | if (Debug == true) |
09fa2df2 | 496 | std::clog << "send: '" << status.str() << "'" << endl; |
f060e833 MV |
497 | pkgFailures++; |
498 | WriteApportReport(list[1], list[3]); | |
09fa2df2 MV |
499 | return; |
500 | } | |
887f5036 | 501 | else if(strncmp(action,"conffile",strlen("conffile")) == 0) |
09fa2df2 MV |
502 | { |
503 | status << "pmconffile:" << list[1] | |
ff56e980 | 504 | << ":" << (PackagesDone/float(PackagesTotal)*100.0) |
09fa2df2 MV |
505 | << ":" << list[3] |
506 | << endl; | |
507 | if(OutStatusFd > 0) | |
508 | write(OutStatusFd, status.str().c_str(), status.str().size()); | |
887f5036 | 509 | if (Debug == true) |
09fa2df2 MV |
510 | std::clog << "send: '" << status.str() << "'" << endl; |
511 | return; | |
512 | } | |
513 | ||
887f5036 | 514 | vector<struct DpkgState> const &states = PackageOps[pkg]; |
09fa2df2 MV |
515 | const char *next_action = NULL; |
516 | if(PackageOpsDone[pkg] < states.size()) | |
517 | next_action = states[PackageOpsDone[pkg]].state; | |
518 | // check if the package moved to the next dpkg state | |
519 | if(next_action && (strcmp(action, next_action) == 0)) | |
520 | { | |
521 | // only read the translation if there is actually a next | |
522 | // action | |
523 | const char *translation = _(states[PackageOpsDone[pkg]].str); | |
524 | char s[200]; | |
525 | snprintf(s, sizeof(s), translation, pkg); | |
526 | ||
527 | // we moved from one dpkg state to a new one, report that | |
528 | PackageOpsDone[pkg]++; | |
ff56e980 | 529 | PackagesDone++; |
09fa2df2 MV |
530 | // build the status str |
531 | status << "pmstatus:" << pkg | |
ff56e980 | 532 | << ":" << (PackagesDone/float(PackagesTotal)*100.0) |
09fa2df2 MV |
533 | << ":" << s |
534 | << endl; | |
535 | if(OutStatusFd > 0) | |
536 | write(OutStatusFd, status.str().c_str(), status.str().size()); | |
887f5036 | 537 | if (Debug == true) |
09fa2df2 MV |
538 | std::clog << "send: '" << status.str() << "'" << endl; |
539 | } | |
887f5036 | 540 | if (Debug == true) |
09fa2df2 MV |
541 | std::clog << "(parsed from dpkg) pkg: " << pkg |
542 | << " action: " << action << endl; | |
6191b008 | 543 | } |
887f5036 | 544 | /*}}}*/ |
eb6f9bac DK |
545 | // DPkgPM::handleDisappearAction /*{{{*/ |
546 | void pkgDPkgPM::handleDisappearAction(string const &pkgname) | |
547 | { | |
548 | // record the package name for display and stuff later | |
549 | disappearedPkgs.insert(pkgname); | |
550 | ||
551 | pkgCache::PkgIterator Pkg = Cache.FindPkg(pkgname); | |
552 | if (unlikely(Pkg.end() == true)) | |
553 | return; | |
554 | // the disappeared package was auto-installed - nothing to do | |
555 | if ((Cache[Pkg].Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) | |
556 | return; | |
75954ae2 | 557 | pkgCache::VerIterator PkgVer = Cache[Pkg].InstVerIter(Cache); |
eb6f9bac DK |
558 | if (unlikely(PkgVer.end() == true)) |
559 | return; | |
560 | /* search in the list of dependencies for (Pre)Depends, | |
561 | check if this dependency has a Replaces on our package | |
562 | and if so transfer the manual installed flag to it */ | |
563 | for (pkgCache::DepIterator Dep = PkgVer.DependsList(); Dep.end() != true; ++Dep) | |
564 | { | |
565 | if (Dep->Type != pkgCache::Dep::Depends && | |
566 | Dep->Type != pkgCache::Dep::PreDepends) | |
567 | continue; | |
568 | pkgCache::PkgIterator Tar = Dep.TargetPkg(); | |
569 | if (unlikely(Tar.end() == true)) | |
570 | continue; | |
571 | // the package is already marked as manual | |
572 | if ((Cache[Tar].Flags & pkgCache::Flag::Auto) != pkgCache::Flag::Auto) | |
573 | continue; | |
75954ae2 DK |
574 | pkgCache::VerIterator TarVer = Cache[Tar].InstVerIter(Cache); |
575 | if (TarVer.end() == true) | |
576 | continue; | |
eb6f9bac DK |
577 | for (pkgCache::DepIterator Rep = TarVer.DependsList(); Rep.end() != true; ++Rep) |
578 | { | |
579 | if (Rep->Type != pkgCache::Dep::Replaces) | |
580 | continue; | |
581 | if (Pkg != Rep.TargetPkg()) | |
582 | continue; | |
583 | // okay, they are strongly connected - transfer manual-bit | |
584 | if (Debug == true) | |
585 | std::clog << "transfer manual-bit from disappeared »" << pkgname << "« to »" << Tar.FullName() << "«" << std::endl; | |
586 | Cache[Tar].Flags &= ~Flag::Auto; | |
587 | break; | |
588 | } | |
589 | } | |
590 | } | |
591 | /*}}}*/ | |
887f5036 | 592 | // DPkgPM::DoDpkgStatusFd /*{{{*/ |
6191b008 MV |
593 | // --------------------------------------------------------------------- |
594 | /* | |
595 | */ | |
09fa2df2 | 596 | void pkgDPkgPM::DoDpkgStatusFd(int statusfd, int OutStatusFd) |
6191b008 MV |
597 | { |
598 | char *p, *q; | |
599 | int len; | |
600 | ||
601 | len=read(statusfd, &dpkgbuf[dpkgbuf_pos], sizeof(dpkgbuf)-dpkgbuf_pos); | |
602 | dpkgbuf_pos += len; | |
603 | if(len <= 0) | |
604 | return; | |
ceabc520 | 605 | |
6191b008 MV |
606 | // process line by line if we have a buffer |
607 | p = q = dpkgbuf; | |
608 | while((q=(char*)memchr(p, '\n', dpkgbuf+dpkgbuf_pos-p)) != NULL) | |
609 | { | |
610 | *q = 0; | |
09fa2df2 | 611 | ProcessDpkgStatusLine(OutStatusFd, p); |
6191b008 MV |
612 | p=q+1; // continue with next line |
613 | } | |
614 | ||
615 | // now move the unprocessed bits (after the final \n that is now a 0x0) | |
616 | // to the start and update dpkgbuf_pos | |
617 | p = (char*)memrchr(dpkgbuf, 0, dpkgbuf_pos); | |
618 | if(p == NULL) | |
619 | return; | |
620 | ||
621 | // we are interessted in the first char *after* 0x0 | |
622 | p++; | |
623 | ||
624 | // move the unprocessed tail to the start and update pos | |
625 | memmove(dpkgbuf, p, p-dpkgbuf); | |
626 | dpkgbuf_pos = dpkgbuf+dpkgbuf_pos-p; | |
627 | } | |
628 | /*}}}*/ | |
d7a4ffd6 | 629 | // DPkgPM::WriteHistoryTag /*{{{*/ |
6cb1060b | 630 | void pkgDPkgPM::WriteHistoryTag(string const &tag, string value) |
d7a4ffd6 | 631 | { |
6cb1060b DK |
632 | size_t const length = value.length(); |
633 | if (length == 0) | |
634 | return; | |
635 | // poor mans rstrip(", ") | |
636 | if (value[length-2] == ',' && value[length-1] == ' ') | |
637 | value.erase(length - 2, 2); | |
638 | fprintf(history_out, "%s: %s\n", tag.c_str(), value.c_str()); | |
d7a4ffd6 | 639 | } /*}}}*/ |
887f5036 | 640 | // DPkgPM::OpenLog /*{{{*/ |
2e1715ea MV |
641 | bool pkgDPkgPM::OpenLog() |
642 | { | |
569cc934 | 643 | string const logdir = _config->FindDir("Dir::Log"); |
7753e468 | 644 | if(CreateAPTDirectoryIfNeeded(logdir, logdir) == false) |
b29c3712 | 645 | // FIXME: use a better string after freeze |
2e1715ea | 646 | return _error->Error(_("Directory '%s' missing"), logdir.c_str()); |
9169c871 MV |
647 | |
648 | // get current time | |
649 | char timestr[200]; | |
569cc934 DK |
650 | time_t const t = time(NULL); |
651 | struct tm const * const tmp = localtime(&t); | |
9169c871 MV |
652 | strftime(timestr, sizeof(timestr), "%F %T", tmp); |
653 | ||
654 | // open terminal log | |
569cc934 | 655 | string const logfile_name = flCombine(logdir, |
2e1715ea MV |
656 | _config->Find("Dir::Log::Terminal")); |
657 | if (!logfile_name.empty()) | |
658 | { | |
659 | term_out = fopen(logfile_name.c_str(),"a"); | |
b39c1859 | 660 | if (term_out == NULL) |
569cc934 | 661 | return _error->WarningE("OpenLog", _("Could not open file '%s'"), logfile_name.c_str()); |
24baab5c | 662 | setvbuf(term_out, NULL, _IONBF, 0); |
fd8bb61d | 663 | SetCloseExec(fileno(term_out), true); |
2e1715ea | 664 | chmod(logfile_name.c_str(), 0600); |
762d7367 | 665 | fprintf(term_out, "\nLog started: %s\n", timestr); |
2e1715ea | 666 | } |
9169c871 | 667 | |
569cc934 DK |
668 | // write your history |
669 | string const history_name = flCombine(logdir, | |
9169c871 MV |
670 | _config->Find("Dir::Log::History")); |
671 | if (!history_name.empty()) | |
672 | { | |
673 | history_out = fopen(history_name.c_str(),"a"); | |
569cc934 DK |
674 | if (history_out == NULL) |
675 | return _error->WarningE("OpenLog", _("Could not open file '%s'"), history_name.c_str()); | |
9169c871 MV |
676 | chmod(history_name.c_str(), 0644); |
677 | fprintf(history_out, "\nStart-Date: %s\n", timestr); | |
678 | string remove, purge, install, upgrade, downgrade; | |
679 | for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++) | |
680 | { | |
d7a4ffd6 | 681 | if (Cache[I].NewInstall()) |
6bf8f0d1 JAK |
682 | { |
683 | install += I.FullName(false) + string(" (") + Cache[I].CandVersion; | |
684 | if (Cache[I].Flags & pkgCache::Flag::Auto) | |
685 | install+= ", automatic"; | |
686 | install += string("), "); | |
687 | } | |
d7a4ffd6 | 688 | else if (Cache[I].Upgrade()) |
621e918e | 689 | upgrade += I.FullName(false) + string(" (") + Cache[I].CurVersion + string(", ") + Cache[I].CandVersion + string("), "); |
9c59aada | 690 | else if (Cache[I].Downgrade()) |
621e918e | 691 | downgrade += I.FullName(false) + string(" (") + Cache[I].CurVersion + string(", ") + Cache[I].CandVersion + string("), "); |
9169c871 MV |
692 | else if (Cache[I].Delete()) |
693 | { | |
694 | if ((Cache[I].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge) | |
621e918e | 695 | purge += I.FullName(false) + string(" (") + Cache[I].CurVersion + string("), "); |
9169c871 | 696 | else |
621e918e | 697 | remove += I.FullName(false) + string(" (") + Cache[I].CurVersion + string("), "); |
9169c871 | 698 | } |
9169c871 | 699 | } |
2bb25574 DK |
700 | if (_config->Exists("Commandline::AsString") == true) |
701 | WriteHistoryTag("Commandline", _config->Find("Commandline::AsString")); | |
d7a4ffd6 MV |
702 | WriteHistoryTag("Install", install); |
703 | WriteHistoryTag("Upgrade", upgrade); | |
704 | WriteHistoryTag("Downgrade",downgrade); | |
705 | WriteHistoryTag("Remove",remove); | |
706 | WriteHistoryTag("Purge",purge); | |
06d5520f | 707 | fflush(history_out); |
9169c871 MV |
708 | } |
709 | ||
2e1715ea MV |
710 | return true; |
711 | } | |
887f5036 DK |
712 | /*}}}*/ |
713 | // DPkg::CloseLog /*{{{*/ | |
2e1715ea MV |
714 | bool pkgDPkgPM::CloseLog() |
715 | { | |
9169c871 MV |
716 | char timestr[200]; |
717 | time_t t = time(NULL); | |
718 | struct tm *tmp = localtime(&t); | |
719 | strftime(timestr, sizeof(timestr), "%F %T", tmp); | |
720 | ||
2e1715ea MV |
721 | if(term_out) |
722 | { | |
8398ac36 | 723 | fprintf(term_out, "Log ended: "); |
9169c871 | 724 | fprintf(term_out, "%s", timestr); |
2e1715ea MV |
725 | fprintf(term_out, "\n"); |
726 | fclose(term_out); | |
727 | } | |
728 | term_out = NULL; | |
9169c871 MV |
729 | |
730 | if(history_out) | |
731 | { | |
6cb1060b DK |
732 | if (disappearedPkgs.empty() == false) |
733 | { | |
734 | string disappear; | |
735 | for (std::set<std::string>::const_iterator d = disappearedPkgs.begin(); | |
736 | d != disappearedPkgs.end(); ++d) | |
737 | { | |
738 | pkgCache::PkgIterator P = Cache.FindPkg(*d); | |
739 | disappear.append(*d); | |
740 | if (P.end() == true) | |
741 | disappear.append(", "); | |
742 | else | |
743 | disappear.append(" (").append(Cache[P].CurVersion).append("), "); | |
744 | } | |
745 | WriteHistoryTag("Disappeared", disappear); | |
746 | } | |
747 | if (dpkg_error.empty() == false) | |
9169c871 MV |
748 | fprintf(history_out, "Error: %s\n", dpkg_error.c_str()); |
749 | fprintf(history_out, "End-Date: %s\n", timestr); | |
750 | fclose(history_out); | |
751 | } | |
5e8b2b74 | 752 | history_out = NULL; |
9169c871 | 753 | |
2e1715ea MV |
754 | return true; |
755 | } | |
887f5036 | 756 | /*}}}*/ |
919e5852 OS |
757 | /*{{{*/ |
758 | // This implements a racy version of pselect for those architectures | |
759 | // that don't have a working implementation. | |
760 | // FIXME: Probably can be removed on Lenny+1 | |
761 | static int racy_pselect(int nfds, fd_set *readfds, fd_set *writefds, | |
762 | fd_set *exceptfds, const struct timespec *timeout, | |
763 | const sigset_t *sigmask) | |
764 | { | |
765 | sigset_t origmask; | |
766 | struct timeval tv; | |
767 | int retval; | |
768 | ||
f6b37f38 OS |
769 | tv.tv_sec = timeout->tv_sec; |
770 | tv.tv_usec = timeout->tv_nsec/1000; | |
919e5852 | 771 | |
f6b37f38 | 772 | sigprocmask(SIG_SETMASK, sigmask, &origmask); |
919e5852 OS |
773 | retval = select(nfds, readfds, writefds, exceptfds, &tv); |
774 | sigprocmask(SIG_SETMASK, &origmask, 0); | |
775 | return retval; | |
776 | } | |
777 | /*}}}*/ | |
03e39e59 AL |
778 | // DPkgPM::Go - Run the sequence /*{{{*/ |
779 | // --------------------------------------------------------------------- | |
75ef8f14 MV |
780 | /* This globs the operations and calls dpkg |
781 | * | |
782 | * If it is called with "OutStatusFd" set to a valid file descriptor | |
783 | * apt will report the install progress over this fd. It maps the | |
784 | * dpkg states a package goes through to human readable (and i10n-able) | |
785 | * names and calculates a percentage for each step. | |
786 | */ | |
787 | bool pkgDPkgPM::Go(int OutStatusFd) | |
03e39e59 | 788 | { |
17745b02 MV |
789 | fd_set rfds; |
790 | struct timespec tv; | |
791 | sigset_t sigmask; | |
792 | sigset_t original_sigmask; | |
793 | ||
887f5036 DK |
794 | unsigned int const MaxArgs = _config->FindI("Dpkg::MaxArgs",8*1024); |
795 | unsigned int const MaxArgBytes = _config->FindI("Dpkg::MaxArgBytes",32*1024); | |
5e312de7 | 796 | bool const NoTriggers = _config->FindB("DPkg::NoTriggers", false); |
aff4e2f1 | 797 | |
6dd55be7 AL |
798 | if (RunScripts("DPkg::Pre-Invoke") == false) |
799 | return false; | |
db0c350f AL |
800 | |
801 | if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false) | |
802 | return false; | |
fc2d32c0 | 803 | |
3e9c4f70 DK |
804 | // support subpressing of triggers processing for special |
805 | // cases like d-i that runs the triggers handling manually | |
5e312de7 | 806 | bool const SmartConf = (_config->Find("PackageManager::Configure", "all") != "all"); |
5c23dbcc | 807 | bool const TriggersPending = _config->FindB("DPkg::TriggersPending", false); |
5e312de7 DK |
808 | if (_config->FindB("DPkg::ConfigurePending", SmartConf) == true) |
809 | List.push_back(Item(Item::ConfigurePending, PkgIterator())); | |
3e9c4f70 | 810 | |
75ef8f14 MV |
811 | // map the dpkg states to the operations that are performed |
812 | // (this is sorted in the same way as Item::Ops) | |
9d06bc80 | 813 | static const struct DpkgState DpkgStatesOpMap[][7] = { |
75ef8f14 MV |
814 | // Install operation |
815 | { | |
21e1008e MV |
816 | {"half-installed", N_("Preparing %s")}, |
817 | {"unpacked", N_("Unpacking %s") }, | |
75ef8f14 MV |
818 | {NULL, NULL} |
819 | }, | |
820 | // Configure operation | |
821 | { | |
21e1008e MV |
822 | {"unpacked",N_("Preparing to configure %s") }, |
823 | {"half-configured", N_("Configuring %s") }, | |
824 | { "installed", N_("Installed %s")}, | |
75ef8f14 MV |
825 | {NULL, NULL} |
826 | }, | |
827 | // Remove operation | |
828 | { | |
21e1008e MV |
829 | {"half-configured", N_("Preparing for removal of %s")}, |
830 | {"half-installed", N_("Removing %s")}, | |
831 | {"config-files", N_("Removed %s")}, | |
75ef8f14 MV |
832 | {NULL, NULL} |
833 | }, | |
834 | // Purge operation | |
835 | { | |
21e1008e MV |
836 | {"config-files", N_("Preparing to completely remove %s")}, |
837 | {"not-installed", N_("Completely removed %s")}, | |
75ef8f14 MV |
838 | {NULL, NULL} |
839 | }, | |
840 | }; | |
db0c350f | 841 | |
75ef8f14 MV |
842 | // init the PackageOps map, go over the list of packages that |
843 | // that will be [installed|configured|removed|purged] and add | |
844 | // them to the PackageOps map (the dpkg states it goes through) | |
845 | // and the PackageOpsTranslations (human readable strings) | |
887f5036 | 846 | for (vector<Item>::const_iterator I = List.begin(); I != List.end();I++) |
75ef8f14 | 847 | { |
3e9c4f70 DK |
848 | if((*I).Pkg.end() == true) |
849 | continue; | |
850 | ||
887f5036 | 851 | string const name = (*I).Pkg.Name(); |
75ef8f14 MV |
852 | PackageOpsDone[name] = 0; |
853 | for(int i=0; (DpkgStatesOpMap[(*I).Op][i]).state != NULL; i++) | |
854 | { | |
855 | PackageOps[name].push_back(DpkgStatesOpMap[(*I).Op][i]); | |
ff56e980 | 856 | PackagesTotal++; |
75ef8f14 | 857 | } |
887f5036 | 858 | } |
75ef8f14 | 859 | |
9983591d OS |
860 | stdin_is_dev_null = false; |
861 | ||
ff56e980 | 862 | // create log |
2e1715ea | 863 | OpenLog(); |
ff56e980 | 864 | |
75ef8f14 | 865 | // this loop is runs once per operation |
887f5036 | 866 | for (vector<Item>::const_iterator I = List.begin(); I != List.end();) |
03e39e59 | 867 | { |
5c23dbcc | 868 | // Do all actions with the same Op in one run |
887f5036 | 869 | vector<Item>::const_iterator J = I; |
5c23dbcc DK |
870 | if (TriggersPending == true) |
871 | for (; J != List.end(); J++) | |
872 | { | |
873 | if (J->Op == I->Op) | |
874 | continue; | |
875 | if (J->Op != Item::TriggersPending) | |
876 | break; | |
877 | vector<Item>::const_iterator T = J + 1; | |
878 | if (T != List.end() && T->Op == I->Op) | |
879 | continue; | |
880 | break; | |
881 | } | |
882 | else | |
883 | for (; J != List.end() && J->Op == I->Op; J++) | |
884 | /* nothing */; | |
30e1eab5 | 885 | |
03e39e59 | 886 | // Generate the argument list |
aff4e2f1 | 887 | const char *Args[MaxArgs + 50]; |
599d6ad5 MV |
888 | |
889 | // Now check if we are within the MaxArgs limit | |
890 | // | |
891 | // this code below is problematic, because it may happen that | |
892 | // the argument list is split in a way that A depends on B | |
893 | // and they are in the same "--configure A B" run | |
894 | // - with the split they may now be configured in different | |
895 | // runs | |
aff4e2f1 AL |
896 | if (J - I > (signed)MaxArgs) |
897 | J = I + MaxArgs; | |
03e39e59 | 898 | |
30e1eab5 AL |
899 | unsigned int n = 0; |
900 | unsigned long Size = 0; | |
887f5036 | 901 | string const Tmp = _config->Find("Dir::Bin::dpkg","dpkg"); |
50914ffa | 902 | Args[n++] = Tmp.c_str(); |
30e1eab5 | 903 | Size += strlen(Args[n-1]); |
03e39e59 | 904 | |
6dd55be7 AL |
905 | // Stick in any custom dpkg options |
906 | Configuration::Item const *Opts = _config->Tree("DPkg::Options"); | |
907 | if (Opts != 0) | |
908 | { | |
909 | Opts = Opts->Child; | |
910 | for (; Opts != 0; Opts = Opts->Next) | |
911 | { | |
912 | if (Opts->Value.empty() == true) | |
913 | continue; | |
914 | Args[n++] = Opts->Value.c_str(); | |
915 | Size += Opts->Value.length(); | |
916 | } | |
917 | } | |
918 | ||
007dc9e0 | 919 | char status_fd_buf[20]; |
75ef8f14 MV |
920 | int fd[2]; |
921 | pipe(fd); | |
922 | ||
923 | Args[n++] = "--status-fd"; | |
924 | Size += strlen(Args[n-1]); | |
925 | snprintf(status_fd_buf,sizeof(status_fd_buf),"%i", fd[1]); | |
926 | Args[n++] = status_fd_buf; | |
927 | Size += strlen(Args[n-1]); | |
007dc9e0 | 928 | |
03e39e59 AL |
929 | switch (I->Op) |
930 | { | |
931 | case Item::Remove: | |
932 | Args[n++] = "--force-depends"; | |
30e1eab5 | 933 | Size += strlen(Args[n-1]); |
03e39e59 | 934 | Args[n++] = "--force-remove-essential"; |
30e1eab5 | 935 | Size += strlen(Args[n-1]); |
03e39e59 | 936 | Args[n++] = "--remove"; |
30e1eab5 | 937 | Size += strlen(Args[n-1]); |
03e39e59 AL |
938 | break; |
939 | ||
fc4b5c9f AL |
940 | case Item::Purge: |
941 | Args[n++] = "--force-depends"; | |
942 | Size += strlen(Args[n-1]); | |
943 | Args[n++] = "--force-remove-essential"; | |
944 | Size += strlen(Args[n-1]); | |
945 | Args[n++] = "--purge"; | |
946 | Size += strlen(Args[n-1]); | |
947 | break; | |
948 | ||
03e39e59 AL |
949 | case Item::Configure: |
950 | Args[n++] = "--configure"; | |
30e1eab5 | 951 | Size += strlen(Args[n-1]); |
03e39e59 | 952 | break; |
3e9c4f70 DK |
953 | |
954 | case Item::ConfigurePending: | |
955 | Args[n++] = "--configure"; | |
956 | Size += strlen(Args[n-1]); | |
957 | Args[n++] = "--pending"; | |
958 | Size += strlen(Args[n-1]); | |
959 | break; | |
960 | ||
5e312de7 DK |
961 | case Item::TriggersPending: |
962 | Args[n++] = "--triggers-only"; | |
963 | Size += strlen(Args[n-1]); | |
964 | Args[n++] = "--pending"; | |
965 | Size += strlen(Args[n-1]); | |
966 | break; | |
967 | ||
03e39e59 AL |
968 | case Item::Install: |
969 | Args[n++] = "--unpack"; | |
30e1eab5 | 970 | Size += strlen(Args[n-1]); |
857a1d4a MV |
971 | Args[n++] = "--auto-deconfigure"; |
972 | Size += strlen(Args[n-1]); | |
03e39e59 AL |
973 | break; |
974 | } | |
3e9c4f70 | 975 | |
5e312de7 | 976 | if (NoTriggers == true && I->Op != Item::TriggersPending && |
d5081aee | 977 | I->Op != Item::ConfigurePending) |
3e9c4f70 DK |
978 | { |
979 | Args[n++] = "--no-triggers"; | |
980 | Size += strlen(Args[n-1]); | |
981 | } | |
982 | ||
03e39e59 AL |
983 | // Write in the file or package names |
984 | if (I->Op == Item::Install) | |
30e1eab5 | 985 | { |
aff4e2f1 | 986 | for (;I != J && Size < MaxArgBytes; I++) |
30e1eab5 | 987 | { |
cf544e14 AL |
988 | if (I->File[0] != '/') |
989 | return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str()); | |
03e39e59 | 990 | Args[n++] = I->File.c_str(); |
30e1eab5 AL |
991 | Size += strlen(Args[n-1]); |
992 | } | |
993 | } | |
03e39e59 | 994 | else |
30e1eab5 | 995 | { |
aff4e2f1 | 996 | for (;I != J && Size < MaxArgBytes; I++) |
30e1eab5 | 997 | { |
3e9c4f70 DK |
998 | if((*I).Pkg.end() == true) |
999 | continue; | |
642ebc1a DK |
1000 | if (I->Op == Item::Configure && disappearedPkgs.find(I->Pkg.Name()) != disappearedPkgs.end()) |
1001 | continue; | |
03e39e59 | 1002 | Args[n++] = I->Pkg.Name(); |
30e1eab5 AL |
1003 | Size += strlen(Args[n-1]); |
1004 | } | |
1005 | } | |
03e39e59 | 1006 | Args[n] = 0; |
30e1eab5 AL |
1007 | J = I; |
1008 | ||
1009 | if (_config->FindB("Debug::pkgDPkgPM",false) == true) | |
1010 | { | |
1011 | for (unsigned int k = 0; k != n; k++) | |
1012 | clog << Args[k] << ' '; | |
1013 | clog << endl; | |
1014 | continue; | |
1015 | } | |
03e39e59 | 1016 | |
03e39e59 AL |
1017 | cout << flush; |
1018 | clog << flush; | |
1019 | cerr << flush; | |
1020 | ||
1021 | /* Mask off sig int/quit. We do this because dpkg also does when | |
1022 | it forks scripts. What happens is that when you hit ctrl-c it sends | |
1023 | it to all processes in the group. Since dpkg ignores the signal | |
1024 | it doesn't die but we do! So we must also ignore it */ | |
7f9a6360 AL |
1025 | sighandler_t old_SIGQUIT = signal(SIGQUIT,SIG_IGN); |
1026 | sighandler_t old_SIGINT = signal(SIGINT,SIG_IGN); | |
d8cb4aa4 | 1027 | |
73e598c3 MV |
1028 | // ignore SIGHUP as well (debian #463030) |
1029 | sighandler_t old_SIGHUP = signal(SIGHUP,SIG_IGN); | |
1030 | ||
d8cb4aa4 MV |
1031 | struct termios tt; |
1032 | struct winsize win; | |
4e550036 MV |
1033 | int master = -1; |
1034 | int slave = -1; | |
d8cb4aa4 | 1035 | |
4e550036 MV |
1036 | // if tcgetattr does not return zero there was a error |
1037 | // and we do not do any pty magic | |
1038 | if (tcgetattr(0, &tt) == 0) | |
090c6566 | 1039 | { |
4e550036 MV |
1040 | ioctl(0, TIOCGWINSZ, (char *)&win); |
1041 | if (openpty(&master, &slave, NULL, &tt, &win) < 0) | |
1042 | { | |
1043 | const char *s = _("Can not write log, openpty() " | |
1044 | "failed (/dev/pts not mounted?)\n"); | |
1045 | fprintf(stderr, "%s",s); | |
6847d275 MV |
1046 | if(term_out) |
1047 | fprintf(term_out, "%s",s); | |
4e550036 MV |
1048 | master = slave = -1; |
1049 | } else { | |
1050 | struct termios rtt; | |
1051 | rtt = tt; | |
1052 | cfmakeraw(&rtt); | |
1053 | rtt.c_lflag &= ~ECHO; | |
4d7ac88c | 1054 | rtt.c_lflag |= ISIG; |
4e550036 MV |
1055 | // block SIGTTOU during tcsetattr to prevent a hang if |
1056 | // the process is a member of the background process group | |
1057 | // http://www.opengroup.org/onlinepubs/000095399/functions/tcsetattr.html | |
1058 | sigemptyset(&sigmask); | |
1059 | sigaddset(&sigmask, SIGTTOU); | |
1060 | sigprocmask(SIG_BLOCK,&sigmask, &original_sigmask); | |
1061 | tcsetattr(0, TCSAFLUSH, &rtt); | |
1062 | sigprocmask(SIG_SETMASK, &original_sigmask, 0); | |
1063 | } | |
d8cb4aa4 MV |
1064 | } |
1065 | ||
75ef8f14 | 1066 | // Fork dpkg |
007dc9e0 | 1067 | pid_t Child; |
75ef8f14 | 1068 | _config->Set("APT::Keep-Fds::",fd[1]); |
ccd8e28f MV |
1069 | // send status information that we are about to fork dpkg |
1070 | if(OutStatusFd > 0) { | |
1071 | ostringstream status; | |
1072 | status << "pmstatus:dpkg-exec:" | |
1073 | << (PackagesDone/float(PackagesTotal)*100.0) | |
1074 | << ":" << _("Running dpkg") | |
1075 | << endl; | |
1076 | write(OutStatusFd, status.str().c_str(), status.str().size()); | |
1077 | } | |
75ef8f14 | 1078 | Child = ExecFork(); |
6dd55be7 | 1079 | |
03e39e59 AL |
1080 | // This is the child |
1081 | if (Child == 0) | |
1082 | { | |
a4cf3665 MV |
1083 | if(slave >= 0 && master >= 0) |
1084 | { | |
1085 | setsid(); | |
1086 | ioctl(slave, TIOCSCTTY, 0); | |
1087 | close(master); | |
1088 | dup2(slave, 0); | |
1089 | dup2(slave, 1); | |
1090 | dup2(slave, 2); | |
1091 | close(slave); | |
1092 | } | |
75ef8f14 | 1093 | close(fd[0]); // close the read end of the pipe |
d8cb4aa4 | 1094 | |
4b7cfe96 MV |
1095 | if (_config->FindDir("DPkg::Chroot-Directory","/") != "/") |
1096 | { | |
1097 | std::cerr << "Chrooting into " | |
1098 | << _config->FindDir("DPkg::Chroot-Directory") | |
1099 | << std::endl; | |
1100 | if (chroot(_config->FindDir("DPkg::Chroot-Directory","/").c_str()) != 0) | |
1101 | _exit(100); | |
1102 | } | |
1103 | ||
cf544e14 | 1104 | if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0) |
0dbb95d8 | 1105 | _exit(100); |
03e39e59 | 1106 | |
421ff807 | 1107 | if (_config->FindB("DPkg::FlushSTDIN",true) == true && isatty(STDIN_FILENO)) |
8b5fe26c AL |
1108 | { |
1109 | int Flags,dummy; | |
1110 | if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0) | |
1111 | _exit(100); | |
1112 | ||
1113 | // Discard everything in stdin before forking dpkg | |
1114 | if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0) | |
1115 | _exit(100); | |
1116 | ||
1117 | while (read(STDIN_FILENO,&dummy,1) == 1); | |
1118 | ||
1119 | if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0) | |
1120 | _exit(100); | |
1121 | } | |
d8cb4aa4 | 1122 | |
03e39e59 AL |
1123 | /* No Job Control Stop Env is a magic dpkg var that prevents it |
1124 | from using sigstop */ | |
71afbdb5 | 1125 | putenv((char *)"DPKG_NO_TSTP=yes"); |
d568ed2d | 1126 | execvp(Args[0],(char **)Args); |
03e39e59 | 1127 | cerr << "Could not exec dpkg!" << endl; |
0dbb95d8 | 1128 | _exit(100); |
03e39e59 AL |
1129 | } |
1130 | ||
cebe0287 MV |
1131 | // apply ionice |
1132 | if (_config->FindB("DPkg::UseIoNice", false) == true) | |
1133 | ionice(Child); | |
1134 | ||
75ef8f14 MV |
1135 | // clear the Keep-Fd again |
1136 | _config->Clear("APT::Keep-Fds",fd[1]); | |
1137 | ||
03e39e59 AL |
1138 | // Wait for dpkg |
1139 | int Status = 0; | |
75ef8f14 MV |
1140 | |
1141 | // we read from dpkg here | |
887f5036 | 1142 | int const _dpkgin = fd[0]; |
75ef8f14 MV |
1143 | close(fd[1]); // close the write end of the pipe |
1144 | ||
a4cf3665 MV |
1145 | if(slave > 0) |
1146 | close(slave); | |
6191b008 | 1147 | |
97efd303 | 1148 | // setups fds |
7052511e MV |
1149 | sigemptyset(&sigmask); |
1150 | sigprocmask(SIG_BLOCK,&sigmask,&original_sigmask); | |
1151 | ||
887f5036 DK |
1152 | // the result of the waitpid call |
1153 | int res; | |
090c6566 | 1154 | int select_ret; |
75ef8f14 MV |
1155 | while ((res=waitpid(Child,&Status, WNOHANG)) != Child) { |
1156 | if(res < 0) { | |
1157 | // FIXME: move this to a function or something, looks ugly here | |
1158 | // error handling, waitpid returned -1 | |
1159 | if (errno == EINTR) | |
1160 | continue; | |
1161 | RunScripts("DPkg::Post-Invoke"); | |
1162 | ||
1163 | // Restore sig int/quit | |
1164 | signal(SIGQUIT,old_SIGQUIT); | |
1165 | signal(SIGINT,old_SIGINT); | |
e306ec47 | 1166 | signal(SIGHUP,old_SIGHUP); |
75ef8f14 MV |
1167 | return _error->Errno("waitpid","Couldn't wait for subprocess"); |
1168 | } | |
d8cb4aa4 MV |
1169 | |
1170 | // wait for input or output here | |
955a6ddb | 1171 | FD_ZERO(&rfds); |
897165af | 1172 | if (master >= 0 && !stdin_is_dev_null) |
9983591d | 1173 | FD_SET(0, &rfds); |
955a6ddb | 1174 | FD_SET(_dpkgin, &rfds); |
a4cf3665 MV |
1175 | if(master >= 0) |
1176 | FD_SET(master, &rfds); | |
090c6566 | 1177 | tv.tv_sec = 1; |
7052511e MV |
1178 | tv.tv_nsec = 0; |
1179 | select_ret = pselect(max(master, _dpkgin)+1, &rfds, NULL, NULL, | |
1180 | &tv, &original_sigmask); | |
919e5852 OS |
1181 | if (select_ret < 0 && (errno == EINVAL || errno == ENOSYS)) |
1182 | select_ret = racy_pselect(max(master, _dpkgin)+1, &rfds, NULL, | |
1183 | NULL, &tv, &original_sigmask); | |
da50ba30 MV |
1184 | if (select_ret == 0) |
1185 | continue; | |
1186 | else if (select_ret < 0 && errno == EINTR) | |
1187 | continue; | |
1188 | else if (select_ret < 0) | |
1189 | { | |
1190 | perror("select() returned error"); | |
1191 | continue; | |
1192 | } | |
1193 | ||
a4cf3665 | 1194 | if(master >= 0 && FD_ISSET(master, &rfds)) |
1ba38171 | 1195 | DoTerminalPty(master); |
a4cf3665 | 1196 | if(master >= 0 && FD_ISSET(0, &rfds)) |
955a6ddb | 1197 | DoStdin(master); |
955a6ddb | 1198 | if(FD_ISSET(_dpkgin, &rfds)) |
09fa2df2 | 1199 | DoDpkgStatusFd(_dpkgin, OutStatusFd); |
03e39e59 | 1200 | } |
75ef8f14 | 1201 | close(_dpkgin); |
03e39e59 AL |
1202 | |
1203 | // Restore sig int/quit | |
7f9a6360 AL |
1204 | signal(SIGQUIT,old_SIGQUIT); |
1205 | signal(SIGINT,old_SIGINT); | |
d9ec0fac | 1206 | signal(SIGHUP,old_SIGHUP); |
d8cb4aa4 | 1207 | |
477b5d6c MV |
1208 | if(master >= 0) |
1209 | { | |
a4cf3665 | 1210 | tcsetattr(0, TCSAFLUSH, &tt); |
477b5d6c MV |
1211 | close(master); |
1212 | } | |
6dd55be7 AL |
1213 | |
1214 | // Check for an error code. | |
1215 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
1216 | { | |
c70496f9 MV |
1217 | // if it was set to "keep-dpkg-runing" then we won't return |
1218 | // here but keep the loop going and just report it as a error | |
1219 | // for later | |
887f5036 | 1220 | bool const stopOnError = _config->FindB("Dpkg::StopOnError",true); |
f956efb4 | 1221 | |
c70496f9 MV |
1222 | if(stopOnError) |
1223 | RunScripts("DPkg::Post-Invoke"); | |
1224 | ||
1225 | if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV) | |
9169c871 | 1226 | strprintf(dpkg_error, "Sub-process %s received a segmentation fault.",Args[0]); |
c70496f9 | 1227 | else if (WIFEXITED(Status) != 0) |
9169c871 | 1228 | strprintf(dpkg_error, "Sub-process %s returned an error code (%u)",Args[0],WEXITSTATUS(Status)); |
c70496f9 | 1229 | else |
9169c871 MV |
1230 | strprintf(dpkg_error, "Sub-process %s exited unexpectedly",Args[0]); |
1231 | ||
1232 | if(dpkg_error.size() > 0) | |
1233 | _error->Error(dpkg_error.c_str()); | |
c70496f9 | 1234 | |
ff56e980 MV |
1235 | if(stopOnError) |
1236 | { | |
2e1715ea | 1237 | CloseLog(); |
c70496f9 | 1238 | return false; |
ff56e980 | 1239 | } |
6dd55be7 | 1240 | } |
03e39e59 | 1241 | } |
2e1715ea | 1242 | CloseLog(); |
6dd55be7 AL |
1243 | |
1244 | if (RunScripts("DPkg::Post-Invoke") == false) | |
1245 | return false; | |
b462d75a MV |
1246 | |
1247 | Cache.writeStateFile(NULL); | |
03e39e59 AL |
1248 | return true; |
1249 | } | |
1250 | /*}}}*/ | |
281daf46 AL |
1251 | // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/ |
1252 | // --------------------------------------------------------------------- | |
1253 | /* */ | |
1254 | void pkgDPkgPM::Reset() | |
1255 | { | |
1256 | List.erase(List.begin(),List.end()); | |
1257 | } | |
1258 | /*}}}*/ | |
5e457a93 MV |
1259 | // pkgDpkgPM::WriteApportReport - write out error report pkg failure /*{{{*/ |
1260 | // --------------------------------------------------------------------- | |
1261 | /* */ | |
1262 | void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg) | |
1263 | { | |
1264 | string pkgname, reportfile, srcpkgname, pkgver, arch; | |
1265 | string::size_type pos; | |
1266 | FILE *report; | |
1267 | ||
23c5897c | 1268 | if (_config->FindB("Dpkg::ApportFailureReport", false) == false) |
ff38d63b MV |
1269 | { |
1270 | std::clog << "configured to not write apport reports" << std::endl; | |
5e457a93 | 1271 | return; |
ff38d63b | 1272 | } |
5e457a93 | 1273 | |
d6a4afcb | 1274 | // only report the first errors |
5273f1bf | 1275 | if(pkgFailures > _config->FindI("APT::Apport::MaxReports", 3)) |
ff38d63b MV |
1276 | { |
1277 | std::clog << _("No apport report written because MaxReports is reached already") << std::endl; | |
5e457a93 | 1278 | return; |
ff38d63b | 1279 | } |
5e457a93 | 1280 | |
d6a4afcb MV |
1281 | // check if its not a follow up error |
1282 | const char *needle = dgettext("dpkg", "dependency problems - leaving unconfigured"); | |
1283 | if(strstr(errormsg, needle) != NULL) { | |
1284 | std::clog << _("No apport report written because the error message indicates its a followup error from a previous failure.") << std::endl; | |
1285 | return; | |
1286 | } | |
1287 | ||
2f0d5dea MV |
1288 | // do not report disk-full failures |
1289 | if(strstr(errormsg, strerror(ENOSPC)) != NULL) { | |
1290 | std::clog << _("No apport report written because the error message indicates a disk full error") << std::endl; | |
1291 | return; | |
1292 | } | |
1293 | ||
3024a85e MV |
1294 | // do not report out-of-memory failures |
1295 | if(strstr(errormsg, strerror(ENOMEM)) != NULL) { | |
1296 | std::clog << _("No apport report written because the error message indicates a out of memory error") << std::endl; | |
1297 | return; | |
1298 | } | |
1299 | ||
076c46e5 MZ |
1300 | // do not report dpkg I/O errors |
1301 | // XXX - this message is localized, but this only matches the English version. This is better than nothing. | |
1302 | if(strstr(errormsg, "short read in buffer_copy (")) { | |
1303 | std::clog << _("No apport report written because the error message indicates a dpkg I/O error") << std::endl; | |
1304 | return; | |
1305 | } | |
1306 | ||
5e457a93 MV |
1307 | // get the pkgname and reportfile |
1308 | pkgname = flNotDir(pkgpath); | |
25ffa4e8 | 1309 | pos = pkgname.find('_'); |
5e457a93 | 1310 | if(pos != string::npos) |
25ffa4e8 | 1311 | pkgname = pkgname.substr(0, pos); |
5e457a93 MV |
1312 | |
1313 | // find the package versin and source package name | |
1314 | pkgCache::PkgIterator Pkg = Cache.FindPkg(pkgname); | |
1315 | if (Pkg.end() == true) | |
1316 | return; | |
1317 | pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg); | |
5e457a93 MV |
1318 | if (Ver.end() == true) |
1319 | return; | |
986d97bb | 1320 | pkgver = Ver.VerStr() == NULL ? "unknown" : Ver.VerStr(); |
5e457a93 MV |
1321 | pkgRecords Recs(Cache); |
1322 | pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList()); | |
1323 | srcpkgname = Parse.SourcePkg(); | |
1324 | if(srcpkgname.empty()) | |
1325 | srcpkgname = pkgname; | |
1326 | ||
1327 | // if the file exists already, we check: | |
1328 | // - if it was reported already (touched by apport). | |
1329 | // If not, we do nothing, otherwise | |
1330 | // we overwrite it. This is the same behaviour as apport | |
1331 | // - if we have a report with the same pkgversion already | |
1332 | // then we skip it | |
1333 | reportfile = flCombine("/var/crash",pkgname+".0.crash"); | |
1334 | if(FileExists(reportfile)) | |
1335 | { | |
1336 | struct stat buf; | |
1337 | char strbuf[255]; | |
1338 | ||
1339 | // check atime/mtime | |
1340 | stat(reportfile.c_str(), &buf); | |
1341 | if(buf.st_mtime > buf.st_atime) | |
1342 | return; | |
1343 | ||
1344 | // check if the existing report is the same version | |
1345 | report = fopen(reportfile.c_str(),"r"); | |
1346 | while(fgets(strbuf, sizeof(strbuf), report) != NULL) | |
1347 | { | |
1348 | if(strstr(strbuf,"Package:") == strbuf) | |
1349 | { | |
1350 | char pkgname[255], version[255]; | |
1351 | if(sscanf(strbuf, "Package: %s %s", pkgname, version) == 2) | |
1352 | if(strcmp(pkgver.c_str(), version) == 0) | |
1353 | { | |
1354 | fclose(report); | |
1355 | return; | |
1356 | } | |
1357 | } | |
1358 | } | |
1359 | fclose(report); | |
1360 | } | |
1361 | ||
1362 | // now write the report | |
1363 | arch = _config->Find("APT::Architecture"); | |
1364 | report = fopen(reportfile.c_str(),"w"); | |
1365 | if(report == NULL) | |
1366 | return; | |
1367 | if(_config->FindB("DPkgPM::InitialReportOnly",false) == true) | |
1368 | chmod(reportfile.c_str(), 0); | |
1369 | else | |
1370 | chmod(reportfile.c_str(), 0600); | |
1371 | fprintf(report, "ProblemType: Package\n"); | |
1372 | fprintf(report, "Architecture: %s\n", arch.c_str()); | |
1373 | time_t now = time(NULL); | |
1374 | fprintf(report, "Date: %s" , ctime(&now)); | |
1375 | fprintf(report, "Package: %s %s\n", pkgname.c_str(), pkgver.c_str()); | |
1376 | fprintf(report, "SourcePackage: %s\n", srcpkgname.c_str()); | |
1377 | fprintf(report, "ErrorMessage:\n %s\n", errormsg); | |
8ecd1fed MV |
1378 | |
1379 | // ensure that the log is flushed | |
1380 | if(term_out) | |
1381 | fflush(term_out); | |
1382 | ||
1383 | // attach terminal log it if we have it | |
1384 | string logfile_name = _config->FindFile("Dir::Log::Terminal"); | |
1385 | if (!logfile_name.empty()) | |
1386 | { | |
1387 | FILE *log = NULL; | |
1388 | char buf[1024]; | |
1389 | ||
1390 | fprintf(report, "DpkgTerminalLog:\n"); | |
1391 | log = fopen(logfile_name.c_str(),"r"); | |
1392 | if(log != NULL) | |
1393 | { | |
1394 | while( fgets(buf, sizeof(buf), log) != NULL) | |
1395 | fprintf(report, " %s", buf); | |
1396 | fclose(log); | |
1397 | } | |
1398 | } | |
76dbdfc7 | 1399 | |
5c8a2aa8 MV |
1400 | // log the ordering |
1401 | const char *ops_str[] = {"Install", "Configure","Remove","Purge"}; | |
1402 | fprintf(report, "AptOrdering:\n"); | |
1403 | for (vector<Item>::iterator I = List.begin(); I != List.end(); I++) | |
1404 | fprintf(report, " %s: %s\n", (*I).Pkg.Name(), ops_str[(*I).Op]); | |
1405 | ||
76dbdfc7 MV |
1406 | // attach dmesg log (to learn about segfaults) |
1407 | if (FileExists("/bin/dmesg")) | |
1408 | { | |
1409 | FILE *log = NULL; | |
1410 | char buf[1024]; | |
1411 | ||
1412 | fprintf(report, "Dmesg:\n"); | |
1413 | log = popen("/bin/dmesg","r"); | |
1414 | if(log != NULL) | |
1415 | { | |
1416 | while( fgets(buf, sizeof(buf), log) != NULL) | |
1417 | fprintf(report, " %s", buf); | |
1418 | fclose(log); | |
1419 | } | |
1420 | } | |
2183a086 MV |
1421 | |
1422 | // attach df -l log (to learn about filesystem status) | |
1423 | if (FileExists("/bin/df")) | |
1424 | { | |
1425 | FILE *log = NULL; | |
1426 | char buf[1024]; | |
1427 | ||
1428 | fprintf(report, "Df:\n"); | |
1429 | log = popen("/bin/df -l","r"); | |
1430 | if(log != NULL) | |
1431 | { | |
1432 | while( fgets(buf, sizeof(buf), log) != NULL) | |
1433 | fprintf(report, " %s", buf); | |
1434 | fclose(log); | |
1435 | } | |
1436 | } | |
1437 | ||
5e457a93 | 1438 | fclose(report); |
76dbdfc7 | 1439 | |
5e457a93 MV |
1440 | } |
1441 | /*}}}*/ |