s3:rpc_server: Rename setup_named_pipe_socket
[Samba.git] / source3 / rpc_server / epmd.c
blob4cf07a4eacf39841d189deacb67df822cb43dc45
1 /*
2 * Unix SMB/CIFS implementation.
4 * SMBD RPC service callbacks
6 * Copyright (c) 2011 Andreas Schneider <asn@samba.org>
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.
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.
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/>.
22 #include "includes.h"
24 #include "ntdomain.h"
25 #include "messages.h"
27 #include "librpc/rpc/dcerpc_ep.h"
28 #include "../librpc/gen_ndr/srv_epmapper.h"
29 #include "rpc_server/rpc_server.h"
30 #include "rpc_server/rpc_sock_helper.h"
31 #include "rpc_server/epmapper/srv_epmapper.h"
32 #include "rpc_server/epmd.h"
34 #define DAEMON_NAME "epmd"
36 static void epmd_reopen_logs(void)
38 char *lfile = lp_logfile(talloc_tos());
39 int rc;
41 if (lfile == NULL || lfile[0] == '\0') {
42 rc = asprintf(&lfile, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME);
43 if (rc > 0) {
44 lp_set_logfile(lfile);
45 SAFE_FREE(lfile);
47 } else {
48 if (strstr(lfile, DAEMON_NAME) == NULL) {
49 rc = asprintf(&lfile, "%s.%s",
50 lp_logfile(talloc_tos()), DAEMON_NAME);
51 if (rc > 0) {
52 lp_set_logfile(lfile);
53 SAFE_FREE(lfile);
58 reopen_logs();
61 static void epmd_smb_conf_updated(struct messaging_context *msg,
62 void *private_data,
63 uint32_t msg_type,
64 struct server_id server_id,
65 DATA_BLOB *data)
67 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
68 change_to_root_user();
69 epmd_reopen_logs();
72 static void epmd_sig_term_handler(struct tevent_context *ev,
73 struct tevent_signal *se,
74 int signum,
75 int count,
76 void *siginfo,
77 void *private_data)
79 rpc_epmapper_shutdown();
81 exit_server_cleanly("termination signal");
84 static void epmd_setup_sig_term_handler(struct tevent_context *ev_ctx)
86 struct tevent_signal *se;
88 se = tevent_add_signal(ev_ctx,
89 ev_ctx,
90 SIGTERM, 0,
91 epmd_sig_term_handler,
92 NULL);
93 if (se == NULL) {
94 exit_server("failed to setup SIGTERM handler");
98 static void epmd_sig_hup_handler(struct tevent_context *ev,
99 struct tevent_signal *se,
100 int signum,
101 int count,
102 void *siginfo,
103 void *private_data)
105 change_to_root_user();
107 DEBUG(1,("Reloading printers after SIGHUP\n"));
108 epmd_reopen_logs();
111 static void epmd_setup_sig_hup_handler(struct tevent_context *ev_ctx,
112 struct messaging_context *msg_ctx)
114 struct tevent_signal *se;
116 se = tevent_add_signal(ev_ctx,
117 ev_ctx,
118 SIGHUP, 0,
119 epmd_sig_hup_handler,
120 msg_ctx);
121 if (se == NULL) {
122 exit_server("failed to setup SIGHUP handler");
126 static bool epmapper_shutdown_cb(void *ptr) {
127 srv_epmapper_cleanup();
129 return true;
132 void start_epmd(struct tevent_context *ev_ctx,
133 struct messaging_context *msg_ctx)
135 struct rpc_srv_callbacks epmapper_cb;
136 NTSTATUS status;
137 pid_t pid;
138 bool ok;
139 int rc;
141 epmapper_cb.init = NULL;
142 epmapper_cb.shutdown = epmapper_shutdown_cb;
143 epmapper_cb.private_data = NULL;
145 DEBUG(1, ("Forking Endpoint Mapper Daemon\n"));
147 pid = fork();
149 if (pid == -1) {
150 DEBUG(0, ("Failed to fork Endpoint Mapper [%s], aborting ...\n",
151 strerror(errno)));
152 exit(1);
155 if (pid) {
156 /* parent */
157 return;
160 status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true, "epmd");
161 if (!NT_STATUS_IS_OK(status)) {
162 DEBUG(0,("reinit_after_fork() failed\n"));
163 smb_panic("reinit_after_fork() failed");
166 epmd_reopen_logs();
168 epmd_setup_sig_term_handler(ev_ctx);
169 epmd_setup_sig_hup_handler(ev_ctx, msg_ctx);
171 messaging_register(msg_ctx,
172 ev_ctx,
173 MSG_SMB_CONF_UPDATED,
174 epmd_smb_conf_updated);
176 status = rpc_epmapper_init(&epmapper_cb);
177 if (!NT_STATUS_IS_OK(status)) {
178 DEBUG(0, ("Failed to register epmd rpc interface! (%s)\n",
179 nt_errstr(status)));
180 exit(1);
183 status = rpc_setup_tcpip_sockets(ev_ctx,
184 msg_ctx,
185 &ndr_table_epmapper,
186 NULL,
187 135);
188 if (!NT_STATUS_IS_OK(status)) {
189 DEBUG(0, ("Failed to open epmd tcpip sockets!\n"));
190 exit(1);
193 ok = setup_dcerpc_ncalrpc_socket(ev_ctx,
194 msg_ctx,
195 "EPMAPPER",
196 srv_epmapper_delete_endpoints);
197 if (!ok) {
198 DEBUG(0, ("Failed to open epmd ncalrpc pipe!\n"));
199 exit(1);
202 ok = dcesrv_setup_ncacn_np_socket("epmapper", ev_ctx, msg_ctx);
203 if (!ok) {
204 DEBUG(0, ("Failed to open epmd named pipe!\n"));
205 exit(1);
208 DEBUG(1, ("Endpoint Mapper Daemon Started (%u)\n", (unsigned int)getpid()));
210 /* loop forever */
211 rc = tevent_loop_wait(ev_ctx);
213 /* should not be reached */
214 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
215 rc, (rc == 0) ? "out of events" : strerror(errno)));
217 exit(1);
220 /* vim: set ts=8 sw=8 noet cindent ft=c.doxygen: */