source: vendor/current/source3/rpc_server/epmd.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 5.3 KB
Line 
1/*
2 * Unix SMB/CIFS implementation.
3 *
4 * SMBD RPC service callbacks
5 *
6 * Copyright (c) 2011 Andreas Schneider <asn@samba.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "includes.h"
23
24#include "serverid.h"
25#include "ntdomain.h"
26#include "messages.h"
27
28#include "librpc/rpc/dcerpc_ep.h"
29#include "../librpc/gen_ndr/srv_epmapper.h"
30#include "rpc_server/rpc_server.h"
31#include "rpc_server/rpc_sock_helper.h"
32#include "rpc_server/epmapper/srv_epmapper.h"
33
34#define DAEMON_NAME "epmd"
35
36void start_epmd(struct tevent_context *ev_ctx,
37 struct messaging_context *msg_ctx);
38
39static void epmd_reopen_logs(void)
40{
41 char *lfile = lp_logfile(talloc_tos());
42 int rc;
43
44 if (lfile == NULL || lfile[0] == '\0') {
45 rc = asprintf(&lfile, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME);
46 if (rc > 0) {
47 lp_set_logfile(lfile);
48 SAFE_FREE(lfile);
49 }
50 } else {
51 if (strstr(lfile, DAEMON_NAME) == NULL) {
52 rc = asprintf(&lfile, "%s.%s",
53 lp_logfile(talloc_tos()), DAEMON_NAME);
54 if (rc > 0) {
55 lp_set_logfile(lfile);
56 SAFE_FREE(lfile);
57 }
58 }
59 }
60
61 reopen_logs();
62}
63
64static void epmd_smb_conf_updated(struct messaging_context *msg,
65 void *private_data,
66 uint32_t msg_type,
67 struct server_id server_id,
68 DATA_BLOB *data)
69{
70 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
71 change_to_root_user();
72 epmd_reopen_logs();
73}
74
75static void epmd_sig_term_handler(struct tevent_context *ev,
76 struct tevent_signal *se,
77 int signum,
78 int count,
79 void *siginfo,
80 void *private_data)
81{
82 rpc_epmapper_shutdown();
83
84 exit_server_cleanly("termination signal");
85}
86
87static void epmd_setup_sig_term_handler(struct tevent_context *ev_ctx)
88{
89 struct tevent_signal *se;
90
91 se = tevent_add_signal(ev_ctx,
92 ev_ctx,
93 SIGTERM, 0,
94 epmd_sig_term_handler,
95 NULL);
96 if (se == NULL) {
97 exit_server("failed to setup SIGTERM handler");
98 }
99}
100
101static void epmd_sig_hup_handler(struct tevent_context *ev,
102 struct tevent_signal *se,
103 int signum,
104 int count,
105 void *siginfo,
106 void *private_data)
107{
108 change_to_root_user();
109
110 DEBUG(1,("Reloading printers after SIGHUP\n"));
111 epmd_reopen_logs();
112}
113
114static void epmd_setup_sig_hup_handler(struct tevent_context *ev_ctx,
115 struct messaging_context *msg_ctx)
116{
117 struct tevent_signal *se;
118
119 se = tevent_add_signal(ev_ctx,
120 ev_ctx,
121 SIGHUP, 0,
122 epmd_sig_hup_handler,
123 msg_ctx);
124 if (se == NULL) {
125 exit_server("failed to setup SIGHUP handler");
126 }
127}
128
129static bool epmapper_shutdown_cb(void *ptr) {
130 srv_epmapper_cleanup();
131
132 return true;
133}
134
135void start_epmd(struct tevent_context *ev_ctx,
136 struct messaging_context *msg_ctx)
137{
138 struct rpc_srv_callbacks epmapper_cb;
139 NTSTATUS status;
140 pid_t pid;
141 bool ok;
142 int rc;
143
144 epmapper_cb.init = NULL;
145 epmapper_cb.shutdown = epmapper_shutdown_cb;
146 epmapper_cb.private_data = NULL;
147
148 DEBUG(1, ("Forking Endpoint Mapper Daemon\n"));
149
150 pid = fork();
151
152 if (pid == -1) {
153 DEBUG(0, ("Failed to fork Endpoint Mapper [%s], aborting ...\n",
154 strerror(errno)));
155 exit(1);
156 }
157
158 if (pid) {
159 /* parent */
160 return;
161 }
162
163 status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true, "epmd");
164 if (!NT_STATUS_IS_OK(status)) {
165 DEBUG(0,("reinit_after_fork() failed\n"));
166 smb_panic("reinit_after_fork() failed");
167 }
168
169 epmd_reopen_logs();
170
171 epmd_setup_sig_term_handler(ev_ctx);
172 epmd_setup_sig_hup_handler(ev_ctx, msg_ctx);
173
174 ok = serverid_register(messaging_server_id(msg_ctx),
175 FLAG_MSG_GENERAL |
176 FLAG_MSG_PRINT_GENERAL);
177 if (!ok) {
178 DEBUG(0, ("Failed to register serverid in epmd!\n"));
179 exit(1);
180 }
181
182 messaging_register(msg_ctx,
183 ev_ctx,
184 MSG_SMB_CONF_UPDATED,
185 epmd_smb_conf_updated);
186
187 status = rpc_epmapper_init(&epmapper_cb);
188 if (!NT_STATUS_IS_OK(status)) {
189 DEBUG(0, ("Failed to register epmd rpc interface! (%s)\n",
190 nt_errstr(status)));
191 exit(1);
192 }
193
194 status = rpc_setup_tcpip_sockets(ev_ctx,
195 msg_ctx,
196 &ndr_table_epmapper,
197 NULL,
198 135);
199 if (!NT_STATUS_IS_OK(status)) {
200 DEBUG(0, ("Failed to open epmd tcpip sockets!\n"));
201 exit(1);
202 }
203
204 ok = setup_dcerpc_ncalrpc_socket(ev_ctx,
205 msg_ctx,
206 "EPMAPPER",
207 srv_epmapper_delete_endpoints);
208 if (!ok) {
209 DEBUG(0, ("Failed to open epmd ncalrpc pipe!\n"));
210 exit(1);
211 }
212
213 ok = setup_named_pipe_socket("epmapper", ev_ctx, msg_ctx);
214 if (!ok) {
215 DEBUG(0, ("Failed to open epmd named pipe!\n"));
216 exit(1);
217 }
218
219 DEBUG(1, ("Endpoint Mapper Daemon Started (%u)\n", (unsigned int)getpid()));
220
221 /* loop forever */
222 rc = tevent_loop_wait(ev_ctx);
223
224 /* should not be reached */
225 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
226 rc, (rc == 0) ? "out of events" : strerror(errno)));
227
228 exit(1);
229}
230
231/* vim: set ts=8 sw=8 noet cindent ft=c.doxygen: */
Note: See TracBrowser for help on using the repository browser.