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