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