s4:libcli: split out smb_raw_negotiate_fill_transport()
[Samba.git] / source3 / lib / messages_dgm_ref.c
blob12ff21ca6283a90c503f13996d054f9f208e48df
1 /*
2 * Unix SMB/CIFS implementation.
3 * Samba internal messaging functions
4 * Copyright (C) 2014 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_dgm.h"
23 #include "messages_dgm_ref.h"
24 #include "lib/util/debug.h"
25 #include "lib/util/dlinklist.h"
27 struct msg_dgm_ref {
28 struct msg_dgm_ref *prev, *next;
29 struct messaging_dgm_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 dgm_pid = 0;
37 static struct msg_dgm_ref *refs = NULL;
38 static struct msg_dgm_ref *next_ref = NULL;
40 static int msg_dgm_ref_destructor(struct msg_dgm_ref *r);
41 static void msg_dgm_ref_recv(struct tevent_context *ev,
42 const uint8_t *msg, size_t msg_len,
43 int *fds, size_t num_fds, void *private_data);
45 void *messaging_dgm_ref(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
46 uint64_t *unique,
47 const char *socket_dir,
48 const char *lockfile_dir,
49 void (*recv_cb)(struct tevent_context *ev,
50 const uint8_t *msg, size_t msg_len,
51 int *fds, size_t num_fds,
52 void *private_data),
53 void *recv_cb_private_data,
54 int *err)
56 struct msg_dgm_ref *result, *tmp_refs;
58 result = talloc(mem_ctx, struct msg_dgm_ref);
59 if (result == NULL) {
60 *err = ENOMEM;
61 return NULL;
63 result->fde = NULL;
65 tmp_refs = refs;
67 if ((refs != NULL) && (dgm_pid != getpid())) {
69 * Have to reinit after fork
71 messaging_dgm_destroy();
72 refs = NULL;
75 if (refs == NULL) {
76 int ret;
78 if (tevent_context_is_wrapper(ev)) {
80 * This is really a programmer error!
82 * The main/raw tevent context should
83 * have been registered first!
85 DBG_ERR("Should not be used with a wrapper tevent context\n");
86 *err = EINVAL;
87 return NULL;
90 ret = messaging_dgm_init(ev, unique, socket_dir, lockfile_dir,
91 msg_dgm_ref_recv, NULL);
92 DBG_DEBUG("messaging_dgm_init returned %s\n", strerror(ret));
93 if (ret != 0) {
94 DEBUG(10, ("messaging_dgm_init failed: %s\n",
95 strerror(ret)));
96 TALLOC_FREE(result);
97 *err = ret;
98 return NULL;
100 dgm_pid = getpid();
101 } else {
102 int ret;
103 ret = messaging_dgm_get_unique(getpid(), unique);
104 DBG_DEBUG("messaging_dgm_get_unique returned %s\n",
105 strerror(ret));
106 if (ret != 0) {
107 TALLOC_FREE(result);
108 *err = ret;
109 return NULL;
114 result->fde = messaging_dgm_register_tevent_context(result, ev);
115 if (result->fde == NULL) {
116 TALLOC_FREE(result);
117 *err = ENOMEM;
118 return NULL;
121 DBG_DEBUG("unique = %"PRIu64"\n", *unique);
123 refs = tmp_refs;
125 result->recv_cb = recv_cb;
126 result->recv_cb_private_data = recv_cb_private_data;
127 DLIST_ADD(refs, result);
128 talloc_set_destructor(result, msg_dgm_ref_destructor);
130 return result;
133 static void msg_dgm_ref_recv(struct tevent_context *ev,
134 const uint8_t *msg, size_t msg_len,
135 int *fds, size_t num_fds, void *private_data)
137 struct msg_dgm_ref *r;
140 * We have to broadcast incoming messages to all refs. The first ref
141 * that grabs the fd's will get them.
143 for (r = refs; r != NULL; r = next_ref) {
144 bool active;
146 next_ref = r->next;
148 active = messaging_dgm_fde_active(r->fde);
149 if (!active) {
151 * r's tevent_context has died.
153 continue;
156 r->recv_cb(ev, msg, msg_len, fds, num_fds,
157 r->recv_cb_private_data);
161 static int msg_dgm_ref_destructor(struct msg_dgm_ref *r)
163 if (refs == NULL) {
164 abort();
167 if (r == next_ref) {
168 next_ref = r->next;
171 DLIST_REMOVE(refs, r);
173 TALLOC_FREE(r->fde);
175 DBG_DEBUG("refs=%p\n", refs);
177 if (refs == NULL) {
178 messaging_dgm_destroy();
180 return 0;