]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/deb/dpkgpm.cc
* merged from apt--mvo
[apt.git] / apt-pkg / deb / dpkgpm.cc
index bef35ab4c49b621abf8b5672c6d1f6c41d1556be..de84ba96af445d88e3665cb1d7011610b3459740 100644 (file)
@@ -12,6 +12,7 @@
 #include <apt-pkg/error.h>
 #include <apt-pkg/configuration.h>
 #include <apt-pkg/depcache.h>
+#include <apt-pkg/pkgrecords.h>
 #include <apt-pkg/strutl.h>
 
 #include <unistd.h>
@@ -43,7 +44,8 @@ using namespace std;
 // ---------------------------------------------------------------------
 /* */
 pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache) 
-   : pkgPackageManager(Cache), dpkgbuf_pos(0), Total(0), Done(0)
+   : pkgPackageManager(Cache), dpkgbuf_pos(0), 
+     PackagesTotal(0), PackagesDone(0), term_out(NULL)
 {
 }
                                                                        /*}}}*/
@@ -350,15 +352,16 @@ void pkgDPkgPM::DoStdin(int master)
 /*
  * read the terminal pty and write log
  */
-void pkgDPkgPM::DoTerminalPty(int master, FILE *term_out)
+void pkgDPkgPM::DoTerminalPty(int master)
 {
    char term_buf[1024] = {0,};
 
    int len=read(master, term_buf, sizeof(term_buf));
    if(len <= 0)
       return;
-   fwrite(term_buf, len, sizeof(char), term_out);
    write(1, term_buf, len);
+   if(term_out)
+      fwrite(term_buf, len, sizeof(char), term_out);
 }
                                                                        /*}}}*/
 // DPkgPM::ProcessDpkgStatusBuf                                                /*{{{*/
@@ -401,19 +404,21 @@ void pkgDPkgPM::ProcessDpkgStatusLine(int OutStatusFd, char *line)
    if(strncmp(action,"error",strlen("error")) == 0)
    {
       status << "pmerror:" << list[1]
-            << ":"  << (Done/float(Total)*100.0) 
+            << ":"  << (PackagesDone/float(PackagesTotal)*100.0) 
             << ":" << list[3]
             << endl;
       if(OutStatusFd > 0)
         write(OutStatusFd, status.str().c_str(), status.str().size());
       if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
         std::clog << "send: '" << status.str() << "'" << endl;
+      pkgFailures++;
+      WriteApportReport(list[1], list[3]);
       return;
    }
    if(strncmp(action,"conffile",strlen("conffile")) == 0)
    {
       status << "pmconffile:" << list[1]
-            << ":"  << (Done/float(Total)*100.0) 
+            << ":"  << (PackagesDone/float(PackagesTotal)*100.0) 
             << ":" << list[3]
             << endl;
       if(OutStatusFd > 0)
@@ -438,10 +443,10 @@ void pkgDPkgPM::ProcessDpkgStatusLine(int OutStatusFd, char *line)
 
       // we moved from one dpkg state to a new one, report that
       PackageOpsDone[pkg]++;
-      Done++;
+      PackagesDone++;
       // build the status str
       status << "pmstatus:" << pkg 
-            << ":"  << (Done/float(Total)*100.0) 
+            << ":"  << (PackagesDone/float(PackagesTotal)*100.0) 
             << ":" << s
             << endl;
       if(OutStatusFd > 0)
@@ -555,10 +560,30 @@ bool pkgDPkgPM::Go(int OutStatusFd)
       for(int i=0; (DpkgStatesOpMap[(*I).Op][i]).state != NULL;  i++) 
       {
         PackageOps[name].push_back(DpkgStatesOpMap[(*I).Op][i]);
-        Total++;
+        PackagesTotal++;
       }
    }   
 
+   // create log
+   string logdir = _config->FindDir("Dir::Log");
+   if(not FileExists(logdir))
+      return _error->Error(_("Directory '%s' missing"), logdir.c_str());
+   string logfile_name = flCombine(logdir,
+                                  _config->Find("Dir::Log::Terminal"));
+   if (!logfile_name.empty())
+   {
+      term_out = fopen(logfile_name.c_str(),"a");
+      chmod(logfile_name.c_str(), 0600);
+      // output current time
+      char outstr[200];
+      time_t t = time(NULL);
+      struct tm *tmp = localtime(&t);
+      strftime(outstr, sizeof(outstr), "%F  %T", tmp);
+      fprintf(term_out, "\nLog started: ");
+      fprintf(term_out, outstr);
+      fprintf(term_out, "\n");
+   }
+
    // this loop is runs once per operation
    for (vector<Item>::iterator I = List.begin(); I != List.end();)
    {
@@ -748,25 +773,10 @@ bool pkgDPkgPM::Go(int OutStatusFd)
       int _dpkgin = fd[0];
       close(fd[1]);                        // close the write end of the pipe
 
-      // the read buffers for the communication with dpkg
-      char buf[2] = {0,0};
-      
       // the result of the waitpid call
       int res;
       close(slave);
 
-      // FIXME: make this a apt config option and add a logrotate file
-      FILE *term_out = fopen("/var/log/dpkg-out.log","a");
-      chmod("/var/log/dpkg-out.log", 0600);
-      // output current time
-      char outstr[200];
-      time_t t = time(NULL);
-      struct tm *tmp = localtime(&t);
-      strftime(outstr, sizeof(outstr), "%F  %T", tmp);
-      fprintf(term_out, "Log started: ");
-      fprintf(term_out, outstr);
-      fprintf(term_out, "\n");
-
       // setups fds
       fd_set rfds;
       struct timeval tv;
@@ -799,14 +809,13 @@ bool pkgDPkgPM::Go(int OutStatusFd)
            continue;
 
         if(FD_ISSET(master, &rfds))
-           DoTerminalPty(master, term_out);
+           DoTerminalPty(master);
         if(FD_ISSET(0, &rfds))
            DoStdin(master);
         if(FD_ISSET(_dpkgin, &rfds))
            DoDpkgStatusFd(_dpkgin, OutStatusFd);
       }
       close(_dpkgin);
