Sun Dec 18 20:55:41 2011

Asterisk developer's documentation


res_watchdog.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- A telephony toolkit for Linux.
00003  *
00004  * Resource to make watchdogs happy
00005  *
00006  * Copyright (C) 2005, Junghanns.NET GmbH
00007  *
00008  * Klaus-Peter Junghanns <kpj@junghanns.net>
00009  *
00010  * This program is free software, distributed under the terms of
00011  * the GNU General Public License
00012  */
00013 
00014 #include "asterisk.h"
00015 #include <stdlib.h>
00016 #include <errno.h>
00017 #include <unistd.h>
00018 #include <string.h>
00019 #include <stdlib.h>
00020 #include <stdio.h>
00021 #include <sys/time.h>
00022 #include <sys/signal.h>
00023 #include <netinet/in.h>
00024 
00025 #include <asterisk/logger.h>
00026 #include <asterisk/channel.h>
00027 #include <asterisk/pbx.h>
00028 #include <asterisk/options.h>
00029 #include <asterisk/module.h>
00030 #include <asterisk/translate.h>
00031 #include <asterisk/say.h>
00032 #include <asterisk/features.h>
00033 #include <asterisk/musiconhold.h>
00034 #include <asterisk/config.h>
00035 #include <asterisk/cli.h>
00036 #include <asterisk/manager.h>
00037 #include <asterisk/utils.h>
00038 #include <asterisk/lock.h>
00039 #include <asterisk/adsi.h>
00040 
00041 static struct watchdog_pvt *watchdogs = NULL;
00042 
00043 typedef struct watchdog_pvt {
00044     char device[80];
00045     int fd;
00046     int type;
00047     int interval;
00048     pthread_t watchdog_thread;
00049     struct watchdog_pvt *next;
00050 } watchdog_pvt;
00051 
00052 static void *do_watchdog_thread(void *data) {
00053     struct watchdog_pvt *woof = (struct watchdog_pvt *)data;
00054     for (;;) {
00055    if (woof->fd) {
00056        write(woof->fd, "PING\n", 5);
00057    }
00058    usleep(woof->interval * 1000);
00059     }
00060     return NULL;
00061 }
00062 
00063 
00064 static int load_module(void)
00065 {
00066    int res = 0;
00067    const char *cat, *utype, *udevice, *uinterval;
00068    struct ast_config *cfg;
00069    struct watchdog_pvt *woof = NULL;
00070 
00071    cfg = ast_config_load("watchdog.conf");
00072    if (cfg) {
00073        cat = ast_category_browse(cfg, NULL);
00074        while(cat) {
00075       cat = ast_category_browse(cfg, cat);
00076       utype = ast_variable_retrieve(cfg, cat, "type");
00077 /*    if (utype) {
00078           ast_log(LOG_NOTICE, "type = %s\n", utype);
00079       } */
00080       udevice = ast_variable_retrieve(cfg, cat, "device");
00081 /*    if (udevice) {
00082           ast_log(LOG_NOTICE, "device = %s\n", udevice);
00083       } */
00084       uinterval = ast_variable_retrieve(cfg, cat, "interval");
00085 /*    if (uinterval) {
00086           ast_log(LOG_NOTICE, "interval = %s\n", uinterval);
00087       } */
00088       if (uinterval && udevice && utype) {
00089           woof = malloc(sizeof(struct watchdog_pvt));
00090           if (!woof) {
00091          ast_log(LOG_ERROR, "unable to malloc!\n");
00092          return -1;
00093           }
00094           memset(woof, 0x0, sizeof(struct watchdog_pvt));
00095           strncpy(woof->device, udevice, sizeof(woof->device) - 1);
00096           
00097           woof->interval = atoi(uinterval);;
00098           woof->next = watchdogs;
00099           watchdogs = woof;
00100           woof->fd = open(woof->device, O_WRONLY | O_SYNC);
00101           if (woof->fd) {
00102          if (!strncmp(utype, "isdnguard", sizeof(utype))) {
00103              woof->type = 1;
00104              write(woof->fd, "START\n", 6);
00105          }
00106          ast_pthread_create(&woof->watchdog_thread, NULL, do_watchdog_thread, woof);
00107           } else {
00108          ast_log(LOG_WARNING, "error opening watchdog device %s !\n", woof->device);
00109           }
00110       }
00111        }
00112           ast_config_destroy(cfg);
00113    }
00114    return res;
00115 }
00116 
00117 
00118 static int unload_module(void)
00119 {
00120    struct watchdog_pvt *dogs, *woof;
00121    dogs = watchdogs;
00122    while (dogs) {
00123        pthread_cancel(dogs->watchdog_thread);
00124        pthread_join(dogs->watchdog_thread, NULL);
00125        close(dogs->fd);
00126        woof = dogs->next;
00127        free(dogs);
00128        dogs = woof;
00129    }
00130    return 0;
00131 }
00132 
00133 
00134 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Watchdog Resource",
00135       .load = load_module,
00136       .unload = unload_module,
00137 );

Generated on Sun Dec 18 20:55:41 2011 for Asterisk - the Open Source PBX by  doxygen 1.5.6