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