xdasd_main.c

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002  * Copyright (c) 2006, Novell, Inc.
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions are 
00007  * met:
00008  * 
00009  *     * Redistributions of source code must retain the above copyright 
00010  *       notice, this list of conditions and the following disclaimer.
00011  *     * Redistributions in binary form must reproduce the above copyright 
00012  *       notice, this list of conditions and the following disclaimer in the 
00013  *       documentation and/or other materials provided with the distribution.
00014  *     * Neither the name of the Novell nor the names of its contributors 
00015  *       may be used to endorse or promote products derived from this 
00016  *       software without specific prior written permission.
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00019  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00020  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00021  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00022  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
00025  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
00026  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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    /* error paths */
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}; /* 2 seconds */
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 

Generated on Thu Aug 20 22:33:05 2009 for OpenXDAS by  doxygen 1.5.6