LCOV - code coverage report
Current view: top level - src - cstat.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 63 66 95.5 %
Date: 2017-04-01 Functions: 11 11 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         120 : struct cstat *cstat_alloc(void)
      10             : {
      11         120 :         return (struct cstat *)calloc_w(1, sizeof(struct cstat), __func__);
      12             : }
      13             : 
      14         119 : int cstat_init(struct cstat *cstat,
      15             :         const char *name, const char *clientconfdir)
      16             : {
      17         119 :         if((clientconfdir && !(cstat->conffile=prepend_s(clientconfdir, name)))
      18         119 :           || !(cstat->name=strdup_w(name, __func__)))
      19             :                 return -1;
      20             :         return 0;
      21             : }
      22             : 
      23          97 : int cstat_init_with_cntr(struct cstat *cstat,
      24             :         const char *name, const char *clientconfdir)
      25             : {
      26          97 :         if(cstat_init(cstat, name, clientconfdir)
      27          97 :           || !(cstat->cntr=cntr_alloc())
      28          97 :           || cntr_init(cstat->cntr, name))
      29             :                 return -1;
      30             :         return 0;
      31             : }
      32             : 
      33         119 : static void cstat_free_content(struct cstat *c)
      34             : {
      35         238 :         if(!c) return;
      36         119 :         bu_list_free(&c->bu);
      37         119 :         free_w(&c->name);
      38         119 :         free_w(&c->conffile);
      39         119 :         strlists_free(&c->labels);
      40         119 :         cntr_free(&c->cntr);
      41         119 :         if(c->sdirs) logp("%s() called without freeing sdirs\n", __func__);
      42         119 :         c->clientdir_mtime=0;
      43         119 :         c->lockfile_mtime=0;
      44             : }
      45             : 
      46         119 : void cstat_free(struct cstat **cstat)
      47             : {
      48         238 :         if(!cstat || !*cstat) return;
      49         119 :         cstat_free_content(*cstat);
      50         119 :         free_v((void **)cstat);
      51             : }
      52             : 
      53         104 : void cstat_add_to_list(struct cstat **clist, struct cstat *cnew)
      54             : {
      55         104 :         struct cstat *c=NULL;
      56         104 :         struct cstat *clast=NULL;
      57             : 
      58         200 :         for(c=*clist; c; c=c->next)
      59             :         {
      60         117 :                 if(strcmp(cnew->name, c->name)<0)
      61             :                 {
      62          21 :                         c->prev=cnew;
      63          21 :                         break;
      64             :                 }
      65          96 :                 c->prev=clast;
      66          96 :                 clast=c;
      67             :         }
      68         104 :         if(clast)
      69             :         {
      70          62 :                 cnew->next=clast->next;
      71          62 :                 clast->next=cnew;
      72          62 :                 cnew->prev=clast;
      73             :         }
      74             :         else
      75             :         {
      76          42 :                 *clist=cnew;
      77          42 :                 cnew->next=c;
      78             :         }
      79         104 : }
      80             : 
      81          55 : void cstat_list_free(struct cstat **clist)
      82             : {
      83             :         struct cstat *c;
      84             :         struct cstat *next;
      85          55 :         struct cstat *prev=NULL;
      86          55 :         if(*clist) prev=(*clist)->prev;
      87         151 :         for(c=*clist; c; c=next)
      88             :         {
      89          96 :                 next=c->next;
      90          96 :                 cstat_free(&c);
      91             :         }
      92             :         // Do it in both directions.
      93          55 :         for(c=prev; c; c=prev)
      94             :         {
      95           0 :                 prev=c->prev;
      96           0 :                 cstat_free(&c);
      97             :         }
      98          55 :         *clist=NULL;
      99          55 : }
     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             : }
     135             : 
     136           4 : int cstat_count(struct cstat *clist)
     137             : {
     138           4 :         int count=0;
     139             :         struct cstat *c;
     140           4 :         for(c=clist; c; c=c->next)
     141           0 :                 count++;
     142           4 :         return count;
     143             : }

Generated by: LCOV version 1.10