s3: smbd: init_smb1_request() isn't being passed zero'ed memory from any codepath.
[Samba.git] / ctdb / server / ctdb_lock_helper.c
blob51d2992b64ebf27c33ec88f8a2cf93f555294fdd
1 /*
2 ctdb lock helper
4 Copyright (C) Amitay Isaacs 2013
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.
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.
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/>.
20 #include "replace.h"
21 #include "system/filesys.h"
22 #include "system/network.h"
23 #include "system/wait.h"
25 #include <talloc.h>
26 #include <tevent.h>
27 #include <tdb.h>
29 #include "lib/util/sys_rw.h"
30 #include "lib/util/tevent_unix.h"
32 #include "protocol/protocol.h"
34 #include "common/system.h"
36 static bool realtime = true;
38 struct lock_state {
39 struct tdb_context *tdb;
40 TDB_DATA key;
43 static void set_priority(void)
45 const char *ptr;
47 ptr = getenv("CTDB_NOSETSCHED");
48 if (ptr != NULL) {
49 realtime = false;
52 if (! realtime) {
53 return;
56 realtime = set_scheduler();
57 if (! realtime) {
58 fprintf(stderr,
59 "locking: Unable to set real-time scheduler priority\n");
63 static void reset_priority(void)
65 if (realtime) {
66 reset_scheduler();
70 static void send_result(int fd, char result)
72 sys_write(fd, &result, 1);
73 if (result == 1) {
74 exit(1);
79 static void usage(const char *progname)
81 fprintf(stderr, "\n");
82 fprintf(stderr, "Usage: %s <ctdbd-pid> <output-fd> RECORD <db-path> <db-flags> <db-key>\n", progname);
83 fprintf(stderr, " %s <ctdbd-pid> <output-fd> DB <db-path> <db-flags>\n", progname);
86 static uint8_t *hex_decode_talloc(TALLOC_CTX *mem_ctx,
87 const char *hex_in, size_t *len)
89 unsigned int i;
90 int num;
91 uint8_t *buffer;
93 *len = strlen(hex_in) / 2;
94 buffer = talloc_array(mem_ctx, unsigned char, *len);
96 for (i=0; i<*len; i++) {
97 sscanf(&hex_in[i*2], "%02X", &num);
98 buffer[i] = (uint8_t)num;
101 return buffer;
104 static int lock_record(const char *dbpath, const char *dbflags,
105 const char *dbkey, struct lock_state *state)
107 int tdb_flags;
109 /* No error checking since CTDB always passes sane values */
110 tdb_flags = strtol(dbflags, NULL, 0);
112 /* Convert hex key to key */
113 if (strcmp(dbkey, "NULL") == 0) {
114 state->key.dptr = NULL;
115 state->key.dsize = 0;
116 } else {
117 state->key.dptr = hex_decode_talloc(NULL, dbkey,
118 &state->key.dsize);
121 state->tdb = tdb_open(dbpath, 0, tdb_flags, O_RDWR, 0600);
122 if (state->tdb == NULL) {
123 fprintf(stderr, "locking: Error opening database %s\n", dbpath);
124 return 1;
127 set_priority();
129 if (tdb_chainlock(state->tdb, state->key) < 0) {
130 fprintf(stderr, "locking: Error getting record lock (%s)\n",
131 tdb_errorstr(state->tdb));
132 return 1;
135 reset_priority();
137 return 0;
141 static int lock_db(const char *dbpath, const char *dbflags,
142 struct lock_state *state)
144 int tdb_flags;
146 /* No error checking since CTDB always passes sane values */
147 tdb_flags = strtol(dbflags, NULL, 0);
149 state->tdb = tdb_open(dbpath, 0, tdb_flags, O_RDWR, 0600);
150 if (state->tdb == NULL) {
151 fprintf(stderr, "locking: Error opening database %s\n", dbpath);
152 return 1;
155 set_priority();
157 if (tdb_lockall(state->tdb) < 0) {
158 fprintf(stderr, "locking: Error getting db lock (%s)\n",
159 tdb_errorstr(state->tdb));
160 return 1;
163 reset_priority();
165 return 0;
168 struct wait_for_parent_state {
169 struct tevent_context *ev;
170 pid_t ppid;
173 static void wait_for_parent_check(struct tevent_req *subreq);
175 static struct tevent_req *wait_for_parent_send(TALLOC_CTX *mem_ctx,
176 struct tevent_context *ev,
177 pid_t ppid)
179 struct tevent_req *req, *subreq;
180 struct wait_for_parent_state *state;
182 req = tevent_req_create(mem_ctx, &state, struct wait_for_parent_state);
183 if (req == NULL) {
184 return NULL;
187 state->ev = ev;
188 state->ppid = ppid;
190 if (ppid == 1) {
191 tevent_req_done(req);
192 return tevent_req_post(req, ev);
195 subreq = tevent_wakeup_send(state, ev,
196 tevent_timeval_current_ofs(5,0));
197 if (tevent_req_nomem(subreq, req)) {
198 return tevent_req_post(req, ev);
200 tevent_req_set_callback(subreq, wait_for_parent_check, req);
202 return req;
205 static void wait_for_parent_check(struct tevent_req *subreq)
207 struct tevent_req *req = tevent_req_callback_data(
208 subreq, struct tevent_req);
209 struct wait_for_parent_state *state = tevent_req_data(
210 req, struct wait_for_parent_state);
211 bool status;
213 status = tevent_wakeup_recv(subreq);
214 TALLOC_FREE(subreq);
215 if (! status) {
216 /* Ignore error */
217 fprintf(stderr, "locking: tevent_wakeup_recv() failed\n");
220 if (kill(state->ppid, 0) == -1 && errno == ESRCH) {
221 tevent_req_done(req);
222 return;
225 subreq = tevent_wakeup_send(state, state->ev,
226 tevent_timeval_current_ofs(5,0));
227 if (tevent_req_nomem(subreq, req)) {
228 return;
230 tevent_req_set_callback(subreq, wait_for_parent_check, req);
233 static bool wait_for_parent_recv(struct tevent_req *req, int *perr)
235 if (tevent_req_is_unix_error(req, perr)) {
236 return false;
239 return true;
242 static void cleanup(struct lock_state *state)
244 if (state->tdb != NULL) {
245 if (state->key.dsize == 0) {
246 tdb_unlockall(state->tdb);
247 } else {
248 tdb_chainunlock(state->tdb, state->key);
250 tdb_close(state->tdb);
254 static void signal_handler(struct tevent_context *ev,
255 struct tevent_signal *se,
256 int signum, int count, void *siginfo,
257 void *private_data)
259 struct lock_state *state = (struct lock_state *)private_data;
261 cleanup(state);
262 exit(0);
265 int main(int argc, char *argv[])
267 struct tevent_context *ev;
268 struct tevent_signal *se;
269 struct tevent_req *req;
270 struct lock_state state = { 0 };
271 int write_fd;
272 char result = 0;
273 int ppid;
274 const char *lock_type;
275 bool status;
276 int err;
278 reset_scheduler();
280 if (argc < 4) {
281 usage(argv[0]);
282 exit(1);
285 ppid = atoi(argv[1]);
286 write_fd = atoi(argv[2]);
287 lock_type = argv[3];
289 ev = tevent_context_init(NULL);
290 if (ev == NULL) {
291 fprintf(stderr, "locking: tevent_context_init() failed\n");
292 exit(1);
295 se = tevent_add_signal(ev, ev, SIGTERM, 0,
296 signal_handler, &state);
297 if (se == NULL) {
298 fprintf(stderr, "locking: tevent_add_signal() failed\n");
299 talloc_free(ev);
300 exit(1);
303 if (strcmp(lock_type, "RECORD") == 0) {
304 if (argc != 7) {
305 fprintf(stderr,
306 "locking: Invalid number of arguments (%d)\n",
307 argc);
308 usage(argv[0]);
309 exit(1);
311 result = lock_record(argv[4], argv[5], argv[6], &state);
313 } else if (strcmp(lock_type, "DB") == 0) {
314 if (argc != 6) {
315 fprintf(stderr,
316 "locking: Invalid number of arguments (%d)\n",
317 argc);
318 usage(argv[0]);
319 exit(1);
321 result = lock_db(argv[4], argv[5], &state);
323 } else {
324 fprintf(stderr, "locking: Invalid lock-type '%s'\n", lock_type);
325 usage(argv[0]);
326 exit(1);
329 send_result(write_fd, result);
331 req = wait_for_parent_send(ev, ev, ppid);
332 if (req == NULL) {
333 fprintf(stderr, "locking: wait_for_parent_send() failed\n");
334 cleanup(&state);
335 exit(1);
338 tevent_req_poll(req, ev);
340 status = wait_for_parent_recv(req, &err);
341 if (! status) {
342 fprintf(stderr,
343 "locking: wait_for_parent_recv() failed (%d)\n",
344 err);
347 talloc_free(ev);
348 cleanup(&state);
349 return 0;