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