xdasd_trigger.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_trigger.h"
00040 #include "xdasd_exec.h"
00041 #include "xdasd_parse.h"
00042 #include "xdasd_mqueue.h"
00043 #include "xdasd_list.h"
00044 #include "xdasd_log.h"
00045 
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #include <string.h>
00049 
00050 /* module statics */
00051 static XDList s_list_trigger;    
00052 static MsgQueue s_mq_trigger;    
00062 static int tr_put(void * msg)
00063 {
00064    Parsed * parsed = (Parsed *)msg;
00065    Parsed * cpy;
00066    int i;
00067 
00068    /* allocate a copy for the queue */
00069    if ((cpy = (Parsed *)malloc(parsed->structsz)) == 0)
00070       return -1;
00071 
00072    /* copy all fields up to parsed - triggers list becomes ours */
00073    memcpy(cpy, parsed, offsetof(Parsed, parsed));
00074    memset(&parsed->triggers, 0, sizeof(parsed->triggers));
00075 
00076    /* copy message data */
00077    memcpy(cpy->msg, parsed->msg, parsed->parsed[XDAS_FIELD_COUNT] - parsed->msg);
00078 
00079    /* setup message field pointers */
00080    for (i = 0; i < XDAS_FIELD_COUNT + 1; i++)
00081       cpy->parsed[i] = cpy->msg + (parsed->parsed[i] - parsed->msg);
00082 
00083    xdasd_list_link_tail(&s_list_trigger, &cpy->elem);
00084 
00085    return 0;
00086 }
00087 
00095 static void * tr_get(void)
00096 {
00097    return s_list_trigger.count != 0? 
00098          xdasd_list_unlink(&s_list_trigger, s_list_trigger.tail): 
00099          0;
00100 }
00101 
00108 static void tr_process(void * msg)
00109 {
00110    Parsed * parsed = (Parsed *)msg;
00111    Trigger * t = (Trigger *)parsed->triggers.head;
00112    while (t)
00113    {
00114       if (xdasd_exec(t->script, parsed->parsed) != 0)
00115          xdasd_log(0, "trigger: Error processing script for message:\n[ %s ]\n", 
00116                parsed->msg);
00117       t = (Trigger *)t->elem.next;
00118    }
00119    xdasd_parse_free(parsed);
00120 }
00121 
00122 /*--------------------------------------------------------------------------*/
00123 
00151 int xdasd_trigger_append(Parsed * parsed)
00152 {
00153    return xdasd_mqueue_append(s_mq_trigger, parsed);
00154 }
00155 
00162 int xdasd_trigger_init(void)
00163 {
00164    memset(&s_list_trigger, 0, sizeof(s_list_trigger));
00165    if ((s_mq_trigger = xdasd_mqueue_create(tr_put, tr_get, tr_process)) == 0)
00166    {
00167       xdasd_log(0, "trigger: Unable to create trigger queue.\n");
00168       return -1;
00169    }
00170    return 0;
00171 }
00172 
00177 void xdasd_trigger_exit(void)
00178 {
00179    xdasd_mqueue_destroy(s_mq_trigger);
00180 }
00181 

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