]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/res.c
Fix header file includes.
[bacula/bacula] / bacula / src / lib / res.c
1 /*
2  *  This file handles locking and seaching resources
3  *
4  *     Kern Sibbald, January MM
5  *       Split from parse_conf.c April MMV
6  *
7  *   Version $Id$
8  */
9 /*
10    Copyright (C) 2000-2006 Kern Sibbald
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License
14    version 2 as amended with additional clauses defined in the
15    file LICENSE in the main source directory.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
20    the file LICENSE for additional details.
21
22  */
23
24 #include "bacula.h"
25
26 extern int debug_level;
27
28 /* Each daemon has a slightly different set of
29  * resources, so it will define the following
30  * global values.
31  */
32 extern int r_first;
33 extern int r_last;
34 extern RES_TABLE resources[];
35 extern RES **res_head;
36
37 brwlock_t res_lock;                   /* resource lock */
38 static int res_locked = 0;            /* set when resource chains locked -- for debug */
39
40
41 /* #define TRACE_RES */
42
43 void b_LockRes(const char *file, int line)
44 {
45    int errstat;
46 #ifdef TRACE_RES
47    Pmsg4(000, "LockRes  locked=%d w_active=%d at %s:%d\n", 
48          res_locked, res_lock.w_active, file, line);
49     if (res_locked) {
50        Pmsg2(000, "LockRes writerid=%d myid=%d\n", res_lock.writer_id,
51           pthread_self());
52      }
53 #endif
54    if ((errstat=rwl_writelock(&res_lock)) != 0) {
55       Emsg3(M_ABORT, 0, _("rwl_writelock failure at %s:%d:  ERR=%s\n"),
56            file, line, strerror(errstat));
57    }
58    res_locked++;
59 }
60
61 void b_UnlockRes(const char *file, int line)
62 {
63    int errstat;
64    if ((errstat=rwl_writeunlock(&res_lock)) != 0) {
65       Emsg3(M_ABORT, 0, _("rwl_writeunlock failure at %s:%d:. ERR=%s\n"),
66            file, line, strerror(errstat));
67    }
68    res_locked--;
69 #ifdef TRACE_RES
70    Pmsg4(000, "UnLockRes locked=%d wactive=%d at %s:%d\n", 
71          res_locked, res_lock.w_active, file, line);
72 #endif
73 }
74
75 /*
76  * Return resource of type rcode that matches name
77  */
78 RES *
79 GetResWithName(int rcode, char *name)
80 {
81    RES *res;
82    int rindex = rcode - r_first;
83
84    LockRes();
85    res = res_head[rindex];
86    while (res) {
87       if (strcmp(res->name, name) == 0) {
88          break;
89       }
90       res = res->next;
91    }
92    UnlockRes();
93    return res;
94
95 }
96
97 /*
98  * Return next resource of type rcode. On first
99  * call second arg (res) is NULL, on subsequent
100  * calls, it is called with previous value.
101  */
102 RES *
103 GetNextRes(int rcode, RES *res)
104 {
105    RES *nres;
106    int rindex = rcode - r_first;
107
108    if (res == NULL) {
109       nres = res_head[rindex];
110    } else {
111       nres = res->next;
112    }
113    return nres;
114 }
115
116
117 /* Parser state */
118 enum parse_state {
119    p_none,
120    p_resource
121 };