]>
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 | |
fc2d32c0 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 | |
fc2d32c0 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]; | |
fc2d32c0 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) | |
fc2d32c0 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); |
fc2d32c0 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: "); | |
9b5d79ec | 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: "); |
9b5d79ec | 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 | { |
17745b02 MV |
590 | fd_set rfds; |
591 | struct timespec tv; | |
592 | sigset_t sigmask; | |
593 | sigset_t original_sigmask; | |
594 | ||
6b8147b8 MZ |
595 | unsigned int MaxArgs = _config->FindI("Dpkg::MaxArgs",8*1024); |
596 | unsigned int MaxArgBytes = _config->FindI("Dpkg::MaxArgBytes",32*1024); | |
870ce08f | 597 | bool NoTriggers = _config->FindB("DPkg::NoTriggers",false); |
aff4e2f1 | 598 | |
6dd55be7 AL |
599 | if (RunScripts("DPkg::Pre-Invoke") == false) |
600 | return false; | |
db0c350f AL |
601 | |
602 | if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false) | |
603 | return false; | |
fc2d32c0 | 604 | |
75ef8f14 MV |
605 | // map the dpkg states to the operations that are performed |
606 | // (this is sorted in the same way as Item::Ops) | |
9d06bc80 | 607 | static const struct DpkgState DpkgStatesOpMap[][7] = { |
75ef8f14 MV |
608 | // Install operation |
609 | { | |
21e1008e MV |
610 | {"half-installed", N_("Preparing %s")}, |
611 | {"unpacked", N_("Unpacking %s") }, | |
75ef8f14 MV |
612 | {NULL, NULL} |
613 | }, | |
614 | // Configure operation | |
615 | { | |
21e1008e MV |
616 | {"unpacked",N_("Preparing to configure %s") }, |
617 | {"half-configured", N_("Configuring %s") }, | |
646501c8 | 618 | #if 0 |
9d06bc80 MV |
619 | {"triggers-awaited", N_("Processing triggers for %s") }, |
620 | {"triggers-pending", N_("Processing triggers for %s") }, | |
646501c8 | 621 | #endif |
21e1008e | 622 | { "installed", N_("Installed %s")}, |
75ef8f14 MV |
623 | {NULL, NULL} |
624 | }, | |
625 | // Remove operation | |
626 | { | |
21e1008e | 627 | {"half-configured", N_("Preparing for removal of %s")}, |
646501c8 | 628 | #if 0 |
9d06bc80 MV |
629 | {"triggers-awaited", N_("Preparing for removal of %s")}, |
630 | {"triggers-pending", N_("Preparing for removal of %s")}, | |
646501c8 | 631 | #endif |
21e1008e MV |
632 | {"half-installed", N_("Removing %s")}, |
633 | {"config-files", N_("Removed %s")}, | |
75ef8f14 MV |
634 | {NULL, NULL} |
635 | }, | |
636 | // Purge operation | |
637 | { | |
21e1008e MV |
638 | {"config-files", N_("Preparing to completely remove %s")}, |
639 | {"not-installed", N_("Completely removed %s")}, | |
75ef8f14 MV |
640 | {NULL, NULL} |
641 | }, | |
642 | }; | |
db0c350f | 643 | |
75ef8f14 MV |
644 | // init the PackageOps map, go over the list of packages that |
645 | // that will be [installed|configured|removed|purged] and add | |
646 | // them to the PackageOps map (the dpkg states it goes through) | |
647 | // and the PackageOpsTranslations (human readable strings) | |
648 | for (vector<Item>::iterator I = List.begin(); I != List.end();I++) | |
649 | { | |
650 | string name = (*I).Pkg.Name(); | |
651 | PackageOpsDone[name] = 0; | |
652 | for(int i=0; (DpkgStatesOpMap[(*I).Op][i]).state != NULL; i++) | |
653 | { | |
654 | PackageOps[name].push_back(DpkgStatesOpMap[(*I).Op][i]); | |
ff56e980 | 655 | PackagesTotal++; |
75ef8f14 MV |
656 | } |
657 | } | |
658 | ||
9983591d OS |
659 | stdin_is_dev_null = false; |
660 | ||
ff56e980 | 661 | // create log |
2e1715ea | 662 | OpenLog(); |
ff56e980 | 663 | |
75ef8f14 | 664 | // this loop is runs once per operation |
03e39e59 AL |
665 | for (vector<Item>::iterator I = List.begin(); I != List.end();) |
666 | { | |
667 | vector<Item>::iterator J = I; | |
668 | for (; J != List.end() && J->Op == I->Op; J++); | |
30e1eab5 | 669 | |
03e39e59 | 670 | // Generate the argument list |
aff4e2f1 AL |
671 | const char *Args[MaxArgs + 50]; |
672 | if (J - I > (signed)MaxArgs) | |
673 | J = I + MaxArgs; | |
03e39e59 | 674 | |
30e1eab5 AL |
675 | unsigned int n = 0; |
676 | unsigned long Size = 0; | |
43b3b626 | 677 | string Tmp = _config->Find("Dir::Bin::dpkg","dpkg"); |
50914ffa | 678 | Args[n++] = Tmp.c_str(); |
30e1eab5 | 679 | Size += strlen(Args[n-1]); |
03e39e59 | 680 | |
6dd55be7 AL |
681 | // Stick in any custom dpkg options |
682 | Configuration::Item const *Opts = _config->Tree("DPkg::Options"); | |
683 | if (Opts != 0) | |
684 | { | |
685 | Opts = Opts->Child; | |
686 | for (; Opts != 0; Opts = Opts->Next) | |
687 | { | |
688 | if (Opts->Value.empty() == true) | |
689 | continue; | |
690 | Args[n++] = Opts->Value.c_str(); | |
691 | Size += Opts->Value.length(); | |
692 | } | |
693 | } | |
694 | ||
007dc9e0 | 695 | char status_fd_buf[20]; |
75ef8f14 MV |
696 | int fd[2]; |
697 | pipe(fd); | |
698 | ||
699 | Args[n++] = "--status-fd"; | |
700 | Size += strlen(Args[n-1]); | |
701 | snprintf(status_fd_buf,sizeof(status_fd_buf),"%i", fd[1]); | |
702 | Args[n++] = status_fd_buf; | |
703 | Size += strlen(Args[n-1]); | |
007dc9e0 | 704 | |
03e39e59 AL |
705 | switch (I->Op) |
706 | { | |
707 | case Item::Remove: | |
708 | Args[n++] = "--force-depends"; | |
30e1eab5 | 709 | Size += strlen(Args[n-1]); |
03e39e59 | 710 | Args[n++] = "--force-remove-essential"; |
30e1eab5 | 711 | Size += strlen(Args[n-1]); |
03e39e59 | 712 | Args[n++] = "--remove"; |
30e1eab5 | 713 | Size += strlen(Args[n-1]); |
03e39e59 AL |
714 | break; |
715 | ||
fc4b5c9f AL |
716 | case Item::Purge: |
717 | Args[n++] = "--force-depends"; | |
718 | Size += strlen(Args[n-1]); | |
719 | Args[n++] = "--force-remove-essential"; | |
720 | Size += strlen(Args[n-1]); | |
721 | Args[n++] = "--purge"; | |
722 | Size += strlen(Args[n-1]); | |
723 | break; | |
724 | ||
03e39e59 AL |
725 | case Item::Configure: |
726 | Args[n++] = "--configure"; | |
870ce08f MV |
727 | if (NoTriggers) |
728 | Args[n++] = "--no-triggers"; | |
30e1eab5 | 729 | Size += strlen(Args[n-1]); |
03e39e59 AL |
730 | break; |
731 | ||
732 | case Item::Install: | |
733 | Args[n++] = "--unpack"; | |
30e1eab5 | 734 | Size += strlen(Args[n-1]); |
857a1d4a MV |
735 | Args[n++] = "--auto-deconfigure"; |
736 | Size += strlen(Args[n-1]); | |
03e39e59 AL |
737 | break; |
738 | } | |
739 | ||
740 | // Write in the file or package names | |
741 | if (I->Op == Item::Install) | |
30e1eab5 | 742 | { |
aff4e2f1 | 743 | for (;I != J && Size < MaxArgBytes; I++) |
30e1eab5 | 744 | { |
cf544e14 AL |
745 | if (I->File[0] != '/') |
746 | return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str()); | |
03e39e59 | 747 | Args[n++] = I->File.c_str(); |
30e1eab5 AL |
748 | Size += strlen(Args[n-1]); |
749 | } | |
750 | } | |
03e39e59 | 751 | else |
30e1eab5 | 752 | { |
aff4e2f1 | 753 | for (;I != J && Size < MaxArgBytes; I++) |
30e1eab5 | 754 | { |
03e39e59 | 755 | Args[n++] = I->Pkg.Name(); |
30e1eab5 AL |
756 | Size += strlen(Args[n-1]); |
757 | } | |
758 | } | |
03e39e59 | 759 | Args[n] = 0; |
30e1eab5 AL |
760 | J = I; |
761 | ||
762 | if (_config->FindB("Debug::pkgDPkgPM",false) == true) | |
763 | { | |
764 | for (unsigned int k = 0; k != n; k++) | |
765 | clog << Args[k] << ' '; | |
766 | clog << endl; | |
767 | continue; | |
768 | } | |
03e39e59 | 769 | |
03e39e59 AL |
770 | cout << flush; |
771 | clog << flush; | |
772 | cerr << flush; | |
773 | ||
774 | /* Mask off sig int/quit. We do this because dpkg also does when | |
775 | it forks scripts. What happens is that when you hit ctrl-c it sends | |
776 | it to all processes in the group. Since dpkg ignores the signal | |
777 | it doesn't die but we do! So we must also ignore it */ | |
7f9a6360 AL |
778 | sighandler_t old_SIGQUIT = signal(SIGQUIT,SIG_IGN); |
779 | sighandler_t old_SIGINT = signal(SIGINT,SIG_IGN); | |
d8cb4aa4 | 780 | |
73e598c3 MV |
781 | // ignore SIGHUP as well (debian #463030) |
782 | sighandler_t old_SIGHUP = signal(SIGHUP,SIG_IGN); | |
783 | ||
d8cb4aa4 | 784 | struct termios tt; |
51dbf368 | 785 | struct termios tt_out; |
d8cb4aa4 MV |
786 | struct winsize win; |
787 | int master; | |
788 | int slave; | |
789 | ||
ceabc520 | 790 | // FIXME: setup sensible signal handling (*ick*) |
d8cb4aa4 | 791 | tcgetattr(0, &tt); |
51dbf368 | 792 | tcgetattr(1, &tt_out); |
d8cb4aa4 | 793 | ioctl(0, TIOCGWINSZ, (char *)&win); |
51dbf368 | 794 | if (openpty(&master, &slave, NULL, &tt_out, &win) < 0) |
090c6566 | 795 | { |
a4cf3665 MV |
796 | const char *s = _("Can not write log, openpty() " |
797 | "failed (/dev/pts not mounted?)\n"); | |
798 | fprintf(stderr, "%s",s); | |
799 | fprintf(term_out, "%s",s); | |
800 | master = slave = -1; | |
801 | } else { | |
802 | struct termios rtt; | |
803 | rtt = tt; | |
804 | cfmakeraw(&rtt); | |
805 | rtt.c_lflag &= ~ECHO; | |
17745b02 MV |
806 | // block SIGTTOU during tcsetattr to prevent a hang if |
807 | // the process is a member of the background process group | |
808 | // http://www.opengroup.org/onlinepubs/000095399/functions/tcsetattr.html | |
809 | sigemptyset(&sigmask); | |
810 | sigaddset(&sigmask, SIGTTOU); | |
811 | sigprocmask(SIG_BLOCK,&sigmask, &original_sigmask); | |
a4cf3665 | 812 | tcsetattr(0, TCSAFLUSH, &rtt); |
17745b02 | 813 | sigprocmask(SIG_SETMASK, &original_sigmask, 0); |
d8cb4aa4 MV |
814 | } |
815 | ||
75ef8f14 | 816 | // Fork dpkg |
007dc9e0 | 817 | pid_t Child; |
75ef8f14 MV |
818 | _config->Set("APT::Keep-Fds::",fd[1]); |
819 | Child = ExecFork(); | |
6dd55be7 | 820 | |
03e39e59 AL |
821 | // This is the child |
822 | if (Child == 0) | |
823 | { | |
a4cf3665 MV |
824 | if(slave >= 0 && master >= 0) |
825 | { | |
826 | setsid(); | |
827 | ioctl(slave, TIOCSCTTY, 0); | |
828 | close(master); | |
829 | dup2(slave, 0); | |
830 | dup2(slave, 1); | |
831 | dup2(slave, 2); | |
832 | close(slave); | |
833 | } | |
75ef8f14 | 834 | close(fd[0]); // close the read end of the pipe |
d8cb4aa4 | 835 | |
cf544e14 | 836 | if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0) |
0dbb95d8 | 837 | _exit(100); |
03e39e59 | 838 | |
421ff807 | 839 | if (_config->FindB("DPkg::FlushSTDIN",true) == true && isatty(STDIN_FILENO)) |
8b5fe26c AL |
840 | { |
841 | int Flags,dummy; | |
842 | if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0) | |
843 | _exit(100); | |
844 | ||
845 | // Discard everything in stdin before forking dpkg | |
846 | if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0) | |
847 | _exit(100); | |
848 | ||
849 | while (read(STDIN_FILENO,&dummy,1) == 1); | |
850 | ||
851 | if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0) | |
852 | _exit(100); | |
853 | } | |
d8cb4aa4 MV |
854 | |
855 | ||
03e39e59 AL |
856 | /* No Job Control Stop Env is a magic dpkg var that prevents it |
857 | from using sigstop */ | |
71afbdb5 | 858 | putenv((char *)"DPKG_NO_TSTP=yes"); |
d568ed2d | 859 | execvp(Args[0],(char **)Args); |
03e39e59 | 860 | cerr << "Could not exec dpkg!" << endl; |
0dbb95d8 | 861 | _exit(100); |
03e39e59 AL |
862 | } |
863 | ||
75ef8f14 MV |
864 | // clear the Keep-Fd again |
865 | _config->Clear("APT::Keep-Fds",fd[1]); | |
866 | ||
03e39e59 AL |
867 | // Wait for dpkg |
868 | int Status = 0; | |
75ef8f14 MV |
869 | |
870 | // we read from dpkg here | |
871 | int _dpkgin = fd[0]; | |
75ef8f14 MV |
872 | close(fd[1]); // close the write end of the pipe |
873 | ||
75ef8f14 MV |
874 | // the result of the waitpid call |
875 | int res; | |
a4cf3665 MV |
876 | if(slave > 0) |
877 | close(slave); | |
6191b008 | 878 | |
97efd303 | 879 | // setups fds |
7052511e MV |
880 | sigemptyset(&sigmask); |
881 | sigprocmask(SIG_BLOCK,&sigmask,&original_sigmask); | |
882 | ||
090c6566 | 883 | int select_ret; |
75ef8f14 MV |
884 | while ((res=waitpid(Child,&Status, WNOHANG)) != Child) { |
885 | if(res < 0) { | |
886 | // FIXME: move this to a function or something, looks ugly here | |
887 | // error handling, waitpid returned -1 | |
888 | if (errno == EINTR) | |
889 | continue; | |
890 | RunScripts("DPkg::Post-Invoke"); | |
891 | ||
892 | // Restore sig int/quit | |
893 | signal(SIGQUIT,old_SIGQUIT); | |
894 | signal(SIGINT,old_SIGINT); | |
e306ec47 | 895 | signal(SIGHUP,old_SIGHUP); |
75ef8f14 MV |
896 | return _error->Errno("waitpid","Couldn't wait for subprocess"); |
897 | } | |
d8cb4aa4 MV |
898 | |
899 | // wait for input or output here | |
955a6ddb | 900 | FD_ZERO(&rfds); |
9983591d OS |
901 | if (!stdin_is_dev_null) |
902 | FD_SET(0, &rfds); | |
955a6ddb | 903 | FD_SET(_dpkgin, &rfds); |
a4cf3665 MV |
904 | if(master >= 0) |
905 | FD_SET(master, &rfds); | |
090c6566 | 906 | tv.tv_sec = 1; |
7052511e MV |
907 | tv.tv_nsec = 0; |
908 | select_ret = pselect(max(master, _dpkgin)+1, &rfds, NULL, NULL, | |
909 | &tv, &original_sigmask); | |
919e5852 OS |
910 | if (select_ret < 0 && (errno == EINVAL || errno == ENOSYS)) |
911 | select_ret = racy_pselect(max(master, _dpkgin)+1, &rfds, NULL, | |
912 | NULL, &tv, &original_sigmask); | |
da50ba30 MV |
913 | if (select_ret == 0) |
914 | continue; | |
915 | else if (select_ret < 0 && errno == EINTR) | |
916 | continue; | |
917 | else if (select_ret < 0) | |
918 | { | |
919 | perror("select() returned error"); | |
920 | continue; | |
921 | } | |
922 | ||
a4cf3665 | 923 | if(master >= 0 && FD_ISSET(master, &rfds)) |
1ba38171 | 924 | DoTerminalPty(master); |
a4cf3665 | 925 | if(master >= 0 && FD_ISSET(0, &rfds)) |
955a6ddb | 926 | DoStdin(master); |
955a6ddb | 927 | if(FD_ISSET(_dpkgin, &rfds)) |
09fa2df2 | 928 | DoDpkgStatusFd(_dpkgin, OutStatusFd); |
03e39e59 | 929 | } |
75ef8f14 | 930 | close(_dpkgin); |
03e39e59 AL |
931 | |
932 | // Restore sig int/quit | |
7f9a6360 AL |
933 | signal(SIGQUIT,old_SIGQUIT); |
934 | signal(SIGINT,old_SIGINT); | |
d9ec0fac | 935 | signal(SIGHUP,old_SIGHUP); |
d8cb4aa4 | 936 | |
477b5d6c MV |
937 | if(master >= 0) |
938 | { | |
a4cf3665 | 939 | tcsetattr(0, TCSAFLUSH, &tt); |
477b5d6c MV |
940 | close(master); |
941 | } | |
6dd55be7 AL |
942 | |
943 | // Check for an error code. | |
944 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
945 | { | |
c70496f9 MV |
946 | // if it was set to "keep-dpkg-runing" then we won't return |
947 | // here but keep the loop going and just report it as a error | |
948 | // for later | |
949 | bool stopOnError = _config->FindB("Dpkg::StopOnError",true); | |
f956efb4 | 950 | |
c70496f9 MV |
951 | if(stopOnError) |
952 | RunScripts("DPkg::Post-Invoke"); | |
953 | ||
954 | if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV) | |
955 | _error->Error("Sub-process %s received a segmentation fault.",Args[0]); | |
956 | else if (WIFEXITED(Status) != 0) | |
957 | _error->Error("Sub-process %s returned an error code (%u)",Args[0],WEXITSTATUS(Status)); | |
958 | else | |
959 | _error->Error("Sub-process %s exited unexpectedly",Args[0]); | |
960 | ||
ff56e980 MV |
961 | if(stopOnError) |
962 | { | |
2e1715ea | 963 | CloseLog(); |
c70496f9 | 964 | return false; |
ff56e980 | 965 | } |
6dd55be7 | 966 | } |
03e39e59 | 967 | } |
2e1715ea | 968 | CloseLog(); |
6dd55be7 AL |
969 | |
970 | if (RunScripts("DPkg::Post-Invoke") == false) | |
971 | return false; | |
b462d75a MV |
972 | |
973 | Cache.writeStateFile(NULL); | |
03e39e59 AL |
974 | return true; |
975 | } | |
976 | /*}}}*/ | |
281daf46 AL |
977 | // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/ |
978 | // --------------------------------------------------------------------- | |
979 | /* */ | |
980 | void pkgDPkgPM::Reset() | |
981 | { | |
982 | List.erase(List.begin(),List.end()); | |
983 | } | |
984 | /*}}}*/ |