LCOV - code coverage report
Current view: top level - src - cstat.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 61 63 96.8 %
Date: 2016-07-02 Functions: 10 10 100.0 %

          Line data    Source code
       1             : #include "burp.h"
       2             : #include "alloc.h"
       3             : #include "bu.h"
       4             : #include "cstat.h"
       5             : #include "log.h"
       6             : #include "prepend.h"
       7             : #include "strlist.h"
       8             : 
       9         117 : struct cstat *cstat_alloc(void)
      10             : {
      11         117 :         return (struct cstat *)calloc_w(1, sizeof(struct cstat), __func__);
      12             : }
      13             : 
      14         116 : int cstat_init(struct cstat *cstat,
      15             :         const char *name, const char *clientconfdir)
      16             : {
      17         188 :         if((clientconfdir && !(cstat->conffile=prepend_s(clientconfdir, name)))
      18         232 :           || !(cstat->name=strdup_w(name, __func__)))
      19             :                 return -1;
      20         116 :         return 0;
      21             : }
      22             : 
      23          97 : int cstat_init_with_cntr(struct cstat *cstat,
      24             :         const char *name, const char *clientconfdir)
      25             : {
      26         194 :         if(cstat_init(cstat, name, clientconfdir)
      27          97 :           || !(cstat->cntr=cntr_alloc())
      28         194 :           || cntr_init(cstat->cntr, name))
      29             :                 return -1;
      30          97 :         return 0;
      31             : }
      32             : 
      33         116 : static void cstat_free_content(struct cstat *c)
      34             : {
      35         232 :         if(!c) return;
      36         116 :         bu_list_free(&c->bu);
      37         116 :         free_w(&c->name);
      38         116 :         free_w(&c->conffile);
      39         116 :         strlists_free(&c->labels);
      40         116 :         cntr_free(&c->cntr);
      41         116 :         if(c->sdirs) logp("%s() called without freeing sdirs\n", __func__);
      42         116 :         c->clientdir_mtime=0;
      43         116 :         c->lockfile_mtime=0;
      44             : }
      45             : 
      46         116 : void cstat_free(struct cstat **cstat)
      47             : {
      48         232 :         if(!cstat || !*cstat) return;
      49         116 :         cstat_free_content(*cstat);
      50         116 :         free_v((void **)cstat);
      51             : }
      52             : 
      53         101 : void cstat_add_to_list(struct cstat **clist, struct cstat *cnew)
      54             : {
      55         101 :         struct cstat *c=NULL;
      56         101 :         struct cstat *clast=NULL;
      57             : 
      58         194 :         for(c=*clist; c; c=c->next)
      59             :         {
      60         114 :                 if(strcmp(cnew->name, c->name)<0)
      61             :                 {
      62          21 :                         c->prev=cnew;
      63          21 :                         break;
      64             :                 }
      65          93 :                 c->prev=clast;
      66          93 :                 clast=c;
      67             :         }
      68         101 :         if(clast)
      69             :         {
      70          60 :                 cnew->next=clast->next;
      71          60 :                 clast->next=cnew;
      72          60 :                 cnew->prev=clast;
      73             :         }
      74             :         else
      75             :         {
      76          41 :                 *clist=cnew;
      77          41 :                 cnew->next=c;
      78             :         }
      79         101 : }
      80             : 
      81          46 : void cstat_list_free(struct cstat **clist)
      82             : {
      83             :         struct cstat *c;
      84             :         struct cstat *next;
      85          46 :         struct cstat *prev=NULL;
      86          46 :         if(*clist) prev=(*clist)->prev;
      87         139 :         for(c=*clist; c; c=next)
      88             :         {
      89          93 :                 next=c->next;
      90          93 :                 cstat_free(&c);
      91             :         }
      92             :         // Do it in both directions.
      93          46 :         for(c=prev; c; c=prev)
      94             :         {
      95           0 :                 prev=c->prev;
      96           0 :                 cstat_free(&c);
      97             :         }
      98          46 :         *clist=NULL;
      99          46 : }
     100             : 
     101          45 : const char *run_status_to_str(struct cstat *cstat)
     102             : {
     103          45 :         switch(cstat->run_status)
     104             :         {
     105             :                 case RUN_STATUS_IDLE:
     106             :                         return RUN_STATUS_STR_IDLE;
     107             :                 case RUN_STATUS_CLIENT_CRASHED:
     108             :                         return RUN_STATUS_STR_CLIENT_CRASHED;
     109             :                 case RUN_STATUS_SERVER_CRASHED:
     110             :                         return RUN_STATUS_STR_SERVER_CRASHED;
     111             :                 case RUN_STATUS_RUNNING:
     112             :                         return RUN_STATUS_STR_RUNNING;
     113             :                 default: return "unknown";
     114             :         }
     115             : }
     116             : 
     117          92 : enum run_status run_str_to_status(const char *str)
     118             : {
     119          92 :         if(!strcmp(str, RUN_STATUS_STR_IDLE)) return RUN_STATUS_IDLE;
     120          92 :         else if(!strcmp(str, RUN_STATUS_STR_RUNNING)) return RUN_STATUS_RUNNING;
     121          92 :         else if(!strcmp(str, RUN_STATUS_STR_CLIENT_CRASHED))
     122             :                 return RUN_STATUS_CLIENT_CRASHED;
     123          92 :         else if(!strcmp(str, RUN_STATUS_STR_SERVER_CRASHED))
     124             :                 return RUN_STATUS_CLIENT_CRASHED;
     125          92 :         else if(!strcmp(str, RUN_STATUS_STR_RUNNING)) return RUN_STATUS_RUNNING;
     126          92 :         return RUN_STATUS_UNSET;
     127             : }
     128             : 
     129          93 : struct cstat *cstat_get_by_name(struct cstat *clist, const char *name)
     130             : {
     131             :         struct cstat *c;
     132          93 :         for(c=clist; c; c=c->next) if(!strcmp(c->name, name)) return c;
     133             :         return NULL;
     134             : }

Generated by: LCOV version 1.10