nwrap: Don't leak memory from gethostbyname*() functions
[obnox/samba/samba-obnox.git] / source3 / lib / messages_dgm_ref.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Samba internal messaging functions
4  * Copyright (C) 2014 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_dgm.h"
23 #include "messages_dgm_ref.h"
24 #include "lib/util/debug.h"
25 #include "lib/util/dlinklist.h"
26
27 struct msg_dgm_ref {
28         struct msg_dgm_ref *prev, *next;
29         void *tevent_handle;
30         void (*recv_cb)(const uint8_t *msg, size_t msg_len,
31                         int *fds, size_t num_fds, void *private_data);
32         void *recv_cb_private_data;
33 };
34
35 static pid_t dgm_pid = 0;
36 static struct msg_dgm_ref *refs = NULL;
37
38 static int msg_dgm_ref_destructor(struct msg_dgm_ref *r);
39 static void msg_dgm_ref_recv(const uint8_t *msg, size_t msg_len,
40                              int *fds, size_t num_fds, void *private_data);
41
42 void *messaging_dgm_ref(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
43                         uint64_t *unique,
44                         const char *socket_dir,
45                         const char *lockfile_dir,
46                         void (*recv_cb)(const uint8_t *msg, size_t msg_len,
47                                         int *fds, size_t num_fds,
48                                         void *private_data),
49                         void *recv_cb_private_data,
50                         int *err)
51 {
52         struct msg_dgm_ref *result, *tmp_refs;
53
54         result = talloc(mem_ctx, struct msg_dgm_ref);
55         if (result == NULL) {
56                 *err = ENOMEM;
57                 return NULL;
58         }
59         result->tevent_handle = NULL;
60
61         tmp_refs = refs;
62
63         if ((refs != NULL) && (dgm_pid != getpid())) {
64                 /*
65                  * Have to reinit after fork
66                  */
67                 messaging_dgm_destroy();
68                 refs = NULL;
69         }
70
71         if (refs == NULL) {
72                 int ret;
73
74                 ret = messaging_dgm_init(ev, unique, socket_dir, lockfile_dir,
75                                          msg_dgm_ref_recv, NULL);
76                 DBG_DEBUG("messaging_dgm_init returned %s\n", strerror(ret));
77                 if (ret != 0) {
78                         DEBUG(10, ("messaging_dgm_init failed: %s\n",
79                                    strerror(ret)));
80                         TALLOC_FREE(result);
81                         *err = ret;
82                         return NULL;
83                 }
84                 dgm_pid = getpid();
85         } else {
86                 int ret;
87                 ret = messaging_dgm_get_unique(getpid(), unique);
88                 DBG_DEBUG("messaging_dgm_get_unique returned %s\n",
89                           strerror(ret));
90                 if (ret != 0) {
91                         TALLOC_FREE(result);
92                         *err = ret;
93                         return NULL;
94                 }
95
96                 result->tevent_handle = messaging_dgm_register_tevent_context(
97                         result, ev);
98                 if (result->tevent_handle == NULL) {
99                         TALLOC_FREE(result);
100                         *err = ENOMEM;
101                         return NULL;
102                 }
103         }
104
105         DBG_DEBUG("unique = %"PRIu64"\n", *unique);
106
107         refs = tmp_refs;
108
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_dgm_ref_destructor);
113
114         return result;
115 }
116
117 static void msg_dgm_ref_recv(const uint8_t *msg, size_t msg_len,
118                              int *fds, size_t num_fds, void *private_data)
119 {
120         struct msg_dgm_ref *r, *next;
121
122         /*
123          * We have to broadcast incoming messages to all refs. The first ref
124          * that grabs the fd's will get them.
125          */
126         for (r = refs; r != NULL; r = next) {
127                 next = r->next;
128                 r->recv_cb(msg, msg_len, fds, num_fds,
129                            r->recv_cb_private_data);
130         }
131 }
132
133 static int msg_dgm_ref_destructor(struct msg_dgm_ref *r)
134 {
135         if (refs == NULL) {
136                 abort();
137         }
138         DLIST_REMOVE(refs, r);
139
140         TALLOC_FREE(r->tevent_handle);
141
142         DBG_DEBUG("refs=%p\n", refs);
143
144         if (refs == NULL) {
145                 messaging_dgm_destroy();
146         }
147         return 0;
148 }