xdasd_event.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.h>
00040 
00041 #include "xdasd_log.h"
00042 #include "xdasd_event.h"
00043 #include "xdasd_parse.h"
00044 #include "xdasd_logger.h"
00045 #include "xdasd_trigger.h"
00046 #include "xdasd_filter.h"
00047 
00048 #include <stdlib.h>
00049 #include <stddef.h>
00050 
00075 static int ev_check(int * minorp, Parsed * parsed, int onlycheck)
00076 {
00080    switch(xdasd_filter_check(parsed, onlycheck))
00081    {
00082       case 1:  return XDAS_S_COMPLETE;
00083       case 0:  return XDAS_S_NO_AUDIT;
00084       case -1: return (*minorp = OXDAS_MS_OUT_OF_MEMORY), XDAS_S_FAILURE;
00085       case -2: return XDAS_S_NO_DECISION_YET;
00086    }
00087    return (*minorp = OXDAS_MS_UNKNOWN_ERROR), XDAS_S_FAILURE;
00088 }
00089 
00105 static int ev_parse(int * minorp, int impflag, 
00106       size_t msgsz, const char * msg, Parsed ** parsedp)
00107 {
00108    xdasd_log(10, "ev_parse: [%.*s]\n", msgsz, msg);
00109    switch(xdasd_parse_message(impflag? PMF_IMPORTED: 0, msgsz, msg, parsedp))
00110    {
00111       case 0:  return XDAS_S_COMPLETE;
00112       case -1: return (*minorp = OXDAS_MS_OUT_OF_MEMORY), XDAS_S_FAILURE;
00113       case -2: return XDAS_S_INCOMPLETE_RECORD;
00114    }
00115    return (*minorp = OXDAS_MS_UNKNOWN_ERROR), XDAS_S_FAILURE;
00116 }
00117 
00127 static int ev_logger_append(Parsed * parsed)
00128 {
00129    if (xdasd_logger_append(parsed) != 0)
00130       return XDAS_S_STORAGE_FAILURE;
00131    return XDAS_S_COMPLETE;
00132 }
00133 
00154 static int xdasd_event_check(int * minorp, int impflag, 
00155       size_t msgsz, const char * msg)
00156 {
00157    int err;
00158    Parsed * parsed;
00159 
00160    /* parse an message */
00161    if ((err = ev_parse(minorp, impflag, msgsz, msg, &parsed)) != 0)
00162       return err;
00163 
00164    /* check and free parsed message */
00165    err = ev_check(minorp, parsed, 1);
00166    xdasd_parse_free(parsed);
00167    return err;
00168 }
00169 
00197 static int xdasd_event_check_and_submit(int * minorp, int impflag, 
00198       size_t msgsz, const char * msg)
00199 {
00200    int err;
00201    Parsed * parsed;
00202 
00203    if ((err = ev_parse(minorp, impflag, msgsz, msg, &parsed)) != 0)
00204       return err;
00205 
00206    /* check & submit a parsed message, then free it */
00207    if ((err = ev_check(minorp, parsed, 0)) == XDAS_S_COMPLETE
00208          || err == XDAS_S_NO_DECISION_YET)
00209    {
00210 /*    if (parsed->severity)
00211          if (xdasd_alarm_append(parsed) != 0)
00212             xdasd_log(0, "event: Unable to execute alarm for message:\n[ %s ]\n", 
00213                   parsed->parsed[0]);
00214 */
00215       if (parsed->triggers.head != 0)
00216          if (xdasd_trigger_append(parsed) != 0)
00217             xdasd_log(0, "event: Unable to execute script for message:\n[ %s ].\n", 
00218                   parsed->parsed[0]);
00219 
00220       if (parsed->flags & PMF_LOG)
00221          err = ev_logger_append(parsed);
00222    }
00223    xdasd_parse_free(parsed);
00224 
00225    /* Since (from the client perspective) we've already either discarded, 
00226       or committed the record, we never return NO_AUDIT, or NO_DECISION 
00227       codes from this routine. */
00228    if (err == XDAS_S_NO_AUDIT || err == XDAS_S_NO_DECISION_YET)
00229       err = XDAS_S_COMPLETE;
00230 
00231    return err;
00232 }
00233 
00237 /*--------------------------------------------------------------------------*/
00238 
00273 int xdasd_event_process(int * minorp, xdas_buffer req, xdas_buffer * rspp)
00274 {
00275    static int(*handler_table[])(int *, int, size_t, const char *) = 
00276    { 
00277       xdasd_event_check,               /* sub-function 0 */
00278       xdasd_event_check_and_submit,    /* sub-function 1 */
00279       xdasd_event_check_and_submit     /* sub-function 2 */
00280    };
00281 
00282    unsigned reqsfn;
00283    int impflag;
00284    size_t msgsz, reqsz = req->end - req->curpos;
00285 
00286    (void)rspp; /* extended response data not provided by this function */
00287 
00288    if (reqsz < 12 || (reqsfn = xdas_buffer_get_uint32(req)) 
00289          >= xdas_elemcount(handler_table))
00290    {
00291       const char * errmsg = reqsz < 12? 
00292             "request buffer too small": "unknown function requested";
00293       xdasd_log(1, "xdasd_event_process: Protocol error - %s.\n", errmsg);
00294       return (*minorp = OXDAS_MS_PROTOCOL), XDAS_S_FAILURE;
00295    }
00296 
00297    impflag = (int)xdas_buffer_get_uint32(req);
00298    msgsz = xdas_buffer_get_uint32(req);
00299 
00300    return handler_table[reqsfn](minorp, impflag, msgsz, req->curpos);
00301 }
00302 

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