]> git.saurik.com Git - apt.git/blobdiff - test/interactive-helper/aptwebserver.cc
Merge remote-tracking branch 'upstream/debian/sid'
[apt.git] / test / interactive-helper / aptwebserver.cc
index de235fa0591ecb3063d22a5cbe790707b184e338..a8d191d0e332d261a6e603e4d70c02afe5e73fff 100644 (file)
@@ -387,6 +387,41 @@ int main(int const argc, const char * argv[])
       return 2;
    }
 
+   FileFd pidfile;
+   if (_config->FindB("aptwebserver::fork", false) == true)
+   {
+      std::string const pidfilename = _config->Find("aptwebserver::pidfile", "aptwebserver.pid");
+      int const pidfilefd = GetLock(pidfilename);
+      if (pidfilefd < 0 || pidfile.OpenDescriptor(pidfilefd, FileFd::WriteOnly) == false)
+      {
+        _error->Errno("aptwebserver", "Couldn't acquire lock on pidfile '%s'", pidfilename.c_str());
+        _error->DumpErrors(std::cerr);
+        return 3;
+      }
+
+      pid_t child = fork();
+      if (child < 0)
+      {
+        _error->Errno("aptwebserver", "Forking failed");
+        _error->DumpErrors(std::cerr);
+        return 4;
+      }
+      else if (child != 0)
+      {
+        // successfully forked: ready to serve!
+        std::string pidcontent;
+        strprintf(pidcontent, "%d", child);
+        pidfile.Write(pidcontent.c_str(), pidcontent.size());
+        if (_error->PendingError() == true)
+        {
+           _error->DumpErrors(std::cerr);
+           return 5;
+        }
+        std::cout << "Successfully forked as " << child << std::endl;
+        return 0;
+      }
+   }
+
    std::clog << "Serving ANY file on port: " << port << std::endl;
 
    listen(sock, 1);
@@ -435,6 +470,32 @@ int main(int const argc, const char * argv[])
               }
            }
 
+           ::Configuration::Item const *Overwrite = _config->Tree("aptwebserver::overwrite");
+           if (Overwrite != NULL)
+           {
+              for (::Configuration::Item *I = Overwrite->Child; I != NULL; I = I->Next)
+              {
+                 regex_t *pattern = new regex_t;
+                 int const res = regcomp(pattern, I->Tag.c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB);
+                 if (res != 0)
+                 {
+                    char error[300];
+                    regerror(res, pattern, error, sizeof(error));
+                    sendError(client, 500, *m, sendContent, error);
+                    continue;
+                 }
+                 if (regexec(pattern, filename.c_str(), 0, 0, 0) == 0)
+                 {
+                     filename = _config->Find("aptwebserver::overwrite::" + I->Tag + "::filename", filename);
+                     if (filename[0] == '/')
+                        filename.erase(0,1);
+                     regfree(pattern);
+                     break;
+                 }
+                 regfree(pattern);
+              }
+           }
+
            // deal with the request
            if (RealFileExists(filename) == true)
            {
@@ -476,5 +537,7 @@ int main(int const argc, const char * argv[])
                << " on socket " << sock << std::endl;
       close(client);
    }
+   pidfile.Close();
+
    return 0;
 }