]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/deb/dpkgpm.cc
* Applied patch from Brian M. Carlson <sandals@crustytoothpaste.ath.cx>
[apt.git] / apt-pkg / deb / dpkgpm.cc
index 8cb076edd94560dc6aff18a6203249f950c69811..e3fe8d845a1e1bcb7cfdb108964a44db034d5231 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), PackagesTotal(0), PackagesDone(0)
+   : pkgPackageManager(Cache), dpkgbuf_pos(0),
+     term_out(NULL), PackagesDone(0), PackagesTotal(0)
 {
 }
                                                                        /*}}}*/
@@ -350,15 +352,24 @@ 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)
+   if(len == -1 && errno == EIO)
+   {
+      // this happens when the child is about to exit, we
+      // give it time to actually exit, otherwise we run
+      // into a race
+      usleep(500000);
+      return;
+   }  
+   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                                                /*{{{*/
@@ -389,14 +400,14 @@ void pkgDPkgPM::ProcessDpkgStatusLine(int OutStatusFd, char *line)
    //        statusfd or by rewriting the code here to deal with
    //        it. for now we just ignore it and not crash
    TokSplitString(':', line, list, sizeof(list)/sizeof(list[0]));
-   char *pkg = list[1];
-   char *action = _strstrip(list[2]);
-   if( pkg == NULL || action == NULL) 
+   if( list[0] == NULL || list[1] == NULL || list[2] == NULL) 
    {
       if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
         std::clog << "ignoring line: not enough ':'" << std::endl;
       return;
    }
+   char *pkg = list[1];
+   char *action = _strstrip(list[2]);
 
    if(strncmp(action,"error",strlen("error")) == 0)
    {
@@ -492,6 +503,67 @@ 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;
+}
+
+/*{{{*/
+// This implements a racy version of pselect for those architectures
+// that don't have a working implementation.
+// FIXME: Probably can be removed on Lenny+1
+static int racy_pselect(int nfds, fd_set *readfds, fd_set *writefds,
+   fd_set *exceptfds, const struct timespec *timeout,
+   const sigset_t *sigmask)
+{
+   sigset_t origmask;
+   struct timeval tv;
+   int retval;
+
+   tv.tv_sec = timeout->tv.tv_sec;
+   tv.tv_usec = timeout->tv.tv_nsec/1000;
+
+   sigprocmask(SIG_SETMASK, &sigmask, &origmask);
+   retval = select(nfds, readfds, writefds, exceptfds, &tv);
+   sigprocmask(SIG_SETMASK, &origmask, 0);
+   return retval;
+}
+/*}}}*/
 
 // DPkgPM::Go - Run the sequence                                       /*{{{*/
 // ---------------------------------------------------------------------
@@ -515,7 +587,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 +598,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}
@@ -560,21 +640,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::Name"));
-   FILE *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, "Log 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();)
@@ -701,15 +767,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]);
@@ -718,13 +788,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)
@@ -749,7 +822,7 @@ bool pkgDPkgPM::Go(int OutStatusFd)
 
         /* No Job Control Stop Env is a magic dpkg var that prevents it
            from using sigstop */
-        putenv("DPKG_NO_TSTP=yes");
+        putenv((char *)"DPKG_NO_TSTP=yes");
         execvp(Args[0],(char **)Args);
         cerr << "Could not exec dpkg!" << endl;
         _exit(100);
@@ -765,16 +838,19 @@ 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;
-      struct timeval tv;
+      struct timespec tv;
+      sigset_t sigmask;
+      sigset_t original_sigmask;
+      sigemptyset(&sigmask);
+      sigprocmask(SIG_BLOCK,&sigmask,&original_sigmask);
+
       int select_ret;
       while ((res=waitpid(Child,&Status, WNOHANG)) != Child) {
         if(res < 0) {
@@ -794,18 +870,28 @@ 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))
+        tv.tv_nsec = 0;
+        select_ret = pselect(max(master, _dpkgin)+1, &rfds, NULL, NULL, 
+                             &tv, &original_sigmask);
+        if (select_ret < 0 && (errno == EINVAL || errno == ENOSYS))
+           select_ret = racy_pselect(max(master, _dpkgin)+1, &rfds, NULL,
+                                     NULL, &tv, &original_sigmask);
+        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);
@@ -816,7 +902,11 @@ bool pkgDPkgPM::Go(int OutStatusFd)
       signal(SIGQUIT,old_SIGQUIT);
       signal(SIGINT,old_SIGINT);
 
-      tcsetattr(0, TCSAFLUSH, &tt);
+      if(master >= 0) 
+      {
+        tcsetattr(0, TCSAFLUSH, &tt);
+        close(master);
+      }
        
       // Check for an error code.
       if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
@@ -838,12 +928,12 @@ bool pkgDPkgPM::Go(int OutStatusFd)
 
         if(stopOnError) 
         {
-           fclose(term_out);
+           CloseLog();
            return false;
         }
       }      
    }
-   fclose(term_out);
+   CloseLog();
 
    if (RunScripts("DPkg::Post-Invoke") == false)
       return false;