Changeset 8885

Show
Ignore:
Timestamp:
07/15/12 18:25:24 (10 months ago)
Author:
jow
Message:

contrib/package: make freifunk-watchdog more generic

This change is based on a patch by "flyn" from  https://dev.openwrt.org/ticket/11868 .

Location:
luci/trunk/contrib/package/freifunk-watchdog
Files:
1 added
3 modified

Legend:

Unmodified
Added
Removed
  • luci/trunk/contrib/package/freifunk-watchdog/Makefile

    r7771 r8885  
    11# 
    2 # Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org> 
     2# Copyright (C) 2009-2012 Jo-Philipp Wich <xm@subsignal.org> 
    33# 
    44# This is free software, licensed under the GNU General Public License v2. 
     
    99 
    1010PKG_NAME:=freifunk-watchdog 
    11 PKG_RELEASE:=7 
     11PKG_RELEASE:=8 
    1212 
    1313PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) 
     
    4747    $(INSTALL_DIR) $(1)/etc/init.d 
    4848    $(INSTALL_BIN) ./files/freifunk-watchdog.init $(1)/etc/init.d/freifunk-watchdog 
     49    $(INSTALL_DIR) $(1)/etc/config 
     50    $(INSTALL_CONF) ./files/freifunk-watchdog.config $(1)/etc/config/freifunk-watchdog 
    4951    $(INSTALL_DIR) $(1)/usr/sbin 
    5052    $(INSTALL_BIN) $(PKG_BUILD_DIR)/ffwatchd $(1)/usr/sbin/ 
  • luci/trunk/contrib/package/freifunk-watchdog/src/watchdog.c

    r5359 r8885  
    198198 
    199199/* Add tuple */ 
    200 static void load_wifi_uci_add_iface(const char *section, struct uci_itr_ctx *itr) 
     200static void load_wifi_uci_add_iface(const char *section, struct uci_wifi_iface_itr_ctx *itr) 
    201201{ 
    202202    wifi_tuple_t *t; 
     
    254254{ 
    255255    struct uci_context *ctx; 
    256     struct uci_itr_ctx itr; 
     256    struct uci_wifi_iface_itr_ctx itr; 
    257257    wifi_tuple_t *cur, *next; 
    258258 
    259259    if( check_uci_update("wireless", modtime) ) 
    260260    { 
    261         syslog(LOG_INFO, "Config changed, reloading"); 
     261        syslog(LOG_INFO, "Wireless config changed, reloading"); 
    262262 
    263263        if( (ctx = ucix_init("wireless")) != NULL ) 
     
    285285} 
    286286 
     287/* Add tuple */ 
     288static void load_watchdog_uci_add_process(const char *section, struct uci_process_itr_ctx *itr) 
     289{ 
     290    process_tuple_t *t; 
     291    const char *ucitmp; 
     292    int val = 0; 
     293 
     294    if( (t = (process_tuple_t *)malloc(sizeof(process_tuple_t))) != NULL ) 
     295    { 
     296        t->restart = 0; 
     297 
     298        ucitmp = ucix_get_option(itr->ctx, "freifunk-watchdog", section, "process"); 
     299        if(ucitmp) 
     300        { 
     301            strncpy(t->process, ucitmp, sizeof(t->process)); 
     302            val++; 
     303        } 
     304 
     305        ucitmp = ucix_get_option(itr->ctx, "freifunk-watchdog", section, "initscript"); 
     306        if(ucitmp) 
     307        { 
     308            strncpy(t->initscript, ucitmp, sizeof(t->initscript)); 
     309            val++; 
     310        } 
     311 
     312        if( val == 2 ) 
     313        { 
     314            syslog(LOG_INFO, "Monitoring %s: initscript=%s", 
     315                t->process, t->initscript); 
     316 
     317                t->next = itr->list; 
     318                itr->list = t; 
     319        } 
     320        else 
     321        { 
     322            free(t); 
     323        } 
     324    } 
     325} 
     326 
     327/* Load config */ 
     328static process_tuple_t * load_watchdog_uci(process_tuple_t *procs) 
     329{ 
     330    struct uci_context *ctx; 
     331    struct uci_process_itr_ctx itr; 
     332    process_tuple_t *cur, *next; 
     333 
     334    syslog(LOG_INFO, "Loading watchdog config"); 
     335 
     336    if( (ctx = ucix_init("freifunk-watchdog")) != NULL ) 
     337    { 
     338        if( procs != NULL ) 
     339        { 
     340            for(cur = procs; cur; cur = next) 
     341            { 
     342                next = cur->next; 
     343                free(cur); 
     344            } 
     345        } 
     346 
     347        itr.list = NULL; 
     348        itr.ctx = ctx; 
     349 
     350        ucix_for_each_section_type(ctx, "freifunk-watchdog", "process", 
     351            (void *)load_watchdog_uci_add_process, &itr); 
     352 
     353        return itr.list; 
     354    } 
     355 
     356    return procs; 
     357} 
     358 
    287359/* Daemon implementation */ 
    288360static int do_daemon(void) 
     
    297369    struct sigaction sa; 
    298370 
    299     wifi_tuple_t *ifs = NULL, *curif; 
    300     time_t modtime = 0; 
     371    wifi_tuple_t *ifs = NULL, *curr_if; 
     372    process_tuple_t *procs = NULL, *curr_proc; 
     373    time_t wireless_modtime = 0; 
    301374 
    302375    int action_intv = 0; 
    303376    int restart_wifi = 0; 
    304     int restart_cron = 0; 
    305     int restart_sshd = 0; 
    306377    int loadavg_panic = 0; 
    307378 
     
    341412    sigaction(SIGCHLD, &sa, NULL); 
    342413 
     414    /* Load watchdog configuration only once */ 
     415    procs = load_watchdog_uci(procs); 
     416 
    343417    while( 1 ) 
    344418    { 
     
    355429                loadavg_panic = 0; 
    356430 
    357             /* Check crond */ 
    358             if( find_process("crond") < 0 ) 
    359                 restart_cron++; 
    360             else 
    361                 restart_cron = 0; 
    362  
    363             /* Check SSHd */ 
    364             if( find_process("dropbear") < 0 ) 
    365                 restart_sshd++; 
    366             else 
    367                 restart_sshd = 0; 
    368  
    369431            /* Check wireless interfaces */ 
    370             ifs = load_wifi_uci(ifs, &modtime); 
    371             for( curif = ifs; curif; curif = curif->next ) 
     432            ifs = load_wifi_uci(ifs, &wireless_modtime); 
     433            for( curr_if = ifs; curr_if; curr_if = curr_if->next ) 
    372434            { 
    373435                /* Get current channel and bssid */ 
    374                 if( (iw_get_bssid(iwfd, curif->ifname, bssid) == 0) && 
    375                 (iw_get_channel(iwfd, curif->ifname, &channel) == 0) ) 
     436                if( (iw_get_bssid(iwfd, curr_if->ifname, bssid) == 0) && 
     437                (iw_get_channel(iwfd, curr_if->ifname, &channel) == 0) ) 
    376438                { 
    377439                    /* Check BSSID */ 
    378                     if( strcasecmp(bssid, curif->bssid) != 0 ) 
     440                    if( strcasecmp(bssid, curr_if->bssid) != 0 ) 
    379441                    { 
    380442                        syslog(LOG_WARNING, "BSSID mismatch on %s: current=%s wanted=%s", 
    381                             curif->ifname, bssid, curif->bssid); 
     443                            curr_if->ifname, bssid, curr_if->bssid); 
    382444 
    383445                        restart_wifi++; 
     
    385447 
    386448                    /* Check channel */ 
    387                     else if( channel != curif->channel ) 
     449                    else if( channel != curr_if->channel ) 
    388450                    { 
    389451                        syslog(LOG_WARNING, "Channel mismatch on %s: current=%d wanted=%d", 
    390                             curif->ifname, channel, curif->channel); 
     452                            curr_if->ifname, channel, curr_if->channel); 
    391453 
    392454                        restart_wifi++; 
     
    395457                else 
    396458                { 
    397                     syslog(LOG_WARNING, "Requested interface %s not present", curif->ifname); 
     459                    syslog(LOG_WARNING, "Requested interface %s not present", curr_if->ifname); 
     460                } 
     461            } 
     462 
     463            /* Check processes */ 
     464            for( curr_proc = procs; curr_proc; curr_proc = curr_proc->next ) 
     465            { 
     466                if( find_process(curr_proc->process) < 0 ) 
     467                    curr_proc->restart++; 
     468                else 
     469                    curr_proc->restart = 0; 
     470 
     471                /* Process restart required? */ 
     472                if( curr_proc->restart >= HYSTERESIS ) 
     473                { 
     474                    curr_proc->restart = 0; 
     475                    syslog(LOG_WARNING, "The %s process died, restarting", curr_proc->process); 
     476                    EXEC(PROC_ACTION); 
    398477                } 
    399478            } 
     
    406485                syslog(LOG_WARNING, "Channel or BSSID mismatch on wireless interface, restarting"); 
    407486                EXEC(WIFI_ACTION); 
    408             } 
    409  
    410             /* Cron restart required? */ 
    411             if( restart_cron >= HYSTERESIS ) 
    412             { 
    413                 restart_cron = 0; 
    414                 syslog(LOG_WARNING, "The cron process died, restarting"); 
    415                 EXEC(CRON_ACTION); 
    416             } 
    417  
    418             /* SSHd restart required? */ 
    419             if( restart_sshd >= HYSTERESIS ) 
    420             { 
    421                 restart_sshd = 0; 
    422                 syslog(LOG_WARNING, "The ssh process died, restarting"); 
    423                 EXEC(SSHD_ACTION); 
    424487            } 
    425488 
  • luci/trunk/contrib/package/freifunk-watchdog/src/watchdog.h

    r5359 r8885  
    5353#define SYSLOG_IDENT    "Freifunk Watchdog" 
    5454 
     55/* Process error action */ 
     56#define PROC_ACTION     curr_proc->initscript, curr_proc->initscript, "restart" 
     57 
    5558/* Wifi error action */ 
    5659#define WIFI_ACTION     "/sbin/wifi", "/sbin/wifi" 
    57  
    58 /* Crond error action */ 
    59 #define CRON_ACTION     "/etc/init.d/cron", "/etc/init.d/cron", "restart" 
    60  
    61 /* SSHd error action */ 
    62 #define SSHD_ACTION     "/etc/init.d/dropbear", "/etc/init.d/dropbear", "restart" 
    6360 
    6461/* Watchdog device */ 
     
    8683 
    8784/* structure to hold tuple-list and uci context during iteration */ 
    88 struct uci_itr_ctx { 
     85struct uci_wifi_iface_itr_ctx { 
    8986    struct wifi_tuple *list; 
    9087    struct uci_context *ctx; 
     
    9289 
    9390typedef struct wifi_tuple wifi_tuple_t; 
     91 
     92 
     93/* process name/exec tuples */ 
     94struct process_tuple { 
     95    char process[PATH_MAX + 1]; 
     96    char initscript[PATH_MAX + 1]; 
     97    int restart; 
     98    struct process_tuple *next; 
     99}; 
     100 
     101/* structure to hold tuple-list and uci context during iteration */ 
     102struct uci_process_itr_ctx { 
     103    struct process_tuple *list; 
     104    struct uci_context *ctx; 
     105}; 
     106 
     107typedef struct process_tuple process_tuple_t; 
    94108 
    95109