null+****@clear*****
null+****@clear*****
2011年 1月 7日 (金) 19:06:51 JST
Tasuku SUENAGA a.k.a. gunyarakun 2011-01-07 10:06:51 +0000 (Fri, 07 Jan 2011)
New Revision: 4742bc9d5dc00122c470a0f23830127a499b0560
Log:
Added daemonize function to suggest learners.
Added files:
src/suggest/util.h
Modified files:
src/suggest/groonga_suggest_httpd.c
src/suggest/groonga_suggest_learner.c
src/suggest/util.c
Modified: src/suggest/groonga_suggest_httpd.c (+14 -4)
===================================================================
--- src/suggest/groonga_suggest_httpd.c 2011-01-06 06:55:07 +0000 (01b5bc7)
+++ src/suggest/groonga_suggest_httpd.c 2011-01-07 10:06:51 +0000 (e46802a)
@@ -37,10 +37,11 @@
#include <groonga.h>
#include <pthread.h>
+#include "util.h"
+
#define DEFAULT_PORT 8080
#define DEFAULT_MAX_THREADS 8
-int print_error(const char *format, ...);
grn_rc grn_ctx_close(grn_ctx *ctx);
#define CONST_STR_LEN(x) x, x ? sizeof(x) - 1 : 0
@@ -655,14 +656,15 @@ usage(FILE *output)
" -c <thread number> : server thread number (default: %d)\n"
" -s <send endpoint> : send endpoint (ex. tcp://example.com:1234)\n"
" -r <recv endpoint> : recv endpoint (ex. tcp://example.com:1235)\n"
- " -l <path prefix> : log path prefix\n",
+ " -l <path prefix> : log path prefix\n"
+ " -d : daemonize\n",
DEFAULT_PORT, default_max_threads);
}
int
main(int argc, char **argv)
{
- int port_no = DEFAULT_PORT;
+ int port_no = DEFAULT_PORT, daemon = 0;
const char *send_endpoint = NULL, *recv_endpoint = NULL, *log_path = NULL;
/* check environment */
@@ -685,7 +687,7 @@ main(int argc, char **argv)
{
int ch;
- while ((ch = getopt(argc, argv, "c:p:s:r:l:")) != -1) {
+ while ((ch = getopt(argc, argv, "c:p:s:r:l:d")) != -1) {
switch(ch) {
case 'c':
default_max_threads = atoi(optarg);
@@ -706,6 +708,9 @@ main(int argc, char **argv)
case 'l':
log_path = optarg;
break;
+ case 'd':
+ daemon = 1;
+ break;
}
}
argc -= optind; argv += optind;
@@ -717,6 +722,11 @@ main(int argc, char **argv)
} else {
grn_ctx ctx;
void *zmq_ctx;
+
+ if (daemon) {
+ daemonize();
+ }
+
grn_init();
grn_ctx_init(&ctx, 0);
if ((db = grn_db_open(&ctx, argv[0]))) {
Modified: src/suggest/groonga_suggest_learner.c (+13 -4)
===================================================================
--- src/suggest/groonga_suggest_learner.c 2011-01-06 06:55:07 +0000 (fbced5f)
+++ src/suggest/groonga_suggest_learner.c 2011-01-07 10:06:51 +0000 (705fb3e)
@@ -22,12 +22,12 @@
#include <pthread.h>
#include <groonga.h>
+#include "util.h"
+
#define DEFAULT_RECV_ENDPOINT "tcp://*:1234"
#define DEFAULT_SEND_ENDPOINT "tcp://*:1235"
#define SEND_WAIT 1000 /* 0.001sec */
-int print_error(const char *format, ...);
-
#define CONST_STR_LEN(x) x, x ? sizeof(x) - 1 : 0
typedef struct {
@@ -466,7 +466,8 @@ usage(FILE *output)
"Usage: groonga-suggest-learner [options...] db_path\n"
"options:\n"
" -r <recv endpoint>: recv endpoint (default: %s)\n"
- " -s <send endpoint>: send endpoint (default: %s)\n",
+ " -s <send endpoint>: send endpoint (default: %s)\n"
+ " -d : daemonize\n",
DEFAULT_RECV_ENDPOINT, DEFAULT_SEND_ENDPOINT);
}
@@ -479,6 +480,7 @@ signal_handler(int sig)
int
main(int argc, char **argv)
{
+ int daemon = 0;
const char *recv_endpoint = DEFAULT_RECV_ENDPOINT,
*send_endpoint = DEFAULT_SEND_ENDPOINT;
@@ -486,7 +488,7 @@ main(int argc, char **argv)
{
int ch;
- while ((ch = getopt(argc, argv, "r:s:")) != -1) {
+ while ((ch = getopt(argc, argv, "r:s:d")) != -1) {
switch(ch) {
case 'r':
recv_endpoint = optarg;
@@ -494,6 +496,9 @@ main(int argc, char **argv)
case 's':
send_endpoint = optarg;
break;
+ case 'd':
+ daemon = 1;
+ break;
}
}
argc -= optind; argv += optind;
@@ -506,6 +511,10 @@ main(int argc, char **argv)
grn_ctx *ctx;
msgpack_zone *mempool;
+ if (daemon) {
+ daemonize();
+ }
+
grn_init();
signal(SIGTERM, signal_handler);
Modified: src/suggest/util.c (+59 -0)
===================================================================
--- src/suggest/util.c 2011-01-06 06:55:07 +0000 (10c820c)
+++ src/suggest/util.c 2011-01-07 10:06:51 +0000 (c21f4c7)
@@ -1,5 +1,27 @@
+/* -*- c-basic-offset: 2 -*- */
+/* Copyright(C) 2010- Brazil
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License version 2.1 as published by the Free Software Foundation.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
#include <stdio.h>
#include <stdarg.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
int
print_error(const char *format, ...)
@@ -15,3 +37,40 @@ print_error(const char *format, ...)
return r;
}
+
+int
+daemonize(void)
+{
+ pid_t pid;
+
+ switch (fork()) {
+ case 0:
+ break;
+ case -1:
+ print_error("fork failed.");
+ return -1;
+ default:
+ wait(NULL);
+ _exit(0);
+ }
+ switch ((pid = fork())) {
+ case 0:
+ break;
+ case -1:
+ perror("fork");
+ return -1;
+ default:
+ fprintf(stderr, "%d\n", pid);
+ _exit(0);
+ }
+ {
+ int null_fd = open("/dev/null", O_RDWR, 0);
+ if (null_fd != -1) {
+ dup2(null_fd, 0);
+ dup2(null_fd, 1);
+ dup2(null_fd, 2);
+ if (null_fd > 2) { close(null_fd); }
+ }
+ }
+ return 1;
+}
Added: src/suggest/util.h (+23 -0) 100644
===================================================================
--- /dev/null
+++ src/suggest/util.h 2011-01-07 10:06:51 +0000 (1983481)
@@ -0,0 +1,23 @@
+/* -*- c-basic-offset: 2 -*- */
+/* Copyright(C) 2010- Brazil
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License version 2.1 as published by the Free Software Foundation.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+#ifndef GRN_SUGGEST_UTIL_H
+#define GRN_SUGGEST_UTIL_H
+
+int print_error(const char *format, ...);
+int daemonize(void);
+
+#endif /* GRN_SUGGEST_UTIL_H */