2 * 2008+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
18 #include <sys/types.h>
20 #include <sys/socket.h>
34 #include <netinet/in.h>
36 #include "elliptics/packet.h"
37 #include "elliptics/interface.h"
42 #define __unused __attribute__ ((unused))
45 static void dnet_usage(char *p
)
47 fprintf(stderr
, "Usage: %s\n"
48 " -c config - config file\n"
49 " -m - run under internal monitor\n"
50 " -l log - log file\n"
55 static int ioserv_monitor(void)
61 fprintf(stderr
, "Failed to fork to background: %s.\n", strerror(errno
));
66 printf("Children pid: %d\n", pid
);
79 static struct dnet_node
*global_n
;
80 static void ioserv_destroy_handler(int sig __unused
, siginfo_t
*si __unused
, void *uc __unused
)
82 dnet_set_need_exit(global_n
);
85 extern char *dnet_logger_value
;
86 struct dnet_config_backend
;
87 extern int dnet_set_log(struct dnet_config_backend
*b __unused
, char *key __unused
, char *value
);
89 static void ioserv_reload_handler(int sig __unused
, siginfo_t
*si __unused
, void *uc __unused
)
91 dnet_set_log(NULL
, NULL
, dnet_logger_value
);
94 static void ioserv_sigchild_handler(int sig __unused
, siginfo_t
*si __unused
, void *uc __unused
)
98 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0) {
99 dnet_srw_update(global_n
, pid
);
103 static int ioserv_setup_signals(void)
107 memset(&sa
, 0, sizeof(sa
));
108 sa
.sa_flags
= SA_SIGINFO
;
109 sa
.sa_sigaction
= ioserv_destroy_handler
;
110 sigemptyset(&sa
.sa_mask
);
111 sigaction(SIGTERM
, &sa
, NULL
);
112 sigaction(SIGINT
, &sa
, NULL
);
114 sa
.sa_flags
= SA_SIGINFO
;
115 sa
.sa_sigaction
= ioserv_reload_handler
;
116 sigemptyset(&sa
.sa_mask
);
117 sigaction(SIGHUP
, &sa
, NULL
);
119 sa
.sa_flags
= SA_SIGINFO
;
120 sa
.sa_sigaction
= ioserv_sigchild_handler
;
121 sigemptyset(&sa
.sa_mask
);
122 sigaction(SIGCHLD
, &sa
, NULL
);
124 sigemptyset(&sa
.sa_mask
);
125 sigaddset(&sa
.sa_mask
, SIGTERM
);
126 sigaddset(&sa
.sa_mask
, SIGINT
);
127 sigaddset(&sa
.sa_mask
, SIGHUP
);
128 sigaddset(&sa
.sa_mask
, SIGCHLD
);
129 pthread_sigmask(SIG_UNBLOCK
, &sa
.sa_mask
, NULL
);
130 sigprocmask(SIG_UNBLOCK
, &sa
.sa_mask
, NULL
);
135 static void ioserv_cleanup_signals(void)
140 sigaddset(&sig
, SIGTERM
);
141 sigaddset(&sig
, SIGINT
);
142 sigaddset(&sig
, SIGHUP
);
143 sigprocmask(SIG_BLOCK
, &sig
, NULL
);
146 static int ioserv_start(char *conf
, int mon
)
150 n
= dnet_parse_config(conf
, mon
);
155 ioserv_setup_signals();
157 while (!dnet_need_exit(n
))
160 ioserv_cleanup_signals();
161 dnet_server_node_destroy(n
);
165 int main(int argc
, char *argv
[])
167 int ch
, mon
= 0, err
;
170 while ((ch
= getopt(argc
, argv
, "mc:h")) != -1) {
186 fprintf(stderr
, "No config file provided. Exiting.\n");
192 err
= ioserv_monitor();
197 err
= ioserv_monitor();
201 waitpid(err
, &status
, 0);
203 err
= WEXITSTATUS(status
);
204 fprintf(stderr
, "child exited with status: %d\n", err
);
205 if (WIFEXITED(status
)) {
206 printf("exited, status=%d\n", WEXITSTATUS(status
));
207 } else if (WIFSIGNALED(status
)) {
208 printf("killed by signal %d\n", WTERMSIG(status
));
209 } else if (WIFSTOPPED(status
)) {
210 printf("stopped by signal %d\n", WSTOPSIG(status
));
211 } else if (WIFCONTINUED(status
)) {
212 printf("continued\n");
215 exit(ioserv_start(conf
, mon
));
221 ioserv_start(conf
, mon
);