]> 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 68f0a339f0bbe5d4934edf848dd0c10eb49019ce..e3fe8d845a1e1bcb7cfdb108964a44db034d5231 100644 (file)
@@ -44,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)
 {
 }
                                                                        /*}}}*/
@@ -351,12 +352,20 @@ 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;
    write(1, term_buf, len);
    if(term_out)
@@ -391,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)
    {
@@ -494,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                                       /*{{{*/
 // ---------------------------------------------------------------------
@@ -517,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")}, 
@@ -528,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}
@@ -562,25 +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::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();)
@@ -762,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);
@@ -778,9 +838,6 @@ 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;
       if(slave > 0)
@@ -788,7 +845,12 @@ bool pkgDPkgPM::Go(int OutStatusFd)
 
       // 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) {
@@ -811,15 +873,24 @@ bool pkgDPkgPM::Go(int OutStatusFd)
         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;
-
+        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, term_out);
+           DoTerminalPty(master);
         if(master >= 0 && FD_ISSET(0, &rfds))
            DoStdin(master);
         if(FD_ISSET(_dpkgin, &rfds))
@@ -831,8 +902,11 @@ bool pkgDPkgPM::Go(int OutStatusFd)
       signal(SIGQUIT,old_SIGQUIT);
       signal(SIGINT,old_SIGINT);
 
-      if(master >= 0 && slave >= 0)
+      if(master >= 0) 
+      {
         tcsetattr(0, TCSAFLUSH, &tt);
+        close(master);
+      }
        
       // Check for an error code.
       if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
@@ -854,14 +928,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;