s3: Remove procid_self() from connections_fetch_entry()
[kai/samba.git] / source3 / lib / conn_tdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Low-level connections.tdb access functions
4    Copyright (C) Volker Lendecke 2007
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "smbd/globals.h"
22
23 static struct db_context *connections_db_ctx(bool rw)
24 {
25         static struct db_context *db_ctx;
26         int open_flags;
27
28         if (db_ctx != NULL) {
29                 return db_ctx;
30         }
31
32         open_flags = rw ? (O_RDWR|O_CREAT) : O_RDONLY;
33
34         db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
35                          TDB_CLEAR_IF_FIRST|TDB_DEFAULT, open_flags, 0644);
36         return db_ctx;
37 }
38
39 static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
40                                                   TDB_DATA key)
41 {
42         struct db_context *ctx = connections_db_ctx(True);
43
44         if (ctx == NULL) {
45                 return NULL;
46         }
47
48         return ctx->fetch_locked(ctx, mem_ctx, key);
49 }
50
51 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
52                                           connection_struct *conn,
53                                           const char *name)
54 {
55         struct connections_key ckey;
56         TDB_DATA key;
57
58         ZERO_STRUCT(ckey);
59         ckey.pid = sconn_server_id(conn->sconn);
60         ckey.cnum = conn->cnum;
61         strlcpy(ckey.name, name, sizeof(ckey.name));
62
63         key.dsize = sizeof(ckey);
64         key.dptr = (uint8 *)&ckey;
65
66         return connections_fetch_record(mem_ctx, key);
67 }
68
69 struct conn_traverse_state {
70         int (*fn)(struct db_record *rec,
71                   const struct connections_key *key,
72                   const struct connections_data *data,
73                   void *private_data);
74         void *private_data;
75 };
76
77 static int conn_traverse_fn(struct db_record *rec, void *private_data)
78 {
79         struct conn_traverse_state *state =
80                 (struct conn_traverse_state *)private_data;
81
82         if ((rec->key.dsize != sizeof(struct connections_key))
83             || (rec->value.dsize != sizeof(struct connections_data))) {
84                 return 0;
85         }
86
87         return state->fn(rec, (const struct connections_key *)rec->key.dptr,
88                          (const struct connections_data *)rec->value.dptr,
89                          state->private_data);
90 }
91
92 int connections_traverse(int (*fn)(struct db_record *rec,
93                                    void *private_data),
94                          void *private_data)
95 {
96         struct db_context *ctx = connections_db_ctx(False);
97
98         if (ctx == NULL) {
99                 return -1;
100         }
101
102         return ctx->traverse(ctx, fn, private_data);
103 }
104
105 int connections_forall(int (*fn)(struct db_record *rec,
106                                  const struct connections_key *key,
107                                  const struct connections_data *data,
108                                  void *private_data),
109                        void *private_data)
110 {
111         struct db_context *ctx;
112         struct conn_traverse_state state;
113
114         ctx = connections_db_ctx(true);
115         if (ctx == NULL) {
116                 return -1;
117         }
118
119         state.fn = fn;
120         state.private_data = private_data;
121
122         return ctx->traverse(ctx, conn_traverse_fn, (void *)&state);
123 }
124
125 struct conn_traverse_read_state {
126         int (*fn)(const struct connections_key *key,
127                   const struct connections_data *data,
128                   void *private_data);
129         void *private_data;
130 };
131
132 static int connections_forall_read_fn(struct db_record *rec,
133                                       void *private_data)
134 {
135         struct conn_traverse_read_state *state =
136                 (struct conn_traverse_read_state *)private_data;
137
138         if ((rec->key.dsize != sizeof(struct connections_key))
139             || (rec->value.dsize != sizeof(struct connections_data))) {
140                 return 0;
141         }
142         return state->fn((const struct connections_key *)rec->key.dptr,
143                          (const struct connections_data *)rec->value.dptr,
144                          state->private_data);
145 }
146
147 int connections_forall_read(int (*fn)(const struct connections_key *key,
148                                       const struct connections_data *data,
149                                       void *private_data),
150                             void *private_data)
151 {
152         struct db_context *ctx;
153         struct conn_traverse_read_state state;
154
155         ctx = connections_db_ctx(false);
156         if (ctx == NULL) {
157                 return -1;
158         }
159
160         state.fn = fn;
161         state.private_data = private_data;
162
163         return ctx->traverse_read(ctx, connections_forall_read_fn,
164                                   (void *)&state);
165 }
166
167 bool connections_init(bool rw)
168 {
169         return (connections_db_ctx(rw) != NULL);
170 }