s3-spoolssd: Register spoolssd endpoints.
[Samba.git] / source3 / printing / spoolssd.c
blob94694fc284181b39114a86ba1bc6e66d8b35a11e
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 "serverid.h"
22 #include "librpc/gen_ndr/messaging.h"
23 #include "include/printing.h"
24 #include "printing/nt_printing_migrate.h"
25 #include "librpc/gen_ndr/srv_winreg.h"
26 #include "librpc/gen_ndr/srv_spoolss.h"
27 #include "rpc_server/rpc_server.h"
28 #include "rpc_server/rpc_ep_setup.h"
30 #define SPOOLSS_PIPE_NAME "spoolss"
31 #define DAEMON_NAME "spoolssd"
33 void start_spoolssd(struct tevent_context *ev_ctx,
34 struct messaging_context *msg_ctx);
36 static void spoolss_reopen_logs(void)
38 char *lfile = lp_logfile();
39 int rc;
41 if (lfile == NULL || lfile[0] == '\0') {
42 rc = asprintf(&lfile, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME);
43 if (rc > 0) {
44 lp_set_logfile(lfile);
45 SAFE_FREE(lfile);
47 } else {
48 if (strstr(lfile, DAEMON_NAME) == NULL) {
49 rc = asprintf(&lfile, "%s.%s", lp_logfile(), DAEMON_NAME);
50 if (rc > 0) {
51 lp_set_logfile(lfile);
52 SAFE_FREE(lfile);
57 reopen_logs();
60 static void smb_conf_updated(struct messaging_context *msg,
61 void *private_data,
62 uint32_t msg_type,
63 struct server_id server_id,
64 DATA_BLOB *data)
66 struct tevent_context *ev_ctx = talloc_get_type_abort(private_data,
67 struct tevent_context);
69 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
70 change_to_root_user();
71 reload_printers(ev_ctx, msg);
72 spoolss_reopen_logs();
75 static void spoolss_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 exit_server_cleanly("termination signal");
85 static void spoolss_setup_sig_term_handler(struct tevent_context *ev_ctx)
87 struct tevent_signal *se;
89 se = tevent_add_signal(ev_ctx,
90 ev_ctx,
91 SIGTERM, 0,
92 spoolss_sig_term_handler,
93 NULL);
94 if (!se) {
95 exit_server("failed to setup SIGTERM handler");
99 static void spoolss_sig_hup_handler(struct tevent_context *ev,
100 struct tevent_signal *se,
101 int signum,
102 int count,
103 void *siginfo,
104 void *private_data)
106 struct messaging_context *msg_ctx = talloc_get_type_abort(private_data,
107 struct messaging_context);
109 change_to_root_user();
110 DEBUG(1,("Reloading printers after SIGHUP\n"));
111 reload_printers(ev, msg_ctx);
112 spoolss_reopen_logs();
115 static void spoolss_setup_sig_hup_handler(struct tevent_context *ev_ctx,
116 struct messaging_context *msg_ctx)
118 struct tevent_signal *se;
120 se = tevent_add_signal(ev_ctx,
121 ev_ctx,
122 SIGHUP, 0,
123 spoolss_sig_hup_handler,
124 msg_ctx);
125 if (!se) {
126 exit_server("failed to setup SIGHUP handler");
130 static bool spoolss_init_cb(void *ptr)
132 struct messaging_context *msg_ctx = talloc_get_type_abort(
133 ptr, struct messaging_context);
135 return nt_printing_tdb_migrate(msg_ctx);
138 static bool spoolss_shutdown_cb(void *ptr)
140 srv_spoolss_cleanup();
142 return true;
145 void start_spoolssd(struct tevent_context *ev_ctx,
146 struct messaging_context *msg_ctx)
148 struct rpc_srv_callbacks spoolss_cb;
149 pid_t pid;
150 NTSTATUS status;
151 int ret;
153 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
155 pid = sys_fork();
157 if (pid == -1) {
158 DEBUG(0, ("Failed to fork SPOOLSS [%s], aborting ...\n",
159 strerror(errno)));
160 exit(1);
163 if (pid) {
164 /* parent */
165 return;
168 /* child */
169 close_low_fds(false);
171 status = reinit_after_fork(msg_ctx,
172 ev_ctx,
173 procid_self(), true);
174 if (!NT_STATUS_IS_OK(status)) {
175 DEBUG(0,("reinit_after_fork() failed\n"));
176 smb_panic("reinit_after_fork() failed");
179 spoolss_reopen_logs();
181 spoolss_setup_sig_term_handler(ev_ctx);
182 spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
184 if (!serverid_register(procid_self(),
185 FLAG_MSG_GENERAL|FLAG_MSG_SMBD
186 |FLAG_MSG_PRINT_GENERAL)) {
187 exit(1);
190 if (!locking_init()) {
191 exit(1);
194 messaging_register(msg_ctx, NULL,
195 MSG_PRINTER_UPDATE, print_queue_receive);
196 messaging_register(msg_ctx, ev_ctx,
197 MSG_SMB_CONF_UPDATED, smb_conf_updated);
200 * Initialize spoolss with an init function to convert printers first.
201 * static_init_rpc will try to initialize the spoolss server too but you
202 * can't register it twice.
204 spoolss_cb.init = spoolss_init_cb;
205 spoolss_cb.shutdown = spoolss_shutdown_cb;
206 spoolss_cb.private_data = msg_ctx;
208 status = rpc_winreg_init(NULL);
209 if (!NT_STATUS_IS_OK(status)) {
210 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
211 nt_errstr(status)));
212 exit(1);
215 status = rpc_spoolss_init(&spoolss_cb);
216 if (!NT_STATUS_IS_OK(status)) {
217 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
218 nt_errstr(status)));
219 exit(1);
222 if (!setup_named_pipe_socket(SPOOLSS_PIPE_NAME, ev_ctx)) {
223 exit(1);
226 status = rpc_ep_setup_register(ev_ctx, msg_ctx, &ndr_table_spoolss, NULL, 0);
227 if (!NT_STATUS_IS_OK(status)) {
228 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
229 nt_errstr(status)));
230 exit(1);
233 DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
235 /* loop forever */
236 ret = tevent_loop_wait(ev_ctx);
238 /* should not be reached */
239 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
240 ret, (ret == 0) ? "out of events" : strerror(errno)));
241 exit(1);