]>
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 /*{{{*/ | |
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "apt-pkg/dpkgpm.h" | |
13 | #endif | |
14 | #include <apt-pkg/dpkgpm.h> | |
15 | #include <apt-pkg/error.h> | |
16 | #include <apt-pkg/configuration.h> | |
b2e465d6 AL |
17 | #include <apt-pkg/depcache.h> |
18 | #include <apt-pkg/strutl.h> | |
233b185f | 19 | |
03e39e59 AL |
20 | #include <unistd.h> |
21 | #include <stdlib.h> | |
22 | #include <fcntl.h> | |
23 | #include <sys/types.h> | |
24 | #include <sys/wait.h> | |
25 | #include <signal.h> | |
26 | #include <errno.h> | |
db0c350f | 27 | #include <stdio.h> |
75ef8f14 MV |
28 | #include <sstream> |
29 | #include <map> | |
30 | ||
31 | #include <config.h> | |
32 | #include <apti18n.h> | |
b0ebdef5 | 33 | /*}}}*/ |
233b185f AL |
34 | |
35 | using namespace std; | |
03e39e59 AL |
36 | |
37 | // DPkgPM::pkgDPkgPM - Constructor /*{{{*/ | |
38 | // --------------------------------------------------------------------- | |
39 | /* */ | |
b2e465d6 | 40 | pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache) : pkgPackageManager(Cache) |
03e39e59 AL |
41 | { |
42 | } | |
43 | /*}}}*/ | |
44 | // DPkgPM::pkgDPkgPM - Destructor /*{{{*/ | |
45 | // --------------------------------------------------------------------- | |
46 | /* */ | |
47 | pkgDPkgPM::~pkgDPkgPM() | |
48 | { | |
49 | } | |
50 | /*}}}*/ | |
51 | // DPkgPM::Install - Install a package /*{{{*/ | |
52 | // --------------------------------------------------------------------- | |
53 | /* Add an install operation to the sequence list */ | |
54 | bool pkgDPkgPM::Install(PkgIterator Pkg,string File) | |
55 | { | |
56 | if (File.empty() == true || Pkg.end() == true) | |
57 | return _error->Error("Internal Error, No file name for %s",Pkg.Name()); | |
58 | ||
59 | List.push_back(Item(Item::Install,Pkg,File)); | |
60 | return true; | |
61 | } | |
62 | /*}}}*/ | |
63 | // DPkgPM::Configure - Configure a package /*{{{*/ | |
64 | // --------------------------------------------------------------------- | |
65 | /* Add a configure operation to the sequence list */ | |
66 | bool pkgDPkgPM::Configure(PkgIterator Pkg) | |
67 | { | |
68 | if (Pkg.end() == true) | |
69 | return false; | |
70 | ||
71 | List.push_back(Item(Item::Configure,Pkg)); | |
72 | return true; | |
73 | } | |
74 | /*}}}*/ | |
75 | // DPkgPM::Remove - Remove a package /*{{{*/ | |
76 | // --------------------------------------------------------------------- | |
77 | /* Add a remove operation to the sequence list */ | |
fc4b5c9f | 78 | bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge) |
03e39e59 AL |
79 | { |
80 | if (Pkg.end() == true) | |
81 | return false; | |
82 | ||
fc4b5c9f AL |
83 | if (Purge == true) |
84 | List.push_back(Item(Item::Purge,Pkg)); | |
85 | else | |
86 | List.push_back(Item(Item::Remove,Pkg)); | |
6dd55be7 AL |
87 | return true; |
88 | } | |
89 | /*}}}*/ | |
90 | // DPkgPM::RunScripts - Run a set of scripts /*{{{*/ | |
91 | // --------------------------------------------------------------------- | |
92 | /* This looks for a list of script sto run from the configuration file, | |
93 | each one is run with system from a forked child. */ | |
94 | bool pkgDPkgPM::RunScripts(const char *Cnf) | |
95 | { | |
96 | Configuration::Item const *Opts = _config->Tree(Cnf); | |
97 | if (Opts == 0 || Opts->Child == 0) | |
98 | return true; | |
99 | Opts = Opts->Child; | |
100 | ||
101 | // Fork for running the system calls | |
54676e1a | 102 | pid_t Child = ExecFork(); |
6dd55be7 AL |
103 | |
104 | // This is the child | |
105 | if (Child == 0) | |
106 | { | |
6dd55be7 AL |
107 | if (chdir("/tmp/") != 0) |
108 | _exit(100); | |
109 | ||
6dd55be7 AL |
110 | unsigned int Count = 1; |
111 | for (; Opts != 0; Opts = Opts->Next, Count++) | |
112 | { | |
113 | if (Opts->Value.empty() == true) | |
114 | continue; | |
115 | ||
116 | if (system(Opts->Value.c_str()) != 0) | |
117 | _exit(100+Count); | |
118 | } | |
119 | _exit(0); | |
120 | } | |
121 | ||
122 | // Wait for the child | |
123 | int Status = 0; | |
124 | while (waitpid(Child,&Status,0) != Child) | |
125 | { | |
126 | if (errno == EINTR) | |
127 | continue; | |
128 | return _error->Errno("waitpid","Couldn't wait for subprocess"); | |
129 | } | |
130 | ||
131 | // Restore sig int/quit | |
132 | signal(SIGQUIT,SIG_DFL); | |
133 | signal(SIGINT,SIG_DFL); | |
ddc1d8d0 | 134 | |
6dd55be7 AL |
135 | // Check for an error code. |
136 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
137 | { | |
138 | unsigned int Count = WEXITSTATUS(Status); | |
139 | if (Count > 100) | |
140 | { | |
141 | Count -= 100; | |
142 | for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--); | |
cf544e14 | 143 | _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str()); |
6dd55be7 AL |
144 | } |
145 | ||
146 | return _error->Error("Sub-process returned an error code"); | |
147 | } | |
148 | ||
03e39e59 AL |
149 | return true; |
150 | } | |
db0c350f | 151 | /*}}}*/ |
b2e465d6 AL |
152 | // DPkgPM::SendV2Pkgs - Send version 2 package info /*{{{*/ |
153 | // --------------------------------------------------------------------- | |
154 | /* This is part of the helper script communication interface, it sends | |
155 | very complete information down to the other end of the pipe.*/ | |
156 | bool pkgDPkgPM::SendV2Pkgs(FILE *F) | |
157 | { | |
158 | fprintf(F,"VERSION 2\n"); | |
159 | ||
160 | /* Write out all of the configuration directives by walking the | |
161 | configuration tree */ | |
162 | const Configuration::Item *Top = _config->Tree(0); | |
163 | for (; Top != 0;) | |
164 | { | |
165 | if (Top->Value.empty() == false) | |
166 | { | |
167 | fprintf(F,"%s=%s\n", | |
168 | QuoteString(Top->FullTag(),"=\"\n").c_str(), | |
169 | QuoteString(Top->Value,"\n").c_str()); | |
170 | } | |
171 | ||
172 | if (Top->Child != 0) | |
173 | { | |
174 | Top = Top->Child; | |
175 | continue; | |
176 | } | |
177 | ||
178 | while (Top != 0 && Top->Next == 0) | |
179 | Top = Top->Parent; | |
180 | if (Top != 0) | |
181 | Top = Top->Next; | |
182 | } | |
183 | fprintf(F,"\n"); | |
184 | ||
185 | // Write out the package actions in order. | |
186 | for (vector<Item>::iterator I = List.begin(); I != List.end(); I++) | |
187 | { | |
188 | pkgDepCache::StateCache &S = Cache[I->Pkg]; | |
189 | ||
190 | fprintf(F,"%s ",I->Pkg.Name()); | |
191 | // Current version | |
192 | if (I->Pkg->CurrentVer == 0) | |
193 | fprintf(F,"- "); | |
194 | else | |
195 | fprintf(F,"%s ",I->Pkg.CurrentVer().VerStr()); | |
196 | ||
197 | // Show the compare operator | |
198 | // Target version | |
199 | if (S.InstallVer != 0) | |
200 | { | |
201 | int Comp = 2; | |
202 | if (I->Pkg->CurrentVer != 0) | |
203 | Comp = S.InstVerIter(Cache).CompareVer(I->Pkg.CurrentVer()); | |
204 | if (Comp < 0) | |
205 | fprintf(F,"> "); | |
206 | if (Comp == 0) | |
207 | fprintf(F,"= "); | |
208 | if (Comp > 0) | |
209 | fprintf(F,"< "); | |
210 | fprintf(F,"%s ",S.InstVerIter(Cache).VerStr()); | |
211 | } | |
212 | else | |
213 | fprintf(F,"> - "); | |
214 | ||
215 | // Show the filename/operation | |
216 | if (I->Op == Item::Install) | |
217 | { | |
218 | // No errors here.. | |
219 | if (I->File[0] != '/') | |
220 | fprintf(F,"**ERROR**\n"); | |
221 | else | |
222 | fprintf(F,"%s\n",I->File.c_str()); | |
223 | } | |
224 | if (I->Op == Item::Configure) | |
225 | fprintf(F,"**CONFIGURE**\n"); | |
226 | if (I->Op == Item::Remove || | |
227 | I->Op == Item::Purge) | |
228 | fprintf(F,"**REMOVE**\n"); | |
229 | ||
230 | if (ferror(F) != 0) | |
231 | return false; | |
232 | } | |
233 | return true; | |
234 | } | |
235 | /*}}}*/ | |
db0c350f AL |
236 | // DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/ |
237 | // --------------------------------------------------------------------- | |
238 | /* This looks for a list of scripts to run from the configuration file | |
239 | each one is run and is fed on standard input a list of all .deb files | |
240 | that are due to be installed. */ | |
241 | bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf) | |
242 | { | |
243 | Configuration::Item const *Opts = _config->Tree(Cnf); | |
244 | if (Opts == 0 || Opts->Child == 0) | |
245 | return true; | |
246 | Opts = Opts->Child; | |
247 | ||
248 | unsigned int Count = 1; | |
249 | for (; Opts != 0; Opts = Opts->Next, Count++) | |
250 | { | |
251 | if (Opts->Value.empty() == true) | |
252 | continue; | |
b2e465d6 AL |
253 | |
254 | // Determine the protocol version | |
255 | string OptSec = Opts->Value; | |
256 | string::size_type Pos; | |
257 | if ((Pos = OptSec.find(' ')) == string::npos || Pos == 0) | |
258 | Pos = OptSec.length(); | |
b2e465d6 AL |
259 | OptSec = "DPkg::Tools::Options::" + string(Opts->Value.c_str(),Pos); |
260 | ||
261 | unsigned int Version = _config->FindI(OptSec+"::Version",1); | |
262 | ||
db0c350f AL |
263 | // Create the pipes |
264 | int Pipes[2]; | |
265 | if (pipe(Pipes) != 0) | |
266 | return _error->Errno("pipe","Failed to create IPC pipe to subprocess"); | |
267 | SetCloseExec(Pipes[0],true); | |
268 | SetCloseExec(Pipes[1],true); | |
269 | ||
270 | // Purified Fork for running the script | |
271 | pid_t Process = ExecFork(); | |
272 | if (Process == 0) | |
273 | { | |
274 | // Setup the FDs | |
275 | dup2(Pipes[0],STDIN_FILENO); | |
276 | SetCloseExec(STDOUT_FILENO,false); | |
277 | SetCloseExec(STDIN_FILENO,false); | |
278 | SetCloseExec(STDERR_FILENO,false); | |
90ecbd7d AL |
279 | |
280 | const char *Args[4]; | |
db0c350f | 281 | Args[0] = "/bin/sh"; |
90ecbd7d AL |
282 | Args[1] = "-c"; |
283 | Args[2] = Opts->Value.c_str(); | |
284 | Args[3] = 0; | |
db0c350f AL |
285 | execv(Args[0],(char **)Args); |
286 | _exit(100); | |
287 | } | |
288 | close(Pipes[0]); | |
b2e465d6 AL |
289 | FILE *F = fdopen(Pipes[1],"w"); |
290 | if (F == 0) | |
291 | return _error->Errno("fdopen","Faild to open new FD"); | |
292 | ||
db0c350f | 293 | // Feed it the filenames. |
b2e465d6 AL |
294 | bool Die = false; |
295 | if (Version <= 1) | |
db0c350f | 296 | { |
b2e465d6 | 297 | for (vector<Item>::iterator I = List.begin(); I != List.end(); I++) |
db0c350f | 298 | { |
b2e465d6 AL |
299 | // Only deal with packages to be installed from .deb |
300 | if (I->Op != Item::Install) | |
301 | continue; | |
302 | ||
303 | // No errors here.. | |
304 | if (I->File[0] != '/') | |
305 | continue; | |
306 | ||
307 | /* Feed the filename of each package that is pending install | |
308 | into the pipe. */ | |
309 | fprintf(F,"%s\n",I->File.c_str()); | |
310 | if (ferror(F) != 0) | |
311 | { | |
312 | Die = true; | |
313 | break; | |
314 | } | |
90ecbd7d | 315 | } |
db0c350f | 316 | } |
b2e465d6 AL |
317 | else |
318 | Die = !SendV2Pkgs(F); | |
319 | ||
320 | fclose(F); | |
db0c350f AL |
321 | |
322 | // Clean up the sub process | |
323 | if (ExecWait(Process,Opts->Value.c_str()) == false) | |
90ecbd7d | 324 | return _error->Error("Failure running script %s",Opts->Value.c_str()); |
db0c350f AL |
325 | } |
326 | ||
327 | return true; | |
328 | } | |
03e39e59 AL |
329 | /*}}}*/ |
330 | // DPkgPM::Go - Run the sequence /*{{{*/ | |
331 | // --------------------------------------------------------------------- | |
75ef8f14 MV |
332 | /* This globs the operations and calls dpkg |
333 | * | |
334 | * If it is called with "OutStatusFd" set to a valid file descriptor | |
335 | * apt will report the install progress over this fd. It maps the | |
336 | * dpkg states a package goes through to human readable (and i10n-able) | |
337 | * names and calculates a percentage for each step. | |
338 | */ | |
339 | bool pkgDPkgPM::Go(int OutStatusFd) | |
03e39e59 | 340 | { |
6b8147b8 MZ |
341 | unsigned int MaxArgs = _config->FindI("Dpkg::MaxArgs",8*1024); |
342 | unsigned int MaxArgBytes = _config->FindI("Dpkg::MaxArgBytes",32*1024); | |
aff4e2f1 | 343 | |
6dd55be7 AL |
344 | if (RunScripts("DPkg::Pre-Invoke") == false) |
345 | return false; | |
db0c350f AL |
346 | |
347 | if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false) | |
348 | return false; | |
75ef8f14 MV |
349 | |
350 | // prepare the progress reporting | |
351 | int Done = 0; | |
352 | int Total = 0; | |
353 | // map the dpkg states to the operations that are performed | |
354 | // (this is sorted in the same way as Item::Ops) | |
355 | static const struct DpkgState DpkgStatesOpMap[][5] = { | |
356 | // Install operation | |
357 | { | |
21e1008e MV |
358 | {"half-installed", N_("Preparing %s")}, |
359 | {"unpacked", N_("Unpacking %s") }, | |
75ef8f14 MV |
360 | {NULL, NULL} |
361 | }, | |
362 | // Configure operation | |
363 | { | |
21e1008e MV |
364 | {"unpacked",N_("Preparing to configure %s") }, |
365 | {"half-configured", N_("Configuring %s") }, | |
366 | { "installed", N_("Installed %s")}, | |
75ef8f14 MV |
367 | {NULL, NULL} |
368 | }, | |
369 | // Remove operation | |
370 | { | |
21e1008e MV |
371 | {"half-configured", N_("Preparing for removal of %s")}, |
372 | {"half-installed", N_("Removing %s")}, | |
373 | {"config-files", N_("Removed %s")}, | |
75ef8f14 MV |
374 | {NULL, NULL} |
375 | }, | |
376 | // Purge operation | |
377 | { | |
21e1008e MV |
378 | {"config-files", N_("Preparing to completely remove %s")}, |
379 | {"not-installed", N_("Completely removed %s")}, | |
75ef8f14 MV |
380 | {NULL, NULL} |
381 | }, | |
382 | }; | |
db0c350f | 383 | |
75ef8f14 MV |
384 | // the dpkg states that the pkg will run through, the string is |
385 | // the package, the vector contains the dpkg states that the package | |
386 | // will go through | |
387 | map<string,vector<struct DpkgState> > PackageOps; | |
388 | // the dpkg states that are already done; the string is the package | |
389 | // the int is the state that is already done (e.g. a package that is | |
390 | // going to be install is already in state "half-installed") | |
391 | map<string,int> PackageOpsDone; | |
392 | ||
393 | // init the PackageOps map, go over the list of packages that | |
394 | // that will be [installed|configured|removed|purged] and add | |
395 | // them to the PackageOps map (the dpkg states it goes through) | |
396 | // and the PackageOpsTranslations (human readable strings) | |
397 | for (vector<Item>::iterator I = List.begin(); I != List.end();I++) | |
398 | { | |
399 | string name = (*I).Pkg.Name(); | |
400 | PackageOpsDone[name] = 0; | |
401 | for(int i=0; (DpkgStatesOpMap[(*I).Op][i]).state != NULL; i++) | |
402 | { | |
403 | PackageOps[name].push_back(DpkgStatesOpMap[(*I).Op][i]); | |
404 | Total++; | |
405 | } | |
406 | } | |
407 | ||
408 | // this loop is runs once per operation | |
03e39e59 AL |
409 | for (vector<Item>::iterator I = List.begin(); I != List.end();) |
410 | { | |
411 | vector<Item>::iterator J = I; | |
412 | for (; J != List.end() && J->Op == I->Op; J++); | |
30e1eab5 | 413 | |
03e39e59 | 414 | // Generate the argument list |
aff4e2f1 AL |
415 | const char *Args[MaxArgs + 50]; |
416 | if (J - I > (signed)MaxArgs) | |
417 | J = I + MaxArgs; | |
03e39e59 | 418 | |
30e1eab5 AL |
419 | unsigned int n = 0; |
420 | unsigned long Size = 0; | |
43b3b626 | 421 | string Tmp = _config->Find("Dir::Bin::dpkg","dpkg"); |
50914ffa | 422 | Args[n++] = Tmp.c_str(); |
30e1eab5 | 423 | Size += strlen(Args[n-1]); |
03e39e59 | 424 | |
6dd55be7 AL |
425 | // Stick in any custom dpkg options |
426 | Configuration::Item const *Opts = _config->Tree("DPkg::Options"); | |
427 | if (Opts != 0) | |
428 | { | |
429 | Opts = Opts->Child; | |
430 | for (; Opts != 0; Opts = Opts->Next) | |
431 | { | |
432 | if (Opts->Value.empty() == true) | |
433 | continue; | |
434 | Args[n++] = Opts->Value.c_str(); | |
435 | Size += Opts->Value.length(); | |
436 | } | |
437 | } | |
438 | ||
007dc9e0 | 439 | char status_fd_buf[20]; |
75ef8f14 MV |
440 | int fd[2]; |
441 | pipe(fd); | |
442 | ||
443 | Args[n++] = "--status-fd"; | |
444 | Size += strlen(Args[n-1]); | |
445 | snprintf(status_fd_buf,sizeof(status_fd_buf),"%i", fd[1]); | |
446 | Args[n++] = status_fd_buf; | |
447 | Size += strlen(Args[n-1]); | |
007dc9e0 | 448 | |
03e39e59 AL |
449 | switch (I->Op) |
450 | { | |
451 | case Item::Remove: | |
452 | Args[n++] = "--force-depends"; | |
30e1eab5 | 453 | Size += strlen(Args[n-1]); |
03e39e59 | 454 | Args[n++] = "--force-remove-essential"; |
30e1eab5 | 455 | Size += strlen(Args[n-1]); |
03e39e59 | 456 | Args[n++] = "--remove"; |
30e1eab5 | 457 | Size += strlen(Args[n-1]); |
03e39e59 AL |
458 | break; |
459 | ||
fc4b5c9f AL |
460 | case Item::Purge: |
461 | Args[n++] = "--force-depends"; | |
462 | Size += strlen(Args[n-1]); | |
463 | Args[n++] = "--force-remove-essential"; | |
464 | Size += strlen(Args[n-1]); | |
465 | Args[n++] = "--purge"; | |
466 | Size += strlen(Args[n-1]); | |
467 | break; | |
468 | ||
03e39e59 AL |
469 | case Item::Configure: |
470 | Args[n++] = "--configure"; | |
30e1eab5 | 471 | Size += strlen(Args[n-1]); |
03e39e59 AL |
472 | break; |
473 | ||
474 | case Item::Install: | |
475 | Args[n++] = "--unpack"; | |
30e1eab5 | 476 | Size += strlen(Args[n-1]); |
857a1d4a MV |
477 | Args[n++] = "--auto-deconfigure"; |
478 | Size += strlen(Args[n-1]); | |
03e39e59 AL |
479 | break; |
480 | } | |
481 | ||
482 | // Write in the file or package names | |
483 | if (I->Op == Item::Install) | |
30e1eab5 | 484 | { |
aff4e2f1 | 485 | for (;I != J && Size < MaxArgBytes; I++) |
30e1eab5 | 486 | { |
cf544e14 AL |
487 | if (I->File[0] != '/') |
488 | return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str()); | |
03e39e59 | 489 | Args[n++] = I->File.c_str(); |
30e1eab5 AL |
490 | Size += strlen(Args[n-1]); |
491 | } | |
492 | } | |
03e39e59 | 493 | else |
30e1eab5 | 494 | { |
aff4e2f1 | 495 | for (;I != J && Size < MaxArgBytes; I++) |
30e1eab5 | 496 | { |
03e39e59 | 497 | Args[n++] = I->Pkg.Name(); |
30e1eab5 AL |
498 | Size += strlen(Args[n-1]); |
499 | } | |
500 | } | |
03e39e59 | 501 | Args[n] = 0; |
30e1eab5 AL |
502 | J = I; |
503 | ||
504 | if (_config->FindB("Debug::pkgDPkgPM",false) == true) | |
505 | { | |
506 | for (unsigned int k = 0; k != n; k++) | |
507 | clog << Args[k] << ' '; | |
508 | clog << endl; | |
509 | continue; | |
510 | } | |
03e39e59 | 511 | |
03e39e59 AL |
512 | cout << flush; |
513 | clog << flush; | |
514 | cerr << flush; | |
515 | ||
516 | /* Mask off sig int/quit. We do this because dpkg also does when | |
517 | it forks scripts. What happens is that when you hit ctrl-c it sends | |
518 | it to all processes in the group. Since dpkg ignores the signal | |
519 | it doesn't die but we do! So we must also ignore it */ | |
7f9a6360 AL |
520 | sighandler_t old_SIGQUIT = signal(SIGQUIT,SIG_IGN); |
521 | sighandler_t old_SIGINT = signal(SIGINT,SIG_IGN); | |
75ef8f14 MV |
522 | |
523 | // Fork dpkg | |
007dc9e0 | 524 | pid_t Child; |
75ef8f14 MV |
525 | _config->Set("APT::Keep-Fds::",fd[1]); |
526 | Child = ExecFork(); | |
6dd55be7 | 527 | |
03e39e59 AL |
528 | // This is the child |
529 | if (Child == 0) | |
530 | { | |
75ef8f14 MV |
531 | close(fd[0]); // close the read end of the pipe |
532 | ||
cf544e14 | 533 | if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0) |
0dbb95d8 | 534 | _exit(100); |
03e39e59 | 535 | |
421ff807 | 536 | if (_config->FindB("DPkg::FlushSTDIN",true) == true && isatty(STDIN_FILENO)) |
8b5fe26c AL |
537 | { |
538 | int Flags,dummy; | |
539 | if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0) | |
540 | _exit(100); | |
541 | ||
542 | // Discard everything in stdin before forking dpkg | |
543 | if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0) | |
544 | _exit(100); | |
545 | ||
546 | while (read(STDIN_FILENO,&dummy,1) == 1); | |
547 | ||
548 | if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0) | |
549 | _exit(100); | |
550 | } | |
03e39e59 | 551 | |
03e39e59 AL |
552 | /* No Job Control Stop Env is a magic dpkg var that prevents it |
553 | from using sigstop */ | |
101030ab | 554 | putenv("DPKG_NO_TSTP=yes"); |
d568ed2d | 555 | execvp(Args[0],(char **)Args); |
03e39e59 | 556 | cerr << "Could not exec dpkg!" << endl; |
0dbb95d8 | 557 | _exit(100); |
03e39e59 AL |
558 | } |
559 | ||
75ef8f14 MV |
560 | // clear the Keep-Fd again |
561 | _config->Clear("APT::Keep-Fds",fd[1]); | |
562 | ||
03e39e59 AL |
563 | // Wait for dpkg |
564 | int Status = 0; | |
75ef8f14 MV |
565 | |
566 | // we read from dpkg here | |
567 | int _dpkgin = fd[0]; | |
568 | fcntl(_dpkgin, F_SETFL, O_NONBLOCK); | |
569 | close(fd[1]); // close the write end of the pipe | |
570 | ||
571 | // the read buffers for the communication with dpkg | |
572 | char line[1024] = {0,}; | |
573 | char buf[2] = {0,0}; | |
574 | ||
575 | // the result of the waitpid call | |
576 | int res; | |
577 | ||
578 | while ((res=waitpid(Child,&Status, WNOHANG)) != Child) { | |
579 | if(res < 0) { | |
580 | // FIXME: move this to a function or something, looks ugly here | |
581 | // error handling, waitpid returned -1 | |
582 | if (errno == EINTR) | |
583 | continue; | |
584 | RunScripts("DPkg::Post-Invoke"); | |
585 | ||
586 | // Restore sig int/quit | |
587 | signal(SIGQUIT,old_SIGQUIT); | |
588 | signal(SIGINT,old_SIGINT); | |
589 | return _error->Errno("waitpid","Couldn't wait for subprocess"); | |
590 | } | |
591 | ||
592 | // read a single char, make sure that the read can't block | |
593 | // (otherwise we may leave zombies) | |
594 | int len = read(_dpkgin, buf, 1); | |
595 | ||
596 | // nothing to read, wait a bit for more | |
597 | if(len <= 0) | |
598 | { | |
599 | usleep(1000); | |
03e39e59 | 600 | continue; |
75ef8f14 MV |
601 | } |
602 | ||
603 | // sanity check (should never happen) | |
604 | if(strlen(line) >= sizeof(line)-10) | |
605 | { | |
606 | _error->Error("got a overlong line from dpkg: '%s'",line); | |
607 | line[0]=0; | |
608 | } | |
609 | // append to line, check if we got a complete line | |
610 | strcat(line, buf); | |
611 | if(buf[0] != '\n') | |
612 | continue; | |
613 | ||
614 | if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true) | |
615 | std::clog << "got from dpkg '" << line << "'" << std::endl; | |
616 | ||
617 | // the status we output | |
618 | ostringstream status; | |
619 | ||
620 | /* dpkg sends strings like this: | |
621 | 'status: <pkg>: <pkg qstate>' | |
622 | errors look like this: | |
623 | '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 | |
624 | and conffile-prompt like this | |
625 | 'status: conffile-prompt: conffile : 'current-conffile' 'new-conffile' useredited distedited | |
626 | ||
627 | */ | |
eda1bb3c | 628 | char* list[5]; |
ed94a0d6 MV |
629 | // dpkg sends multiline error messages sometimes (see |
630 | // #374195 for a example. we should support this by | |
631 | // either patching dpkg to not send multiline over the | |
632 | // statusfd or by rewriting the code here to deal with | |
633 | // it. for now we just ignore it and not crash | |
634 | TokSplitString(':', line, list, sizeof(list)/sizeof(list[0])); | |
75ef8f14 MV |
635 | char *pkg = list[1]; |
636 | char *action = _strstrip(list[2]); | |
ed94a0d6 MV |
637 | if( pkg == NULL || action == NULL) |
638 | { | |
639 | if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true) | |
640 | std::clog << "ignoring line: not enough ':'" << std::endl; | |
7882f8fe MV |
641 | // reset the line buffer |
642 | line[0]=0; | |
ed94a0d6 MV |
643 | continue; |
644 | } | |
75ef8f14 MV |
645 | |
646 | if(strncmp(action,"error",strlen("error")) == 0) | |
647 | { | |
648 | status << "pmerror:" << list[1] | |
649 | << ":" << (Done/float(Total)*100.0) | |
650 | << ":" << list[3] | |
651 | << endl; | |
652 | if(OutStatusFd > 0) | |
653 | write(OutStatusFd, status.str().c_str(), status.str().size()); | |
654 | line[0]=0; | |
655 | if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true) | |
656 | std::clog << "send: '" << status.str() << "'" << endl; | |
657 | continue; | |
658 | } | |
659 | if(strncmp(action,"conffile",strlen("conffile")) == 0) | |
660 | { | |
661 | status << "pmconffile:" << list[1] | |
662 | << ":" << (Done/float(Total)*100.0) | |
663 | << ":" << list[3] | |
664 | << endl; | |
665 | if(OutStatusFd > 0) | |
666 | write(OutStatusFd, status.str().c_str(), status.str().size()); | |
667 | line[0]=0; | |
668 | if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true) | |
669 | std::clog << "send: '" << status.str() << "'" << endl; | |
670 | continue; | |
671 | } | |
672 | ||
673 | vector<struct DpkgState> &states = PackageOps[pkg]; | |
674 | const char *next_action = NULL; | |
675 | if(PackageOpsDone[pkg] < states.size()) | |
676 | next_action = states[PackageOpsDone[pkg]].state; | |
677 | // check if the package moved to the next dpkg state | |
678 | if(next_action && (strcmp(action, next_action) == 0)) | |
679 | { | |
680 | // only read the translation if there is actually a next | |
681 | // action | |
21e1008e | 682 | const char *translation = _(states[PackageOpsDone[pkg]].str); |
75ef8f14 MV |
683 | char s[200]; |
684 | snprintf(s, sizeof(s), translation, pkg); | |
685 | ||
686 | // we moved from one dpkg state to a new one, report that | |
687 | PackageOpsDone[pkg]++; | |
688 | Done++; | |
689 | // build the status str | |
690 | status << "pmstatus:" << pkg | |
691 | << ":" << (Done/float(Total)*100.0) | |
692 | << ":" << s | |
693 | << endl; | |
694 | if(OutStatusFd > 0) | |
695 | write(OutStatusFd, status.str().c_str(), status.str().size()); | |
696 | if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true) | |
697 | std::clog << "send: '" << status.str() << "'" << endl; | |
698 | ||
699 | } | |
700 | if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true) | |
701 | std::clog << "(parsed from dpkg) pkg: " << pkg | |
702 | << " action: " << action << endl; | |
7f9a6360 | 703 | |
75ef8f14 MV |
704 | // reset the line buffer |
705 | line[0]=0; | |
03e39e59 | 706 | } |
75ef8f14 | 707 | close(_dpkgin); |
03e39e59 AL |
708 | |
709 | // Restore sig int/quit | |
7f9a6360 AL |
710 | signal(SIGQUIT,old_SIGQUIT); |
711 | signal(SIGINT,old_SIGINT); | |
6dd55be7 AL |
712 | |
713 | // Check for an error code. | |
714 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
715 | { | |
c70496f9 MV |
716 | // if it was set to "keep-dpkg-runing" then we won't return |
717 | // here but keep the loop going and just report it as a error | |
718 | // for later | |
719 | bool stopOnError = _config->FindB("Dpkg::StopOnError",true); | |
f956efb4 | 720 | |
c70496f9 MV |
721 | if(stopOnError) |
722 | RunScripts("DPkg::Post-Invoke"); | |
723 | ||
724 | if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV) | |
725 | _error->Error("Sub-process %s received a segmentation fault.",Args[0]); | |
726 | else if (WIFEXITED(Status) != 0) | |
727 | _error->Error("Sub-process %s returned an error code (%u)",Args[0],WEXITSTATUS(Status)); | |
728 | else | |
729 | _error->Error("Sub-process %s exited unexpectedly",Args[0]); | |
730 | ||
731 | if(stopOnError) | |
732 | return false; | |
6dd55be7 | 733 | } |
03e39e59 | 734 | } |
6dd55be7 AL |
735 | |
736 | if (RunScripts("DPkg::Post-Invoke") == false) | |
737 | return false; | |
03e39e59 AL |
738 | return true; |
739 | } | |
740 | /*}}}*/ | |
281daf46 AL |
741 | // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/ |
742 | // --------------------------------------------------------------------- | |
743 | /* */ | |
744 | void pkgDPkgPM::Reset() | |
745 | { | |
746 | List.erase(List.begin(),List.end()); | |
747 | } | |
748 | /*}}}*/ |