s4:libcli: split out smb_raw_negotiate_fill_transport()
[Samba.git] / source3 / lib / messages_ctdb_ref.c
blob47b4b758dac54c80029b12c040050780052ff758
1 /*
2 * Unix SMB/CIFS implementation.
3 * Samba internal messaging functions
4 * Copyright (C) 2017 by Volker Lendecke
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.
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.
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/>.
20 #include "replace.h"
21 #include <talloc.h>
22 #include "messages_ctdb.h"
23 #include "messages_ctdb_ref.h"
24 #include "lib/util/debug.h"
25 #include "lib/util/dlinklist.h"
27 struct msg_ctdb_ref {
28 struct msg_ctdb_ref *prev, *next;
29 struct messaging_ctdb_fde *fde;
30 void (*recv_cb)(struct tevent_context *ev,
31 const uint8_t *msg, size_t msg_len,
32 int *fds, size_t num_fds, void *private_data);
33 void *recv_cb_private_data;
36 static pid_t ctdb_pid = 0;
37 static struct msg_ctdb_ref *refs = NULL;
39 static int msg_ctdb_ref_destructor(struct msg_ctdb_ref *r);
40 static void msg_ctdb_ref_recv(struct tevent_context *ev,
41 const uint8_t *msg, size_t msg_len,
42 int *fds, size_t num_fds, void *private_data);
44 void *messaging_ctdb_ref(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
45 const char *sockname, int timeout, uint64_t unique_id,
46 void (*recv_cb)(struct tevent_context *ev,
47 const uint8_t *msg, size_t msg_len,
48 int *fds, size_t num_fds,
49 void *private_data),
50 void *recv_cb_private_data,
51 int *err)
53 struct msg_ctdb_ref *result, *tmp_refs;
55 if (tevent_context_is_wrapper(ev)) {
57 * This is really a programmer error!
59 * The main/raw tevent context should
60 * have been registered first!
62 DBG_ERR("Should not be used with a wrapper tevent context\n");
63 *err = EINVAL;
64 return NULL;
67 result = talloc(mem_ctx, struct msg_ctdb_ref);
68 if (result == NULL) {
69 *err = ENOMEM;
70 return NULL;
72 result->fde = NULL;
74 tmp_refs = refs;
76 if ((refs != NULL) && (ctdb_pid != getpid())) {
78 * Have to reinit after fork
80 messaging_ctdb_destroy();
81 refs = NULL;
84 if (refs == NULL) {
85 int ret;
87 ret = messaging_ctdb_init(sockname, timeout, unique_id,
88 msg_ctdb_ref_recv, NULL);
89 DBG_DEBUG("messaging_ctdb_init returned %s\n", strerror(ret));
90 if (ret != 0) {
91 DEBUG(10, ("messaging_ctdb_init failed: %s\n",
92 strerror(ret)));
93 TALLOC_FREE(result);
94 *err = ret;
95 return NULL;
97 ctdb_pid = getpid();
100 result->fde = messaging_ctdb_register_tevent_context(result, ev);
101 if (result->fde == NULL) {
102 TALLOC_FREE(result);
103 *err = ENOMEM;
104 return NULL;
107 refs = tmp_refs;
109 result->recv_cb = recv_cb;
110 result->recv_cb_private_data = recv_cb_private_data;
111 DLIST_ADD(refs, result);
112 talloc_set_destructor(result, msg_ctdb_ref_destructor);
114 return result;
117 static void msg_ctdb_ref_recv(struct tevent_context *ev,
118 const uint8_t *msg, size_t msg_len,
119 int *fds, size_t num_fds, void *private_data)
121 struct msg_ctdb_ref *r, *next;
123 for (r = refs; r != NULL; r = next) {
124 bool active;
126 next = r->next;
128 active = messaging_ctdb_fde_active(r->fde);
129 if (!active) {
131 * r's tevent_context has died.
133 continue;
136 r->recv_cb(ev, msg, msg_len, fds, num_fds,
137 r->recv_cb_private_data);
138 break;
142 static int msg_ctdb_ref_destructor(struct msg_ctdb_ref *r)
144 if (refs == NULL) {
145 abort();
147 DLIST_REMOVE(refs, r);
149 TALLOC_FREE(r->fde);
151 DBG_DEBUG("refs=%p\n", refs);
153 if (refs == NULL) {
154 messaging_ctdb_destroy();
156 return 0;