]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/dpkgpm.cc
* apt-pkg/deb/dpkgpm.cc:
[apt.git] / apt-pkg / deb / dpkgpm.cc
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/pkgrecords.h>
16 #include <apt-pkg/strutl.h>
17 #include <apti18n.h>
18 #include <apt-pkg/fileutl.h>
19
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <fcntl.h>
23 #include <sys/select.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <signal.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <algorithm>
32 #include <sstream>
33 #include <map>
34
35 #include <termios.h>
36 #include <unistd.h>
37 #include <sys/ioctl.h>
38 #include <pty.h>
39
40 #include <config.h>
41 #include <apti18n.h>
42 /*}}}*/
43
44 using namespace std;
45
46 namespace
47 {
48 // Maps the dpkg "processing" info to human readable names. Entry 0
49 // of each array is the key, entry 1 is the value.
50 const std::pair<const char *, const char *> PackageProcessingOps[] = {
51 std::make_pair("install", N_("Installing %s")),
52 std::make_pair("configure", N_("Configuring %s")),
53 std::make_pair("remove", N_("Removing %s")),
54 std::make_pair("purge", N_("Completely removing %s")),
55 std::make_pair("trigproc", N_("Running post-installation trigger %s"))
56 };
57
58 const std::pair<const char *, const char *> * const PackageProcessingOpsBegin = PackageProcessingOps;
59 const std::pair<const char *, const char *> * const PackageProcessingOpsEnd = PackageProcessingOps + sizeof(PackageProcessingOps) / sizeof(PackageProcessingOps[0]);
60
61 // Predicate to test whether an entry in the PackageProcessingOps
62 // array matches a string.
63 class MatchProcessingOp
64 {
65 const char *target;
66
67 public:
68 MatchProcessingOp(const char *the_target)
69 : target(the_target)
70 {
71 }
72
73 bool operator()(const std::pair<const char *, const char *> &pair) const
74 {
75 return strcmp(pair.first, target) == 0;
76 }
77 };
78 }
79
80 /* helper function to ionice the given PID
81
82 there is no C header for ionice yet - just the syscall interface
83 so we use the binary from util-linux
84 */
85 static bool
86 ionice(int PID)
87 {
88 if (!FileExists("/usr/bin/ionice"))
89 return false;
90 pid_t Process = ExecFork();
91 if (Process == 0)
92 {
93 char buf[32];
94 snprintf(buf, sizeof(buf), "-p%d", PID);
95 const char *Args[4];
96 Args[0] = "/usr/bin/ionice";
97 Args[1] = "-c3";
98 Args[2] = buf;
99 Args[3] = 0;
100 execv(Args[0], (char **)Args);
101 }
102 return ExecWait(Process, "ionice");
103 }
104
105 // DPkgPM::pkgDPkgPM - Constructor /*{{{*/
106 // ---------------------------------------------------------------------
107 /* */
108 pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache)
109 : pkgPackageManager(Cache), dpkgbuf_pos(0),
110 term_out(NULL), PackagesDone(0), PackagesTotal(0), pkgFailures(0)
111 {
112 }
113 /*}}}*/
114 // DPkgPM::pkgDPkgPM - Destructor /*{{{*/
115 // ---------------------------------------------------------------------
116 /* */
117 pkgDPkgPM::~pkgDPkgPM()
118 {
119 }
120 /*}}}*/
121 // DPkgPM::Install - Install a package /*{{{*/
122 // ---------------------------------------------------------------------
123 /* Add an install operation to the sequence list */
124 bool pkgDPkgPM::Install(PkgIterator Pkg,string File)
125 {
126 if (File.empty() == true || Pkg.end() == true)
127 return _error->Error("Internal Error, No file name for %s",Pkg.Name());
128
129 List.push_back(Item(Item::Install,Pkg,File));
130 return true;
131 }
132 /*}}}*/
133 // DPkgPM::Configure - Configure a package /*{{{*/
134 // ---------------------------------------------------------------------
135 /* Add a configure operation to the sequence list */
136 bool pkgDPkgPM::Configure(PkgIterator Pkg)
137 {
138 if (Pkg.end() == true)
139 return false;
140
141 List.push_back(Item(Item::Configure, Pkg));
142
143 // Use triggers for config calls if we configure "smart"
144 // as otherwise Pre-Depends will not be satisfied, see #526774
145 if (_config->FindB("DPkg::TriggersPending", false) == true)
146 List.push_back(Item(Item::TriggersPending, PkgIterator()));
147
148 return true;
149 }
150 /*}}}*/
151 // DPkgPM::Remove - Remove a package /*{{{*/
152 // ---------------------------------------------------------------------
153 /* Add a remove operation to the sequence list */
154 bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge)
155 {
156 if (Pkg.end() == true)
157 return false;
158
159 if (Purge == true)
160 List.push_back(Item(Item::Purge,Pkg));
161 else
162 List.push_back(Item(Item::Remove,Pkg));
163 return true;
164 }
165 /*}}}*/
166 // DPkgPM::SendV2Pkgs - Send version 2 package info /*{{{*/
167 // ---------------------------------------------------------------------
168 /* This is part of the helper script communication interface, it sends
169 very complete information down to the other end of the pipe.*/
170 bool pkgDPkgPM::SendV2Pkgs(FILE *F)
171 {
172 fprintf(F,"VERSION 2\n");
173
174 /* Write out all of the configuration directives by walking the
175 configuration tree */
176 const Configuration::Item *Top = _config->Tree(0);
177 for (; Top != 0;)
178 {
179 if (Top->Value.empty() == false)
180 {
181 fprintf(F,"%s=%s\n",
182 QuoteString(Top->FullTag(),"=\"\n").c_str(),
183 QuoteString(Top->Value,"\n").c_str());
184 }
185
186 if (Top->Child != 0)
187 {
188 Top = Top->Child;
189 continue;
190 }
191
192 while (Top != 0 && Top->Next == 0)
193 Top = Top->Parent;
194 if (Top != 0)
195 Top = Top->Next;
196 }
197 fprintf(F,"\n");
198
199 // Write out the package actions in order.
200 for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
201 {
202 if(I->Pkg.end() == true)
203 continue;
204
205 pkgDepCache::StateCache &S = Cache[I->Pkg];
206
207 fprintf(F,"%s ",I->Pkg.Name());
208 // Current version
209 if (I->Pkg->CurrentVer == 0)
210 fprintf(F,"- ");
211 else
212 fprintf(F,"%s ",I->Pkg.CurrentVer().VerStr());
213
214 // Show the compare operator
215 // Target version
216 if (S.InstallVer != 0)
217 {
218 int Comp = 2;
219 if (I->Pkg->CurrentVer != 0)
220 Comp = S.InstVerIter(Cache).CompareVer(I->Pkg.CurrentVer());
221 if (Comp < 0)
222 fprintf(F,"> ");
223 if (Comp == 0)
224 fprintf(F,"= ");
225 if (Comp > 0)
226 fprintf(F,"< ");
227 fprintf(F,"%s ",S.InstVerIter(Cache).VerStr());
228 }
229 else
230 fprintf(F,"> - ");
231
232 // Show the filename/operation
233 if (I->Op == Item::Install)
234 {
235 // No errors here..
236 if (I->File[0] != '/')
237 fprintf(F,"**ERROR**\n");
238 else
239 fprintf(F,"%s\n",I->File.c_str());
240 }
241 if (I->Op == Item::Configure)
242 fprintf(F,"**CONFIGURE**\n");
243 if (I->Op == Item::Remove ||
244 I->Op == Item::Purge)
245 fprintf(F,"**REMOVE**\n");
246
247 if (ferror(F) != 0)
248 return false;
249 }
250 return true;
251 }
252 /*}}}*/
253 // DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/
254 // ---------------------------------------------------------------------
255 /* This looks for a list of scripts to run from the configuration file
256 each one is run and is fed on standard input a list of all .deb files
257 that are due to be installed. */
258 bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf)
259 {
260 Configuration::Item const *Opts = _config->Tree(Cnf);
261 if (Opts == 0 || Opts->Child == 0)
262 return true;
263 Opts = Opts->Child;
264
265 unsigned int Count = 1;
266 for (; Opts != 0; Opts = Opts->Next, Count++)
267 {
268 if (Opts->Value.empty() == true)
269 continue;
270
271 // Determine the protocol version
272 string OptSec = Opts->Value;
273 string::size_type Pos;
274 if ((Pos = OptSec.find(' ')) == string::npos || Pos == 0)
275 Pos = OptSec.length();
276 OptSec = "DPkg::Tools::Options::" + string(Opts->Value.c_str(),Pos);
277
278 unsigned int Version = _config->FindI(OptSec+"::Version",1);
279
280 // Create the pipes
281 int Pipes[2];
282 if (pipe(Pipes) != 0)
283 return _error->Errno("pipe","Failed to create IPC pipe to subprocess");
284 SetCloseExec(Pipes[0],true);
285 SetCloseExec(Pipes[1],true);
286
287 // Purified Fork for running the script
288 pid_t Process = ExecFork();
289 if (Process == 0)
290 {
291 // Setup the FDs
292 dup2(Pipes[0],STDIN_FILENO);
293 SetCloseExec(STDOUT_FILENO,false);
294 SetCloseExec(STDIN_FILENO,false);
295 SetCloseExec(STDERR_FILENO,false);
296
297 const char *Args[4];
298 Args[0] = "/bin/sh";
299 Args[1] = "-c";
300 Args[2] = Opts->Value.c_str();
301 Args[3] = 0;
302 execv(Args[0],(char **)Args);
303 _exit(100);
304 }
305 close(Pipes[0]);
306 FILE *F = fdopen(Pipes[1],"w");
307 if (F == 0)
308 return _error->Errno("fdopen","Faild to open new FD");
309
310 // Feed it the filenames.
311 bool Die = false;
312 if (Version <= 1)
313 {
314 for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
315 {
316 // Only deal with packages to be installed from .deb
317 if (I->Op != Item::Install)
318 continue;
319
320 // No errors here..
321 if (I->File[0] != '/')
322 continue;
323
324 /* Feed the filename of each package that is pending install
325 into the pipe. */
326 fprintf(F,"%s\n",I->File.c_str());
327 if (ferror(F) != 0)
328 {
329 Die = true;
330 break;
331 }
332 }
333 }
334 else
335 Die = !SendV2Pkgs(F);
336
337 fclose(F);
338
339 // Clean up the sub process
340 if (ExecWait(Process,Opts->Value.c_str()) == false)
341 return _error->Error("Failure running script %s",Opts->Value.c_str());
342 }
343
344 return true;
345 }
346 /*}}}*/
347 // DPkgPM::DoStdin - Read stdin and pass to slave pty /*{{{*/
348 // ---------------------------------------------------------------------
349 /*
350 */
351 void pkgDPkgPM::DoStdin(int master)
352 {
353 unsigned char input_buf[256] = {0,};
354 ssize_t len = read(0, input_buf, sizeof(input_buf));
355 if (len)
356 write(master, input_buf, len);
357 else
358 stdin_is_dev_null = true;
359 }
360 /*}}}*/
361 // DPkgPM::DoTerminalPty - Read the terminal pty and write log /*{{{*/
362 // ---------------------------------------------------------------------
363 /*
364 * read the terminal pty and write log
365 */
366 void pkgDPkgPM::DoTerminalPty(int master)
367 {
368 unsigned char term_buf[1024] = {0,0, };
369
370 ssize_t len=read(master, term_buf, sizeof(term_buf));
371 if(len == -1 && errno == EIO)
372 {
373 // this happens when the child is about to exit, we
374 // give it time to actually exit, otherwise we run
375 // into a race
376 usleep(500000);
377 return;
378 }
379 if(len <= 0)
380 return;
381 write(1, term_buf, len);
382 if(term_out)
383 fwrite(term_buf, len, sizeof(char), term_out);
384 }
385 /*}}}*/
386 // DPkgPM::ProcessDpkgStatusBuf /*{{{*/
387 // ---------------------------------------------------------------------
388 /*
389 */
390 void pkgDPkgPM::ProcessDpkgStatusLine(int OutStatusFd, char *line)
391 {
392 bool const Debug = _config->FindB("Debug::pkgDPkgProgressReporting",false);
393 // the status we output
394 ostringstream status;
395
396 if (Debug == true)
397 std::clog << "got from dpkg '" << line << "'" << std::endl;
398
399
400 /* dpkg sends strings like this:
401 'status: <pkg>: <pkg qstate>'
402 errors look like this:
403 '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
404 and conffile-prompt like this
405 'status: conffile-prompt: conffile : 'current-conffile' 'new-conffile' useredited distedited
406
407 Newer versions of dpkg sent also:
408 'processing: install: pkg'
409 'processing: configure: pkg'
410 'processing: remove: pkg'
411 'processing: purge: pkg' - but for apt is it a ignored "unknown" action
412 'processing: trigproc: trigger'
413
414 */
415 char* list[6];
416 // dpkg sends multiline error messages sometimes (see
417 // #374195 for a example. we should support this by
418 // either patching dpkg to not send multiline over the
419 // statusfd or by rewriting the code here to deal with
420 // it. for now we just ignore it and not crash
421 TokSplitString(':', line, list, sizeof(list)/sizeof(list[0]));
422 if( list[0] == NULL || list[1] == NULL || list[2] == NULL)
423 {
424 if (Debug == true)
425 std::clog << "ignoring line: not enough ':'" << std::endl;
426 return;
427 }
428 const char* const pkg = list[1];
429 const char* action = _strstrip(list[2]);
430
431 // 'processing' from dpkg looks like
432 // 'processing: action: pkg'
433 if(strncmp(list[0], "processing", strlen("processing")) == 0)
434 {
435 char s[200];
436 const char* const pkg_or_trigger = _strstrip(list[2]);
437 action = _strstrip( list[1]);
438 const std::pair<const char *, const char *> * const iter =
439 std::find_if(PackageProcessingOpsBegin,
440 PackageProcessingOpsEnd,
441 MatchProcessingOp(action));
442 if(iter == PackageProcessingOpsEnd)
443 {
444 if (Debug == true)
445 std::clog << "ignoring unknown action: " << action << std::endl;
446 return;
447 }
448 snprintf(s, sizeof(s), _(iter->second), pkg_or_trigger);
449
450 status << "pmstatus:" << pkg_or_trigger
451 << ":" << (PackagesDone/float(PackagesTotal)*100.0)
452 << ":" << s
453 << endl;
454 if(OutStatusFd > 0)
455 write(OutStatusFd, status.str().c_str(), status.str().size());
456 if (Debug == true)
457 std::clog << "send: '" << status.str() << "'" << endl;
458 return;
459 }
460
461 if(strncmp(action,"error",strlen("error")) == 0)
462 {
463 // urgs, sometime has ":" in its error string so that we
464 // end up with the error message split between list[3]
465 // and list[4], e.g. the message:
466 // "failed in buffer_write(fd) (10, ret=-1): backend dpkg-deb ..."
467 // concat them again
468 if( list[4] != NULL )
469 list[3][strlen(list[3])] = ':';
470
471 status << "pmerror:" << list[1]
472 << ":" << (PackagesDone/float(PackagesTotal)*100.0)
473 << ":" << list[3]
474 << endl;
475 if(OutStatusFd > 0)
476 write(OutStatusFd, status.str().c_str(), status.str().size());
477 if (Debug == true)
478 std::clog << "send: '" << status.str() << "'" << endl;
479 pkgFailures++;
480 WriteApportReport(list[1], list[3]);
481 return;
482 }
483 else if(strncmp(action,"conffile",strlen("conffile")) == 0)
484 {
485 status << "pmconffile:" << list[1]
486 << ":" << (PackagesDone/float(PackagesTotal)*100.0)
487 << ":" << list[3]
488 << endl;
489 if(OutStatusFd > 0)
490 write(OutStatusFd, status.str().c_str(), status.str().size());
491 if (Debug == true)
492 std::clog << "send: '" << status.str() << "'" << endl;
493 return;
494 }
495
496 vector<struct DpkgState> const &states = PackageOps[pkg];
497 const char *next_action = NULL;
498 if(PackageOpsDone[pkg] < states.size())
499 next_action = states[PackageOpsDone[pkg]].state;
500 // check if the package moved to the next dpkg state
501 if(next_action && (strcmp(action, next_action) == 0))
502 {
503 // only read the translation if there is actually a next
504 // action
505 const char *translation = _(states[PackageOpsDone[pkg]].str);
506 char s[200];
507 snprintf(s, sizeof(s), translation, pkg);
508
509 // we moved from one dpkg state to a new one, report that
510 PackageOpsDone[pkg]++;
511 PackagesDone++;
512 // build the status str
513 status << "pmstatus:" << pkg
514 << ":" << (PackagesDone/float(PackagesTotal)*100.0)
515 << ":" << s
516 << endl;
517 if(OutStatusFd > 0)
518 write(OutStatusFd, status.str().c_str(), status.str().size());
519 if (Debug == true)
520 std::clog << "send: '" << status.str() << "'" << endl;
521 }
522 if (Debug == true)
523 std::clog << "(parsed from dpkg) pkg: " << pkg
524 << " action: " << action << endl;
525 }
526 /*}}}*/
527 // DPkgPM::DoDpkgStatusFd /*{{{*/
528 // ---------------------------------------------------------------------
529 /*
530 */
531 void pkgDPkgPM::DoDpkgStatusFd(int statusfd, int OutStatusFd)
532 {
533 char *p, *q;
534 int len;
535
536 len=read(statusfd, &dpkgbuf[dpkgbuf_pos], sizeof(dpkgbuf)-dpkgbuf_pos);
537 dpkgbuf_pos += len;
538 if(len <= 0)
539 return;
540
541 // process line by line if we have a buffer
542 p = q = dpkgbuf;
543 while((q=(char*)memchr(p, '\n', dpkgbuf+dpkgbuf_pos-p)) != NULL)
544 {
545 *q = 0;
546 ProcessDpkgStatusLine(OutStatusFd, p);
547 p=q+1; // continue with next line
548 }
549
550 // now move the unprocessed bits (after the final \n that is now a 0x0)
551 // to the start and update dpkgbuf_pos
552 p = (char*)memrchr(dpkgbuf, 0, dpkgbuf_pos);
553 if(p == NULL)
554 return;
555
556 // we are interessted in the first char *after* 0x0
557 p++;
558
559 // move the unprocessed tail to the start and update pos
560 memmove(dpkgbuf, p, p-dpkgbuf);
561 dpkgbuf_pos = dpkgbuf+dpkgbuf_pos-p;
562 }
563 /*}}}*/
564 // DPkgPM::WriteHistoryTag /*{{{*/
565 void pkgDPkgPM::WriteHistoryTag(FILE *history_out, string tag, string value)
566 {
567 if (value.size() > 0)
568 {
569 // poor mans rstrip(", ")
570 if (value[value.size()-2] == ',' && value[value.size()-1] == ' ')
571 value.erase(value.size() - 2, 2);
572 fprintf(history_out, "%s: %s\n", tag.c_str(), value.c_str());
573 }
574 } /*}}}*/
575 // DPkgPM::OpenLog /*{{{*/
576 bool pkgDPkgPM::OpenLog()
577 {
578 string const logdir = _config->FindDir("Dir::Log");
579 if(not FileExists(logdir))
580 return _error->Error(_("Directory '%s' missing"), logdir.c_str());
581
582 // get current time
583 char timestr[200];
584 time_t const t = time(NULL);
585 struct tm const * const tmp = localtime(&t);
586 strftime(timestr, sizeof(timestr), "%F %T", tmp);
587
588 // open terminal log
589 string const logfile_name = flCombine(logdir,
590 _config->Find("Dir::Log::Terminal"));
591 if (!logfile_name.empty())
592 {
593 term_out = fopen(logfile_name.c_str(),"a");
594 if (term_out == NULL)
595 return _error->WarningE("OpenLog", _("Could not open file '%s'"), logfile_name.c_str());
596
597 chmod(logfile_name.c_str(), 0600);
598 fprintf(term_out, "\nLog started: %s\n", timestr);
599 }
600
601 // write your history
602 string const history_name = flCombine(logdir,
603 _config->Find("Dir::Log::History"));
604 if (!history_name.empty())
605 {
606 FILE *history_out = fopen(history_name.c_str(),"a");
607 if (history_out == NULL)
608 return _error->WarningE("OpenLog", _("Could not open file '%s'"), history_name.c_str());
609 chmod(history_name.c_str(), 0644);
610 fprintf(history_out, "\nStart-Date: %s\n", timestr);
611 string remove, purge, install, upgrade, downgrade;
612 for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
613 {
614 if (Cache[I].NewInstall())
615 install += I.Name() + string(" (") + Cache[I].CandVersion + string("), ");
616 else if (Cache[I].Upgrade())
617 upgrade += I.Name() + string(" (") + Cache[I].CurVersion + string(", ") + Cache[I].CandVersion + string("), ");
618 else if (Cache[I].Downgrade())
619 downgrade += I.Name() + string(" (") + Cache[I].CurVersion + string(", ") + Cache[I].CandVersion + string("), ");
620 else if (Cache[I].Delete())
621 {
622 if ((Cache[I].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge)
623 purge += I.Name() + string(" (") + Cache[I].CurVersion + string("), ");
624 else
625 remove += I.Name() + string(" (") + Cache[I].CurVersion + string("), ");
626 }
627 }
628 WriteHistoryTag(history_out, "Install", install);
629 WriteHistoryTag(history_out, "Upgrade", upgrade);
630 WriteHistoryTag(history_out, "Downgrade",downgrade);
631 WriteHistoryTag(history_out, "Remove",remove);
632 WriteHistoryTag(history_out, "Purge",purge);
633 fclose(history_out);
634 }
635
636 return true;
637 }
638 /*}}}*/
639 // DPkg::CloseLog /*{{{*/
640 bool pkgDPkgPM::CloseLog()
641 {
642 char timestr[200];
643 time_t t = time(NULL);
644 struct tm *tmp = localtime(&t);
645 strftime(timestr, sizeof(timestr), "%F %T", tmp);
646
647 if(term_out)
648 {
649 fprintf(term_out, "Log ended: ");
650 fprintf(term_out, "%s", timestr);
651 fprintf(term_out, "\n");
652 fclose(term_out);
653 }
654 term_out = NULL;
655
656 // check if the directory exists in which we want to write the file
657 string const logdir = _config->FindDir("Dir::Log");
658 if(not FileExists(logdir))
659 return _error->Error(_("Directory '%s' missing"), logdir.c_str());
660
661 string history_name = flCombine(_config->FindDir("Dir::Log"),
662 _config->Find("Dir::Log::History"));
663 if (!history_name.empty())
664 {
665 FILE *history_out = fopen(history_name.c_str(),"a");
666 fprintf(history_out, "End-Date: %s\n", timestr);
667 fclose(history_out);
668 }
669
670 return true;
671 }
672 /*}}}*/
673 /*{{{*/
674 // This implements a racy version of pselect for those architectures
675 // that don't have a working implementation.
676 // FIXME: Probably can be removed on Lenny+1
677 static int racy_pselect(int nfds, fd_set *readfds, fd_set *writefds,
678 fd_set *exceptfds, const struct timespec *timeout,
679 const sigset_t *sigmask)
680 {
681 sigset_t origmask;
682 struct timeval tv;
683 int retval;
684
685 tv.tv_sec = timeout->tv_sec;
686 tv.tv_usec = timeout->tv_nsec/1000;
687
688 sigprocmask(SIG_SETMASK, sigmask, &origmask);
689 retval = select(nfds, readfds, writefds, exceptfds, &tv);
690 sigprocmask(SIG_SETMASK, &origmask, 0);
691 return retval;
692 }
693 /*}}}*/
694 // DPkgPM::Go - Run the sequence /*{{{*/
695 // ---------------------------------------------------------------------
696 /* This globs the operations and calls dpkg
697 *
698 * If it is called with "OutStatusFd" set to a valid file descriptor
699 * apt will report the install progress over this fd. It maps the
700 * dpkg states a package goes through to human readable (and i10n-able)
701 * names and calculates a percentage for each step.
702 */
703 bool pkgDPkgPM::Go(int OutStatusFd)
704 {
705 fd_set rfds;
706 struct timespec tv;
707 sigset_t sigmask;
708 sigset_t original_sigmask;
709
710 unsigned int const MaxArgs = _config->FindI("Dpkg::MaxArgs",8*1024);
711 unsigned int const MaxArgBytes = _config->FindI("Dpkg::MaxArgBytes",32*1024);
712 bool const NoTriggers = _config->FindB("DPkg::NoTriggers", false);
713
714 if (RunScripts("DPkg::Pre-Invoke") == false)
715 return false;
716
717 if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
718 return false;
719
720 // support subpressing of triggers processing for special
721 // cases like d-i that runs the triggers handling manually
722 bool const SmartConf = (_config->Find("PackageManager::Configure", "all") != "all");
723 bool const TriggersPending = _config->FindB("DPkg::TriggersPending", false);
724 if (_config->FindB("DPkg::ConfigurePending", SmartConf) == true)
725 List.push_back(Item(Item::ConfigurePending, PkgIterator()));
726
727 // map the dpkg states to the operations that are performed
728 // (this is sorted in the same way as Item::Ops)
729 static const struct DpkgState DpkgStatesOpMap[][7] = {
730 // Install operation
731 {
732 {"half-installed", N_("Preparing %s")},
733 {"unpacked", N_("Unpacking %s") },
734 {NULL, NULL}
735 },
736 // Configure operation
737 {
738 {"unpacked",N_("Preparing to configure %s") },
739 {"half-configured", N_("Configuring %s") },
740 { "installed", N_("Installed %s")},
741 {NULL, NULL}
742 },
743 // Remove operation
744 {
745 {"half-configured", N_("Preparing for removal of %s")},
746 {"half-installed", N_("Removing %s")},
747 {"config-files", N_("Removed %s")},
748 {NULL, NULL}
749 },
750 // Purge operation
751 {
752 {"config-files", N_("Preparing to completely remove %s")},
753 {"not-installed", N_("Completely removed %s")},
754 {NULL, NULL}
755 },
756 };
757
758 // init the PackageOps map, go over the list of packages that
759 // that will be [installed|configured|removed|purged] and add
760 // them to the PackageOps map (the dpkg states it goes through)
761 // and the PackageOpsTranslations (human readable strings)
762 for (vector<Item>::const_iterator I = List.begin(); I != List.end();I++)
763 {
764 if((*I).Pkg.end() == true)
765 continue;
766
767 string const name = (*I).Pkg.Name();
768 PackageOpsDone[name] = 0;
769 for(int i=0; (DpkgStatesOpMap[(*I).Op][i]).state != NULL; i++)
770 {
771 PackageOps[name].push_back(DpkgStatesOpMap[(*I).Op][i]);
772 PackagesTotal++;
773 }
774 }
775
776 stdin_is_dev_null = false;
777
778 // create log
779 OpenLog();
780
781 // this loop is runs once per operation
782 for (vector<Item>::const_iterator I = List.begin(); I != List.end();)
783 {
784 // Do all actions with the same Op in one run
785 vector<Item>::const_iterator J = I;
786 if (TriggersPending == true)
787 for (; J != List.end(); J++)
788 {
789 if (J->Op == I->Op)
790 continue;
791 if (J->Op != Item::TriggersPending)
792 break;
793 vector<Item>::const_iterator T = J + 1;
794 if (T != List.end() && T->Op == I->Op)
795 continue;
796 break;
797 }
798 else
799 for (; J != List.end() && J->Op == I->Op; J++)
800 /* nothing */;
801
802 // Generate the argument list
803 const char *Args[MaxArgs + 50];
804
805 // Now check if we are within the MaxArgs limit
806 //
807 // this code below is problematic, because it may happen that
808 // the argument list is split in a way that A depends on B
809 // and they are in the same "--configure A B" run
810 // - with the split they may now be configured in different
811 // runs
812 if (J - I > (signed)MaxArgs)
813 J = I + MaxArgs;
814
815 unsigned int n = 0;
816 unsigned long Size = 0;
817 string const Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
818 Args[n++] = Tmp.c_str();
819 Size += strlen(Args[n-1]);
820
821 // Stick in any custom dpkg options
822 Configuration::Item const *Opts = _config->Tree("DPkg::Options");
823 if (Opts != 0)
824 {
825 Opts = Opts->Child;
826 for (; Opts != 0; Opts = Opts->Next)
827 {
828 if (Opts->Value.empty() == true)
829 continue;
830 Args[n++] = Opts->Value.c_str();
831 Size += Opts->Value.length();
832 }
833 }
834
835 char status_fd_buf[20];
836 int fd[2];
837 pipe(fd);
838
839 Args[n++] = "--status-fd";
840 Size += strlen(Args[n-1]);
841 snprintf(status_fd_buf,sizeof(status_fd_buf),"%i", fd[1]);
842 Args[n++] = status_fd_buf;
843 Size += strlen(Args[n-1]);
844
845 switch (I->Op)
846 {
847 case Item::Remove:
848 Args[n++] = "--force-depends";
849 Size += strlen(Args[n-1]);
850 Args[n++] = "--force-remove-essential";
851 Size += strlen(Args[n-1]);
852 Args[n++] = "--remove";
853 Size += strlen(Args[n-1]);
854 break;
855
856 case Item::Purge:
857 Args[n++] = "--force-depends";
858 Size += strlen(Args[n-1]);
859 Args[n++] = "--force-remove-essential";
860 Size += strlen(Args[n-1]);
861 Args[n++] = "--purge";
862 Size += strlen(Args[n-1]);
863 break;
864
865 case Item::Configure:
866 Args[n++] = "--configure";
867 Size += strlen(Args[n-1]);
868 break;
869
870 case Item::ConfigurePending:
871 Args[n++] = "--configure";
872 Size += strlen(Args[n-1]);
873 Args[n++] = "--pending";
874 Size += strlen(Args[n-1]);
875 break;
876
877 case Item::TriggersPending:
878 Args[n++] = "--triggers-only";
879 Size += strlen(Args[n-1]);
880 Args[n++] = "--pending";
881 Size += strlen(Args[n-1]);
882 break;
883
884 case Item::Install:
885 Args[n++] = "--unpack";
886 Size += strlen(Args[n-1]);
887 Args[n++] = "--auto-deconfigure";
888 Size += strlen(Args[n-1]);
889 break;
890 }
891
892 if (NoTriggers == true && I->Op != Item::TriggersPending &&
893 I->Op != Item::ConfigurePending)
894 {
895 Args[n++] = "--no-triggers";
896 Size += strlen(Args[n-1]);
897 }
898
899 // Write in the file or package names
900 if (I->Op == Item::Install)
901 {
902 for (;I != J && Size < MaxArgBytes; I++)
903 {
904 if (I->File[0] != '/')
905 return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str());
906 Args[n++] = I->File.c_str();
907 Size += strlen(Args[n-1]);
908 }
909 }
910 else
911 {
912 for (;I != J && Size < MaxArgBytes; I++)
913 {
914 if((*I).Pkg.end() == true)
915 continue;
916 Args[n++] = I->Pkg.Name();
917 Size += strlen(Args[n-1]);
918 }
919 }
920 Args[n] = 0;
921 J = I;
922
923 if (_config->FindB("Debug::pkgDPkgPM",false) == true)
924 {
925 for (unsigned int k = 0; k != n; k++)
926 clog << Args[k] << ' ';
927 clog << endl;
928 continue;
929 }
930
931 cout << flush;
932 clog << flush;
933 cerr << flush;
934
935 /* Mask off sig int/quit. We do this because dpkg also does when
936 it forks scripts. What happens is that when you hit ctrl-c it sends
937 it to all processes in the group. Since dpkg ignores the signal
938 it doesn't die but we do! So we must also ignore it */
939 sighandler_t old_SIGQUIT = signal(SIGQUIT,SIG_IGN);
940 sighandler_t old_SIGINT = signal(SIGINT,SIG_IGN);
941
942 // ignore SIGHUP as well (debian #463030)
943 sighandler_t old_SIGHUP = signal(SIGHUP,SIG_IGN);
944
945 struct termios tt;
946 struct winsize win;
947 int master = -1;
948 int slave = -1;
949
950 // if tcgetattr does not return zero there was a error
951 // and we do not do any pty magic
952 if (tcgetattr(0, &tt) == 0)
953 {
954 ioctl(0, TIOCGWINSZ, (char *)&win);
955 if (openpty(&master, &slave, NULL, &tt, &win) < 0)
956 {
957 const char *s = _("Can not write log, openpty() "
958 "failed (/dev/pts not mounted?)\n");
959 fprintf(stderr, "%s",s);
960 if(term_out)
961 fprintf(term_out, "%s",s);
962 master = slave = -1;
963 } else {
964 struct termios rtt;
965 rtt = tt;
966 cfmakeraw(&rtt);
967 rtt.c_lflag &= ~ECHO;
968 rtt.c_lflag |= ISIG;
969 // block SIGTTOU during tcsetattr to prevent a hang if
970 // the process is a member of the background process group
971 // http://www.opengroup.org/onlinepubs/000095399/functions/tcsetattr.html
972 sigemptyset(&sigmask);
973 sigaddset(&sigmask, SIGTTOU);
974 sigprocmask(SIG_BLOCK,&sigmask, &original_sigmask);
975 tcsetattr(0, TCSAFLUSH, &rtt);
976 sigprocmask(SIG_SETMASK, &original_sigmask, 0);
977 }
978 }
979
980 // Fork dpkg
981 pid_t Child;
982 _config->Set("APT::Keep-Fds::",fd[1]);
983 // send status information that we are about to fork dpkg
984 if(OutStatusFd > 0) {
985 ostringstream status;
986 status << "pmstatus:dpkg-exec:"
987 << (PackagesDone/float(PackagesTotal)*100.0)
988 << ":" << _("Running dpkg")
989 << endl;
990 write(OutStatusFd, status.str().c_str(), status.str().size());
991 }
992 Child = ExecFork();
993
994 // This is the child
995 if (Child == 0)
996 {
997 if(slave >= 0 && master >= 0)
998 {
999 setsid();
1000 ioctl(slave, TIOCSCTTY, 0);
1001 close(master);
1002 dup2(slave, 0);
1003 dup2(slave, 1);
1004 dup2(slave, 2);
1005 close(slave);
1006 }
1007 close(fd[0]); // close the read end of the pipe
1008
1009 if (_config->FindDir("DPkg::Chroot-Directory","/") != "/")
1010 {
1011 std::cerr << "Chrooting into "
1012 << _config->FindDir("DPkg::Chroot-Directory")
1013 << std::endl;
1014 if (chroot(_config->FindDir("DPkg::Chroot-Directory","/").c_str()) != 0)
1015 _exit(100);
1016 }
1017
1018 if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
1019 _exit(100);
1020
1021 if (_config->FindB("DPkg::FlushSTDIN",true) == true && isatty(STDIN_FILENO))
1022 {
1023 int Flags,dummy;
1024 if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0)
1025 _exit(100);
1026
1027 // Discard everything in stdin before forking dpkg
1028 if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0)
1029 _exit(100);
1030
1031 while (read(STDIN_FILENO,&dummy,1) == 1);
1032
1033 if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0)
1034 _exit(100);
1035 }
1036
1037 /* No Job Control Stop Env is a magic dpkg var that prevents it
1038 from using sigstop */
1039 putenv((char *)"DPKG_NO_TSTP=yes");
1040 execvp(Args[0],(char **)Args);
1041 cerr << "Could not exec dpkg!" << endl;
1042 _exit(100);
1043 }
1044
1045 // apply ionice
1046 if (_config->FindB("DPkg::UseIoNice", false) == true)
1047 ionice(Child);
1048
1049 // clear the Keep-Fd again
1050 _config->Clear("APT::Keep-Fds",fd[1]);
1051
1052 // Wait for dpkg
1053 int Status = 0;
1054
1055 // we read from dpkg here
1056 int const _dpkgin = fd[0];
1057 close(fd[1]); // close the write end of the pipe
1058
1059 if(slave > 0)
1060 close(slave);
1061
1062 // setups fds
1063 sigemptyset(&sigmask);
1064 sigprocmask(SIG_BLOCK,&sigmask,&original_sigmask);
1065
1066 // the result of the waitpid call
1067 int res;
1068 int select_ret;
1069 while ((res=waitpid(Child,&Status, WNOHANG)) != Child) {
1070 if(res < 0) {
1071 // FIXME: move this to a function or something, looks ugly here
1072 // error handling, waitpid returned -1
1073 if (errno == EINTR)
1074 continue;
1075 RunScripts("DPkg::Post-Invoke");
1076
1077 // Restore sig int/quit
1078 signal(SIGQUIT,old_SIGQUIT);
1079 signal(SIGINT,old_SIGINT);
1080 signal(SIGHUP,old_SIGHUP);
1081 return _error->Errno("waitpid","Couldn't wait for subprocess");
1082 }
1083 // wait for input or output here
1084 FD_ZERO(&rfds);
1085 if (!stdin_is_dev_null)
1086 FD_SET(0, &rfds);
1087 FD_SET(_dpkgin, &rfds);
1088 if(master >= 0)
1089 FD_SET(master, &rfds);
1090 tv.tv_sec = 1;
1091 tv.tv_nsec = 0;
1092 select_ret = pselect(max(master, _dpkgin)+1, &rfds, NULL, NULL,
1093 &tv, &original_sigmask);
1094 if (select_ret < 0 && (errno == EINVAL || errno == ENOSYS))
1095 select_ret = racy_pselect(max(master, _dpkgin)+1, &rfds, NULL,
1096 NULL, &tv, &original_sigmask);
1097 if (select_ret == 0)
1098 continue;
1099 else if (select_ret < 0 && errno == EINTR)
1100 continue;
1101 else if (select_ret < 0)
1102 {
1103 perror("select() returned error");
1104 continue;
1105 }
1106
1107 if(master >= 0 && FD_ISSET(master, &rfds))
1108 DoTerminalPty(master);
1109 if(master >= 0 && FD_ISSET(0, &rfds))
1110 DoStdin(master);
1111 if(FD_ISSET(_dpkgin, &rfds))
1112 DoDpkgStatusFd(_dpkgin, OutStatusFd);
1113 }
1114 close(_dpkgin);
1115
1116 // Restore sig int/quit
1117 signal(SIGQUIT,old_SIGQUIT);
1118 signal(SIGINT,old_SIGINT);
1119 signal(SIGHUP,old_SIGHUP);
1120
1121 if(master >= 0)
1122 {
1123 tcsetattr(0, TCSAFLUSH, &tt);
1124 close(master);
1125 }
1126
1127 // Check for an error code.
1128 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
1129 {
1130 // if it was set to "keep-dpkg-runing" then we won't return
1131 // here but keep the loop going and just report it as a error
1132 // for later
1133 bool const stopOnError = _config->FindB("Dpkg::StopOnError",true);
1134
1135 if(stopOnError)
1136 RunScripts("DPkg::Post-Invoke");
1137
1138 string dpkg_error;
1139 if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV)
1140 strprintf(dpkg_error, "Sub-process %s received a segmentation fault.",Args[0]);
1141 else if (WIFEXITED(Status) != 0)
1142 strprintf(dpkg_error, "Sub-process %s returned an error code (%u)",Args[0],WEXITSTATUS(Status));
1143 else
1144 strprintf(dpkg_error, "Sub-process %s exited unexpectedly",Args[0]);
1145
1146 if(dpkg_error.size() > 0)
1147 {
1148 _error->Error(dpkg_error.c_str());
1149 string history_name = flCombine(_config->FindDir("Dir::Log"),
1150 _config->Find("Dir::Log::History"));
1151 if (!history_name.empty())
1152 {
1153 FILE *history_out = fopen(history_name.c_str(),"a");
1154 fprintf(history_out, "Error: %s\n", dpkg_error.c_str());
1155 fclose(history_out);
1156 }
1157 }
1158
1159 if(stopOnError)
1160 {
1161 CloseLog();
1162 return false;
1163 }
1164 }
1165 }
1166 CloseLog();
1167
1168 if (RunScripts("DPkg::Post-Invoke") == false)
1169 return false;
1170
1171 Cache.writeStateFile(NULL);
1172 return true;
1173 }
1174 /*}}}*/
1175 // pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
1176 // ---------------------------------------------------------------------
1177 /* */
1178 void pkgDPkgPM::Reset()
1179 {
1180 List.erase(List.begin(),List.end());
1181 }
1182 /*}}}*/
1183 // pkgDpkgPM::WriteApportReport - write out error report pkg failure /*{{{*/
1184 // ---------------------------------------------------------------------
1185 /* */
1186 void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg)
1187 {
1188 string pkgname, reportfile, srcpkgname, pkgver, arch;
1189 string::size_type pos;
1190 FILE *report;
1191
1192 if (_config->FindB("Dpkg::ApportFailureReport",true) == false)
1193 {
1194 std::clog << "configured to not write apport reports" << std::endl;
1195 return;
1196 }
1197
1198 // only report the first errors
1199 if(pkgFailures > _config->FindI("APT::Apport::MaxReports", 3))
1200 {
1201 std::clog << _("No apport report written because MaxReports is reached already") << std::endl;
1202 return;
1203 }
1204
1205 // check if its not a follow up error
1206 const char *needle = dgettext("dpkg", "dependency problems - leaving unconfigured");
1207 if(strstr(errormsg, needle) != NULL) {
1208 std::clog << _("No apport report written because the error message indicates its a followup error from a previous failure.") << std::endl;
1209 return;
1210 }
1211
1212 // do not report disk-full failures
1213 if(strstr(errormsg, strerror(ENOSPC)) != NULL) {
1214 std::clog << _("No apport report written because the error message indicates a disk full error") << std::endl;
1215 return;
1216 }
1217
1218 // do not report out-of-memory failures
1219 if(strstr(errormsg, strerror(ENOMEM)) != NULL) {
1220 std::clog << _("No apport report written because the error message indicates a out of memory error") << std::endl;
1221 return;
1222 }
1223
1224 // do not report dpkg I/O errors
1225 // XXX - this message is localized, but this only matches the English version. This is better than nothing.
1226 if(strstr(errormsg, "short read in buffer_copy (")) {
1227 std::clog << _("No apport report written because the error message indicates a dpkg I/O error") << std::endl;
1228 return;
1229 }
1230
1231 // get the pkgname and reportfile
1232 pkgname = flNotDir(pkgpath);
1233 pos = pkgname.find('_');
1234 if(pos != string::npos)
1235 pkgname = pkgname.substr(0, pos);
1236
1237 // find the package versin and source package name
1238 pkgCache::PkgIterator Pkg = Cache.FindPkg(pkgname);
1239 if (Pkg.end() == true)
1240 return;
1241 pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
1242 if (Ver.end() == true)
1243 return;
1244 pkgver = Ver.VerStr() == NULL ? "unknown" : Ver.VerStr();
1245 pkgRecords Recs(Cache);
1246 pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
1247 srcpkgname = Parse.SourcePkg();
1248 if(srcpkgname.empty())
1249 srcpkgname = pkgname;
1250
1251 // if the file exists already, we check:
1252 // - if it was reported already (touched by apport).
1253 // If not, we do nothing, otherwise
1254 // we overwrite it. This is the same behaviour as apport
1255 // - if we have a report with the same pkgversion already
1256 // then we skip it
1257 reportfile = flCombine("/var/crash",pkgname+".0.crash");
1258 if(FileExists(reportfile))
1259 {
1260 struct stat buf;
1261 char strbuf[255];
1262
1263 // check atime/mtime
1264 stat(reportfile.c_str(), &buf);
1265 if(buf.st_mtime > buf.st_atime)
1266 return;
1267
1268 // check if the existing report is the same version
1269 report = fopen(reportfile.c_str(),"r");
1270 while(fgets(strbuf, sizeof(strbuf), report) != NULL)
1271 {
1272 if(strstr(strbuf,"Package:") == strbuf)
1273 {
1274 char pkgname[255], version[255];
1275 if(sscanf(strbuf, "Package: %s %s", pkgname, version) == 2)
1276 if(strcmp(pkgver.c_str(), version) == 0)
1277 {
1278 fclose(report);
1279 return;
1280 }
1281 }
1282 }
1283 fclose(report);
1284 }
1285
1286 // now write the report
1287 arch = _config->Find("APT::Architecture");
1288 report = fopen(reportfile.c_str(),"w");
1289 if(report == NULL)
1290 return;
1291 if(_config->FindB("DPkgPM::InitialReportOnly",false) == true)
1292 chmod(reportfile.c_str(), 0);
1293 else
1294 chmod(reportfile.c_str(), 0600);
1295 fprintf(report, "ProblemType: Package\n");
1296 fprintf(report, "Architecture: %s\n", arch.c_str());
1297 time_t now = time(NULL);
1298 fprintf(report, "Date: %s" , ctime(&now));
1299 fprintf(report, "Package: %s %s\n", pkgname.c_str(), pkgver.c_str());
1300 fprintf(report, "SourcePackage: %s\n", srcpkgname.c_str());
1301 fprintf(report, "ErrorMessage:\n %s\n", errormsg);
1302
1303 // ensure that the log is flushed
1304 if(term_out)
1305 fflush(term_out);
1306
1307 // attach terminal log it if we have it
1308 string logfile_name = _config->FindFile("Dir::Log::Terminal");
1309 if (!logfile_name.empty())
1310 {
1311 FILE *log = NULL;
1312 char buf[1024];
1313
1314 fprintf(report, "DpkgTerminalLog:\n");
1315 log = fopen(logfile_name.c_str(),"r");
1316 if(log != NULL)
1317 {
1318 while( fgets(buf, sizeof(buf), log) != NULL)
1319 fprintf(report, " %s", buf);
1320 fclose(log);
1321 }
1322 }
1323
1324 // log the ordering
1325 const char *ops_str[] = {"Install", "Configure","Remove","Purge"};
1326 fprintf(report, "AptOrdering:\n");
1327 for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
1328 fprintf(report, " %s: %s\n", (*I).Pkg.Name(), ops_str[(*I).Op]);
1329
1330 // attach dmesg log (to learn about segfaults)
1331 if (FileExists("/bin/dmesg"))
1332 {
1333 FILE *log = NULL;
1334 char buf[1024];
1335
1336 fprintf(report, "Dmesg:\n");
1337 log = popen("/bin/dmesg","r");
1338 if(log != NULL)
1339 {
1340 while( fgets(buf, sizeof(buf), log) != NULL)
1341 fprintf(report, " %s", buf);
1342 fclose(log);
1343 }
1344 }
1345
1346 // attach df -l log (to learn about filesystem status)
1347 if (FileExists("/bin/df"))
1348 {
1349 FILE *log = NULL;
1350 char buf[1024];
1351
1352 fprintf(report, "Df:\n");
1353 log = popen("/bin/df -l","r");
1354 if(log != NULL)
1355 {
1356 while( fgets(buf, sizeof(buf), log) != NULL)
1357 fprintf(report, " %s", buf);
1358 fclose(log);
1359 }
1360 }
1361
1362 fclose(report);
1363
1364 }
1365 /*}}}*/