]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/res.c
- Convert more atoi to str_to_int64() for DB.
[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 /*
11    Copyright (C) 2000-2005 Kern Sibbald
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30
31 #include "bacula.h"
32
33 extern int debug_level;
34
35 /* Each daemon has a slightly different set of
36  * resources, so it will define the following
37  * global values.
38  */
39 extern int r_first;
40 extern int r_last;
41 extern RES_TABLE resources[];
42 extern RES **res_head;
43
44 #ifdef HAVE_WIN32
45 // work around visual studio name manling preventing external linkage since res_all
46 // is declared as a different type when instantiated.
47 extern "C" CURES res_all;
48 extern "C" int res_all_size;
49 #else
50 extern  CURES res_all;
51 extern int res_all_size;
52 #endif
53
54
55 brwlock_t res_lock;                   /* resource lock */
56 static int res_locked = 0;            /* set when resource chains locked -- for debug */
57
58
59
60 /* #define TRACE_RES */
61
62 void b_LockRes(const char *file, int line)
63 {
64    int errstat;
65 #ifdef TRACE_RES
66    Pmsg4(000, "LockRes  locked=%d w_active=%d at %s:%d\n", 
67          res_locked, res_lock.w_active, file, line);
68     if (res_locked) {
69        Pmsg2(000, "LockRes writerid=%d myid=%d\n", res_lock.writer_id,
70           pthread_self());
71      }
72 #endif
73    if ((errstat=rwl_writelock(&res_lock)) != 0) {
74       Emsg3(M_ABORT, 0, "rwl_writelock failure at %s:%d:  ERR=%s\n",
75            file, line, strerror(errstat));
76    }
77    res_locked++;
78 }
79
80 void b_UnlockRes(const char *file, int line)
81 {
82    int errstat;
83    if ((errstat=rwl_writeunlock(&res_lock)) != 0) {
84       Emsg3(M_ABORT, 0, "rwl_writeunlock failure at %s:%d:. ERR=%s\n",
85            file, line, strerror(errstat));
86    }
87    res_locked--;
88 #ifdef TRACE_RES
89    Pmsg4(000, "UnLockRes locked=%d wactive=%d at %s:%d\n", 
90          res_locked, res_lock.w_active, file, line);
91 #endif
92 }
93
94 /*
95  * Return resource of type rcode that matches name
96  */
97 RES *
98 GetResWithName(int rcode, char *name)
99 {
100    RES *res;
101    int rindex = rcode - r_first;
102
103    LockRes();
104    res = res_head[rindex];
105    while (res) {
106       if (strcmp(res->name, name) == 0) {
107          break;
108       }
109       res = res->next;
110    }
111    UnlockRes();
112    return res;
113
114 }
115
116 /*
117  * Return next resource of type rcode. On first
118  * call second arg (res) is NULL, on subsequent
119  * calls, it is called with previous value.
120  */
121 RES *
122 GetNextRes(int rcode, RES *res)
123 {
124    RES *nres;
125    int rindex = rcode - r_first;
126
127
128 //   if (!res_locked) {
129 //      Emsg0(M_ABORT, 0, "Resource chain not locked.\n");
130 //   }
131    if (res == NULL) {
132       nres = res_head[rindex];
133    } else {
134       nres = res->next;
135    }
136    return nres;
137 }
138
139
140 /* Parser state */
141 enum parse_state {
142    p_none,
143    p_resource
144 };