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