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