s3:smbd: remove deprecated handling of "profile acls = yes"
[Samba.git] / ctdb / server / ctdb_lock_helper.c
blobf918b73dbd37db532d9b23d64d8645d061ab568b
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 int i, num;
90 uint8_t *buffer;
92 *len = strlen(hex_in) / 2;
93 buffer = talloc_array(mem_ctx, unsigned char, *len);
95 for (i=0; i<*len; i++) {
96 sscanf(&hex_in[i*2], "%02X", &num);
97 buffer[i] = (uint8_t)num;
100 return buffer;
103 static int lock_record(const char *dbpath, const char *dbflags,
104 const char *dbkey, struct lock_state *state)
106 int tdb_flags;
108 /* No error checking since CTDB always passes sane values */
109 tdb_flags = strtol(dbflags, NULL, 0);
111 /* Convert hex key to key */
112 if (strcmp(dbkey, "NULL") == 0) {
113 state->key.dptr = NULL;
114 state->key.dsize = 0;
115 } else {
116 state->key.dptr = hex_decode_talloc(NULL, dbkey,
117 &state->key.dsize);
120 state->tdb = tdb_open(dbpath, 0, tdb_flags, O_RDWR, 0600);
121 if (state->tdb == NULL) {
122 fprintf(stderr, "locking: Error opening database %s\n", dbpath);
123 return 1;
126 set_priority();
128 if (tdb_chainlock(state->tdb, state->key) < 0) {
129 fprintf(stderr, "locking: Error getting record lock (%s)\n",
130 tdb_errorstr(state->tdb));
131 return 1;
134 reset_priority();
136 return 0;
140 static int lock_db(const char *dbpath, const char *dbflags,
141 struct lock_state *state)
143 int tdb_flags;
145 /* No error checking since CTDB always passes sane values */
146 tdb_flags = strtol(dbflags, NULL, 0);
148 state->tdb = tdb_open(dbpath, 0, tdb_flags, O_RDWR, 0600);
149 if (state->tdb == NULL) {
150 fprintf(stderr, "locking: Error opening database %s\n", dbpath);
151 return 1;
154 set_priority();
156 if (tdb_lockall(state->tdb) < 0) {
157 fprintf(stderr, "locking: Error getting db lock (%s)\n",
158 tdb_errorstr(state->tdb));
159 return 1;
162 reset_priority();
164 return 0;
167 struct wait_for_parent_state {
168 struct tevent_context *ev;
169 pid_t ppid;
172 static void wait_for_parent_check(struct tevent_req *subreq);
174 static struct tevent_req *wait_for_parent_send(TALLOC_CTX *mem_ctx,
175 struct tevent_context *ev,
176 pid_t ppid)
178 struct tevent_req *req, *subreq;
179 struct wait_for_parent_state *state;
181 req = tevent_req_create(mem_ctx, &state, struct wait_for_parent_state);
182 if (req == NULL) {
183 return NULL;
186 state->ev = ev;
187 state->ppid = ppid;
189 if (ppid == 1) {
190 tevent_req_done(req);
191 return tevent_req_post(req, ev);
194 subreq = tevent_wakeup_send(state, ev,
195 tevent_timeval_current_ofs(5,0));
196 if (tevent_req_nomem(subreq, req)) {
197 return tevent_req_post(req, ev);
199 tevent_req_set_callback(subreq, wait_for_parent_check, req);
201 return req;
204 static void wait_for_parent_check(struct tevent_req *subreq)
206 struct tevent_req *req = tevent_req_callback_data(
207 subreq, struct tevent_req);
208 struct wait_for_parent_state *state = tevent_req_data(
209 req, struct wait_for_parent_state);
210 bool status;
212 status = tevent_wakeup_recv(subreq);
213 TALLOC_FREE(subreq);
214 if (! status) {
215 /* Ignore error */
216 fprintf(stderr, "locking: tevent_wakeup_recv() failed\n");
219 if (kill(state->ppid, 0) == -1 && errno == ESRCH) {
220 tevent_req_done(req);
221 return;
224 subreq = tevent_wakeup_send(state, state->ev,
225 tevent_timeval_current_ofs(5,0));
226 if (tevent_req_nomem(subreq, req)) {
227 return;
229 tevent_req_set_callback(subreq, wait_for_parent_check, req);
232 static bool wait_for_parent_recv(struct tevent_req *req)
234 if (tevent_req_is_unix_error(req, NULL)) {
235 return false;
238 return true;
241 static void cleanup(struct lock_state *state)
243 if (state->tdb != NULL) {
244 if (state->key.dsize == 0) {
245 tdb_unlockall(state->tdb);
246 } else {
247 tdb_chainunlock(state->tdb, state->key);
249 tdb_close(state->tdb);
253 static void signal_handler(struct tevent_context *ev,
254 struct tevent_signal *se,
255 int signum, int count, void *siginfo,
256 void *private_data)
258 struct lock_state *state = (struct lock_state *)private_data;
260 cleanup(state);
261 exit(0);
264 int main(int argc, char *argv[])
266 struct tevent_context *ev;
267 struct tevent_signal *se;
268 struct tevent_req *req;
269 struct lock_state state = { 0 };
270 int write_fd;
271 char result = 0;
272 int ppid;
273 const char *lock_type;
274 bool status;
276 reset_scheduler();
278 if (argc < 4) {
279 usage(argv[0]);
280 exit(1);
283 ppid = atoi(argv[1]);
284 write_fd = atoi(argv[2]);
285 lock_type = argv[3];
287 ev = tevent_context_init(NULL);
288 if (ev == NULL) {
289 fprintf(stderr, "locking: tevent_context_init() failed\n");
290 exit(1);
293 se = tevent_add_signal(ev, ev, SIGTERM, 0,
294 signal_handler, &state);
295 if (se == NULL) {
296 fprintf(stderr, "locking: tevent_add_signal() failed\n");
297 talloc_free(ev);
298 exit(1);
301 if (strcmp(lock_type, "RECORD") == 0) {
302 if (argc != 7) {
303 fprintf(stderr,
304 "locking: Invalid number of arguments (%d)\n",
305 argc);
306 usage(argv[0]);
307 exit(1);
309 result = lock_record(argv[4], argv[5], argv[6], &state);
311 } else if (strcmp(lock_type, "DB") == 0) {
312 if (argc != 6) {
313 fprintf(stderr,
314 "locking: Invalid number of arguments (%d)\n",
315 argc);
316 usage(argv[0]);
317 exit(1);
319 result = lock_db(argv[4], argv[5], &state);
321 } else {
322 fprintf(stderr, "locking: Invalid lock-type '%s'\n", lock_type);
323 usage(argv[0]);
324 exit(1);
327 send_result(write_fd, result);
329 req = wait_for_parent_send(ev, ev, ppid);
330 if (req == NULL) {
331 fprintf(stderr, "locking: wait_for_parent_send() failed\n");
332 cleanup(&state);
333 exit(1);
336 tevent_req_poll(req, ev);
338 status = wait_for_parent_recv(req);
339 if (! status) {
340 fprintf(stderr, "locking: wait_for_parent_recv() failed\n");
343 talloc_free(ev);
344 cleanup(&state);
345 return 0;