1 /*
2 Unix SMB/Netbios implementation.
3 SPOOLSS Daemon
4 Copyright (C) Simo Sorce <idra@samba.org> 2010-2011
5
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.
10
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.
15
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/>.
18 */
19 #include "includes.h"
20 #include "smbd/smbd.h"
21
22 #include "messages.h"
23 #include "include/printing.h"
24 #include "printing/nt_printing_migrate_internal.h"
25 #include "printing/queue_process.h"
26 #include "printing/pcap.h"
27 #include "printing/load.h"
28 #include "printing/spoolssd.h"
29 #include "ntdomain.h"
30 #include "librpc/gen_ndr/srv_winreg.h"
31 #include "librpc/gen_ndr/srv_spoolss.h"
32 #include "rpc_server/rpc_server.h"
33 #include "rpc_server/rpc_ep_register.h"
34 #include "rpc_server/rpc_config.h"
35 #include "rpc_server/spoolss/srv_spoolss_nt.h"
36 #include "librpc/rpc/dcerpc_ep.h"
37 #include "lib/server_prefork.h"
38 #include "lib/server_prefork_util.h"
39
40 #undef DBGC_CLASS
41 #define DBGC_CLASS DBGC_RPC_SRV
42
43 #define SPOOLSS_PIPE_NAME "spoolss"
44 #define DAEMON_NAME "spoolssd"
45
46 static struct server_id parent_id;
47 static struct prefork_pool *spoolss_pool = NULL;
48 static int spoolss_child_id = 0;
49
50 static struct pf_daemon_config default_pf_spoolss_cfg = {
51 .prefork_status = PFH_INIT,
52 .min_children = 5,
53 .max_children = 25,
54 .spawn_rate = 5,
55 .max_allowed_clients = 100,
56 .child_min_life = 60 /* 1 minute minimum life time */
57 };
58 static struct pf_daemon_config pf_spoolss_cfg = { 0 };
59
spoolss_reopen_logs(int child_id)60 static void spoolss_reopen_logs(int child_id)
61 {
62 const struct loadparm_substitution *lp_sub =
63 loadparm_s3_global_substitution();
64 char *lfile = lp_logfile(talloc_tos(), lp_sub);
65 char *ext;
66 int rc;
67
68 if (child_id) {
69 rc = asprintf(&ext, "%s.%d", DAEMON_NAME, child_id);
70 } else {
71 rc = asprintf(&ext, "%s", DAEMON_NAME);
72 }
73
74 if (rc == -1) {
75 return;
76 }
77
78 rc = 0;
79 if (lfile == NULL || lfile[0] == '\0') {
80 rc = asprintf(&lfile, "%s/log.%s",
81 get_dyn_LOGFILEBASE(), ext);
82 } else {
83 if (strstr(lfile, ext) == NULL) {
84 if (child_id) {
85 rc = asprintf(&lfile, "%s.%d",
86 lp_logfile(talloc_tos(), lp_sub),
87 child_id);
88 } else {
89 rc = asprintf(&lfile, "%s.%s",
90 lp_logfile(talloc_tos(), lp_sub),
91 ext);
92 }
93 }
94 }
95
96 if (rc > 0) {
97 lp_set_logfile(lfile);
98 SAFE_FREE(lfile);
99 }
100
101 SAFE_FREE(ext);
102
103 reopen_logs();
104 }
105
update_conf(struct tevent_context * ev,struct messaging_context * msg)106 static void update_conf(struct tevent_context *ev,
107 struct messaging_context *msg)
108 {
109 change_to_root_user();
110 lp_load_global(get_dyn_CONFIGFILE());
111 load_printers();
112
113 spoolss_reopen_logs(spoolss_child_id);
114 if (spoolss_child_id == 0) {
115 pfh_daemon_config(DAEMON_NAME,
116 &pf_spoolss_cfg,
117 &default_pf_spoolss_cfg);
118 pfh_manage_pool(ev, msg, &pf_spoolss_cfg, spoolss_pool);
119 }
120 }
121
smb_conf_updated(struct messaging_context * msg,void * private_data,uint32_t msg_type,struct server_id server_id,DATA_BLOB * data)122 static void smb_conf_updated(struct messaging_context *msg,
123 void *private_data,
124 uint32_t msg_type,
125 struct server_id server_id,
126 DATA_BLOB *data)
127 {
128 struct tevent_context *ev_ctx = talloc_get_type_abort(private_data,
129 struct tevent_context);
130
131 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
132 update_conf(ev_ctx, msg);
133 }
134
spoolss_sig_term_handler(struct tevent_context * ev,struct tevent_signal * se,int signum,int count,void * siginfo,void * private_data)135 static void spoolss_sig_term_handler(struct tevent_context *ev,
136 struct tevent_signal *se,
137 int signum,
138 int count,
139 void *siginfo,
140 void *private_data)
141 {
142 exit_server_cleanly("termination signal");
143 }
144
spoolss_setup_sig_term_handler(struct tevent_context * ev_ctx)145 static void spoolss_setup_sig_term_handler(struct tevent_context *ev_ctx)
146 {
147 struct tevent_signal *se;
148
149 se = tevent_add_signal(ev_ctx,
150 ev_ctx,
151 SIGTERM, 0,
152 spoolss_sig_term_handler,
153 NULL);
154 if (!se) {
155 exit_server("failed to setup SIGTERM handler");
156 }
157 }
158
spoolss_sig_hup_handler(struct tevent_context * ev,struct tevent_signal * se,int signum,int count,void * siginfo,void * pvt)159 static void spoolss_sig_hup_handler(struct tevent_context *ev,
160 struct tevent_signal *se,
161 int signum,
162 int count,
163 void *siginfo,
164 void *pvt)
165 {
166 struct messaging_context *msg_ctx;
167
168 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
169
170 DEBUG(1,("Reloading printers after SIGHUP\n"));
171 update_conf(ev, msg_ctx);
172
173 /* relay to all children */
174 if (spoolss_pool) {
175 prefork_send_signal_to_all(spoolss_pool, SIGHUP);
176 }
177 }
178
spoolss_setup_sig_hup_handler(struct tevent_context * ev_ctx,struct messaging_context * msg_ctx)179 static void spoolss_setup_sig_hup_handler(struct tevent_context *ev_ctx,
180 struct messaging_context *msg_ctx)
181 {
182 struct tevent_signal *se;
183
184 se = tevent_add_signal(ev_ctx,
185 ev_ctx,
186 SIGHUP, 0,
187 spoolss_sig_hup_handler,
188 msg_ctx);
189 if (!se) {
190 exit_server("failed to setup SIGHUP handler");
191 }
192 }
193
spoolss_init_cb(void * ptr)194 static bool spoolss_init_cb(void *ptr)
195 {
196 struct messaging_context *msg_ctx = talloc_get_type_abort(
197 ptr, struct messaging_context);
198
199 return nt_printing_tdb_migrate(msg_ctx);
200 }
201
spoolss_shutdown_cb(void * ptr)202 static bool spoolss_shutdown_cb(void *ptr)
203 {
204 srv_spoolss_cleanup();
205
206 return true;
207 }
208
209 /* Children */
210
spoolss_chld_sig_hup_handler(struct tevent_context * ev,struct tevent_signal * se,int signum,int count,void * siginfo,void * pvt)211 static void spoolss_chld_sig_hup_handler(struct tevent_context *ev,
212 struct tevent_signal *se,
213 int signum,
214 int count,
215 void *siginfo,
216 void *pvt)
217 {
218 change_to_root_user();
219 DEBUG(1,("Reloading printers after SIGHUP\n"));
220 load_printers();
221 spoolss_reopen_logs(spoolss_child_id);
222 }
223
spoolss_setup_chld_hup_handler(struct tevent_context * ev_ctx,struct messaging_context * msg_ctx,struct pf_worker_data * pf)224 static bool spoolss_setup_chld_hup_handler(struct tevent_context *ev_ctx,
225 struct messaging_context *msg_ctx,
226 struct pf_worker_data *pf)
227 {
228 struct tevent_signal *se;
229
230 se = tevent_add_signal(ev_ctx,
231 ev_ctx,
232 SIGHUP, 0,
233 spoolss_chld_sig_hup_handler,
234 msg_ctx);
235 if (!se) {
236 DEBUG(1, ("failed to setup SIGHUP handler"));
237 return false;
238 }
239
240 return true;
241 }
242
parent_ping(struct messaging_context * msg_ctx,void * private_data,uint32_t msg_type,struct server_id server_id,DATA_BLOB * data)243 static void parent_ping(struct messaging_context *msg_ctx,
244 void *private_data,
245 uint32_t msg_type,
246 struct server_id server_id,
247 DATA_BLOB *data)
248 {
249
250 /* The fact we received this message is enough to let make the event
251 * loop if it was idle. spoolss_children_main will cycle through
252 * spoolss_next_client at least once. That function will take whatever
253 * action is necessary */
254
255 DEBUG(10, ("Got message that the parent changed status.\n"));
256 return;
257 }
258
spoolss_child_init(struct tevent_context * ev_ctx,int child_id,struct pf_worker_data * pf)259 static bool spoolss_child_init(struct tevent_context *ev_ctx,
260 int child_id, struct pf_worker_data *pf)
261 {
262 NTSTATUS status;
263 struct rpc_srv_callbacks spoolss_cb;
264 struct messaging_context *msg_ctx = global_messaging_context();
265 bool ok;
266
267 status = reinit_after_fork(msg_ctx, ev_ctx, true, "spoolssd-child");
268 if (!NT_STATUS_IS_OK(status)) {
269 DEBUG(0,("reinit_after_fork() failed\n"));
270 smb_panic("reinit_after_fork() failed");
271 }
272
273 spoolss_child_id = child_id;
274 spoolss_reopen_logs(child_id);
275
276 ok = spoolss_setup_chld_hup_handler(ev_ctx, msg_ctx, pf);
277 if (!ok) {
278 return false;
279 }
280
281 if (!locking_init()) {
282 return false;
283 }
284
285 messaging_register(msg_ctx, ev_ctx,
286 MSG_SMB_CONF_UPDATED, smb_conf_updated);
287 messaging_register(msg_ctx, ev_ctx,
288 MSG_PREFORK_PARENT_EVENT, parent_ping);
289
290 /* As soon as messaging is up check if pcap has been loaded already.
291 * If so then we probably missed a message and should load_printers()
292 * ourselves. If pcap has not been loaded yet, then ignore, we will get
293 * a message as soon as the bq process completes the reload. */
294 load_printers();
295
296 /* try to reinit rpc queues */
297 spoolss_cb.init = spoolss_init_cb;
298 spoolss_cb.shutdown = spoolss_shutdown_cb;
299 spoolss_cb.private_data = msg_ctx;
300
301 status = rpc_winreg_init(NULL);
302 if (!NT_STATUS_IS_OK(status)) {
303 DEBUG(0, ("Failed to register winreg rpc interface! (%s)\n",
304 nt_errstr(status)));
305 return false;
306 }
307
308 status = rpc_spoolss_init(&spoolss_cb);
309 if (!NT_STATUS_IS_OK(status)) {
310 DEBUG(0, ("Failed to register spoolss rpc interface! (%s)\n",
311 nt_errstr(status)));
312 return false;
313 }
314
315 return true;
316 }
317
318 struct spoolss_children_data {
319 struct tevent_context *ev_ctx;
320 struct messaging_context *msg_ctx;
321 struct pf_worker_data *pf;
322 int listen_fd_size;
323 struct pf_listen_fd *listen_fds;
324 };
325
326 static void spoolss_next_client(void *pvt);
327
spoolss_children_main(struct tevent_context * ev_ctx,struct messaging_context * msg_ctx,struct pf_worker_data * pf,int child_id,int listen_fd_size,struct pf_listen_fd * listen_fds,void * private_data)328 static int spoolss_children_main(struct tevent_context *ev_ctx,
329 struct messaging_context *msg_ctx,
330 struct pf_worker_data *pf,
331 int child_id,
332 int listen_fd_size,
333 struct pf_listen_fd *listen_fds,
334 void *private_data)
335 {
336 struct spoolss_children_data *data;
337 bool ok;
338 int ret = 0;
339
340 ok = spoolss_child_init(ev_ctx, child_id, pf);
341 if (!ok) {
342 return 1;
343 }
344
345 data = talloc(ev_ctx, struct spoolss_children_data);
346 if (!data) {
347 return 1;
348 }
349 data->pf = pf;
350 data->ev_ctx = ev_ctx;
351 data->msg_ctx = msg_ctx;
352 data->listen_fd_size = listen_fd_size;
353 data->listen_fds = listen_fds;
354
355 /* loop until it is time to exit */
356 while (pf->status != PF_WORKER_EXITING) {
357 /* try to see if it is time to schedule the next client */
358 spoolss_next_client(data);
359
360 ret = tevent_loop_once(ev_ctx);
361 if (ret != 0) {
362 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
363 ret, strerror(errno)));
364 pf->status = PF_WORKER_EXITING;
365 }
366 }
367
368 return ret;
369 }
370
spoolss_client_terminated(struct pipes_struct * p,void * pvt)371 static void spoolss_client_terminated(struct pipes_struct *p, void *pvt)
372 {
373 struct spoolss_children_data *data;
374
375 data = talloc_get_type_abort(pvt, struct spoolss_children_data);
376
377 pfh_client_terminated(data->pf);
378
379 spoolss_next_client(pvt);
380 }
381
382 struct spoolss_new_client {
383 struct spoolss_children_data *data;
384 };
385
386 static void spoolss_handle_client(struct tevent_req *req);
387
spoolss_next_client(void * pvt)388 static void spoolss_next_client(void *pvt)
389 {
390 struct tevent_req *req;
391 struct spoolss_children_data *data;
392 struct spoolss_new_client *next;
393
394 data = talloc_get_type_abort(pvt, struct spoolss_children_data);
395
396 if (!pfh_child_allowed_to_accept(data->pf)) {
397 /* nothing to do for now we are already listening
398 * or we are not allowed to listen further */
399 return;
400 }
401
402 next = talloc_zero(data, struct spoolss_new_client);
403 if (!next) {
404 DEBUG(1, ("Out of memory!?\n"));
405 return;
406 }
407 next->data = data;
408
409 req = prefork_listen_send(next, data->ev_ctx, data->pf,
410 data->listen_fd_size,
411 data->listen_fds);
412 if (!req) {
413 DEBUG(1, ("Failed to make listening request!?\n"));
414 talloc_free(next);
415 return;
416 }
417 tevent_req_set_callback(req, spoolss_handle_client, next);
418 }
419
spoolss_handle_client(struct tevent_req * req)420 static void spoolss_handle_client(struct tevent_req *req)
421 {
422 struct spoolss_children_data *data;
423 struct spoolss_new_client *client;
424 const DATA_BLOB ping = data_blob_null;
425 int ret;
426 int sd;
427 struct tsocket_address *srv_addr = NULL;
428 struct tsocket_address *cli_addr = NULL;
429
430 client = tevent_req_callback_data(req, struct spoolss_new_client);
431 data = client->data;
432
433 ret = prefork_listen_recv(req, data, &sd, NULL,
434 &srv_addr, &cli_addr);
435
436 /* this will free the request too */
437 talloc_free(client);
438
439 if (ret != 0) {
440 DEBUG(6, ("No client connection was available after all!\n"));
441 return;
442 }
443
444 /* Warn parent that our status changed */
445 messaging_send(data->msg_ctx, parent_id,
446 MSG_PREFORK_CHILD_EVENT, &ping);
447
448 DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
449 (int)(data->pf->pid)));
450
451 dcerpc_ncacn_accept(data->ev_ctx,
452 data->msg_ctx,
453 NCACN_NP,
454 SPOOLSS_PIPE_NAME,
455 cli_addr,
456 srv_addr,
457 sd,
458 spoolss_client_terminated,
459 data);
460 }
461
462 /* ==== Main Process Functions ==== */
463
464 extern pid_t background_lpq_updater_pid;
465 static char *bq_logfile;
466
check_updater_child(struct tevent_context * ev_ctx,struct messaging_context * msg_ctx)467 static void check_updater_child(struct tevent_context *ev_ctx,
468 struct messaging_context *msg_ctx)
469 {
470 int status;
471 pid_t pid;
472
473 if (background_lpq_updater_pid == -1) {
474 return;
475 }
476
477 pid = waitpid(background_lpq_updater_pid, &status, WNOHANG);
478 if (pid > 0) {
479 DEBUG(2, ("The background queue child died... Restarting!\n"));
480 pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
481 background_lpq_updater_pid = pid;
482 }
483 }
484
child_ping(struct messaging_context * msg_ctx,void * private_data,uint32_t msg_type,struct server_id server_id,DATA_BLOB * data)485 static void child_ping(struct messaging_context *msg_ctx,
486 void *private_data,
487 uint32_t msg_type,
488 struct server_id server_id,
489 DATA_BLOB *data)
490 {
491 struct tevent_context *ev_ctx;
492
493 ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
494
495 DEBUG(10, ("Got message that a child changed status.\n"));
496 pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
497 }
498
499 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
500 struct messaging_context *msg_ctx,
501 struct timeval current_time);
502 static void spoolssd_check_children(struct tevent_context *ev_ctx,
503 struct tevent_timer *te,
504 struct timeval current_time,
505 void *pvt);
506
spoolssd_sigchld_handler(struct tevent_context * ev_ctx,struct prefork_pool * pfp,void * pvt)507 static void spoolssd_sigchld_handler(struct tevent_context *ev_ctx,
508 struct prefork_pool *pfp,
509 void *pvt)
510 {
511 struct messaging_context *msg_ctx;
512
513 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
514
515 /* run pool management so we can fork/retire or increase
516 * the allowed connections per child based on load */
517 pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
518
519 /* also check if the updater child is alive and well */
520 check_updater_child(ev_ctx, msg_ctx);
521 }
522
spoolssd_setup_children_monitor(struct tevent_context * ev_ctx,struct messaging_context * msg_ctx)523 static bool spoolssd_setup_children_monitor(struct tevent_context *ev_ctx,
524 struct messaging_context *msg_ctx)
525 {
526 bool ok;
527
528 /* add our oun sigchld callback */
529 prefork_set_sigchld_callback(spoolss_pool,
530 spoolssd_sigchld_handler, msg_ctx);
531
532 ok = spoolssd_schedule_check(ev_ctx, msg_ctx,
533 tevent_timeval_current());
534 return ok;
535 }
536
spoolssd_schedule_check(struct tevent_context * ev_ctx,struct messaging_context * msg_ctx,struct timeval current_time)537 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
538 struct messaging_context *msg_ctx,
539 struct timeval current_time)
540 {
541 struct tevent_timer *te;
542 struct timeval next_event;
543
544 /* check situation again in 10 seconds */
545 next_event = tevent_timeval_current_ofs(10, 0);
546
547 /* TODO: check when the socket becomes readable, so that children
548 * are checked only when there is some activity ? */
549 te = tevent_add_timer(ev_ctx, spoolss_pool, next_event,
550 spoolssd_check_children, msg_ctx);
551 if (!te) {
552 DEBUG(2, ("Failed to set up children monitoring!\n"));
553 return false;
554 }
555
556 return true;
557 }
558
spoolssd_check_children(struct tevent_context * ev_ctx,struct tevent_timer * te,struct timeval current_time,void * pvt)559 static void spoolssd_check_children(struct tevent_context *ev_ctx,
560 struct tevent_timer *te,
561 struct timeval current_time,
562 void *pvt)
563 {
564 struct messaging_context *msg_ctx;
565
566 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
567
568 pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
569
570 spoolssd_schedule_check(ev_ctx, msg_ctx, current_time);
571 }
572
print_queue_forward(struct messaging_context * msg,void * private_data,uint32_t msg_type,struct server_id server_id,DATA_BLOB * data)573 static void print_queue_forward(struct messaging_context *msg,
574 void *private_data,
575 uint32_t msg_type,
576 struct server_id server_id,
577 DATA_BLOB *data)
578 {
579 messaging_send_buf(msg, pid_to_procid(background_lpq_updater_pid),
580 MSG_PRINTER_UPDATE, data->data, data->length);
581 }
582
get_bq_logfile(void)583 static char *get_bq_logfile(void)
584 {
585 const struct loadparm_substitution *lp_sub =
586 loadparm_s3_global_substitution();
587 char *lfile = lp_logfile(talloc_tos(), lp_sub);
588 int rc;
589
590 if (lfile == NULL || lfile[0] == '\0') {
591 rc = asprintf(&lfile, "%s/log.%s.bq",
592 get_dyn_LOGFILEBASE(), DAEMON_NAME);
593 } else {
594 rc = asprintf(&lfile, "%s.bq", lp_logfile(talloc_tos(), lp_sub));
595 }
596 if (rc == -1) {
597 lfile = NULL;
598 }
599 return lfile;
600 }
601
spoolssd_create_sockets(struct tevent_context * ev_ctx,struct messaging_context * msg_ctx,struct pf_listen_fd * listen_fd,int * listen_fd_size)602 static NTSTATUS spoolssd_create_sockets(struct tevent_context *ev_ctx,
603 struct messaging_context *msg_ctx,
604 struct pf_listen_fd *listen_fd,
605 int *listen_fd_size)
606 {
607 struct dcerpc_binding_vector *v = NULL;
608 TALLOC_CTX *tmp_ctx;
609 NTSTATUS status;
610 int fd = -1;
611 int rc;
612 enum rpc_service_mode_e epm_mode = rpc_epmapper_mode();
613
614 tmp_ctx = talloc_stackframe();
615 if (tmp_ctx == NULL) {
616 return NT_STATUS_NO_MEMORY;
617 }
618
619 status = dcerpc_binding_vector_new(tmp_ctx, &v);
620 if (!NT_STATUS_IS_OK(status)) {
621 DBG_ERR("Failed to create binding vector (%s)\n",
622 nt_errstr(status));
623 goto done;
624 }
625
626 status = dcesrv_create_ncacn_np_socket(SPOOLSS_PIPE_NAME, &fd);
627 if (!NT_STATUS_IS_OK(status)) {
628 goto done;
629 }
630
631 rc = listen(fd, pf_spoolss_cfg.max_allowed_clients);
632 if (rc == -1) {
633 DBG_ERR("Failed to listen on spoolss pipe - %s\n",
634 strerror(errno));
635 goto done;
636 }
637 listen_fd[*listen_fd_size].fd = fd;
638 listen_fd[*listen_fd_size].fd_data = NULL;
639 (*listen_fd_size)++;
640 fd = -1;
641
642 if (epm_mode != RPC_SERVICE_MODE_DISABLED &&
643 (lp_parm_bool(-1, "rpc_server", "register_embedded_np", false))) {
644 status = dcerpc_binding_vector_add_np_default(&ndr_table_spoolss, v);
645 if (!NT_STATUS_IS_OK(status)) {
646 DBG_ERR("Failed to add np to binding vector (%s)\n",
647 nt_errstr(status));
648 goto done;
649 }
650
651 status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_spoolss, v);
652 if (!NT_STATUS_IS_OK(status)) {
653 DBG_ERR("Failed to register spoolss endpoint! (%s)\n",
654 nt_errstr(status));
655 goto done;
656 }
657 }
658
659 status = NT_STATUS_OK;
660 done:
661 if (fd != -1) {
662 close(fd);
663 }
664
665 talloc_free(tmp_ctx);
666 return status;
667 }
668
start_spoolssd(struct tevent_context * ev_ctx,struct messaging_context * msg_ctx)669 pid_t start_spoolssd(struct tevent_context *ev_ctx,
670 struct messaging_context *msg_ctx)
671 {
672 struct rpc_srv_callbacks spoolss_cb;
673 pid_t pid;
674 NTSTATUS status;
675 struct pf_listen_fd listen_fds[1];
676 int listen_fds_size = 0;
677 int ret;
678 bool ok;
679
680 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
681
682 /*
683 * Block signals before forking child as it will have to
684 * set its own handlers. Child will re-enable SIGHUP as
685 * soon as the handlers are set up.
686 */
687 BlockSignals(true, SIGTERM);
688 BlockSignals(true, SIGHUP);
689
690 pid = fork();
691
692 if (pid == -1) {
693 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
694 strerror(errno)));
695 exit(1);
696 }
697
698 /* parent or error */
699 if (pid != 0) {
700
701 /* Re-enable SIGHUP before returnig */
702 BlockSignals(false, SIGTERM);
703 BlockSignals(false, SIGHUP);
704 return pid;
705 }
706
707 status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true,
708 "spoolssd-master");
709 if (!NT_STATUS_IS_OK(status)) {
710 DEBUG(0,("reinit_after_fork() failed\n"));
711 smb_panic("reinit_after_fork() failed");
712 }
713
714 /* save the parent process id so the children can use it later */
715 parent_id = messaging_server_id(msg_ctx);
716
717 spoolss_reopen_logs(0);
718 pfh_daemon_config(DAEMON_NAME,
719 &pf_spoolss_cfg,
720 &default_pf_spoolss_cfg);
721
722 spoolss_setup_sig_term_handler(ev_ctx);
723 spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
724
725 BlockSignals(false, SIGTERM);
726 BlockSignals(false, SIGHUP);
727
728 /* always start the backgroundqueue listner in spoolssd */
729 bq_logfile = get_bq_logfile();
730 pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
731 if (pid > 0) {
732 background_lpq_updater_pid = pid;
733 }
734
735 /* the listening fd must be created before the children are actually
736 * forked out. */
737 status = spoolssd_create_sockets(ev_ctx, msg_ctx, listen_fds,
738 &listen_fds_size);
739 if (!NT_STATUS_IS_OK(status)) {
740 DBG_ERR("Failed to create sockets: %s\n",
741 nt_errstr(status));
742 exit(1);
743 }
744
745 /* start children before any more initialization is done */
746 ok = prefork_create_pool(ev_ctx, /* mem_ctx */
747 ev_ctx, msg_ctx,
748 listen_fds_size, listen_fds,
749 pf_spoolss_cfg.min_children,
750 pf_spoolss_cfg.max_children,
751 &spoolss_children_main, NULL,
752 &spoolss_pool);
753 if (!ok) {
754 exit(1);
755 }
756
757 if (!locking_init()) {
758 exit(1);
759 }
760
761 messaging_register(msg_ctx, ev_ctx,
762 MSG_SMB_CONF_UPDATED, smb_conf_updated);
763 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
764 print_queue_forward);
765 messaging_register(msg_ctx, ev_ctx,
766 MSG_PREFORK_CHILD_EVENT, child_ping);
767
768 /*
769 * As soon as messaging is up check if pcap has been loaded already.
770 * If pcap has not been loaded yet, then ignore, as we will reload on
771 * client enumeration anyway.
772 */
773 load_printers();
774
775 /*
776 * Initialize spoolss with an init function to convert printers first.
777 * static_init_rpc will try to initialize the spoolss server too but you
778 * can't register it twice.
779 */
780 spoolss_cb.init = spoolss_init_cb;
781 spoolss_cb.shutdown = spoolss_shutdown_cb;
782 spoolss_cb.private_data = msg_ctx;
783
784 status = rpc_winreg_init(NULL);
785 if (!NT_STATUS_IS_OK(status)) {
786 DEBUG(0, ("Failed to register winreg rpc interface! (%s)\n",
787 nt_errstr(status)));
788 exit(1);
789 }
790
791 status = rpc_spoolss_init(&spoolss_cb);
792 if (!NT_STATUS_IS_OK(status)) {
793 DEBUG(0, ("Failed to register spoolss rpc interface! (%s)\n",
794 nt_errstr(status)));
795 exit(1);
796 }
797
798 ok = spoolssd_setup_children_monitor(ev_ctx, msg_ctx);
799 if (!ok) {
800 DEBUG(0, ("Failed to setup children monitoring!\n"));
801 exit(1);
802 }
803
804 DEBUG(1, ("SPOOLSS Daemon Started (%u)\n", (unsigned int)getpid()));
805
806 pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
807
808 /* loop forever */
809 ret = tevent_loop_wait(ev_ctx);
810
811 /* should not be reached */
812 DEBUG(0,("spoolssd tevent_loop_wait() exited with %d - %s\n",
813 ret, (ret == 0) ? "out of events" : strerror(errno)));
814 exit(1);
815 }
816