2161a9a0c446a2ec8abb5e4da4e4ea01bf92d865
[obnox/samba/samba-obnox.git] / ctdb / server / ctdb_lock_helper.c
1 /*
2    ctdb lock helper
3
4    Copyright (C) Amitay Isaacs  2013
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "tdb.h"
22 #include "system/filesys.h"
23 #include "ctdb_private.h"
24
25 static char *progname = NULL;
26
27 static void send_result(int fd, char result)
28 {
29         sys_write(fd, &result, 1);
30         if (result == 1) {
31                 exit(1);
32         }
33 }
34
35
36 static void usage(void)
37 {
38         fprintf(stderr, "\n");
39         fprintf(stderr, "Usage: %s <log-fd> <ctdbd-pid> <output-fd> RECORD <db-path> <db-key>\n",
40                 progname);
41         fprintf(stderr, "       %s <log-fd> <ctdbd-pid> <output-fd> DB <db1-path> [<db2-path> ...]\n",
42                 progname);
43 }
44
45 static uint8_t *hex_decode_talloc(TALLOC_CTX *mem_ctx,
46                                   const char *hex_in, size_t *len)
47 {
48         int i, num;
49         uint8_t *buffer;
50
51         *len = strlen(hex_in) / 2;
52         buffer = talloc_array(mem_ctx, unsigned char, *len);
53
54         for (i=0; i<*len; i++) {
55                 sscanf(&hex_in[i*2], "%02X", &num);
56                 buffer[i] = (uint8_t)num;
57         }
58
59         return buffer;
60 }
61
62 static int lock_record(const char *dbpath, const char *dbkey)
63 {
64         TDB_DATA key;
65         struct tdb_context *tdb;
66
67         /* Convert hex key to key */
68         if (strcmp(dbkey, "NULL") == 0) {
69                 key.dptr = NULL;
70                 key.dsize = 0;
71         } else {
72                 key.dptr = hex_decode_talloc(NULL, dbkey, &key.dsize);
73         }
74
75         tdb = tdb_open(dbpath, 0, TDB_DEFAULT, O_RDWR, 0600);
76         if (tdb == NULL) {
77                 fprintf(stderr, "%s: Error opening database %s\n", progname, dbpath);
78                 return 1;
79         }
80
81         if (tdb_chainlock(tdb, key) < 0) {
82                 fprintf(stderr, "%s: Error getting record lock (%s)\n",
83                         progname, tdb_errorstr(tdb));
84                 return 1;
85         }
86
87         return 0;
88
89 }
90
91
92 static int lock_db(const char *dbpath)
93 {
94         struct tdb_context *tdb;
95
96         tdb = tdb_open(dbpath, 0, TDB_DEFAULT, O_RDWR, 0600);
97         if (tdb == NULL) {
98                 fprintf(stderr, "%s: Error opening database %s\n", progname, dbpath);
99                 return 1;
100         }
101
102         if (tdb_lockall(tdb) < 0) {
103                 fprintf(stderr, "%s: Error getting db lock (%s)\n",
104                         progname, tdb_errorstr(tdb));
105                 return 1;
106         }
107
108         return 0;
109 }
110
111
112 int main(int argc, char *argv[])
113 {
114         int write_fd, log_fd;
115         char result = 0;
116         int ppid;
117         const char *lock_type;
118
119         progname = argv[0];
120
121         if (argc < 5) {
122                 usage();
123                 exit(1);
124         }
125
126         if (!set_scheduler()) {
127                 fprintf(stderr, "%s: Unable to set real-time scheduler priority\n",
128                         progname);
129         }
130
131         log_fd = atoi(argv[1]);
132         close(STDOUT_FILENO);
133         close(STDERR_FILENO);
134         dup2(log_fd, STDOUT_FILENO);
135         dup2(log_fd, STDERR_FILENO);
136         close(log_fd);
137
138         ppid = atoi(argv[2]);
139         write_fd = atoi(argv[3]);
140         lock_type = argv[4];
141
142         if (strcmp(lock_type, "RECORD") == 0) {
143                 if (argc != 7) {
144                         fprintf(stderr, "%s: Invalid number of arguments (%d)\n",
145                                 progname, argc);
146                         usage();
147                         exit(1);
148                 }
149                 result = lock_record(argv[5], argv[6]);
150
151         } else if (strcmp(lock_type, "DB") == 0) {
152                 int n;
153
154                 /* If there are no databases specified, no need for lock */
155                 if (argc > 5) {
156                         for (n=5; n<argc; n++) {
157                                 result = lock_db(argv[n]);
158                                 if (result != 0) {
159                                         break;
160                                 }
161                         }
162                 }
163
164         } else {
165                 fprintf(stderr, "%s: Invalid lock-type '%s'\n", progname, lock_type);
166                 usage();
167                 exit(1);
168         }
169
170         send_result(write_fd, result);
171
172         while (kill(ppid, 0) == 0 || errno != ESRCH) {
173                 sleep(5);
174         }
175         return 0;
176 }