1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/*************************************************************************
* $Id: mod.c,v 1.8 2000/04/04 23:37:33 dpotter Exp $
*
* mod.c -- The module handling functions
*
* Copyright (C) by Andreas Neuhaus <andy@fasta.fh-dortmund.de>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "log.h"
#include "modules.inc"
#include "mod.h"
/*************************************************************************
* GLOBALS
*/
fd_set blank_fd;
/*************************************************************************
* LIST OF ALL MODULES
*/
mod_t *mod_list = NULL;
mod_message_t *mod_msgs = NULL;
/*************************************************************************
* REGISTER A NEW MODULE
*/
void mod_register (mod_t *newmod)
{
newmod->next = mod_list;
mod_list = newmod;
}
/*************************************************************************
* SEND MESSAGE TO ALL MODULES
*/
void mod_sendmsg (int msgtype, char *msg)
{
mod_message_t *newmsg, *m;
// create new message entry
newmsg = malloc(sizeof(mod_message_t));
if (!newmsg)
return;
newmsg->msg = malloc(strlen(msg)+1);
if (!newmsg->msg) {
free(newmsg);
return;
}
newmsg->msgtype = msgtype;
strcpy(newmsg->msg, msg);
newmsg->next = NULL;
// append message to queue
log_printf(LOG_NOISYDEBUG, "mod_sendmsg(): %d '%s'\n", newmsg->msgtype, newmsg->msg);
for (m=mod_msgs; m && m->next; m=m->next);
m ? m->next : mod_msgs = newmsg;
}
void mod_sendmsgf (int msgtype, char *msg, ...)
{
va_list ap;
char buf[1024];
va_start(ap, msg);
vsnprintf(buf, sizeof(buf)-1, msg, ap);
buf[sizeof(buf)-1] = 0;
va_end(ap);
mod_sendmsg(msgtype, buf);
}
/*************************************************************************
* INIT ALL MODULES
*/
void mod_init (void)
{
mod_initfunc_t *mi;
char *s;
// call init function of all modules
for (mi=mod_list_init; *mi; mi++) {
s = (*mi)();
if (s)
log_printf(LOG_ERROR, "Error initializing module: %s\n", s);
}
}
/*************************************************************************
* DEINIT ALL MODULES
*/
void mod_deinit (void)
{
mod_t *m;
// call deinit function of all modules
// and clean up module list
for (m=mod_list; m; m=m->next)
if (m->deinit)
m->deinit();
mod_list = NULL;
}
/*************************************************************************
* RELOAD ALL MODULES
*/
void mod_reload (void)
{
mod_t *m;
char *s;
// call reload function of all modules
for (m=mod_list; m; m=m->next)
if (m->reload) {
s = m->reload();
if (s)
log_printf(LOG_ERROR, "Error reloading module: %s\n", s);
}
}
/*************************************************************************
* DISPERSE MESSAGES AND UPDATE ALL MODULES
*/
void mod_update (void)
{
mod_t *m;
mod_message_t *msg;
mod_message_t msgdup;
// process message queue
while (mod_msgs) {
msg = mod_msgs;
// global messages we handle
if (msg->msgtype == MSGTYPE_INPUT && !strcasecmp(msg->msg, "reload")) {
raise(SIGHUP);
// disperse message to modules, make a copy of the message
// so that modules may alter it without affecting
// other modules (think of strtok)
} else {
for (m=mod_list; m; m=m->next)
if (m->message) {
msgdup.msgtype = msg->msgtype;
msgdup.msg = malloc(strlen(msg->msg)+1);
strcpy(msgdup.msg, msg->msg);
m->message(msgdup.msgtype, msgdup.msg);
free(msgdup.msg);
}
}
// remove old message from queue
mod_msgs = mod_msgs->next;
free(msg);
}
// call update function of all modules
for (m=mod_list; m; m=m->next)
if (m->update)
m->update();
}
/*************************************************************************
* HANDLE SIGCHLD CALLS FOR ALL MODULES
*/
void mod_sigchld(int signal)
{
pid_t pid;
mod_t *m;
int status;
// this process was called because a child of IRMP3 just died and
// we received a SIGCHLD signal. wait() won't block.
pid = wait(&status);
log_printf(LOG_DEBUG, "mod_sigchld(): notified that child %d died.\n", pid);
// call child function of all modules
for (m=mod_list;m;m=m->next)
if (m->chld)
m->chld(pid,status);
}
/*************************************************************************
* EOF
*/
|