xdasd_main.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00039 #include "xdasd_main.h"
00040 #include "xdasd_log.h"
00041 #include "xdasd_net.h"
00042 #include "xdasd_conf.h"
00043 #include "xdasd_cmdline.h"
00044 #include "xdasd_filter.h"
00045 #include "xdasd_trigger.h"
00046 #include "xdasd_logger.h"
00047
00048 #include <errno.h>
00049 #include <stdio.h>
00050
00051 #ifdef _WIN32
00052 # define snprintf _snprintf
00053 #endif
00054
00059 static unsigned s_xdasd_sigmask = 0;
00060
00068 static void xdasd_clear_signal(unsigned sigbit)
00069 {
00070 s_xdasd_sigmask &= ~sigbit;
00071 }
00072
00082 static int xdasd_is_signaled(unsigned sigbit)
00083 {
00084 return (s_xdasd_sigmask & sigbit)? 1: 0;
00085 }
00086
00092 static void xdasd_handle_sigterm(void)
00093 {
00094 xdasd_log(0, "----------------------------------\n");
00095 xdasd_log(0, "xdasd terminating.\n");
00096
00097 xdasd_net_exit();
00098
00099 xdasd_log(0, "----------------------------------\n");
00100 xdasd_log(0, "xdasd terminated.\n");
00101
00102 xdasd_clear_signal(XDASD_SIGTERM);
00103 }
00104
00109 void xdasd_handle_sighup(void)
00110 {
00111 xdasd_log(0, "----------------------------------\n");
00112 xdasd_log(0, "xdasd reset by SIGHUP.\n");
00113
00114 xdasd_conf_init(0);
00115
00116 xdasd_log(0, "----------------------------------\n");
00117 xdasd_log(0, "xdasd reset finished.\n");
00118
00119 xdasd_clear_signal(XDASD_SIGHUP);
00120 }
00121
00128 void xdasd_set_signal(unsigned sigbit)
00129 {
00130 s_xdasd_sigmask |= sigbit;
00131 }
00132
00141 int xdasd_main_init(XDASDCmdLine * cmdline)
00142 {
00143 int err;
00144 char ipcfile[FILENAME_MAX];
00145
00146 snprintf(ipcfile, sizeof(ipcfile), "%s/" XDASD_IPCFNAME, cmdline->runpath);
00147 remove(ipcfile);
00148
00149 if ((err = xdasd_log_open(cmdline->logfile, 1, cmdline->loglevel)) != 0)
00150 return err;
00151
00152 xdasd_log(0, "----------------------------------\n");
00153 xdasd_log(0, "Starting xdasd version %s...\n", cmdline->verstr);
00154 xdasd_log(0, "Using configuration file %s.\n", cmdline->confile);
00155 xdasd_log(0, "Using filter specification file %s.\n", cmdline->fltfile);
00156 xdasd_log(0, "Using message queue directory %s.\n", cmdline->msgfdir);
00157 xdasd_log(0, "Using unique ipc path/string %s.\n", ipcfile);
00158
00159 xdasd_conf_init(cmdline->confile);
00160
00161 if ((err = xdasd_filter_init(cmdline->fltfile)) != 0)
00162 {
00163 xdasd_log(0, "Filter initialization failed.\n");
00164 goto e2;
00165 }
00166 if ((err = xdasd_logger_init(cmdline->msgfdir)) != 0)
00167 {
00168 xdasd_log(0, "Logger initialization failed.\n");
00169 goto e3;
00170 }
00171 if ((err = xdasd_trigger_init()) != 0)
00172 {
00173 xdasd_log(0, "Trigger initialization failed.\n");
00174 goto e4;
00175 }
00176 if ((err = xdasd_net_init(ipcfile)) != 0)
00177 {
00178 xdasd_log(0, "Network initialization failed (%d).\n", errno);
00179 goto e5;
00180 }
00181 xdasd_log(0, "main: Successfully initialized.\n");
00182 return 0;
00183
00184
00185 e5:xdasd_trigger_exit();
00186 e4:xdasd_logger_exit();
00187 e3:xdasd_filter_exit();
00188 e2:xdasd_conf_exit();
00189 xdasd_log_close();
00190 return err;
00191 }
00192
00197 void xdasd_main_run(void)
00198 {
00199 xdasd_log(0, "Entering main run loop...\n");
00200
00201 while (!xdasd_is_signaled(XDASD_SIGTERM))
00202 {
00203 fd_set readfds, writefds;
00204 int fdcount, highfd = 0;
00205 struct timeval tmout = {2,0};
00206
00207 if (xdasd_is_signaled(XDASD_SIGHUP))
00208 xdasd_handle_sighup();
00209
00210 FD_ZERO(&readfds);
00211 FD_ZERO(&writefds);
00212 xdasd_net_load_fdsets(&highfd, &readfds, &writefds);
00213 if ((fdcount = select(highfd + 1, &readfds, &writefds, 0, &tmout)) > 0)
00214 xdasd_net_event_handler(fdcount, &readfds, &writefds);
00215 }
00216 xdasd_handle_sigterm();
00217 xdasd_trigger_exit();
00218 xdasd_logger_exit();
00219 xdasd_filter_exit();
00220 xdasd_conf_exit();
00221 xdasd_log_close();
00222 }
00223