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