]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/res.c
Big backport from Enterprise
[bacula/bacula] / bacula / src / lib / res.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2016 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *  This file handles locking and seaching resources
21  *
22  *     Kern Sibbald, January MM
23  *       Split from parse_conf.c April MMV
24  *
25  */
26
27 #include "bacula.h"
28
29 /* Each daemon has a slightly different set of
30  * resources, so it will define the following
31  * global values.
32  */
33 extern int32_t r_first;
34 extern int32_t r_last;
35 extern RES_TABLE resources[];
36 extern RES_HEAD **res_head;
37
38 brwlock_t res_lock;                   /* resource lock */
39 static int res_locked = 0;            /* resource chain lock count -- for debug */
40
41
42 /* #define TRACE_RES */
43
44 void b_LockRes(const char *file, int line)
45 {
46    int errstat;
47 #ifdef TRACE_RES
48    Pmsg4(000, "LockRes  locked=%d w_active=%d at %s:%d\n",
49          res_locked, res_lock.w_active, file, line);
50     if (res_locked) {
51        Pmsg2(000, "LockRes writerid=%d myid=%d\n", res_lock.writer_id,
52           pthread_self());
53      }
54 #endif
55    if ((errstat=rwl_writelock(&res_lock)) != 0) {
56       Emsg3(M_ABORT, 0, _("rwl_writelock failure at %s:%d:  ERR=%s\n"),
57            file, line, strerror(errstat));
58    }
59    res_locked++;
60 }
61
62 void b_UnlockRes(const char *file, int line)
63 {
64    int errstat;
65    if ((errstat=rwl_writeunlock(&res_lock)) != 0) {
66       Emsg3(M_ABORT, 0, _("rwl_writeunlock failure at %s:%d:. ERR=%s\n"),
67            file, line, strerror(errstat));
68    }
69    res_locked--;
70 #ifdef TRACE_RES
71    Pmsg4(000, "UnLockRes locked=%d wactive=%d at %s:%d\n",
72          res_locked, res_lock.w_active, file, line);
73 #endif
74 }
75
76 /*
77  * Compare two resource names
78  */
79 int res_compare(void *item1, void *item2)
80 {
81    RES *res1 = (RES *)item1;
82    RES *res2 = (RES *)item2;
83    return strcmp(res1->name, res2->name);
84 }
85
86 /*
87  * Return resource of type rcode that matches name
88  */
89 RES *
90 GetResWithName(int rcode, const char *name)
91 {
92    RES_HEAD *reshead;
93    int rindex = rcode - r_first;
94    RES item, *res;
95
96    LockRes();
97    reshead = res_head[rindex];
98    item.name = (char *)name;
99    res = (RES *)reshead->res_list->search(&item, res_compare);
100    UnlockRes();
101    return res;
102
103 }
104
105 /*
106  * Return next resource of type rcode. On first
107  * call second arg (res) is NULL, on subsequent
108  * calls, it is called with previous value.
109  */
110 RES *
111 GetNextRes(int rcode, RES *res)
112 {
113    RES *nres;
114    int rindex = rcode - r_first;
115
116    if (res == NULL) {
117       nres = (RES *)res_head[rindex]->first;
118    } else {
119       nres = res->res_next;
120    }
121    return nres;
122 }
123
124 /*
125  * Return next resource of type rcode. On first
126  * call second arg (res) is NULL, on subsequent
127  * calls, it is called with previous value.
128  */
129 RES *
130 GetNextRes(RES_HEAD **rhead, int rcode, RES *res)
131 {
132    RES *nres;
133    int rindex = rcode - r_first;
134
135    if (res == NULL) {
136       nres = (RES *)rhead[rindex]->first;
137    } else {
138       nres = res->res_next;
139    }
140    return nres;
141 }
142
143
144 /* Parser state */
145 enum parse_state {
146    p_none,
147    p_resource
148 };