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