s3-spoolssd: Pass down event and messanging context.
[Samba.git] / source3 / printing / spoolssd.c
blobf8328eb92bb2d6aac0be04ead82e3b605b89b8a8
1 /*
2 Unix SMB/Netbios implementation.
3 SPOOLSS Daemon
4 Copyright (C) Simo Sorce 2010
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "includes.h"
20 #include "librpc/gen_ndr/messaging.h"
21 #include "include/printing.h"
22 #include "printing/nt_printing_migrate.h"
23 #include "librpc/gen_ndr/srv_winreg.h"
24 #include "librpc/gen_ndr/srv_spoolss.h"
25 #include "rpc_server/rpc_server.h"
27 #define SPOOLSS_PIPE_NAME "spoolss"
28 #define DAEMON_NAME "spoolssd"
30 void start_spoolssd(struct tevent_context *ev_ctx,
31 struct messaging_context *msg_ctx);
33 static void spoolss_reopen_logs(void)
35 char *lfile = lp_logfile();
36 int rc;
38 if (strstr(lfile, DAEMON_NAME) == NULL) {
39 rc = asprintf(&lfile, "%s.%s", lp_logfile(), DAEMON_NAME);
40 if (rc > 0) {
41 lp_set_logfile(lfile);
42 SAFE_FREE(lfile);
45 reopen_logs();
48 static void smb_conf_updated(struct messaging_context *msg,
49 void *private_data,
50 uint32_t msg_type,
51 struct server_id server_id,
52 DATA_BLOB *data)
54 struct tevent_context *ev_ctx = talloc_get_type_abort(private_data,
55 struct tevent_context);
57 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
58 change_to_root_user();
59 reload_printers(ev_ctx, msg);
60 spoolss_reopen_logs();
63 static void spoolss_sig_term_handler(struct tevent_context *ev,
64 struct tevent_signal *se,
65 int signum,
66 int count,
67 void *siginfo,
68 void *private_data)
70 exit_server_cleanly("termination signal");
73 static void spoolss_setup_sig_term_handler(struct tevent_context *ev_ctx)
75 struct tevent_signal *se;
77 se = tevent_add_signal(ev_ctx,
78 ev_ctx,
79 SIGTERM, 0,
80 spoolss_sig_term_handler,
81 NULL);
82 if (!se) {
83 exit_server("failed to setup SIGTERM handler");
87 static void spoolss_sig_hup_handler(struct tevent_context *ev,
88 struct tevent_signal *se,
89 int signum,
90 int count,
91 void *siginfo,
92 void *private_data)
94 struct messaging_context *msg_ctx = talloc_get_type_abort(private_data,
95 struct messaging_context);
97 change_to_root_user();
98 DEBUG(1,("Reloading printers after SIGHUP\n"));
99 reload_printers(ev, msg_ctx);
100 spoolss_reopen_logs();
103 static void spoolss_setup_sig_hup_handler(struct tevent_context *ev_ctx,
104 struct messaging_context *msg_ctx)
106 struct tevent_signal *se;
108 se = tevent_add_signal(ev_ctx,
109 ev_ctx,
110 SIGHUP, 0,
111 spoolss_sig_hup_handler,
112 msg_ctx);
113 if (!se) {
114 exit_server("failed to setup SIGHUP handler");
118 static bool spoolss_init_cb(void *ptr)
120 struct messaging_context *msg_ctx = talloc_get_type_abort(
121 ptr, struct messaging_context);
123 return nt_printing_tdb_migrate(msg_ctx);
126 static bool spoolss_shutdown_cb(void *ptr)
128 srv_spoolss_cleanup();
130 return true;
133 void start_spoolssd(struct tevent_context *ev_ctx,
134 struct messaging_context *msg_ctx)
136 struct rpc_srv_callbacks spoolss_cb;
137 pid_t pid;
138 NTSTATUS status;
139 int ret;
141 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
143 pid = sys_fork();
145 if (pid == -1) {
146 DEBUG(0, ("Failed to fork SPOOLSS [%s], aborting ...\n",
147 strerror(errno)));
148 exit(1);
151 if (pid) {
152 /* parent */
153 return;
156 /* child */
157 close_low_fds(false);
159 status = reinit_after_fork(msg_ctx,
160 ev_ctx,
161 procid_self(), true);
162 if (!NT_STATUS_IS_OK(status)) {
163 DEBUG(0,("reinit_after_fork() failed\n"));
164 smb_panic("reinit_after_fork() failed");
167 spoolss_reopen_logs();
169 spoolss_setup_sig_term_handler(ev_ctx);
170 spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
172 if (!serverid_register(procid_self(),
173 FLAG_MSG_GENERAL|FLAG_MSG_SMBD
174 |FLAG_MSG_PRINT_GENERAL)) {
175 exit(1);
178 if (!locking_init()) {
179 exit(1);
182 messaging_register(msg_ctx, NULL,
183 MSG_PRINTER_UPDATE, print_queue_receive);
184 messaging_register(msg_ctx, ev_ctx,
185 MSG_SMB_CONF_UPDATED, smb_conf_updated);
188 * Initialize spoolss with an init function to convert printers first.
189 * static_init_rpc will try to initialize the spoolss server too but you
190 * can't register it twice.
192 spoolss_cb.init = spoolss_init_cb;
193 spoolss_cb.shutdown = spoolss_shutdown_cb;
194 spoolss_cb.private_data = msg_ctx;
196 status = rpc_winreg_init(NULL);
197 if (!NT_STATUS_IS_OK(status)) {
198 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
199 nt_errstr(status)));
200 exit(1);
203 status = rpc_spoolss_init(&spoolss_cb);
204 if (!NT_STATUS_IS_OK(status)) {
205 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
206 nt_errstr(status)));
207 exit(1);
210 if (!setup_named_pipe_socket(SPOOLSS_PIPE_NAME, ev_ctx)) {
211 exit(1);
214 DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
216 /* loop forever */
217 ret = tevent_loop_wait(ev_ctx);
219 /* should not be reached */
220 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
221 ret, (ret == 0) ? "out of events" : strerror(errno)));
222 exit(1);