-      fclose(term_out);
 
       // Restore sig int/quit
       signal(SIGQUIT,old_SIGQUIT);
@@ -832,10 +841,16 @@ bool pkgDPkgPM::Go(int OutStatusFd)
         else 
            _error->Error("Sub-process %s exited unexpectedly",Args[0]);
 
-        if(stopOnError)
+        if(stopOnError) 
+        {
+           if(term_out)
+              fclose(term_out);
            return false;
+        }
       }      
    }
+   if(term_out)
+      fclose(term_out);
 
    if (RunScripts("DPkg::Post-Invoke") == false)
       return false;
@@ -850,3 +865,115 @@ void pkgDPkgPM::Reset()
    List.erase(List.begin(),List.end());
 }
                                                                        /*}}}*/
+// pkgDpkgPM::WriteApportReport - write out error report pkg failure   /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg) 
+{
+   string pkgname, reportfile, srcpkgname, pkgver, arch;
+   string::size_type pos;
+   FILE *report;
+
+   if (_config->FindB("Dpkg::ApportFailureReport",true) == false)
+      return;
+
+   // only report the first error if we are in StopOnError=false mode
+   // to prevent bogus reports
+   if((_config->FindB("Dpkg::StopOnError",true) == false) && pkgFailures > 1)
+      return;
+
+   // get the pkgname and reportfile
+   pkgname = flNotDir(pkgpath);
+   pos = pkgname.rfind('_');
+   if(pos != string::npos)
+      pkgname = string(pkgname, 0, pos);
+
+   // find the package versin and source package name
+   pkgCache::PkgIterator Pkg = Cache.FindPkg(pkgname);
+   if (Pkg.end() == true)
+      return;
+   pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
+   pkgver = Ver.VerStr();
+   if (Ver.end() == true)
+      return;
+   pkgRecords Recs(Cache);
+   pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
+   srcpkgname = Parse.SourcePkg();
+   if(srcpkgname.empty())
+      srcpkgname = pkgname;
+
+   // if the file exists already, we check:
+   // - if it was reported already (touched by apport). 
+   //   If not, we do nothing, otherwise
+   //    we overwrite it. This is the same behaviour as apport
+   // - if we have a report with the same pkgversion already
+   //   then we skip it
+   reportfile = flCombine("/var/crash",pkgname+".0.crash");
+   if(FileExists(reportfile))
+   {
+      struct stat buf;
+      char strbuf[255];
+
+      // check atime/mtime
+      stat(reportfile.c_str(), &buf);
+      if(buf.st_mtime > buf.st_atime)
+        return;
+
+      // check if the existing report is the same version
+      report = fopen(reportfile.c_str(),"r");
+      while(fgets(strbuf, sizeof(strbuf), report) != NULL)
+      {
+        if(strstr(strbuf,"Package:") == strbuf)
+        {
+           char pkgname[255], version[255];
+           if(sscanf(strbuf, "Package: %s %s", pkgname, version) == 2)
+              if(strcmp(pkgver.c_str(), version) == 0)
+              {
+                 fclose(report);
+                 return;
+              }
+        }
+      }
+      fclose(report);
+   }
+
+   // now write the report
+   arch = _config->Find("APT::Architecture");
+   report = fopen(reportfile.c_str(),"w");
+   if(report == NULL)
+      return;
+   if(_config->FindB("DPkgPM::InitialReportOnly",false) == true)
+      chmod(reportfile.c_str(), 0);
+   else
+      chmod(reportfile.c_str(), 0600);
+   fprintf(report, "ProblemType: Package\n");
+   fprintf(report, "Architecture: %s\n", arch.c_str());
+   time_t now = time(NULL);
+   fprintf(report, "Date: %s" , ctime(&now));
+   fprintf(report, "Package: %s %s\n", pkgname.c_str(), pkgver.c_str());
+   fprintf(report, "SourcePackage: %s\n", srcpkgname.c_str());
+   fprintf(report, "ErrorMessage:\n %s\n", errormsg);
+
+   // ensure that the log is flushed
+   if(term_out)
+      fflush(term_out);
+
+   // attach terminal log it if we have it
+   string logfile_name = _config->FindFile("Dir::Log::Terminal");
+   if (!logfile_name.empty())
+   {
+      FILE *log = NULL;
+      char buf[1024];
+
+      fprintf(report, "DpkgTerminalLog:\n");
+      log = fopen(logfile_name.c_str(),"r");
+      if(log != NULL)
+      {
+        while( fgets(buf, sizeof(buf), log) != NULL)
+           fprintf(report, " %s", buf);
+        fclose(log);
+      }
+   }
+   fclose(report);
+}
+                                                                       /*}}}*/