smbd: Simplify validate_lock_entries
[Samba.git] / ctdb / server / ctdb_lock_helper.c
blob709130cf61fd0fb8ee9c2a1b5378ef20b30f38f0
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 "includes.h"
21 #include "tdb.h"
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);
30 if (result == 1) {
31 exit(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",
40 progname);
41 fprintf(stderr, " %s <ctdbd-pid> <output-fd> DB <db1-path> [<db2-path> ...]\n",
42 progname);
46 static int lock_record(const char *dbpath, const char *dbkey)
48 TDB_DATA key;
49 struct tdb_context *tdb;
51 /* Convert hex key to key */
52 if (strcmp(dbkey, "NULL") == 0) {
53 key.dptr = NULL;
54 key.dsize = 0;
55 } else {
56 key.dptr = hex_decode_talloc(NULL, dbkey, &key.dsize);
59 tdb = tdb_open(dbpath, 0, TDB_DEFAULT, O_RDWR, 0600);
60 if (tdb == NULL) {
61 fprintf(stderr, "%s: Error opening database %s\n", progname, dbpath);
62 return 1;
65 if (tdb_chainlock(tdb, key) < 0) {
66 fprintf(stderr, "%s: Error getting record lock (%s)\n",
67 progname, tdb_errorstr(tdb));
68 return 1;
71 return 0;
76 static int lock_db(const char *dbpath)
78 struct tdb_context *tdb;
80 tdb = tdb_open(dbpath, 0, TDB_DEFAULT, O_RDWR, 0600);
81 if (tdb == NULL) {
82 fprintf(stderr, "%s: Error opening database %s\n", progname, dbpath);
83 return 1;
86 if (tdb_lockall(tdb) < 0) {
87 fprintf(stderr, "%s: Error getting db lock (%s)\n",
88 progname, tdb_errorstr(tdb));
89 return 1;
92 return 0;
96 int main(int argc, char *argv[])
98 int write_fd;
99 char result = 0;
100 int ppid;
101 const char *lock_type;
103 progname = argv[0];
105 if (argc < 4) {
106 usage();
107 exit(1);
110 reset_scheduler();
112 ppid = atoi(argv[1]);
113 write_fd = atoi(argv[2]);
114 lock_type = argv[3];
116 if (strcmp(lock_type, "RECORD") == 0) {
117 if (argc != 6) {
118 fprintf(stderr, "%s: Invalid number of arguments (%d)\n",
119 progname, argc);
120 usage();
121 exit(1);
123 result = lock_record(argv[4], argv[5]);
125 } else if (strcmp(lock_type, "DB") == 0) {
126 int n;
128 /* If there are no databases specified, no need for lock */
129 if (argc > 4) {
130 for (n=4; n<argc; n++) {
131 result = lock_db(argv[n]);
132 if (result != 0) {
133 break;
138 } else {
139 fprintf(stderr, "%s: Invalid lock-type '%s'\n", progname, lock_type);
140 usage();
141 exit(1);
144 send_result(write_fd, result);
146 while (kill(ppid, 0) == 0 || errno != ESRCH) {
147 sleep(5);
149 return 0;