upgradeprovision: Change test to always use 2008 R2 schema
[kai/samba-autobuild/.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         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;
34 };
35
36 static pid_t dgm_pid = 0;
37 static struct msg_dgm_ref *refs = NULL;
38
39 static int msg_dgm_ref_destructor(struct msg_dgm_ref *r);
40 static void msg_dgm_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_dgm_ref(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
45                         uint64_t *unique,
46                         const char *socket_dir,
47                         const char *lockfile_dir,
48                         void (*recv_cb)(struct tevent_context *ev,
49                                         const uint8_t *msg, size_t msg_len,
50                                         int *fds, size_t num_fds,
51                                         void *private_data),
52                         void *recv_cb_private_data,
53                         int *err)
54 {
55         struct msg_dgm_ref *result, *tmp_refs;
56
57         result = talloc(mem_ctx, struct msg_dgm_ref);
58         if (result == NULL) {
59                 *err = ENOMEM;
60                 return NULL;
61         }
62         result->fde = NULL;
63
64         tmp_refs = refs;
65
66         if ((refs != NULL) && (dgm_pid != getpid())) {
67                 /*
68                  * Have to reinit after fork
69                  */
70                 messaging_dgm_destroy();
71                 refs = NULL;
72         }
73
74         if (refs == NULL) {
75                 int ret;
76
77                 ret = messaging_dgm_init(ev, unique, socket_dir, lockfile_dir,
78                                          msg_dgm_ref_recv, NULL);
79                 DBG_DEBUG("messaging_dgm_init returned %s\n", strerror(ret));
80                 if (ret != 0) {
81                         DEBUG(10, ("messaging_dgm_init failed: %s\n",
82                                    strerror(ret)));
83                         TALLOC_FREE(result);
84                         *err = ret;
85                         return NULL;
86                 }
87                 dgm_pid = getpid();
88         } else {
89                 int ret;
90                 ret = messaging_dgm_get_unique(getpid(), unique);
91                 DBG_DEBUG("messaging_dgm_get_unique returned %s\n",
92                           strerror(ret));
93                 if (ret != 0) {
94                         TALLOC_FREE(result);
95                         *err = ret;
96                         return NULL;
97                 }
98
99         }
100
101         result->fde = messaging_dgm_register_tevent_context(result, ev);
102         if (result->fde == NULL) {
103                 TALLOC_FREE(result);
104                 *err = ENOMEM;
105                 return NULL;
106         }
107
108         DBG_DEBUG("unique = %"PRIu64"\n", *unique);
109
110         refs = tmp_refs;
111
112         result->recv_cb = recv_cb;
113         result->recv_cb_private_data = recv_cb_private_data;
114         DLIST_ADD(refs, result);
115         talloc_set_destructor(result, msg_dgm_ref_destructor);
116
117         return result;
118 }
119
120 static void msg_dgm_ref_recv(struct tevent_context *ev,
121                              const uint8_t *msg, size_t msg_len,
122                              int *fds, size_t num_fds, void *private_data)
123 {
124         struct msg_dgm_ref *r, *next;
125
126         /*
127          * We have to broadcast incoming messages to all refs. The first ref
128          * that grabs the fd's will get them.
129          */
130         for (r = refs; r != NULL; r = next) {
131                 bool active;
132
133                 next = r->next;
134
135                 active = messaging_dgm_fde_active(r->fde);
136                 if (!active) {
137                         /*
138                          * r's tevent_context has died.
139                          */
140                         continue;
141                 }
142
143                 r->recv_cb(ev, msg, msg_len, fds, num_fds,
144                            r->recv_cb_private_data);
145         }
146 }
147
148 static int msg_dgm_ref_destructor(struct msg_dgm_ref *r)
149 {
150         if (refs == NULL) {
151                 abort();
152         }
153         DLIST_REMOVE(refs, r);
154
155         TALLOC_FREE(r->fde);
156
157         DBG_DEBUG("refs=%p\n", refs);
158
159         if (refs == NULL) {
160                 messaging_dgm_destroy();
161         }
162         return 0;
163 }