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/>.
22 #include "system/filesys.h"
23 #include "ctdb_private.h"
25 static char *progname = NULL;
27 static void send_result(int fd, char result)
29 sys_write(fd, &result, 1);
36 static void usage(void)
38 fprintf(stderr, "\n");
39 fprintf(stderr, "Usage: %s <ctdbd-pid> <output-fd> RECORD <db-path> <db-key>\n",
41 fprintf(stderr, " %s <ctdbd-pid> <output-fd> DB <db1-path> [<db2-path> ...]\n",
45 static uint8_t *hex_decode_talloc(TALLOC_CTX *mem_ctx,
46 const char *hex_in, size_t *len)
51 *len = strlen(hex_in) / 2;
52 buffer = talloc_array(mem_ctx, unsigned char, *len);
54 for (i=0; i<*len; i++) {
55 sscanf(&hex_in[i*2], "%02X", &num);
56 buffer[i] = (uint8_t)num;
62 static int lock_record(const char *dbpath, const char *dbkey)
65 struct tdb_context *tdb;
67 /* Convert hex key to key */
68 if (strcmp(dbkey, "NULL") == 0) {
72 key.dptr = hex_decode_talloc(NULL, dbkey, &key.dsize);
75 tdb = tdb_open(dbpath, 0, TDB_DEFAULT, O_RDWR, 0600);
77 fprintf(stderr, "%s: Error opening database %s\n", progname, dbpath);
81 if (tdb_chainlock(tdb, key) < 0) {
82 fprintf(stderr, "%s: Error getting record lock (%s)\n",
83 progname, tdb_errorstr(tdb));
92 static int lock_db(const char *dbpath)
94 struct tdb_context *tdb;
96 tdb = tdb_open(dbpath, 0, TDB_DEFAULT, O_RDWR, 0600);
98 fprintf(stderr, "%s: Error opening database %s\n", progname, dbpath);
102 if (tdb_lockall(tdb) < 0) {
103 fprintf(stderr, "%s: Error getting db lock (%s)\n",
104 progname, tdb_errorstr(tdb));
112 int main(int argc, char *argv[])
117 const char *lock_type;
128 ppid = atoi(argv[1]);
129 write_fd = atoi(argv[2]);
132 if (strcmp(lock_type, "RECORD") == 0) {
134 fprintf(stderr, "%s: Invalid number of arguments (%d)\n",
139 result = lock_record(argv[4], argv[5]);
141 } else if (strcmp(lock_type, "DB") == 0) {
144 /* If there are no databases specified, no need for lock */
146 for (n=4; n<argc; n++) {
147 result = lock_db(argv[n]);
155 fprintf(stderr, "%s: Invalid lock-type '%s'\n", progname, lock_type);
160 send_result(write_fd, result);
162 while (kill(ppid, 0) == 0 || errno != ESRCH) {