fsrvp: add remote snapshot RPC server
[Samba.git] / source3 / rpc_server / fssd.c
blob96556996c44fbf6b657a0f5e8223eab8be2a7aba
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 status = srv_fssa_start();
139 return NT_STATUS_IS_OK(status);
142 void start_fssd(struct tevent_context *ev_ctx,
143 struct messaging_context *msg_ctx)
145 struct rpc_srv_callbacks fss_cb;
146 NTSTATUS status;
147 pid_t pid;
148 bool ok;
149 int rc;
151 fss_cb.init = fss_init_cb;
152 fss_cb.shutdown = fss_shutdown_cb;
153 fss_cb.private_data = NULL;
155 DEBUG(1, ("Forking File Server Shadow-copy Daemon\n"));
157 pid = fork();
159 if (pid == -1) {
160 DEBUG(0, ("failed to fork file server shadow-copy daemon [%s], "
161 "aborting ...\n", strerror(errno)));
162 exit(1);
165 if (pid) {
166 /* parent */
167 return;
170 /* child */
171 status = reinit_after_fork(msg_ctx,
172 ev_ctx,
173 true);
174 if (!NT_STATUS_IS_OK(status)) {
175 DEBUG(0,("reinit_after_fork() failed\n"));
176 smb_panic("reinit_after_fork() failed");
179 fssd_reopen_logs();
181 fssd_setup_sig_term_handler(ev_ctx);
182 fssd_setup_sig_hup_handler(ev_ctx, msg_ctx);
184 ok = serverid_register(procid_self(),
185 FLAG_MSG_GENERAL |
186 FLAG_MSG_PRINT_GENERAL);
187 if (!ok) {
188 DEBUG(0, ("Failed to register serverid in fssd!\n"));
189 exit(1);
192 messaging_register(msg_ctx,
193 ev_ctx,
194 MSG_SMB_CONF_UPDATED,
195 fssd_smb_conf_updated);
197 status = rpc_FileServerVssAgent_init(&fss_cb);
198 if (!NT_STATUS_IS_OK(status)) {
199 DEBUG(0, ("Failed to register fssd rpc inteface! (%s)\n",
200 nt_errstr(status)));
201 exit(1);
204 /* case is normalized by smbd on connection */
205 ok = setup_named_pipe_socket("fssagentrpc", ev_ctx, msg_ctx);
206 if (!ok) {
207 DEBUG(0, ("Failed to open fssd named pipe!\n"));
208 exit(1);
211 DEBUG(1, ("File Server Shadow-copy Daemon Started (%d)\n", getpid()));
213 /* loop forever */
214 rc = tevent_loop_wait(ev_ctx);
216 /* should not be reached */
217 DEBUG(0,("tevent_loop_wait() exited with %d - %s\n",
218 rc, (rc == 0) ? "out of events" : strerror(errno)));
220 exit(1);