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