libctdb: use bool in API
[rusty/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                         DEBUG(ctdb, LOG_ERR, "ctdb_synchronous: poll failed");
46                         return NULL;
47                 }
48                 if (ctdb_service(ctdb, fds.revents) < 0) {
49                         ctdb_request_free(ctdb, req);
50                         return NULL;
51                 }
52         }
53         return req;
54 }
55
56 static void set(struct ctdb_connection *ctdb,
57                 struct ctdb_request *req, bool *done)
58 {
59         *done = true;
60 }
61
62 bool ctdb_getrecmaster(struct ctdb_connection *ctdb,
63                        uint32_t destnode, uint32_t *recmaster)
64 {
65         struct ctdb_request *req;
66         bool done = false;
67         bool ret = false;
68
69         req = synchronous(ctdb,
70                           ctdb_getrecmaster_send(ctdb, destnode, set, &done),
71                           &done);
72         if (req != NULL) {
73                 ret = ctdb_getrecmaster_recv(ctdb, req, recmaster);
74                 ctdb_request_free(ctdb, req);
75         }
76         return ret;
77 }
78
79 struct ctdb_db *ctdb_attachdb(struct ctdb_connection *ctdb,
80                               const char *name, int persistent,
81                               uint32_t tdb_flags)
82 {
83         struct ctdb_request *req;
84         bool done = false;
85         struct ctdb_db *ret = NULL;
86
87         req = synchronous(ctdb,
88                           ctdb_attachdb_send(ctdb, name, persistent, tdb_flags,
89                                              set, &done),
90                           &done);
91         if (req != NULL) {
92                 ret = ctdb_attachdb_recv(ctdb, req);
93                 ctdb_request_free(ctdb, req);
94         }
95         return ret;
96 }
97
98 bool ctdb_getpnn(struct ctdb_connection *ctdb,
99                  uint32_t destnode, uint32_t *pnn)
100 {
101         struct ctdb_request *req;
102         bool done = false;
103         bool ret = false;
104
105         req = synchronous(ctdb,
106                           ctdb_getpnn_send(ctdb, destnode, set, &done),
107                           &done);
108         if (req != NULL) {
109                 ret = ctdb_getpnn_recv(ctdb, req, pnn);
110                 ctdb_request_free(ctdb, req);
111         }
112         return ret;
113 }