]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/res.c
74598dc647ef25310c18704088085136d1aaadf2
[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 **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  * Return resource of type rcode that matches name
78  */
79 RES *
80 GetResWithName(int rcode, const char *name)
81 {
82    RES *res;
83    int rindex = rcode - r_first;
84
85    LockRes();
86    res = res_head[rindex];
87    while (res) {
88       if (strcmp(res->name, name) == 0) {
89          break;
90       }
91       res = res->next;
92    }
93    UnlockRes();
94    return res;
95
96 }
97
98 /*
99  * Return next resource of type rcode. On first
100  * call second arg (res) is NULL, on subsequent
101  * calls, it is called with previous value.
102  */
103 RES *
104 GetNextRes(int rcode, RES *res)
105 {
106    RES *nres;
107    int rindex = rcode - r_first;
108
109    if (res == NULL) {
110       nres = res_head[rindex];
111    } else {
112       nres = res->next;
113    }
114    return nres;
115 }
116
117
118 /* Parser state */
119 enum parse_state {
120    p_none,
121    p_resource
122 };