]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/res.c
Change JobFiles display from %f to %F in RunScript because %f was already affected...
[bacula/bacula] / bacula / src / lib / res.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2011 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 three of the GNU Affero 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 Affero 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  */
35
36 #include "bacula.h"
37
38 /* Each daemon has a slightly different set of
39  * resources, so it will define the following
40  * global values.
41  */
42 extern int32_t r_first;
43 extern int32_t r_last;
44 extern RES_TABLE resources[];
45 extern RES **res_head;
46
47 brwlock_t res_lock;                   /* resource lock */
48 static int res_locked = 0;            /* resource chain lock count -- for debug */
49
50
51 /* #define TRACE_RES */
52
53 void b_LockRes(const char *file, int line)
54 {
55    int errstat;
56 #ifdef TRACE_RES
57    Pmsg4(000, "LockRes  locked=%d w_active=%d at %s:%d\n", 
58          res_locked, res_lock.w_active, file, line);
59     if (res_locked) {
60        Pmsg2(000, "LockRes writerid=%d myid=%d\n", res_lock.writer_id,
61           pthread_self());
62      }
63 #endif
64    if ((errstat=rwl_writelock(&res_lock)) != 0) {
65       Emsg3(M_ABORT, 0, _("rwl_writelock failure at %s:%d:  ERR=%s\n"),
66            file, line, strerror(errstat));
67    }
68    res_locked++;
69 }
70
71 void b_UnlockRes(const char *file, int line)
72 {
73    int errstat;
74    if ((errstat=rwl_writeunlock(&res_lock)) != 0) {
75       Emsg3(M_ABORT, 0, _("rwl_writeunlock failure at %s:%d:. ERR=%s\n"),
76            file, line, strerror(errstat));
77    }
78    res_locked--;
79 #ifdef TRACE_RES
80    Pmsg4(000, "UnLockRes locked=%d wactive=%d at %s:%d\n", 
81          res_locked, res_lock.w_active, file, line);
82 #endif
83 }
84
85 /*
86  * Return resource of type rcode that matches name
87  */
88 RES *
89 GetResWithName(int rcode, const char *name)
90 {
91    RES *res;
92    int rindex = rcode - r_first;
93
94    LockRes();
95    res = res_head[rindex];
96    while (res) {
97       if (strcmp(res->name, name) == 0) {
98          break;
99       }
100       res = res->next;
101    }
102    UnlockRes();
103    return res;
104
105 }
106
107 /*
108  * Return next resource of type rcode. On first
109  * call second arg (res) is NULL, on subsequent
110  * calls, it is called with previous value.
111  */
112 RES *
113 GetNextRes(int rcode, RES *res)
114 {
115    RES *nres;
116    int rindex = rcode - r_first;
117
118    if (res == NULL) {
119       nres = res_head[rindex];
120    } else {
121       nres = res->next;
122    }
123    return nres;
124 }
125
126
127 /* Parser state */
128 enum parse_state {
129    p_none,
130    p_resource
131 };