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