Browse Source

Cleanup pid (since we now use spawn and do not rely on daemonizing).

Brian Aker 13 years ago
parent
commit
48b3db79be

+ 20 - 7
libgearman-server/gearmand.cc

@@ -273,10 +273,6 @@ gearman_server_st *gearmand_server(gearmand_st *gearmand)
 
 gearmand_error_t gearmand_run(gearmand_st *gearmand)
 {
-  uint32_t x;
-
-  current_epoch_handler(0, 0, 0);
-
   /* Initialize server components. */
   if (gearmand->base == NULL)
   {
@@ -292,11 +288,10 @@ gearmand_error_t gearmand_run(gearmand_st *gearmand)
                                               gearmand->threads) / 2);
     }
 
-    gearmand_debug("Initializing libevent for main thread");
-
     gearmand->base= static_cast<struct event_base *>(event_base_new());
     if (gearmand->base == NULL)
     {
+
       gearmand_fatal("event_base_new(NULL)");
       return GEARMAN_EVENT;
     }
@@ -305,16 +300,20 @@ gearmand_error_t gearmand_run(gearmand_st *gearmand)
 
     gearmand->ret= _listen_init(gearmand);
     if (gearmand->ret != GEARMAN_SUCCESS)
+    {
       return gearmand->ret;
+    }
 
     gearmand->ret= _wakeup_init(gearmand);
     if (gearmand->ret != GEARMAN_SUCCESS)
+    {
       return gearmand->ret;
+    }
 
     gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "Creating %u threads", gearmand->threads);
 
     /* If we have 0 threads we still need to create a fake one for context. */
-    x= 0;
+    uint32_t x= 0;
     do
     {
       gearmand->ret= gearmand_thread_create(gearmand);
@@ -326,13 +325,20 @@ gearmand_error_t gearmand_run(gearmand_st *gearmand)
 
     gearmand->ret= gearman_server_queue_replay(&(gearmand->server));
     if (gearmand->ret != GEARMAN_SUCCESS)
+    {
       return gearmand->ret;
+    }
   }
 
   gearmand->ret= _watch_events(gearmand);
   if (gearmand->ret != GEARMAN_SUCCESS)
+  {
     return gearmand->ret;
+  }
 
+#if 0
+  current_epoch_handler(0, 0, 0);
+#endif
   gearmand_debug("Entering main event loop");
 
   if (event_base_loop(gearmand->base, 0) == -1)
