libctdb: add ctdb arg to more functions.
[sahlberg/ctdb.git] / libctdb / sync.c
1 /*
2    synchronous wrappers for libctdb
3
4    Copyright (C) Rusty Russell 2010
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 #include <ctdb.h>
20 #include <stdbool.h>
21 #include <poll.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include "libctdb_private.h"
25
26 /* On failure, frees req and returns NULL. */
27 static struct ctdb_request *synchronous(struct ctdb_connection *ctdb,
28                                         struct ctdb_request *req,
29                                         bool *done)
30 {
31         struct pollfd fds;
32
33         /* Pass through allocation failures. */
34         if (!req)
35                 return NULL;
36
37         fds.fd = ctdb_get_fd(ctdb);
38         while (!*done) {
39                 fds.events = ctdb_which_events(ctdb);
40                 if (poll(&fds, 1, -1) < 0) {
41                         /* Signalled is OK, other error is bad. */
42                         if (errno == EINTR)
43                                 continue;
44                         ctdb_request_free(ctdb, req);
45                         return NULL;
46                 }
47                 if (ctdb_service(ctdb, fds.revents) < 0) {
48                         ctdb_request_free(ctdb, req);
49                         return NULL;
50                 }
51         }
52         return req;
53 }
54
55 static void set(struct ctdb_connection *ctdb,
56                 struct ctdb_request *req, bool *done)
57 {
58         *done = true;
59 }
60
61 int ctdb_getrecmaster(struct ctdb_connection *ctdb,
62                       uint32_t destnode, uint32_t *recmaster)
63 {
64         struct ctdb_request *req;
65         bool done = false;
66         int ret = -1;
67
68         req = synchronous(ctdb,
69                           ctdb_getrecmaster_send(ctdb, destnode, set, &done),
70                           &done);
71         if (req != NULL) {
72                 ret = ctdb_getrecmaster_recv(ctdb, req, recmaster);
73                 ctdb_request_free(ctdb, req);
74         }
75         return ret;
76 }
77
78 struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb,
79                               const char *name, int persistent,
80                               uint32_t tdb_flags)
81 {
82         struct ctdb_request *req;
83         bool done = false;
84         struct ctdb_db *ret = NULL;
85
86         req = synchronous(ctdb,
87                           ctdb_attachdb_send(ctdb, name, persistent, tdb_flags,
88                                              set, &done),
89                           &done);
90         if (req != NULL) {
91                 ret = ctdb_attachdb_recv(ctdb, req);
92                 ctdb_request_free(ctdb, req);
93         }
94         return ret;
95 }
96
97 int ctdb_getpnn(struct ctdb_connection *ctdb,
98                 uint32_t destnode, uint32_t *pnn)
99 {
100         struct ctdb_request *req;
101         bool done = false;
102         int ret = -1;
103
104         req = synchronous(ctdb,
105                           ctdb_getpnn_send(ctdb, destnode, set, &done),
106                           &done);
107         if (req != NULL) {
108                 ret = ctdb_getpnn_recv(ctdb, req, pnn);
109                 ctdb_request_free(ctdb, req);
110         }
111         return ret;
112 }