s3: rpc_server/srvsvc: Ensure we don't continually realloc inside init_srv_sess_info_1().
[Samba/wip.git] / ctdb / server / ctdb_lock_helper.c
blobd8a1d249b75c7d439fd35b88aa15584e9df1bf94
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 "../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 ppid = atoi(argv[1]);
111 write_fd = atoi(argv[2]);
112 lock_type = argv[3];
114 if (strcmp(lock_type, "RECORD") == 0) {
115 if (argc != 6) {
116 fprintf(stderr, "%s: Invalid number of arguments (%d)\n",
117 progname, argc);
118 usage();
119 exit(1);
121 result = lock_record(argv[4], argv[5]);
123 } else if (strcmp(lock_type, "DB") == 0) {
124 int n;
126 /* If there are no databases specified, no need for lock */
127 if (argc > 4) {
128 for (n=4; n<argc; n++) {
129 result = lock_db(argv[n]);
130 if (result != 0) {
131 break;
136 } else {
137 fprintf(stderr, "%s: Invalid lock-type '%s'\n", progname, lock_type);
138 usage();
139 exit(1);
142 send_result(write_fd, result);
144 while (kill(ppid, 0) == 0 || errno != ESRCH) {
145 sleep(5);
147 return 0;