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/>.
21 #include "system/filesys.h"
22 #include "system/network.h"
23 #include "system/wait.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;
39 struct tdb_context
*tdb
;
43 static void set_priority(void)
47 ptr
= getenv("CTDB_NOSETSCHED");
56 realtime
= set_scheduler();
59 "locking: Unable to set real-time scheduler priority\n");
63 static void reset_priority(void)
70 static void send_result(int fd
, char result
)
72 sys_write(fd
, &result
, 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
)
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
;
103 static int lock_record(const char *dbpath
, const char *dbflags
,
104 const char *dbkey
, struct lock_state
*state
)
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;
116 state
->key
.dptr
= hex_decode_talloc(NULL
, dbkey
,
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
);
128 if (tdb_chainlock(state
->tdb
, state
->key
) < 0) {
129 fprintf(stderr
, "locking: Error getting record lock (%s)\n",
130 tdb_errorstr(state
->tdb
));
140 static int lock_db(const char *dbpath
, const char *dbflags
,
141 struct lock_state
*state
)
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
);
156 if (tdb_lockall(state
->tdb
) < 0) {
157 fprintf(stderr
, "locking: Error getting db lock (%s)\n",
158 tdb_errorstr(state
->tdb
));
167 struct wait_for_parent_state
{
168 struct tevent_context
*ev
;
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
,
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
);
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
);
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
);
212 status
= tevent_wakeup_recv(subreq
);
216 fprintf(stderr
, "locking: tevent_wakeup_recv() failed\n");
219 if (kill(state
->ppid
, 0) == -1 && errno
== ESRCH
) {
220 tevent_req_done(req
);
224 subreq
= tevent_wakeup_send(state
, state
->ev
,
225 tevent_timeval_current_ofs(5,0));
226 if (tevent_req_nomem(subreq
, req
)) {
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
)) {
241 static void cleanup(struct lock_state
*state
)
243 if (state
->tdb
!= NULL
) {
244 if (state
->key
.dsize
== 0) {
245 tdb_unlockall(state
->tdb
);
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
,
258 struct lock_state
*state
= (struct lock_state
*)private_data
;
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 };
273 const char *lock_type
;
283 ppid
= atoi(argv
[1]);
284 write_fd
= atoi(argv
[2]);
287 ev
= tevent_context_init(NULL
);
289 fprintf(stderr
, "locking: tevent_context_init() failed\n");
293 se
= tevent_add_signal(ev
, ev
, SIGTERM
, 0,
294 signal_handler
, &state
);
296 fprintf(stderr
, "locking: tevent_add_signal() failed\n");
301 if (strcmp(lock_type
, "RECORD") == 0) {
304 "locking: Invalid number of arguments (%d)\n",
309 result
= lock_record(argv
[4], argv
[5], argv
[6], &state
);
311 } else if (strcmp(lock_type
, "DB") == 0) {
314 "locking: Invalid number of arguments (%d)\n",
319 result
= lock_db(argv
[4], argv
[5], &state
);
322 fprintf(stderr
, "locking: Invalid lock-type '%s'\n", lock_type
);
327 send_result(write_fd
, result
);
329 req
= wait_for_parent_send(ev
, ev
, ppid
);
331 fprintf(stderr
, "locking: wait_for_parent_send() failed\n");
336 tevent_req_poll(req
, ev
);
338 status
= wait_for_parent_recv(req
);
340 fprintf(stderr
, "locking: wait_for_parent_recv() failed\n");