]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/deb/dpkgpm.cc
* apt-pkg/acquire.cc:
[apt.git] / apt-pkg / deb / dpkgpm.cc
index 08c95b7a4f9d9c7f5780639aa71126c69efa8334..7e1933f062f5da9f4562dbc65891a3f3a5f998a5 100644 (file)
@@ -14,6 +14,7 @@
 #include <apt-pkg/depcache.h>
 #include <apt-pkg/pkgrecords.h>
 #include <apt-pkg/strutl.h>
+#include <apti18n.h>
 
 #include <unistd.h>
 #include <stdlib.h>
@@ -44,7 +45,8 @@ using namespace std;
 // ---------------------------------------------------------------------
 /* */
 pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache) 
-   : pkgPackageManager(Cache), dpkgbuf_pos(0), PackagesTotal(0), PackagesDone(0)
+   : pkgPackageManager(Cache), dpkgbuf_pos(0), PackagesDone(0), 
+     PackagesTotal(0), term_out(NULL)
 {
 }
                                                                        /*}}}*/
@@ -351,7 +353,7 @@ 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,};
 
@@ -496,6 +498,46 @@ void pkgDPkgPM::DoDpkgStatusFd(int statusfd, int OutStatusFd)
 }
                                                                        /*}}}*/
 
+bool pkgDPkgPM::OpenLog()
+{
+   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");
+   }
+   return true;
+}
+
+bool pkgDPkgPM::CloseLog()
+{
+   if(term_out)
+   {
+      char outstr[200];
+      time_t t = time(NULL);
+      struct tm *tmp = localtime(&t);
+      strftime(outstr, sizeof(outstr), "%F  %T", tmp);
+      fprintf(term_out, "Log ended: ");
+      fprintf(term_out, outstr);
+      fprintf(term_out, "\n");
+      fclose(term_out);
+   }
+   term_out = NULL;
+   return true;
+}
+
 
 // DPkgPM::Go - Run the sequence                                       /*{{{*/
 // ---------------------------------------------------------------------
@@ -519,7 +561,7 @@ bool pkgDPkgPM::Go(int OutStatusFd)
    
    // map the dpkg states to the operations that are performed
    // (this is sorted in the same way as Item::Ops)
