s4:lib/socket: simplify iface_list_wildcard() and its callers
[Samba.git] / source3 / rpc_server / epmd.c
blob6b6119b5e35957731fbe3b979a9abf3f4caab4b6
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 "serverid.h"
25 #include "ntdomain.h"
26 #include "messages.h"
28 #include "lib/util/util_process.h"
30 #include "librpc/rpc/dcerpc_ep.h"
31 #include "../librpc/gen_ndr/srv_epmapper.h"
32 #include "rpc_server/rpc_server.h"
33 #include "rpc_server/rpc_sock_helper.h"
34 #include "rpc_server/epmapper/srv_epmapper.h"
36 #define DAEMON_NAME "epmd"
38 void start_epmd(struct tevent_context *ev_ctx,
39 struct messaging_context *msg_ctx);
41 static void epmd_reopen_logs(void)
43 char *lfile = lp_logfile(talloc_tos());
44 int rc;
46 if (lfile == NULL || lfile[0] == '\0') {
47 rc = asprintf(&lfile, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME);
48 if (rc > 0) {
49 lp_set_logfile(lfile);
50 SAFE_FREE(lfile);
52 } else {
53 if (strstr(lfile, DAEMON_NAME) == NULL) {
54 rc = asprintf(&lfile, "%s.%s",
55 lp_logfile(talloc_tos()), DAEMON_NAME);
56 if (rc > 0) {
57 lp_set_logfile(lfile);
58 SAFE_FREE(lfile);
63 reopen_logs();
66 static void epmd_smb_conf_updated(struct messaging_context *msg,
67 void *private_data,
68 uint32_t msg_type,
69 struct server_id server_id,
70 DATA_BLOB *data)
72 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
73 change_to_root_user();
74 epmd_reopen_logs();
77 static void epmd_sig_term_handler(struct tevent_context *ev,
78 struct tevent_signal *se,
79 int signum,
80 int count,
81 void *siginfo,
82 void *private_data)
84 rpc_epmapper_shutdown();
86 exit_server_cleanly("termination signal");
89 static void epmd_setup_sig_term_handler(struct tevent_context *ev_ctx)
91 struct tevent_signal *se;
93 se = tevent_add_signal(ev_ctx,
94 ev_ctx,
95 SIGTERM, 0,
96 epmd_sig_term_handler,
97 NULL);
98 if (se == NULL) {
99 exit_server("failed to setup SIGTERM handler");
103 static void epmd_sig_hup_handler(struct tevent_context *ev,
104 struct tevent_signal *se,
105 int signum,
106 int count,
107 void *siginfo,
108 void *private_data)
110 change_to_root_user();
112 DEBUG(1,("Reloading printers after SIGHUP\n"));
113 epmd_reopen_logs();
116 static void epmd_setup_sig_hup_handler(struct tevent_context *ev_ctx,
117 struct messaging_context *msg_ctx)
119 struct tevent_signal *se;
121 se = tevent_add_signal(ev_ctx,
122 ev_ctx,
123 SIGHUP, 0,
124 epmd_sig_hup_handler,
125 msg_ctx);
126 if (se == NULL) {
127 exit_server("failed to setup SIGHUP handler");
131 static bool epmapper_shutdown_cb(void *ptr) {
132 srv_epmapper_cleanup();
134 return true;
137 void start_epmd(struct tevent_context *ev_ctx,
138 struct messaging_context *msg_ctx)
140 struct rpc_srv_callbacks epmapper_cb;
141 NTSTATUS status;
142 pid_t pid;
143 bool ok;
144 int rc;
146 epmapper_cb.init = NULL;
147 epmapper_cb.shutdown = epmapper_shutdown_cb;
148 epmapper_cb.private_data = NULL;
150 DEBUG(1, ("Forking Endpoint Mapper Daemon\n"));
152 pid = fork();
154 if (pid == -1) {
155 DEBUG(0, ("Failed to fork Endpoint Mapper [%s], aborting ...\n",
156 strerror(errno)));
157 exit(1);
160 if (pid) {
161 /* parent */
162 return;
165 status = reinit_after_fork(msg_ctx,
166 ev_ctx,
167 true);
168 if (!NT_STATUS_IS_OK(status)) {
169 DEBUG(0,("reinit_after_fork() failed\n"));
170 smb_panic("reinit_after_fork() failed");
173 prctl_set_comment("epmd");
175 epmd_reopen_logs();
177 epmd_setup_sig_term_handler(ev_ctx);
178 epmd_setup_sig_hup_handler(ev_ctx, msg_ctx);
180 ok = serverid_register(messaging_server_id(msg_ctx),
181 FLAG_MSG_GENERAL |
182 FLAG_MSG_PRINT_GENERAL);
183 if (!ok) {
184 DEBUG(0, ("Failed to register serverid in epmd!\n"));
185 exit(1);
188 messaging_register(msg_ctx,
189 ev_ctx,
190 MSG_SMB_CONF_UPDATED,
191 epmd_smb_conf_updated);
193 status = rpc_epmapper_init(&epmapper_cb);
194 if (!NT_STATUS_IS_OK(status)) {
195 DEBUG(0, ("Failed to register epmd rpc inteface! (%s)\n",
196 nt_errstr(status)));
197 exit(1);
200 status = rpc_setup_tcpip_sockets(ev_ctx,
201 msg_ctx,
202 &ndr_table_epmapper,
203 NULL,
204 135);
205 if (!NT_STATUS_IS_OK(status)) {
206 DEBUG(0, ("Failed to open epmd tcpip sockets!\n"));
207 exit(1);
210 ok = setup_dcerpc_ncalrpc_socket(ev_ctx,
211 msg_ctx,
212 "EPMAPPER",
213 srv_epmapper_delete_endpoints);
214 if (!ok) {
215 DEBUG(0, ("Failed to open epmd ncalrpc pipe!\n"));
216 exit(1);
219 ok = setup_named_pipe_socket("epmapper", ev_ctx, msg_ctx);
220 if (!ok) {
221 DEBUG(0, ("Failed to open epmd named pipe!\n"));
222 exit(1);
225 DEBUG(1, ("Endpoint Mapper Daemon Started (%u)\n", (unsigned int)getpid()));
227 /* loop forever */
228 rc = tevent_loop_wait(ev_ctx);
230 /* should not be reached */
231 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
232 rc, (rc == 0) ? "out of events" : strerror(errno)));
234 exit(1);
237 /* vim: set ts=8 sw=8 noet cindent ft=c.doxygen: */