later times are a lower priority, not a higher priority
[sahlberg/ctdb.git] / server / ctdb_lockwait.c
1 /* 
2    wait for a tdb chain lock
3
4    Copyright (C) Andrew Tridgell  2006
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
24 #include "system/wait.h"
25 #include "db_wrap.h"
26 #include "lib/tdb/include/tdb.h"
27 #include "../include/ctdb_private.h"
28
29
30 struct lockwait_handle {
31         struct ctdb_context *ctdb;
32         struct ctdb_db_context *ctdb_db;
33         struct fd_event *fde;
34         int fd[2];
35         pid_t child;
36         void *private_data;
37         void (*callback)(void *);
38         TDB_DATA key;
39         struct timeval start_time;
40 };
41
42 static void lockwait_handler(struct event_context *ev, struct fd_event *fde, 
43                              uint16_t flags, void *private_data)
44 {
45         struct lockwait_handle *h = talloc_get_type(private_data, 
46                                                      struct lockwait_handle);
47         void (*callback)(void *) = h->callback;
48         void *p = h->private_data;
49         pid_t child = h->child;
50         TDB_DATA key = h->key;
51         struct tdb_context *tdb = h->ctdb_db->ltdb->tdb;
52         TALLOC_CTX *tmp_ctx = talloc_new(ev);
53
54         key.dptr = talloc_memdup(tmp_ctx, key.dptr, key.dsize);
55
56         talloc_set_destructor(h, NULL);
57         ctdb_latency(&h->ctdb->statistics.max_lockwait_latency, h->start_time);
58         h->ctdb->statistics.pending_lockwait_calls--;
59
60         /* the handle needs to go away when the context is gone - when
61            the handle goes away this implicitly closes the pipe, which
62            kills the child holding the lock */
63         talloc_steal(tmp_ctx, h);
64
65         if (h->ctdb->flags & CTDB_FLAG_TORTURE) {
66                 if (tdb_chainlock_nonblock(tdb, key) == 0) {
67                         ctdb_fatal(h->ctdb, "got chain lock while lockwait child active");
68                 }
69         }
70
71         tdb_chainlock_mark(tdb, key);
72         callback(p);
73         tdb_chainlock_unmark(tdb, key);
74
75         kill(child, SIGKILL);
76         waitpid(child, NULL, 0);
77         talloc_free(tmp_ctx);
78 }
79
80 static int lockwait_destructor(struct lockwait_handle *h)
81 {
82         h->ctdb->statistics.pending_lockwait_calls--;
83         kill(h->child, SIGKILL);
84         waitpid(h->child, NULL, 0);
85         return 0;
86 }
87
88 /*
89   setup a non-blocking chainlock on a tdb record. If this function
90   returns NULL then it could not get the chainlock. Otherwise it
91   returns a opaque handle, and will call callback() once it has
92   managed to get the chainlock. You can cancel it by using talloc_free
93   on the returned handle.
94
95   It is the callers responsibility to unlock the chainlock once
96   acquired
97  */
98 struct lockwait_handle *ctdb_lockwait(struct ctdb_db_context *ctdb_db,
99                                       TDB_DATA key,
100                                       void (*callback)(void *private_data),
101                                       void *private_data)
102 {
103         struct lockwait_handle *result;
104         int ret;
105         pid_t parent = getpid();
106
107         ctdb_db->ctdb->statistics.lockwait_calls++;
108         ctdb_db->ctdb->statistics.pending_lockwait_calls++;
109
110         if (!(result = talloc_zero(private_data, struct lockwait_handle))) {
111                 ctdb_db->ctdb->statistics.pending_lockwait_calls--;
112                 return NULL;
113         }
114
115         ret = pipe(result->fd);
116
117         if (ret != 0) {
118                 talloc_free(result);
119                 ctdb_db->ctdb->statistics.pending_lockwait_calls--;
120                 return NULL;
121         }
122
123         result->child = fork();
124
125         if (result->child == (pid_t)-1) {
126                 close(result->fd[0]);
127                 close(result->fd[1]);
128                 talloc_free(result);
129                 ctdb_db->ctdb->statistics.pending_lockwait_calls--;
130                 return NULL;
131         }
132
133         result->callback = callback;
134         result->private_data = private_data;
135         result->ctdb = ctdb_db->ctdb;
136         result->ctdb_db = ctdb_db;
137         result->key = key;
138
139         if (result->child == 0) {
140                 char c = 0;
141                 close(result->fd[0]);
142                 tdb_chainlock(ctdb_db->ltdb->tdb, key);
143                 write(result->fd[1], &c, 1);
144                 /* make sure we die when our parent dies */
145                 while (kill(parent, 0) == 0 || errno != ESRCH) {
146                         sleep(5);
147                 }
148                 _exit(0);
149         }
150
151         close(result->fd[1]);
152         talloc_set_destructor(result, lockwait_destructor);
153
154         result->fde = event_add_fd(ctdb_db->ctdb->ev, result, result->fd[0],
155                                    EVENT_FD_READ|EVENT_FD_AUTOCLOSE, lockwait_handler,
156                                    (void *)result);
157         if (result->fde == NULL) {
158                 talloc_free(result);
159                 ctdb_db->ctdb->statistics.pending_lockwait_calls--;
160                 return NULL;
161         }
162
163         result->start_time = timeval_current();
164
165         return result;
166 }