s3: RPC: Don't crash on trying to talloc_free(-1) if smb_iconv_open_ex() fails.
[Samba.git] / source3 / rpc_server / epmd.c
blob3e3bc6b4049505b9050428f58e480a3a890b39e8
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 #undef DBGC_CLASS
35 #define DBGC_CLASS DBGC_RPC_SRV
37 #define DAEMON_NAME "epmd"
39 static void epmd_reopen_logs(void)
41 const struct loadparm_substitution *lp_sub =
42 loadparm_s3_global_substitution();
43 char *lfile = lp_logfile(talloc_tos(), lp_sub);
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(), lp_sub), 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 exit_server_cleanly("termination signal");
87 static void epmd_setup_sig_term_handler(struct tevent_context *ev_ctx)
89 struct tevent_signal *se;
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");
101 static 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)
108 change_to_root_user();
110 DEBUG(1,("Reloading printers after SIGHUP\n"));
111 epmd_reopen_logs();
114 static void epmd_setup_sig_hup_handler(struct tevent_context *ev_ctx,
115 struct messaging_context *msg_ctx)
117 struct tevent_signal *se;
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");
129 static bool epmapper_shutdown_cb(void *ptr) {
130 srv_epmapper_cleanup();
132 return true;
135 void start_epmd(struct tevent_context *ev_ctx,
136 struct messaging_context *msg_ctx)
138 struct rpc_srv_callbacks epmapper_cb;
139 NTSTATUS status;
140 pid_t pid;
141 int rc;
143 epmapper_cb.init = NULL;
144 epmapper_cb.shutdown = epmapper_shutdown_cb;
145 epmapper_cb.private_data = NULL;
147 DEBUG(1, ("Forking Endpoint Mapper Daemon\n"));
149 pid = fork();
151 if (pid == -1) {
152 DEBUG(0, ("Failed to fork Endpoint Mapper [%s], aborting ...\n",
153 strerror(errno)));
154 exit(1);
157 if (pid) {
158 /* parent */
159 return;
162 status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true, "epmd");
163 if (!NT_STATUS_IS_OK(status)) {
164 DEBUG(0,("reinit_after_fork() failed\n"));
165 smb_panic("reinit_after_fork() failed");
168 epmd_reopen_logs();
170 epmd_setup_sig_term_handler(ev_ctx);
171 epmd_setup_sig_hup_handler(ev_ctx, msg_ctx);
173 messaging_register(msg_ctx,
174 ev_ctx,
175 MSG_SMB_CONF_UPDATED,
176 epmd_smb_conf_updated);
178 status = rpc_epmapper_init(&epmapper_cb);
179 if (!NT_STATUS_IS_OK(status)) {
180 DEBUG(0, ("Failed to register epmd rpc interface! (%s)\n",
181 nt_errstr(status)));
182 exit(1);
185 status = dcesrv_setup_ncacn_ip_tcp_sockets(ev_ctx,
186 msg_ctx,
187 &ndr_table_epmapper,
188 NULL,
189 135);
190 if (!NT_STATUS_IS_OK(status)) {
191 DEBUG(0, ("Failed to open epmd tcpip sockets!\n"));
192 exit(1);
195 status = dcesrv_setup_ncalrpc_socket(ev_ctx,
196 msg_ctx,
197 "EPMAPPER",
198 srv_epmapper_delete_endpoints,
199 NULL);
200 if (!NT_STATUS_IS_OK(status)) {
201 DEBUG(0, ("Failed to open epmd ncalrpc pipe!\n"));
202 exit(1);
205 status = dcesrv_setup_ncacn_np_socket("epmapper", ev_ctx, msg_ctx);
206 if (!NT_STATUS_IS_OK(status)) {
207 DEBUG(0, ("Failed to open epmd named pipe!\n"));
208 exit(1);
211 DEBUG(1, ("Endpoint Mapper Daemon Started (%u)\n", (unsigned int)getpid()));
213 /* loop forever */
214 rc = tevent_loop_wait(ev_ctx);
216 /* should not be reached */
217 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
218 rc, (rc == 0) ? "out of events" : strerror(errno)));
220 exit(1);
223 /* vim: set ts=8 sw=8 noet cindent ft=c.doxygen: */