s3:smbd: use smbd_reinit_after_fork
[Samba.git] / source3 / rpc_server / fssd.c
blobfc1f630c6bb29dbfe38d370b118ed94aa71ff480
1 /*
2 * File Server Shadow-Copy Daemon
4 * Copyright (C) David Disseldorp 2012-2015
6 * Based on epmd.c:
7 * Copyright (c) 2011 Andreas Schneider <asn@samba.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
25 #include "serverid.h"
26 #include "ntdomain.h"
27 #include "messages.h"
29 #include "librpc/rpc/dcerpc_ep.h"
30 #include "../librpc/gen_ndr/srv_fsrvp.h"
31 #include "rpc_server/rpc_server.h"
32 #include "rpc_server/rpc_sock_helper.h"
33 #include "rpc_server/fss/srv_fss_agent.h"
35 #define DAEMON_NAME "fssd"
37 void start_fssd(struct tevent_context *ev_ctx,
38 struct messaging_context *msg_ctx);
40 static void fssd_reopen_logs(void)
42 char *lfile = lp_logfile(NULL);
43 int rc;
45 if (lfile == NULL || lfile[0] == '\0') {
46 rc = asprintf(&lfile, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME);
47 if (rc > 0) {
48 lp_set_logfile(lfile);
49 SAFE_FREE(lfile);
51 } else {
52 if (strstr(lfile, DAEMON_NAME) == NULL) {
53 rc = asprintf(&lfile, "%s.%s", lp_logfile(NULL), DAEMON_NAME);
54 if (rc > 0) {
55 lp_set_logfile(lfile);
56 SAFE_FREE(lfile);
61 reopen_logs();
64 static void fssd_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)
70 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
71 change_to_root_user();
72 fssd_reopen_logs();
75 static void fssd_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)
82 rpc_FileServerVssAgent_shutdown();
84 exit_server_cleanly("termination signal");
87 static void fssd_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 fssd_sig_term_handler,
95 NULL);
96 if (se == NULL) {
97 exit_server("failed to setup SIGTERM handler");
101 static void fssd_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,("reopening logs after SIGHUP\n"));
111 fssd_reopen_logs();
114 static void fssd_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 fssd_sig_hup_handler,
123 msg_ctx);
124 if (se == NULL) {
125 exit_server("failed to setup SIGHUP handler");
129 static bool fss_shutdown_cb(void *ptr)
131 srv_fssa_cleanup();
132 return true;
135 static bool fss_init_cb(void *ptr)
137 NTSTATUS status;
138 struct messaging_context *msg_ctx;
140 msg_ctx = talloc_get_type_abort(ptr, struct messaging_context);
141 status = srv_fssa_start(msg_ctx);
142 return NT_STATUS_IS_OK(status);
145 void start_fssd(struct tevent_context *ev_ctx,
146 struct messaging_context *msg_ctx)
148 struct rpc_srv_callbacks fss_cb;
149 NTSTATUS status;
150 pid_t pid;
151 bool ok;
152 int rc;
154 fss_cb.init = fss_init_cb;
155 fss_cb.shutdown = fss_shutdown_cb;
156 fss_cb.private_data = msg_ctx;
158 DEBUG(1, ("Forking File Server Shadow-copy Daemon\n"));
160 pid = fork();
162 if (pid == -1) {
163 DEBUG(0, ("failed to fork file server shadow-copy daemon [%s], "
164 "aborting ...\n", strerror(errno)));
165 exit(1);
168 if (pid) {
169 /* parent */
170 return;
173 /* child */
174 status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true);
175 if (!NT_STATUS_IS_OK(status)) {
176 DEBUG(0,("reinit_after_fork() failed\n"));
177 smb_panic("reinit_after_fork() failed");
180 fssd_reopen_logs();
182 fssd_setup_sig_term_handler(ev_ctx);
183 fssd_setup_sig_hup_handler(ev_ctx, msg_ctx);
185 ok = serverid_register(procid_self(),
186 FLAG_MSG_GENERAL |
187 FLAG_MSG_PRINT_GENERAL);
188 if (!ok) {
189 DEBUG(0, ("Failed to register serverid in fssd!\n"));
190 exit(1);
193 messaging_register(msg_ctx,
194 ev_ctx,
195 MSG_SMB_CONF_UPDATED,
196 fssd_smb_conf_updated);
198 status = rpc_FileServerVssAgent_init(&fss_cb);
199 if (!NT_STATUS_IS_OK(status)) {
200 DEBUG(0, ("Failed to register fssd rpc inteface! (%s)\n",
201 nt_errstr(status)));
202 exit(1);
205 /* case is normalized by smbd on connection */
206 ok = setup_named_pipe_socket("fssagentrpc", ev_ctx, msg_ctx);
207 if (!ok) {
208 DEBUG(0, ("Failed to open fssd named pipe!\n"));
209 exit(1);
212 DEBUG(1, ("File Server Shadow-copy Daemon Started (%d)\n", getpid()));
214 /* loop forever */
215 rc = tevent_loop_wait(ev_ctx);
217 /* should not be reached */
218 DEBUG(0,("tevent_loop_wait() exited with %d - %s\n",
219 rc, (rc == 0) ? "out of events" : strerror(errno)));
221 exit(1);