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 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",
46 static int lock_record(const char *dbpath
, const char *dbkey
)
49 struct tdb_context
*tdb
;
51 /* Convert hex key to key */
52 if (strcmp(dbkey
, "NULL") == 0) {
56 key
.dptr
= hex_decode_talloc(NULL
, dbkey
, &key
.dsize
);
59 tdb
= tdb_open(dbpath
, 0, TDB_DEFAULT
, O_RDWR
, 0600);
61 fprintf(stderr
, "%s: Error opening database %s\n", progname
, dbpath
);
65 if (tdb_chainlock(tdb
, key
) < 0) {
66 fprintf(stderr
, "%s: Error getting record lock (%s)\n",
67 progname
, tdb_errorstr(tdb
));
76 static int lock_db(const char *dbpath
)
78 struct tdb_context
*tdb
;
80 tdb
= tdb_open(dbpath
, 0, TDB_DEFAULT
, O_RDWR
, 0600);
82 fprintf(stderr
, "%s: Error opening database %s\n", progname
, dbpath
);
86 if (tdb_lockall(tdb
) < 0) {
87 fprintf(stderr
, "%s: Error getting db lock (%s)\n",
88 progname
, tdb_errorstr(tdb
));
96 int main(int argc
, char *argv
[])
101 const char *lock_type
;
112 ppid
= atoi(argv
[1]);
113 write_fd
= atoi(argv
[2]);
116 if (strcmp(lock_type
, "RECORD") == 0) {
118 fprintf(stderr
, "%s: Invalid number of arguments (%d)\n",
123 result
= lock_record(argv
[4], argv
[5]);
125 } else if (strcmp(lock_type
, "DB") == 0) {
128 /* If there are no databases specified, no need for lock */
130 for (n
=4; n
<argc
; n
++) {
131 result
= lock_db(argv
[n
]);
139 fprintf(stderr
, "%s: Invalid lock-type '%s'\n", progname
, lock_type
);
144 send_result(write_fd
, result
);
146 while (kill(ppid
, 0) == 0 || errno
!= ESRCH
) {