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