@@ -348,6 +354,13 @@ gearmand_error_t gearmand_run(gearmand_st *gearmand)
 
 void gearmand_wakeup(gearmand_st *gearmand, gearmand_wakeup_t wakeup)
 {
+  if (wakeup == GEARMAND_WAKEUP_SHUTDOWN or wakeup == GEARMAND_WAKEUP_SHUTDOWN_GRACEFUL)
+  {
+#if 0
+    shutdown_current_epoch_handler();
+#endif
+  }
+
   uint8_t buffer= wakeup;
 
   /* If this fails, there is not much we can really do. This should never fail

+ 2 - 0
libgearman-server/thread.c

@@ -395,7 +395,9 @@ static gearmand_error_t _proc_thread_start(gearman_server_st *server)
 static void _proc_thread_kill(gearman_server_st *server)
 {
   if (! (server->flags.threaded) || server->proc_shutdown)
+  {
     return;
+  }
 
   server->proc_shutdown= true;
 

+ 17 - 0
libgearman-server/timer.cc

@@ -51,10 +51,27 @@ struct timeval get_current_epoch()
   return current_epoch;
 }
 
+static bool _shutdown_clock= false;
+
+void shutdown_current_epoch_handler()
+{
+  _shutdown_clock= true;
+}
+
 void current_epoch_handler(const int, const short, void*)
 {
   static bool initialized= false;
 
+  if (_shutdown_clock)
+  {
+    if (initialized) 
+    {
+      evtimer_del(&clock_event);
+    }
+
+    return;
+  }
+
   if (initialized) 
   {
     evtimer_del(&clock_event);

+ 2 - 0
libgearman-server/timer.h

@@ -40,3 +40,5 @@
 struct timeval get_current_epoch();
 
 void current_epoch_handler(const int, const short, void*);
+
+void shutdown_current_epoch_handler();

+ 12 - 6
libtest/cmdline.cc

@@ -161,6 +161,13 @@ Application::error_t Application::run(const char *args[])
   stdin_fd.dup_for_spawn(Application::Pipe::READ, file_actions, STDIN_FILENO);
   stdout_fd.dup_for_spawn(Application::Pipe::WRITE, file_actions, STDOUT_FILENO);
   stderr_fd.dup_for_spawn(Application::Pipe::WRITE, file_actions, STDERR_FILENO);
+
+  posix_spawnattr_t spawnattr;
+  posix_spawnattr_init(&spawnattr);
+
+  sigset_t set;
+  sigemptyset(&set);
+  fatal_assert(posix_spawnattr_setsigmask(&spawnattr, &set) == 0);
   
   create_argv(args);
 
@@ -199,7 +206,7 @@ Application::error_t Application::run(const char *args[])
         const_cast<char *>(_exectuble_with_path.c_str()), 
         0};
 
-      spawn_ret= posix_spawnp(&_pid, libtool(), &file_actions, NULL, argv, NULL);
+      spawn_ret= posix_spawnp(&_pid, libtool(), &file_actions, &spawnattr, argv, NULL);
     }
     else
     {
@@ -212,22 +219,23 @@ Application::error_t Application::run(const char *args[])
         const_cast<char *>(gdb_run_file.c_str()), 
         const_cast<char *>(_exectuble_with_path.c_str()), 
         0};
-      spawn_ret= posix_spawnp(&_pid, "gdb", &file_actions, NULL, argv, NULL);
+      spawn_ret= posix_spawnp(&_pid, "gdb", &file_actions, &spawnattr, argv, NULL);
     }
   }
   else
   {
     if (_use_libtool)
     {
-      spawn_ret= posix_spawn(&_pid, built_argv[0], &file_actions, NULL, built_argv, NULL);
+      spawn_ret= posix_spawn(&_pid, built_argv[0], &file_actions, &spawnattr, built_argv, NULL);
     }
     else
     {
-      spawn_ret= posix_spawnp(&_pid, built_argv[0], &file_actions, NULL, built_argv, NULL);
+      spawn_ret= posix_spawnp(&_pid, built_argv[0], &file_actions, &spawnattr, built_argv, NULL);
     }
   }
 
   posix_spawn_file_actions_destroy(&file_actions);
+  posix_spawnattr_destroy(&spawnattr);
 
   stdin_fd.close(Application::Pipe::READ);
   stdout_fd.close(Application::Pipe::WRITE);
@@ -345,8 +353,6 @@ bool Application::slurp()
 
   if (fds[1].revents & POLLIN)
   {
-    stderr_fd.nonblock();
-
     ssize_t read_length;
     char buffer[1024]= { 0 };
     while ((read_length= ::read(stderr_fd.fd()[0], buffer, sizeof(buffer))))

+ 0 - 27
libtest/gearmand.cc

@@ -95,33 +95,6 @@ public:
     set_pid_file();
   }
 
-  pid_t get_pid(bool error_is_ok)
-  {
-    if (pid_file().empty() == false)
-    {
-      Wait wait(pid_file(), 0);
-
-      if (error_is_ok and not wait.successful())
-      {
-        Error << "Pidfile was not found:" << pid_file();
-        return -1;
-      }
-    }
-
-    GetPid *get_instance_pid;
-    util::Instance instance(hostname(), port());
-    instance.set_finish(get_instance_pid= new GetPid);
-
-    instance.push(new util::Operation(test_literal_param("getpid\r\n"), true));
-
-    if (error_is_ok and instance.run() == false)
-    {
-      Error << "Failed to obtain pid of server";
-    }
-
-    return get_instance_pid->pid();
-  }
-
   bool ping()
   {
     gearman_client_st *client= gearman_client_create(NULL);

+ 0 - 97
libtest/memcached.cc

@@ -108,40 +108,6 @@ public:
     return wait.successful();
   }
 
-  pid_t get_pid(bool error_is_ok)
-  {
-    // Memcached is slow to start, so we need to do this
-    if (error_is_ok and
-        wait_for_pidfile() == false)
-    {
-      Error << "Pidfile was not found:" << pid_file();
-      return -1;
-    }
-
-    pid_t local_pid;
-    memcached_return_t rc= MEMCACHED_SUCCESS;
-    if (has_socket())
-    {
-      if (socket().empty())
-      {
-        return -1;
-      }
-
-      local_pid= libmemcached_util_getpid(socket().c_str(), port(), &rc);
-    }
-    else
-    {
-      local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc);
-    }
-
-    if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
-    {
-      Error << "libmemcached_util_getpid(" << memcached_strerror(NULL, rc) << ") pid: " << local_pid << " for:" << *this;
-    }
-
-    return local_pid;
-  }
-
   bool ping()
   {
 #if 0
@@ -253,37 +219,6 @@ public:
     set_pid_file();
   }
 
-  pid_t get_pid(bool error_is_ok)
-  {
-    // Memcached is slow to start, so we need to do this
-    if (pid_file().empty() == false)
-    {
-      if (error_is_ok and wait_for_pidfile() == false)
-      {
-        Error << "Pidfile was not found:" << pid_file();
-        return -1;
-      }
-    }
-
-    bool success= false;
-    std::stringstream error_message;
-    pid_t local_pid= get_pid_from_file(pid_file(), error_message);
-    if (local_pid > 0)
-    {
-      if (::kill(local_pid, 0) > 0)
-      {
-        success= true;
-      }
-    }
-
-    if (error_is_ok and ((success or not is_pid_valid(local_pid))))
-    {
-      Error << "kill(" << " pid: " << local_pid << " errno:" << strerror(errno) << " for:" << *this;
-    }
-
-    return local_pid;
-  }
-
   bool ping()
   {
     // Memcached is slow to start, so we need to do this
@@ -381,38 +316,6 @@ public:
     return MEMCACHED_SASL_BINARY;
   }
 
-  pid_t get_pid(bool error_is_ok)
-  {
-    // Memcached is slow to start, so we need to do this
-    if (pid_file().empty() == false)
-    {
-      if (error_is_ok and 
-          wait_for_pidfile() == false)
-      {
-        Error << "Pidfile was not found:" << pid_file();
-        return -1;
-      }
-    }
-
-    pid_t local_pid;
-    memcached_return_t rc;
-    if (has_socket())
-    {
-      local_pid= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc);
-    }
-    else
-    {
-      local_pid= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc);
-    }
-
-    if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid))))
-    {
-      Error << "libmemcached_util_getpid2(" << memcached_strerror(NULL, rc) << ") username: " << username() << " password: " << password() << " pid: " << local_pid << " for:" << *this;
-    }
-
-    return local_pid;
-  }
-
   bool ping()
   {
     // Memcached is slow to start, so we need to do this

+ 5 - 9
libtest/server.cc

@@ -81,7 +81,6 @@ Server::Server(const std::string& host_arg, const in_port_t port_arg,
                bool is_socket_arg) :
   _magic(MAGIC_MEMORY),
   _is_socket(is_socket_arg),
-  _pid(-1),
   _port(port_arg),
   _hostname(host_arg),
   _app(executable, _is_libtool)
@@ -139,7 +138,7 @@ bool Server::wait_for_pidfile() const
 
 bool Server::has_pid() const
 {
-  return (_pid > 1);
+  return (_app.pid() > 1);
 }
 
 
@@ -234,9 +233,6 @@ bool Server::start()
     return false;
   }
 
-  // A failing get_pid() at this point is considered an error
-  _pid= get_pid(true);
-
   return has_pid();
 }
 
@@ -244,12 +240,11 @@ void Server::reset_pid()
 {
   _running.clear();
   _pid_file.clear();
-  _pid= -1;
 }
 
-pid_t Server::pid()
+pid_t Server::pid() const
 {
-  return _pid;
+  return _app.pid();
 }
 
 void Server::add_option(const std::string& arg)
@@ -393,8 +388,9 @@ bool Server::args(Application& app)
 
 bool Server::kill()
 {
-  if (check_pid(_app.pid()) and kill_pid(_app.pid())) // If we kill it, reset
+  if (check_pid(_app.pid())) // If we kill it, reset
   {
+    _app.murder();
     if (broken_pid_file() and pid_file().empty() == false)
     {
       unlink(pid_file().c_str());

+ 1 - 10
libtest/server.h

@@ -47,7 +47,6 @@ private:
   std::string _log_file;
   std::string _base_command; // executable command which include libtool, valgrind, gdb, etc
   std::string _running; // Current string being used for system()
-  pid_t _pid;
 
 protected:
   in_port_t _port;
@@ -163,8 +162,6 @@ public:
 
   virtual bool ping()= 0;
 
-  virtual pid_t get_pid(bool error_is_ok= false)= 0;
-
   virtual bool build(size_t argc, const char *argv[])= 0;
 
   void add_option(const std::string&);
@@ -188,19 +185,13 @@ public:
   // Reset a server if another process has killed the server
   void reset()
   {
-    _pid= -1;
     _pid_file.clear();
     _log_file.clear();
   }
 
   bool args(Application&);
 
-  pid_t pid();
-
-  pid_t pid() const
-  {
-    return _pid;
-  }
+  pid_t pid() const;
 
   bool has_pid() const;