backupkey: Handle more clearly the case where we find the secret, but it has no value
[Samba.git] / source3 / lib / messages_dgm_ref.c
blob32b9c985296c3ff0a802089193dd8e12f62c0111
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 <talloc.h>
21 #include "replace.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 void (*recv_cb)(const uint8_t *msg, size_t msg_len,
30 int *fds, size_t num_fds, void *private_data);
31 void *recv_cb_private_data;
34 static pid_t dgm_pid = 0;
35 static struct msg_dgm_ref *refs = NULL;
37 static int msg_dgm_ref_destructor(struct msg_dgm_ref *r);
38 static void msg_dgm_ref_recv(const uint8_t *msg, size_t msg_len,
39 int *fds, size_t num_fds, void *private_data);
41 void *messaging_dgm_ref(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
42 uint64_t unique,
43 const char *socket_dir,
44 const char *lockfile_dir,
45 void (*recv_cb)(const uint8_t *msg, size_t msg_len,
46 int *fds, size_t num_fds,
47 void *private_data),
48 void *recv_cb_private_data,
49 int *err)
51 struct msg_dgm_ref *result, *tmp_refs;
53 result = talloc(mem_ctx, struct msg_dgm_ref);
54 if (result == NULL) {
55 *err = ENOMEM;
56 return NULL;
59 tmp_refs = refs;
61 if ((refs != NULL) && (dgm_pid != getpid())) {
63 * Have to reinit after fork
65 messaging_dgm_destroy();
66 refs = NULL;
69 if (refs == NULL) {
70 int ret;
72 ret = messaging_dgm_init(ev, unique, socket_dir, lockfile_dir,
73 msg_dgm_ref_recv, NULL);
74 if (ret != 0) {
75 DEBUG(10, ("messaging_dgm_init failed: %s\n",
76 strerror(ret)));
77 TALLOC_FREE(result);
78 *err = ret;
79 return NULL;
81 dgm_pid = getpid();
84 refs = tmp_refs;
86 result->recv_cb = recv_cb;
87 result->recv_cb_private_data = recv_cb_private_data;
88 DLIST_ADD(refs, result);
89 talloc_set_destructor(result, msg_dgm_ref_destructor);
91 return result;
94 static void msg_dgm_ref_recv(const uint8_t *msg, size_t msg_len,
95 int *fds, size_t num_fds, void *private_data)
97 struct msg_dgm_ref *r, *next;
100 * We have to broadcast incoming messages to all refs. The first ref
101 * that grabs the fd's will get them.
103 for (r = refs; r != NULL; r = next) {
104 next = r->next;
105 r->recv_cb(msg, msg_len, fds, num_fds,
106 r->recv_cb_private_data);
110 static int msg_dgm_ref_destructor(struct msg_dgm_ref *r)
112 if (refs == NULL) {
113 abort();
115 DLIST_REMOVE(refs, r);
117 if (refs == NULL) {
118 messaging_dgm_destroy();
120 return 0;