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