-   static const struct DpkgState DpkgStatesOpMap[][5] = {
+   static const struct DpkgState DpkgStatesOpMap[][7] = {
       // Install operation
       { 
         {"half-installed", N_("Preparing %s")}, 
@@ -530,12 +572,20 @@ bool pkgDPkgPM::Go(int OutStatusFd)
       { 
         {"unpacked",N_("Preparing to configure %s") },
         {"half-configured", N_("Configuring %s") },
+#if 0
+        {"triggers-awaited", N_("Processing triggers for %s") },
+        {"triggers-pending", N_("Processing triggers for %s") },
+#endif
         { "installed", N_("Installed %s")},
         {NULL, NULL}
       },
       // Remove operation
       { 
         {"half-configured", N_("Preparing for removal of %s")},
+#if 0
+        {"triggers-awaited", N_("Preparing for removal of %s")},
+        {"triggers-pending", N_("Preparing for removal of %s")},
+#endif
         {"half-installed", N_("Removing %s")},
         {"config-files",  N_("Removed %s")},
         {NULL, NULL}
@@ -564,25 +614,7 @@ bool pkgDPkgPM::Go(int OutStatusFd)
    }   
 
    // 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"));
-   FILE *term_out = NULL;
-   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");
-   }
+   OpenLog();
 
    // this loop is runs once per operation
    for (vector<Item>::iterator I = List.begin(); I != List.end();)
@@ -709,15 +741,19 @@ bool pkgDPkgPM::Go(int OutStatusFd)
       ioctl(0, TIOCGWINSZ, (char *)&win);
       if (openpty(&master, &slave, NULL, &tt, &win) < 0) 
       {
-        fprintf(stderr, _("openpty failed\n"));
+        const char *s = _("Can not write log, openpty() "
+                          "failed (/dev/pts not mounted?)\n");
+        fprintf(stderr, "%s",s);
+        fprintf(term_out, "%s",s);
+        master = slave = -1;
+      }  else {
+        struct termios rtt;
+        rtt = tt;
+        cfmakeraw(&rtt);
+        rtt.c_lflag &= ~ECHO;
+        tcsetattr(0, TCSAFLUSH, &rtt);
       }
 
-      struct termios rtt;
-      rtt = tt;
-      cfmakeraw(&rtt);
-      rtt.c_lflag &= ~ECHO;
-      tcsetattr(0, TCSAFLUSH, &rtt);
-
        // Fork dpkg
       pid_t Child;
       _config->Set("APT::Keep-Fds::",fd[1]);
@@ -726,13 +762,16 @@ bool pkgDPkgPM::Go(int OutStatusFd)
       // This is the child
       if (Child == 0)
       {
-        setsid();
-        ioctl(slave, TIOCSCTTY, 0);
-        close(master);
-        dup2(slave, 0);
-        dup2(slave, 1);
-        dup2(slave, 2);
-        close(slave);
+        if(slave >= 0 && master >= 0) 
+        {
+           setsid();
+           ioctl(slave, TIOCSCTTY, 0);
+           close(master);
+           dup2(slave, 0);
+           dup2(slave, 1);
+           dup2(slave, 2);
+           close(slave);
+        }
         close(fd[0]); // close the read end of the pipe
 
         if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
@@ -773,12 +812,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);
+      if(slave > 0)
+        close(slave);
 
       // setups fds
       fd_set rfds;
@@ -802,18 +839,24 @@ bool pkgDPkgPM::Go(int OutStatusFd)
         FD_ZERO(&rfds);
         FD_SET(0, &rfds); 
         FD_SET(_dpkgin, &rfds);
-        FD_SET(master, &rfds);
+        if(master >= 0)
+           FD_SET(master, &rfds);
         tv.tv_sec = 1;
         tv.tv_usec = 0;
         select_ret = select(max(master, _dpkgin)+1, &rfds, NULL, NULL, &tv);
-        if (select_ret < 0)
-           std::cerr << "Error in select()" << std::endl;
-        else if (select_ret == 0)
-           continue;
-
-        if(FD_ISSET(master, &rfds))
-           DoTerminalPty(master, term_out);
-        if(FD_ISSET(0, &rfds))
+        if (select_ret == 0) 
+           continue;
+        else if (select_ret < 0 && errno == EINTR)
+           continue;
+        else if (select_ret < 0) 
+        {
+           perror("select() returned error");
+           continue;
+        } 
+        
+        if(master >= 0 && FD_ISSET(master, &rfds))
+           DoTerminalPty(master);
+        if(master >= 0 && FD_ISSET(0, &rfds))
            DoStdin(master);
         if(FD_ISSET(_dpkgin, &rfds))
            DoDpkgStatusFd(_dpkgin, OutStatusFd);
@@ -824,7 +867,8 @@ bool pkgDPkgPM::Go(int OutStatusFd)
       signal(SIGQUIT,old_SIGQUIT);
       signal(SIGINT,old_SIGINT);
 
-      tcsetattr(0, TCSAFLUSH, &tt);
+      if(master >= 0 && slave >= 0)
+        tcsetattr(0, TCSAFLUSH, &tt);
        
       // Check for an error code.
       if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
@@ -846,14 +890,12 @@ bool pkgDPkgPM::Go(int OutStatusFd)
 
         if(stopOnError) 
         {
-           if(term_out)
-              fclose(term_out);
+           CloseLog();
            return false;
         }
       }      
    }
-   if(term_out)
-      fclose(term_out);
+   CloseLog();
 
    if (RunScripts("DPkg::Post-Invoke") == false)
       return false;
@@ -887,9 +929,9 @@ void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg)
 
    // get the pkgname and reportfile
    pkgname = flNotDir(pkgpath);
-   pos = pkgname.rfind('_');
+   pos = pkgname.find('_');
    if(pos != string::npos)
-      pkgname = string(pkgname, 0, pos);
+      pkgname = pkgname.substr(0, pos);
 
    // find the package versin and source package name
    pkgCache::PkgIterator Pkg = Cache.FindPkg(pkgname);
@@ -956,6 +998,27 @@ void pkgDPkgPM::WriteApportReport(const char *pkgpath, const char *errormsg)
    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);
 }
                                                                        /*}}}*/