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.
16 #include <sys/types.h>
18 #include <sys/socket.h>
32 #include <netinet/in.h>
34 #include "elliptics/packet.h"
35 #include "elliptics/interface.h"
40 #define __unused __attribute__ ((unused))
43 static void dnet_usage(char *p
)
45 fprintf(stderr
, "Usage: %s\n"
46 " -c config - config file\n"
47 " -m - run under internal monitor\n"
48 " -l log - log file\n"
53 static int ioserv_monitor(void)
59 fprintf(stderr
, "Failed to fork to background: %s.\n", strerror(errno
));
64 printf("Children pid: %d\n", pid
);
77 static struct dnet_node
*global_n
;
78 static void ioserv_destroy_handler(int sig __unused
, siginfo_t
*si __unused
, void *uc __unused
)
80 dnet_set_need_exit(global_n
);
83 extern char *dnet_logger_value
;
84 struct dnet_config_backend
;
85 extern int dnet_set_log(struct dnet_config_backend
*b __unused
, char *key __unused
, char *value
);
87 static void ioserv_reload_handler(int sig __unused
, siginfo_t
*si __unused
, void *uc __unused
)
89 dnet_set_log(NULL
, NULL
, dnet_logger_value
);
92 static void ioserv_sigchild_handler(int sig __unused
, siginfo_t
*si __unused
, void *uc __unused
)
96 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0) {
97 dnet_srw_update(global_n
, pid
);
101 static int ioserv_setup_signals(void)
105 memset(&sa
, 0, sizeof(sa
));
106 sa
.sa_flags
= SA_SIGINFO
;
107 sa
.sa_sigaction
= ioserv_destroy_handler
;
108 sigemptyset(&sa
.sa_mask
);
109 sigaction(SIGTERM
, &sa
, NULL
);
110 sigaction(SIGINT
, &sa
, NULL
);
112 sa
.sa_flags
= SA_SIGINFO
;
113 sa
.sa_sigaction
= ioserv_reload_handler
;
114 sigemptyset(&sa
.sa_mask
);
115 sigaction(SIGHUP
, &sa
, NULL
);
117 sa
.sa_flags
= SA_SIGINFO
;
118 sa
.sa_sigaction
= ioserv_sigchild_handler
;
119 sigemptyset(&sa
.sa_mask
);
120 sigaction(SIGCHLD
, &sa
, NULL
);
122 sigemptyset(&sa
.sa_mask
);
123 sigaddset(&sa
.sa_mask
, SIGTERM
);
124 sigaddset(&sa
.sa_mask
, SIGINT
);
125 sigaddset(&sa
.sa_mask
, SIGHUP
);
126 sigaddset(&sa
.sa_mask
, SIGCHLD
);
127 pthread_sigmask(SIG_UNBLOCK
, &sa
.sa_mask
, NULL
);
128 sigprocmask(SIG_UNBLOCK
, &sa
.sa_mask
, NULL
);
133 static int ioserv_start(char *conf
, int mon
)
137 n
= dnet_parse_config(conf
, mon
);
142 ioserv_setup_signals();
144 while (!dnet_need_exit(n
))
147 dnet_server_node_destroy(n
);
151 int main(int argc
, char *argv
[])
153 int ch
, mon
= 0, err
;
156 while ((ch
= getopt(argc
, argv
, "mc:h")) != -1) {
172 fprintf(stderr
, "No config file provided. Exiting.\n");
178 err
= ioserv_monitor();
183 err
= ioserv_monitor();
187 waitpid(err
, &status
, 0);
189 err
= WEXITSTATUS(status
);
190 fprintf(stderr
, "child exited with status: %d\n", err
);
191 if (WIFEXITED(status
)) {
192 printf("exited, status=%d\n", WEXITSTATUS(status
));
193 } else if (WIFSIGNALED(status
)) {
194 printf("killed by signal %d\n", WTERMSIG(status
));
195 } else if (WIFSTOPPED(status
)) {
196 printf("stopped by signal %d\n", WSTOPSIG(status
));
197 } else if (WIFCONTINUED(status
)) {
198 printf("continued\n");
201 exit(ioserv_start(conf
, mon
));
207 ioserv_start(conf
, mon
);