smbdotconf: mark "logfile" with substitution="1"
[Samba.git] / source3 / rpc_server / fssd.c
blob8dc8b0e8e64e0f74d691abdff0aef11c2724acdd
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 "ntdomain.h"
26 #include "messages.h"
28 #include "librpc/rpc/dcerpc_ep.h"
29 #include "../librpc/gen_ndr/srv_fsrvp.h"
30 #include "rpc_server/rpc_server.h"
31 #include "rpc_server/rpc_sock_helper.h"
32 #include "rpc_server/fss/srv_fss_agent.h"
33 #include "rpc_server/fssd.h"
35 #undef DBGC_CLASS
36 #define DBGC_CLASS DBGC_RPC_SRV
38 #define DAEMON_NAME "fssd"
40 void start_fssd(struct tevent_context *ev_ctx,
41 struct messaging_context *msg_ctx);
43 static void fssd_reopen_logs(void)
45 const struct loadparm_substitution *lp_sub =
46 loadparm_s3_global_substitution();
47 char *lfile = lp_logfile(NULL, lp_sub);
48 int rc;
50 if (lfile == NULL || lfile[0] == '\0') {
51 rc = asprintf(&lfile, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME);
52 if (rc > 0) {
53 lp_set_logfile(lfile);
54 SAFE_FREE(lfile);
56 } else {
57 if (strstr(lfile, DAEMON_NAME) == NULL) {
58 rc = asprintf(&lfile, "%s.%s", lp_logfile(NULL, lp_sub), DAEMON_NAME);
59 if (rc > 0) {
60 lp_set_logfile(lfile);
61 SAFE_FREE(lfile);
66 reopen_logs();
69 static void fssd_smb_conf_updated(struct messaging_context *msg,
70 void *private_data,
71 uint32_t msg_type,
72 struct server_id server_id,
73 DATA_BLOB *data)
75 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
76 change_to_root_user();
77 fssd_reopen_logs();
80 static void fssd_sig_term_handler(struct tevent_context *ev,
81 struct tevent_signal *se,
82 int signum,
83 int count,
84 void *siginfo,
85 void *private_data)
87 exit_server_cleanly("termination signal");
90 static void fssd_setup_sig_term_handler(struct tevent_context *ev_ctx)
92 struct tevent_signal *se;
94 se = tevent_add_signal(ev_ctx,
95 ev_ctx,
96 SIGTERM, 0,
97 fssd_sig_term_handler,
98 NULL);
99 if (se == NULL) {
100 exit_server("failed to setup SIGTERM handler");
104 static void fssd_sig_hup_handler(struct tevent_context *ev,
105 struct tevent_signal *se,
106 int signum,
107 int count,
108 void *siginfo,
109 void *private_data)
111 change_to_root_user();
113 DEBUG(1,("reopening logs after SIGHUP\n"));
114 fssd_reopen_logs();
117 static void fssd_setup_sig_hup_handler(struct tevent_context *ev_ctx,
118 struct messaging_context *msg_ctx)
120 struct tevent_signal *se;
122 se = tevent_add_signal(ev_ctx,
123 ev_ctx,
124 SIGHUP, 0,
125 fssd_sig_hup_handler,
126 msg_ctx);
127 if (se == NULL) {
128 exit_server("failed to setup SIGHUP handler");
132 static bool fss_shutdown_cb(void *ptr)
134 srv_fssa_cleanup();
135 return true;
138 static bool fss_init_cb(void *ptr)
140 NTSTATUS status;
141 struct messaging_context *msg_ctx;
143 msg_ctx = talloc_get_type_abort(ptr, struct messaging_context);
144 status = srv_fssa_start(msg_ctx);
145 return NT_STATUS_IS_OK(status);
148 void start_fssd(struct tevent_context *ev_ctx,
149 struct messaging_context *msg_ctx)
151 struct rpc_srv_callbacks fss_cb;
152 NTSTATUS status;
153 pid_t pid;
154 int rc;
156 fss_cb.init = fss_init_cb;
157 fss_cb.shutdown = fss_shutdown_cb;
158 fss_cb.private_data = msg_ctx;
160 DEBUG(1, ("Forking File Server Shadow-copy Daemon\n"));
162 pid = fork();
164 if (pid == -1) {
165 DEBUG(0, ("failed to fork file server shadow-copy daemon [%s], "
166 "aborting ...\n", strerror(errno)));
167 exit(1);
170 if (pid) {
171 /* parent */
172 return;
175 /* child */
176 status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true, NULL);
177 if (!NT_STATUS_IS_OK(status)) {
178 DEBUG(0,("reinit_after_fork() failed\n"));
179 smb_panic("reinit_after_fork() failed");
182 fssd_reopen_logs();
184 fssd_setup_sig_term_handler(ev_ctx);
185 fssd_setup_sig_hup_handler(ev_ctx, msg_ctx);
187 messaging_register(msg_ctx,
188 ev_ctx,
189 MSG_SMB_CONF_UPDATED,
190 fssd_smb_conf_updated);
192 status = rpc_FileServerVssAgent_init(&fss_cb);
193 if (!NT_STATUS_IS_OK(status)) {
194 DEBUG(0, ("Failed to register fssd rpc interface! (%s)\n",
195 nt_errstr(status)));
196 exit(1);
199 /* case is normalized by smbd on connection */
200 status = dcesrv_setup_ncacn_np_socket("fssagentrpc", ev_ctx, msg_ctx);
201 if (!NT_STATUS_IS_OK(status)) {
202 DEBUG(0, ("Failed to open fssd named pipe!\n"));
203 exit(1);
206 DEBUG(1, ("File Server Shadow-copy Daemon Started (%d)\n",
207 (int)getpid()));
209 /* loop forever */
210 rc = tevent_loop_wait(ev_ctx);
212 /* should not be reached */
213 DEBUG(0,("tevent_loop_wait() exited with %d - %s\n",
214 rc, (rc == 0) ? "out of events" : strerror(errno)));
216 exit(1);