[Groonga-commit] groonga/groonga [master] groonga: don't daemonize until connection can be accepted

Back to archive index

null+****@clear***** null+****@clear*****
2012年 4月 3日 (火) 16:31:21 JST


Kouhei Sutou	2012-04-03 16:31:21 +0900 (Tue, 03 Apr 2012)

  New Revision: 84351e11197139e4dc9867e44bb587d1b7ef8ef4

  Log:
    groonga: don't daemonize until connection can be accepted
    
    fixes #1326

  Modified files:
    src/groonga.c

  Modified: src/groonga.c (+12 -17)
===================================================================
--- src/groonga.c    2012-04-03 16:24:33 +0900 (2c4d940)
+++ src/groonga.c    2012-04-03 16:31:21 +0900 (e086dd3)
@@ -67,6 +67,7 @@ static int batchmode;
 static int number_of_lines = 0;
 static int newdb;
 static int useql;
+static grn_bool is_daemon_mode = GRN_FALSE;
 static int (*do_client)(int argc, char **argv);
 static int (*do_server)(char *path);
 static FILE *pid_file = NULL;
@@ -951,7 +952,15 @@ run_server(grn_ctx *ctx, grn_obj *db, grn_com_event *ev,
     ev->opaque = db;
     grn_edges_init(ctx, dispatcher);
     if (!grn_com_sopen(ctx, ev, bind_address, port, handler, he)) {
-      run_server_loop(ctx, ev);
+      if (is_daemon_mode) {
+        exit_code = daemonize();
+      }
+      if (exit_code == EXIT_SUCCESS) {
+        run_server_loop(ctx, ev);
+      }
+      if (is_daemon_mode) {
+        clean_pid_file();
+      }
       exit_code = EXIT_SUCCESS;
     } else {
       fprintf(stderr, "grn_com_sopen failed (%s:%d): %s\n",
@@ -1961,20 +1970,6 @@ g_server(char *path)
   return exit_code;
 }
 
-static int
-do_daemon(char *path)
-{
-  int exit_code;
-
-  exit_code = daemonize();
-  if (exit_code == EXIT_SUCCESS) {
-    exit_code = do_server(path);
-  }
-  clean_pid_file();
-
-  return exit_code;
-}
-
 enum {
   mode_alone = 0,
   mode_client,
@@ -2824,8 +2819,8 @@ main(int argc, char **argv)
     exit_code = do_client(argc - i, argv + i);
     break;
   case mode_daemon :
-    exit_code = do_daemon(argc > i ? argv[i] : NULL);
-    break;
+    is_daemon_mode = GRN_TRUE;
+    /* fallthru */
   case mode_server :
     exit_code = do_server(argc > i ? argv[i] : NULL);
     break;




Groonga-commit メーリングリストの案内
Back to archive index