ctdb-locking: Remove unnecessary global variable
[samba.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 "replace.h"
21 #include "system/filesys.h"
22 #include "system/network.h"
23 #include "system/wait.h"
24
25 #include <talloc.h>
26 #include <tevent.h>
27 #include <tdb.h>
28
29 #include "lib/util/sys_rw.h"
30 #include "lib/util/tevent_unix.h"
31
32 #include "protocol/protocol.h"
33
34 #include "common/system.h"
35
36 static bool realtime = true;
37
38 struct lock_state {
39         struct tdb_context *tdb;
40         TDB_DATA key;
41 };
42
43 static void set_priority(void)
44 {
45         const char *ptr;
46
47         ptr = getenv("CTDB_NOSETSCHED");
48         if (ptr != NULL) {
49                 realtime = false;
50         }
51
52         if (! realtime) {
53                 return;
54         }
55
56         realtime = set_scheduler();
57         if (! realtime) {
58                 fprintf(stderr,
59                         "locking: Unable to set real-time scheduler priority\n");
60         }
61 }
62
63 static void reset_priority(void)
64 {
65         if (realtime) {
66                 reset_scheduler();
67         }
68 }
69
70 static void send_result(int fd, char result)
71 {
72         sys_write(fd, &result, 1);
73         if (result == 1) {
74                 exit(1);
75         }
76 }
77
78
79 static void usage(const char *progname)
80 {
81         fprintf(stderr, "\n");
82         fprintf(stderr, "Usage: %s <ctdbd-pid> <output-fd> RECORD <db-path> <db-flags> <db-key>\n", progname);
83         fprintf(stderr, "       %s <ctdbd-pid> <output-fd> DB <db-path> <db-flags>\n", progname);
84 }
85
86 static uint8_t *hex_decode_talloc(TALLOC_CTX *mem_ctx,
87                                   const char *hex_in, size_t *len)
88 {
89         int i, num;
90         uint8_t *buffer;
91
92         *len = strlen(hex_in) / 2;
93         buffer = talloc_array(mem_ctx, unsigned char, *len);
94
95         for (i=0; i<*len; i++) {
96                 sscanf(&hex_in[i*2], "%02X", &num);
97                 buffer[i] = (uint8_t)num;
98         }
99
100         return buffer;
101 }
102
103 static int lock_record(const char *dbpath, const char *dbflags,
104                        const char *dbkey, struct lock_state *state)
105 {
106         int tdb_flags;
107
108         /* No error checking since CTDB always passes sane values */
109         tdb_flags = strtol(dbflags, NULL, 0);
110
111         /* Convert hex key to key */
112         if (strcmp(dbkey, "NULL") == 0) {
113                 state->key.dptr = NULL;
114                 state->key.dsize = 0;
115         } else {
116                 state->key.dptr = hex_decode_talloc(NULL, dbkey,
117                                                     &state->key.dsize);
118         }
119
120         state->tdb = tdb_open(dbpath, 0, tdb_flags, O_RDWR, 0600);
121         if (state->tdb == NULL) {
122                 fprintf(stderr, "locking: Error opening database %s\n", dbpath);
123                 return 1;
124         }
125
126         set_priority();
127
128         if (tdb_chainlock(state->tdb, state->key) < 0) {
129                 fprintf(stderr, "locking: Error getting record lock (%s)\n",
130                         tdb_errorstr(state->tdb));
131                 return 1;
132         }
133
134         reset_priority();
135
136         return 0;
137
138 }
139
140 static int lock_db(const char *dbpath, const char *dbflags,
141                    struct lock_state *state)
142 {
143         int tdb_flags;
144
145         /* No error checking since CTDB always passes sane values */
146         tdb_flags = strtol(dbflags, NULL, 0);
147
148         state->tdb = tdb_open(dbpath, 0, tdb_flags, O_RDWR, 0600);
149         if (state->tdb == NULL) {
150                 fprintf(stderr, "locking: Error opening database %s\n", dbpath);
151                 return 1;
152         }
153
154         set_priority();
155
156         if (tdb_lockall(state->tdb) < 0) {
157                 fprintf(stderr, "locking: Error getting db lock (%s)\n",
158                         tdb_errorstr(state->tdb));
159                 return 1;
160         }
161
162         reset_priority();
163
164         return 0;
165 }
166
167 struct wait_for_parent_state {
168         struct tevent_context *ev;
169         pid_t ppid;
170 };
171
172 static void wait_for_parent_check(struct tevent_req *subreq);
173
174 static struct tevent_req *wait_for_parent_send(TALLOC_CTX *mem_ctx,
175                                                struct tevent_context *ev,
176                                                pid_t ppid)
177 {
178         struct tevent_req *req, *subreq;
179         struct wait_for_parent_state *state;
180
181         req = tevent_req_create(mem_ctx, &state, struct wait_for_parent_state);
182         if (req == NULL) {
183                 return NULL;
184         }
185
186         state->ev = ev;
187         state->ppid = ppid;
188
189         if (ppid == 1) {
190                 tevent_req_done(req);
191                 return tevent_req_post(req, ev);
192         }
193
194         subreq = tevent_wakeup_send(state, ev,
195                                     tevent_timeval_current_ofs(5,0));
196         if (tevent_req_nomem(subreq, req)) {
197                 return tevent_req_post(req, ev);
198         }
199         tevent_req_set_callback(subreq, wait_for_parent_check, req);
200
201         return req;
202 }
203
204 static void wait_for_parent_check(struct tevent_req *subreq)
205 {
206         struct tevent_req *req = tevent_req_callback_data(
207                 subreq, struct tevent_req);
208         struct wait_for_parent_state *state = tevent_req_data(
209                 req, struct wait_for_parent_state);
210         bool status;
211
212         status = tevent_wakeup_recv(subreq);
213         TALLOC_FREE(subreq);
214         if (! status) {
215                 /* Ignore error */
216                 fprintf(stderr, "locking: tevent_wakeup_recv() failed\n");
217         }
218
219         if (kill(state->ppid, 0) == -1 && errno == ESRCH) {
220                 tevent_req_done(req);
221                 return;
222         }
223
224         subreq = tevent_wakeup_send(state, state->ev,
225                                     tevent_timeval_current_ofs(5,0));
226         if (tevent_req_nomem(subreq, req)) {
227                 return;
228         }
229         tevent_req_set_callback(subreq, wait_for_parent_check, req);
230 }
231
232 static bool wait_for_parent_recv(struct tevent_req *req)
233 {
234         if (tevent_req_is_unix_error(req, NULL)) {
235                 return false;
236         }
237
238         return true;
239 }
240
241 static void cleanup(struct lock_state *state)
242 {
243         if (state->tdb != NULL) {
244                 if (state->key.dsize == 0) {
245                         tdb_unlockall(state->tdb);
246                 } else {
247                         tdb_chainunlock(state->tdb, state->key);
248                 }
249                 tdb_close(state->tdb);
250         }
251 }
252
253 static void signal_handler(struct tevent_context *ev,
254                            struct tevent_signal *se,
255                            int signum, int count, void *siginfo,
256                            void *private_data)
257 {
258         struct lock_state *state = (struct lock_state *)private_data;
259
260         cleanup(state);
261         exit(0);
262 }
263
264 int main(int argc, char *argv[])
265 {
266         struct tevent_context *ev;
267         struct tevent_signal *se;
268         struct tevent_req *req;
269         struct lock_state state = { 0 };
270         int write_fd;
271         char result = 0;
272         int ppid;
273         const char *lock_type;
274         bool status;
275
276         reset_scheduler();
277
278         if (argc < 4) {
279                 usage(argv[0]);
280                 exit(1);
281         }
282
283         ppid = atoi(argv[1]);
284         write_fd = atoi(argv[2]);
285         lock_type = argv[3];
286
287         ev = tevent_context_init(NULL);
288         if (ev == NULL) {
289                 fprintf(stderr, "locking: tevent_context_init() failed\n");
290                 exit(1);
291         }
292
293         se = tevent_add_signal(ev, ev, SIGTERM, 0,
294                                signal_handler, &state);
295         if (se == NULL) {
296                 fprintf(stderr, "locking: tevent_add_signal() failed\n");
297                 talloc_free(ev);
298                 exit(1);
299         }
300
301         if (strcmp(lock_type, "RECORD") == 0) {
302                 if (argc != 7) {
303                         fprintf(stderr,
304                                 "locking: Invalid number of arguments (%d)\n",
305                                 argc);
306                         usage(argv[0]);
307                         exit(1);
308                 }
309                 result = lock_record(argv[4], argv[5], argv[6], &state);
310
311         } else if (strcmp(lock_type, "DB") == 0) {
312                 if (argc != 6) {
313                         fprintf(stderr,
314                                 "locking: Invalid number of arguments (%d)\n",
315                                 argc);
316                         usage(argv[0]);
317                         exit(1);
318                 }
319                 result = lock_db(argv[4], argv[5], &state);
320
321         } else {
322                 fprintf(stderr, "locking: Invalid lock-type '%s'\n", lock_type);
323                 usage(argv[0]);
324                 exit(1);
325         }
326
327         send_result(write_fd, result);
328
329         req = wait_for_parent_send(ev, ev, ppid);
330         if (req == NULL) {
331                 fprintf(stderr, "locking: wait_for_parent_send() failed\n");
332                 cleanup(&state);
333                 exit(1);
334         }
335
336         tevent_req_poll(req, ev);
337
338         status = wait_for_parent_recv(req);
339         if (! status) {
340                 fprintf(stderr, "locking: wait_for_parent_recv() failed\n");
341         }
342
343         talloc_free(ev);
344         cleanup(&state);
345         return 0;
346 }