]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/res.c
- Correct compiler complaints in wx-console and tray-monitor.
[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-2005 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 #ifdef HAVE_WIN32
38 // work around visual studio name manling preventing external linkage since res_all
39 // is declared as a different type when instantiated.
40 extern "C" CURES res_all;
41 extern "C" int res_all_size;
42 #else
43 extern  CURES res_all;
44 extern int res_all_size;
45 #endif
46
47
48 brwlock_t res_lock;                   /* resource lock */
49 static int res_locked = 0;            /* set when resource chains locked -- 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, 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 };