]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/deb/dpkgpm.cc
* merged from the mvo branch
[apt.git] / apt-pkg / deb / dpkgpm.cc
index bef35ab4c49b621abf8b5672c6d1f6c41d1556be..ac63ccfdf73d0a55086011a3b89ca0fd13368d3f 100644 (file)
@@ -13,6 +13,7 @@
 #include <apt-pkg/configuration.h>
 #include <apt-pkg/depcache.h>
 #include <apt-pkg/strutl.h>
+#include <apti18n.h>
 
 #include <unistd.h>
 #include <stdlib.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), PackagesDone(0), 
+     PackagesTotal(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,7 +404,7 @@ 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)
@@ -413,7 +416,7 @@ void pkgDPkgPM::ProcessDpkgStatusLine(int OutStatusFd, char *line)
    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 +441,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)
@@ -515,7 +518,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")}, 
@@ -526,12 +529,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}
@@ -555,10 +566,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();)
    {
@@ -684,15 +715,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]);
@@ -701,13 +736,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)
@@ -748,24 +786,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");
+      if(slave > 0)
+        close(slave);
 
       // setups fds
       fd_set rfds;
@@ -789,30 +813,36 @@ 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);
       }
       close(_dpkgin);
-      fclose(term_out);
 
       // Restore sig int/quit
       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)
@@ -832,10 +862,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;