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
)
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
;
104 static int lock_record(const char *dbpath
, const char *dbflags
,
105 const char *dbkey
, struct lock_state
*state
)
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;
117 state
->key
.dptr
= hex_decode_talloc(NULL
, dbkey
,
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
);
129 if (tdb_chainlock(state
->tdb
, state
->key
) < 0) {
130 fprintf(stderr
, "locking: Error getting record lock (%s)\n",
131 tdb_errorstr(state
->tdb
));
141 static int lock_db(const char *dbpath
, const char *dbflags
,
142 struct lock_state
*state
)
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
);
157 if (tdb_lockall(state
->tdb
) < 0) {
158 fprintf(stderr
, "locking: Error getting db lock (%s)\n",
159 tdb_errorstr(state
->tdb
));
168 struct wait_for_parent_state
{
169 struct tevent_context
*ev
;
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
,
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
);
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
);
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
);
213 status
= tevent_wakeup_recv(subreq
);
217 fprintf(stderr
, "locking: tevent_wakeup_recv() failed\n");
220 if (kill(state
->ppid
, 0) == -1 && errno
== ESRCH
) {
221 tevent_req_done(req
);
225 subreq
= tevent_wakeup_send(state
, state
->ev
,
226 tevent_timeval_current_ofs(5,0));
227 if (tevent_req_nomem(subreq
, req
)) {
230 tevent_req_set_callback(subreq
, wait_for_parent_check
, req
);
233 static bool wait_for_parent_recv(struct tevent_req
*req
)
235 if (tevent_req_is_unix_error(req
, NULL
)) {
242 static void cleanup(struct lock_state
*state
)
244 if (state
->tdb
!= NULL
) {
245 if (state
->key
.dsize
== 0) {
246 tdb_unlockall(state
->tdb
);
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
,
259 struct lock_state
*state
= (struct lock_state
*)private_data
;
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 };
274 const char *lock_type
;
284 ppid
= atoi(argv
[1]);
285 write_fd
= atoi(argv
[2]);
288 ev
= tevent_context_init(NULL
);
290 fprintf(stderr
, "locking: tevent_context_init() failed\n");
294 se
= tevent_add_signal(ev
, ev
, SIGTERM
, 0,
295 signal_handler
, &state
);
297 fprintf(stderr
, "locking: tevent_add_signal() failed\n");
302 if (strcmp(lock_type
, "RECORD") == 0) {
305 "locking: Invalid number of arguments (%d)\n",
310 result
= lock_record(argv
[4], argv
[5], argv
[6], &state
);
312 } else if (strcmp(lock_type
, "DB") == 0) {
315 "locking: Invalid number of arguments (%d)\n",
320 result
= lock_db(argv
[4], argv
[5], &state
);
323 fprintf(stderr
, "locking: Invalid lock-type '%s'\n", lock_type
);
328 send_result(write_fd
, result
);
330 req
= wait_for_parent_send(ev
, ev
, ppid
);
332 fprintf(stderr
, "locking: wait_for_parent_send() failed\n");
337 tevent_req_poll(req
, ev
);
339 status
= wait_for_parent_recv(req
);
341 fprintf(stderr
, "locking: wait_for_parent_recv() failed\n");