build: Remove sys_opendir wrapper
[kai/samba.git] / source3 / rpc_server / epmd.c
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
36 void start_epmd(struct tevent_context *ev_ctx,
37                 struct messaging_context *msg_ctx);
38
39 static void epmd_reopen_logs(void)
40 {
41         char *lfile = lp_logfile();
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", lp_logfile(), DAEMON_NAME);
53                         if (rc > 0) {
54                                 lp_set_logfile(lfile);
55                                 SAFE_FREE(lfile);
56                         }
57                 }
58         }
59
60         reopen_logs();
61 }
62
63 static void epmd_smb_conf_updated(struct messaging_context *msg,
64                                   void *private_data,
65                                   uint32_t msg_type,
66                                   struct server_id server_id,
67                                   DATA_BLOB *data)
68 {
69         DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
70         change_to_root_user();
71         epmd_reopen_logs();
72 }
73
74 static void epmd_sig_term_handler(struct tevent_context *ev,
75                                   struct tevent_signal *se,
76                                   int signum,
77                                   int count,
78                                   void *siginfo,
79                                   void *private_data)
80 {
81         rpc_epmapper_shutdown();
82
83         exit_server_cleanly("termination signal");
84 }
85
86 static void epmd_setup_sig_term_handler(struct tevent_context *ev_ctx)
87 {
88         struct tevent_signal *se;
89
90         se = tevent_add_signal(ev_ctx,
91                                ev_ctx,
92                                SIGTERM, 0,
93                                epmd_sig_term_handler,
94                                NULL);
95         if (se == NULL) {
96                 exit_server("failed to setup SIGTERM handler");
97         }
98 }
99
100 static void epmd_sig_hup_handler(struct tevent_context *ev,
101                                     struct tevent_signal *se,
102                                     int signum,
103                                     int count,
104                                     void *siginfo,
105                                     void *private_data)
106 {
107         change_to_root_user();
108
109         DEBUG(1,("Reloading printers after SIGHUP\n"));
110         epmd_reopen_logs();
111 }
112
113 static void epmd_setup_sig_hup_handler(struct tevent_context *ev_ctx,
114                                        struct messaging_context *msg_ctx)
115 {
116         struct tevent_signal *se;
117
118         se = tevent_add_signal(ev_ctx,
119                                ev_ctx,
120                                SIGHUP, 0,
121                                epmd_sig_hup_handler,
122                                msg_ctx);
123         if (se == NULL) {
124                 exit_server("failed to setup SIGHUP handler");
125         }
126 }
127
128 static bool epmapper_shutdown_cb(void *ptr) {
129         srv_epmapper_cleanup();
130
131         return true;
132 }
133
134 void start_epmd(struct tevent_context *ev_ctx,
135                 struct messaging_context *msg_ctx)
136 {
137         struct rpc_srv_callbacks epmapper_cb;
138         NTSTATUS status;
139         pid_t pid;
140         bool ok;
141         int rc;
142
143         epmapper_cb.init = NULL;
144         epmapper_cb.shutdown = epmapper_shutdown_cb;
145         epmapper_cb.private_data = NULL;
146
147         DEBUG(1, ("Forking Endpoint Mapper Daemon\n"));
148
149         pid = fork();
150
151         if (pid == -1) {
152                 DEBUG(0, ("Failed to fork Endpoint Mapper [%s], aborting ...\n",
153                           strerror(errno)));
154                 exit(1);
155         }
156
157         if (pid) {
158                 /* parent */
159                 return;
160         }
161
162         status = reinit_after_fork(msg_ctx,
163                                    ev_ctx,
164                                    true);
165         if (!NT_STATUS_IS_OK(status)) {
166                 DEBUG(0,("reinit_after_fork() failed\n"));
167                 smb_panic("reinit_after_fork() failed");
168         }
169
170         epmd_reopen_logs();
171
172         epmd_setup_sig_term_handler(ev_ctx);
173         epmd_setup_sig_hup_handler(ev_ctx, msg_ctx);
174
175         ok = serverid_register(procid_self(),
176                                FLAG_MSG_GENERAL |
177                                FLAG_MSG_PRINT_GENERAL);
178         if (!ok) {
179                 DEBUG(0, ("Failed to register serverid in epmd!\n"));
180                 exit(1);
181         }
182
183         messaging_register(msg_ctx,
184                            ev_ctx,
185                            MSG_SMB_CONF_UPDATED,
186                            epmd_smb_conf_updated);
187
188         status = rpc_epmapper_init(&epmapper_cb);
189         if (!NT_STATUS_IS_OK(status)) {
190                 DEBUG(0, ("Failed to register epmd rpc inteface! (%s)\n",
191                           nt_errstr(status)));
192                 exit(1);
193         }
194
195         status = rpc_setup_tcpip_sockets(ev_ctx,
196                                          msg_ctx,
197                                          &ndr_table_epmapper,
198                                          NULL,
199                                          135);
200         if (!NT_STATUS_IS_OK(status)) {
201                 DEBUG(0, ("Failed to open epmd tcpip sockets!\n"));
202                 exit(1);
203         }
204
205         ok = setup_dcerpc_ncalrpc_socket(ev_ctx,
206                                          msg_ctx,
207                                          "EPMAPPER",
208                                          srv_epmapper_delete_endpoints);
209         if (!ok) {
210                 DEBUG(0, ("Failed to open epmd ncalrpc pipe!\n"));
211                 exit(1);
212         }
213
214         ok = setup_named_pipe_socket("epmapper", ev_ctx, msg_ctx);
215         if (!ok) {
216                 DEBUG(0, ("Failed to open epmd named pipe!\n"));
217                 exit(1);
218         }
219
220         DEBUG(1, ("Endpoint Mapper Daemon Started (%d)\n", getpid()));
221
222         /* loop forever */
223         rc = tevent_loop_wait(ev_ctx);
224
225         /* should not be reached */
226         DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
227                  rc, (rc == 0) ? "out of events" : strerror(errno)));
228
229         exit(1);
230 }
231
232 /* vim: set ts=8 sw=8 noet cindent ft=c.doxygen: */