winbind: Remove winbind_event_context
[samba.git] / source3 / lib / messages_ctdb_ref.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Samba internal messaging functions
4  * Copyright (C) 2017 by Volker Lendecke
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 <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"
26
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;
34 };
35
36 static pid_t ctdb_pid = 0;
37 static struct msg_ctdb_ref *refs = NULL;
38
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);
43
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)
52 {
53         struct msg_ctdb_ref *result, *tmp_refs;
54
55         result = talloc(mem_ctx, struct msg_ctdb_ref);
56         if (result == NULL) {
57                 *err = ENOMEM;
58                 return NULL;
59         }
60         result->fde = NULL;
61
62         tmp_refs = refs;
63
64         if ((refs != NULL) && (ctdb_pid != getpid())) {
65                 /*
66                  * Have to reinit after fork
67                  */
68                 messaging_ctdb_destroy();
69                 refs = NULL;
70         }
71
72         if (refs == NULL) {
73                 int ret;
74
75                 ret = messaging_ctdb_init(sockname, timeout, unique_id,
76                                           msg_ctdb_ref_recv, NULL);
77                 DBG_DEBUG("messaging_ctdb_init returned %s\n", strerror(ret));
78                 if (ret != 0) {
79                         DEBUG(10, ("messaging_ctdb_init failed: %s\n",
80                                    strerror(ret)));
81                         TALLOC_FREE(result);
82                         *err = ret;
83                         return NULL;
84                 }
85                 ctdb_pid = getpid();
86         }
87
88         result->fde = messaging_ctdb_register_tevent_context(result, ev);
89         if (result->fde == NULL) {
90                 TALLOC_FREE(result);
91                 *err = ENOMEM;
92                 return NULL;
93         }
94
95         refs = tmp_refs;
96
97         result->recv_cb = recv_cb;
98         result->recv_cb_private_data = recv_cb_private_data;
99         DLIST_ADD(refs, result);
100         talloc_set_destructor(result, msg_ctdb_ref_destructor);
101
102         return result;
103 }
104
105 static void msg_ctdb_ref_recv(struct tevent_context *ev,
106                               const uint8_t *msg, size_t msg_len,
107                               int *fds, size_t num_fds, void *private_data)
108 {
109         struct msg_ctdb_ref *r, *next;
110
111         for (r = refs; r != NULL; r = next) {
112                 bool active;
113
114                 next = r->next;
115
116                 active = messaging_ctdb_fde_active(r->fde);
117                 if (!active) {
118                         /*
119                          * r's tevent_context has died.
120                          */
121                         continue;
122                 }
123
124                 r->recv_cb(ev, msg, msg_len, fds, num_fds,
125                            r->recv_cb_private_data);
126                 break;
127         }
128 }
129
130 static int msg_ctdb_ref_destructor(struct msg_ctdb_ref *r)
131 {
132         if (refs == NULL) {
133                 abort();
134         }
135         DLIST_REMOVE(refs, r);
136
137         TALLOC_FREE(r->fde);
138
139         DBG_DEBUG("refs=%p\n", refs);
140
141         if (refs == NULL) {
142                 messaging_ctdb_destroy();
143         }
144         return 0;
145 }