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