]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/res.c
kes Apply Marco van Wieringen's set of patches, cleans up Migration/Copy
[bacula/bacula] / bacula / src / lib / res.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *  This file handles locking and seaching resources
30  *
31  *     Kern Sibbald, January MM
32  *       Split from parse_conf.c April MMV
33  *
34  *   Version $Id$
35  */
36
37 #include "bacula.h"
38
39 /* Each daemon has a slightly different set of
40  * resources, so it will define the following
41  * global values.
42  */
43 extern int32_t r_first;
44 extern int32_t r_last;
45 extern RES_TABLE resources[];
46 extern RES **res_head;
47
48 brwlock_t res_lock;                   /* resource lock */
49 static int res_locked = 0;            /* resource chain lock count -- for debug */
50
51
52 /* #define TRACE_RES */
53
54 void b_LockRes(const char *file, int line)
55 {
56    int errstat;
57 #ifdef TRACE_RES
58    Pmsg4(000, "LockRes  locked=%d w_active=%d at %s:%d\n", 
59          res_locked, res_lock.w_active, file, line);
60     if (res_locked) {
61        Pmsg2(000, "LockRes writerid=%d myid=%d\n", res_lock.writer_id,
62           pthread_self());
63      }
64 #endif
65    if ((errstat=rwl_writelock(&res_lock)) != 0) {
66       Emsg3(M_ABORT, 0, _("rwl_writelock failure at %s:%d:  ERR=%s\n"),
67            file, line, strerror(errstat));
68    }
69    res_locked++;
70 }
71
72 void b_UnlockRes(const char *file, int line)
73 {
74    int errstat;
75    if ((errstat=rwl_writeunlock(&res_lock)) != 0) {
76       Emsg3(M_ABORT, 0, _("rwl_writeunlock failure at %s:%d:. ERR=%s\n"),
77            file, line, strerror(errstat));
78    }
79    res_locked--;
80 #ifdef TRACE_RES
81    Pmsg4(000, "UnLockRes locked=%d wactive=%d at %s:%d\n", 
82          res_locked, res_lock.w_active, file, line);
83 #endif
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 *res;
93    int rindex = rcode - r_first;
94
95    LockRes();
96    res = res_head[rindex];
97    while (res) {
98       if (strcmp(res->name, name) == 0) {
99          break;
100       }
101       res = res->next;
102    }
103    UnlockRes();
104    return res;
105
106 }
107
108 /*
109  * Return next resource of type rcode. On first
110  * call second arg (res) is NULL, on subsequent
111  * calls, it is called with previous value.
112  */
113 RES *
114 GetNextRes(int rcode, RES *res)
115 {
116    RES *nres;
117    int rindex = rcode - r_first;
118
119    if (res == NULL) {
120       nres = res_head[rindex];
121    } else {
122       nres = res->next;
123    }
124    return nres;
125 }
126
127
128 /* Parser state */
129 enum parse_state {
130    p_none,
131    p_resource
132 };