Get rid of unused var
[elliptics.git] / example / ioserv.c
blob35a9e2ca5aaae10833c7a2e287379d671be1d955
1 /*
2 * 2008+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
3 * All rights reserved.
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 "config.h"
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/socket.h>
21 #include <sys/time.h>
22 #include <sys/wait.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
34 #include <netinet/in.h>
36 #include "elliptics/packet.h"
37 #include "elliptics/interface.h"
39 #include "common.h"
41 #ifndef __unused
42 #define __unused __attribute__ ((unused))
43 #endif
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"
51 " -h - this help\n"
52 , p);
55 static int ioserv_monitor(void)
57 pid_t pid;
59 pid = fork();
60 if (pid == -1) {
61 fprintf(stderr, "Failed to fork to background: %s.\n", strerror(errno));
62 exit(pid);
65 if (pid != 0) {
66 printf("Children pid: %d\n", pid);
67 return pid;
69 setsid();
71 #if 0
72 close(0);
73 close(1);
74 close(2);
75 #endif
76 return 0;
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)
96 int status, pid;
98 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
99 dnet_srw_update(global_n, pid);
103 static int ioserv_setup_signals(void)
105 struct sigaction sa;
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);
132 return 0;
135 static void ioserv_cleanup_signals(void)
137 sigset_t sig;
139 sigemptyset(&sig);
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)
148 struct dnet_node *n;
150 n = dnet_parse_config(conf, mon);
151 if (!n)
152 return -1;
154 global_n = n;
155 ioserv_setup_signals();
157 while (!dnet_need_exit(n))
158 sleep(1);
160 ioserv_cleanup_signals();
161 dnet_server_node_destroy(n);
162 return 0;
165 int main(int argc, char *argv[])
167 int ch, mon = 0, err;
168 char *conf = NULL;
170 while ((ch = getopt(argc, argv, "mc:h")) != -1) {
171 switch (ch) {
172 case 'm':
173 mon = 1;
174 break;
175 case 'c':
176 conf = optarg;
177 break;
178 case 'h':
179 default:
180 dnet_usage(argv[0]);
181 return -1;
185 if (!conf) {
186 fprintf(stderr, "No config file provided. Exiting.\n");
187 return -1;
190 if (mon) {
191 #if 0
192 err = ioserv_monitor();
193 if (err > 0)
194 exit();
195 #endif
196 while (1) {
197 err = ioserv_monitor();
198 if (err > 0) {
199 int status;
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");
214 } else {
215 exit(ioserv_start(conf, mon));
218 sleep(1);
220 } else {
221 ioserv_start(conf, mon);
224 return 0;