s3-spoolss: Fix printers related messaging
[Samba.git] / source3 / printing / spoolssd.c
blob77846c28ae931a0306258fa0505aebe28ef7e7aa
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"
21 #include "smbd/smbd.h"
23 #include "messages.h"
24 #include "include/printing.h"
25 #include "printing/nt_printing_migrate_internal.h"
26 #include "printing/queue_process.h"
27 #include "printing/pcap.h"
28 #include "ntdomain.h"
29 #include "librpc/gen_ndr/srv_winreg.h"
30 #include "librpc/gen_ndr/srv_spoolss.h"
31 #include "rpc_server/rpc_server.h"
32 #include "rpc_server/rpc_ep_register.h"
33 #include "rpc_server/spoolss/srv_spoolss_nt.h"
34 #include "librpc/rpc/dcerpc_ep.h"
35 #include "lib/server_prefork.h"
37 #define SPOOLSS_PIPE_NAME "spoolss"
38 #define DAEMON_NAME "spoolssd"
40 #define SPOOLSS_MIN_CHILDREN 5
41 #define SPOOLSS_MAX_CHILDREN 25
42 #define SPOOLSS_SPAWN_RATE 5
43 #define SPOOLSS_MIN_LIFE 60 /* 1 minute minimum life time */
45 #define SPOOLSS_INIT 0x00
46 #define SPOOLSS_NEW_MAX 0x01
47 #define SPOLLSS_ENOSPC 0x02
49 static struct prefork_pool *spoolss_pool;
50 static int spoolss_min_children;
51 static int spoolss_max_children;
52 static int spoolss_spawn_rate;
53 static int spoolss_prefork_status;
54 static int spoolss_child_id = 0;
56 static void spoolss_prefork_config(void)
58 static int spoolss_prefork_config_init = false;
59 const char *prefork_str;
60 int min, max, rate;
61 bool use_defaults = false;
62 int ret;
64 if (!spoolss_prefork_config_init) {
65 spoolss_pool = NULL;
66 spoolss_prefork_status = SPOOLSS_INIT;
67 spoolss_min_children = 0;
68 spoolss_max_children = 0;
69 spoolss_spawn_rate = 0;
70 spoolss_prefork_config_init = true;
73 prefork_str = lp_parm_const_string(GLOBAL_SECTION_SNUM,
74 "spoolssd", "prefork", "none");
75 if (strcmp(prefork_str, "none") == 0) {
76 use_defaults = true;
77 } else {
78 ret = sscanf(prefork_str, "%d:%d:%d", &min, &max, &rate);
79 if (ret != 3) {
80 DEBUG(0, ("invalid format for spoolssd:prefork!\n"));
81 use_defaults = true;
85 if (use_defaults) {
86 min = SPOOLSS_MIN_CHILDREN;
87 max = SPOOLSS_MAX_CHILDREN;
88 rate = SPOOLSS_SPAWN_RATE;
91 if (max > spoolss_max_children && spoolss_max_children != 0) {
92 spoolss_prefork_status |= SPOOLSS_NEW_MAX;
95 spoolss_min_children = min;
96 spoolss_max_children = max;
97 spoolss_spawn_rate = rate;
100 static void spoolss_reopen_logs(int child_id)
102 char *lfile = lp_logfile();
103 char *ext;
104 int rc;
106 if (child_id) {
107 rc = asprintf(&ext, ".%s.%d", DAEMON_NAME, child_id);
108 } else {
109 rc = asprintf(&ext, ".%s", DAEMON_NAME);
112 if (rc == -1) {
113 /* if we can't allocate, set it to NULL
114 * and logging will flow in the original file */
115 ext = NULL;
118 rc = 0;
119 if (lfile == NULL || lfile[0] == '\0') {
120 rc = asprintf(&lfile, "%s/log%s",
121 get_dyn_LOGFILEBASE(), ext?ext:"");
122 } else {
123 if (ext && strstr(lfile, ext) == NULL) {
124 if (strstr(lfile, DAEMON_NAME) == NULL) {
125 rc = asprintf(&lfile, "%s%s",
126 lp_logfile(), ext?ext:"");
127 } else {
128 rc = asprintf(&lfile, "%s.%d",
129 lp_logfile(), child_id);
134 if (rc > 0) {
135 lp_set_logfile(lfile);
136 SAFE_FREE(lfile);
139 SAFE_FREE(ext);
141 reopen_logs();
144 static void update_conf(struct tevent_context *ev,
145 struct messaging_context *msg)
147 change_to_root_user();
148 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
149 reload_printers(ev, msg);
151 spoolss_reopen_logs(spoolss_child_id);
152 if (spoolss_child_id == 0) {
153 spoolss_prefork_config();
157 static void smb_conf_updated(struct messaging_context *msg,
158 void *private_data,
159 uint32_t msg_type,
160 struct server_id server_id,
161 DATA_BLOB *data)
163 struct tevent_context *ev_ctx = talloc_get_type_abort(private_data,
164 struct tevent_context);
166 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
167 update_conf(ev_ctx, msg);
170 static void update_pcap(struct tevent_context *ev_ctx,
171 struct messaging_context *msg_ctx)
173 change_to_root_user();
174 reload_printers(ev_ctx, msg_ctx);
177 static void pcap_updated(struct messaging_context *msg,
178 void *private_data,
179 uint32_t msg_type,
180 struct server_id server_id,
181 DATA_BLOB *data)
183 struct tevent_context *ev_ctx;
185 ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
187 DEBUG(10, ("Got message that pcap updated. Reloading.\n"));
188 update_pcap(ev_ctx, msg);
191 static void spoolss_sig_term_handler(struct tevent_context *ev,
192 struct tevent_signal *se,
193 int signum,
194 int count,
195 void *siginfo,
196 void *private_data)
198 exit_server_cleanly("termination signal");
201 static void spoolss_setup_sig_term_handler(struct tevent_context *ev_ctx)
203 struct tevent_signal *se;
205 se = tevent_add_signal(ev_ctx,
206 ev_ctx,
207 SIGTERM, 0,
208 spoolss_sig_term_handler,
209 NULL);
210 if (!se) {
211 exit_server("failed to setup SIGTERM handler");
215 static void spoolss_sig_hup_handler(struct tevent_context *ev,
216 struct tevent_signal *se,
217 int signum,
218 int count,
219 void *siginfo,
220 void *pvt)
222 struct messaging_context *msg_ctx;
224 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
226 DEBUG(1,("Reloading printers after SIGHUP\n"));
227 update_conf(ev, msg_ctx);
229 /* relay to all children */
230 if (spoolss_pool) {
231 prefork_send_signal_to_all(spoolss_pool, SIGHUP);
235 static void spoolss_setup_sig_hup_handler(struct tevent_context *ev_ctx,
236 struct messaging_context *msg_ctx)
238 struct tevent_signal *se;
240 se = tevent_add_signal(ev_ctx,
241 ev_ctx,
242 SIGHUP, 0,
243 spoolss_sig_hup_handler,
244 msg_ctx);
245 if (!se) {
246 exit_server("failed to setup SIGHUP handler");
250 static bool spoolss_init_cb(void *ptr)
252 struct messaging_context *msg_ctx = talloc_get_type_abort(
253 ptr, struct messaging_context);
255 return nt_printing_tdb_migrate(msg_ctx);
258 static bool spoolss_shutdown_cb(void *ptr)
260 srv_spoolss_cleanup();
262 return true;
265 /* Children */
267 struct spoolss_chld_sig_hup_ctx {
268 struct messaging_context *msg_ctx;
269 struct pf_worker_data *pf;
272 static void spoolss_chld_sig_hup_handler(struct tevent_context *ev,
273 struct tevent_signal *se,
274 int signum,
275 int count,
276 void *siginfo,
277 void *pvt)
279 struct spoolss_chld_sig_hup_ctx *shc;
281 shc = talloc_get_type_abort(pvt, struct spoolss_chld_sig_hup_ctx);
283 /* avoid wasting CPU cycles if we are going to exit soon anyways */
284 if (shc->pf != NULL &&
285 shc->pf->cmds == PF_SRV_MSG_EXIT) {
286 return;
289 change_to_root_user();
290 DEBUG(1,("Reloading printers after SIGHUP\n"));
291 reload_printers(ev, shc->msg_ctx);
292 spoolss_reopen_logs(spoolss_child_id);
295 static bool spoolss_setup_chld_hup_handler(struct tevent_context *ev_ctx,
296 struct messaging_context *msg_ctx,
297 struct pf_worker_data *pf)
299 struct spoolss_chld_sig_hup_ctx *shc;
300 struct tevent_signal *se;
302 shc = talloc(ev_ctx, struct spoolss_chld_sig_hup_ctx);
303 if (!shc) {
304 DEBUG(1, ("failed to setup SIGHUP handler"));
305 return false;
307 shc->pf = pf;
308 shc->msg_ctx = msg_ctx;
310 se = tevent_add_signal(ev_ctx,
311 ev_ctx,
312 SIGHUP, 0,
313 spoolss_chld_sig_hup_handler,
314 shc);
315 if (!se) {
316 DEBUG(1, ("failed to setup SIGHUP handler"));
317 return false;
320 return true;
323 static bool spoolss_child_init(struct tevent_context *ev_ctx,
324 int child_id, struct pf_worker_data *pf)
326 NTSTATUS status;
327 struct rpc_srv_callbacks spoolss_cb;
328 struct messaging_context *msg_ctx = server_messaging_context();
329 bool ok;
331 status = reinit_after_fork(msg_ctx, ev_ctx,
332 procid_self(), true);
333 if (!NT_STATUS_IS_OK(status)) {
334 DEBUG(0,("reinit_after_fork() failed\n"));
335 smb_panic("reinit_after_fork() failed");
338 spoolss_child_id = child_id;
339 spoolss_reopen_logs(child_id);
341 ok = spoolss_setup_chld_hup_handler(ev_ctx, msg_ctx, pf);
342 if (!ok) {
343 return false;
346 if (!serverid_register(procid_self(),
347 FLAG_MSG_GENERAL |
348 FLAG_MSG_PRINT_NOTIFY |
349 FLAG_MSG_PRINT_GENERAL)) {
350 return false;
353 if (!locking_init()) {
354 return false;
357 messaging_register(msg_ctx, ev_ctx,
358 MSG_SMB_CONF_UPDATED, smb_conf_updated);
359 messaging_register(msg_ctx, ev_ctx, MSG_PRINTER_PCAP,
360 pcap_updated);
362 /* try to reinit rpc queues */
363 spoolss_cb.init = spoolss_init_cb;
364 spoolss_cb.shutdown = spoolss_shutdown_cb;
365 spoolss_cb.private_data = msg_ctx;
367 status = rpc_winreg_init(NULL);
368 if (!NT_STATUS_IS_OK(status)) {
369 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
370 nt_errstr(status)));
371 return false;
374 status = rpc_spoolss_init(&spoolss_cb);
375 if (!NT_STATUS_IS_OK(status)) {
376 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
377 nt_errstr(status)));
378 return false;
381 pcap_cache_reload(ev_ctx, msg_ctx, &update_pcap);
383 return true;
386 struct spoolss_children_data {
387 struct tevent_context *ev_ctx;
388 struct messaging_context *msg_ctx;
389 struct pf_worker_data *pf;
390 int listen_fd_size;
391 int *listen_fds;
392 int lock_fd;
394 bool listening;
397 static void spoolss_next_client(void *pvt);
399 static int spoolss_children_main(struct tevent_context *ev_ctx,
400 struct messaging_context *msg_ctx,
401 struct pf_worker_data *pf,
402 int child_id,
403 int listen_fd_size,
404 int *listen_fds,
405 int lock_fd,
406 void *private_data)
408 struct spoolss_children_data *data;
409 bool ok;
410 int ret;
412 ok = spoolss_child_init(ev_ctx, child_id, pf);
413 if (!ok) {
414 return 1;
417 data = talloc(ev_ctx, struct spoolss_children_data);
418 if (!data) {
419 return 1;
421 data->pf = pf;
422 data->ev_ctx = ev_ctx;
423 data->msg_ctx = msg_ctx;
424 data->lock_fd = lock_fd;
425 data->listen_fd_size = listen_fd_size;
426 data->listen_fds = listen_fds;
427 data->listening = false;
429 /* loop until it is time to exit */
430 while (pf->status != PF_WORKER_EXITING) {
431 /* try to see if it is time to schedule the next client */
432 spoolss_next_client(data);
434 ret = tevent_loop_once(ev_ctx);
435 if (ret != 0) {
436 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
437 ret, strerror(errno)));
438 pf->status = PF_WORKER_EXITING;
442 return ret;
445 static void spoolss_client_terminated(void *pvt)
447 struct spoolss_children_data *data;
449 data = talloc_get_type_abort(pvt, struct spoolss_children_data);
451 if (data->pf->num_clients) {
452 data->pf->num_clients--;
453 } else {
454 DEBUG(2, ("Invalid num clients, aborting!\n"));
455 data->pf->status = PF_WORKER_EXITING;
456 return;
459 spoolss_next_client(pvt);
462 struct spoolss_new_client {
463 struct spoolss_children_data *data;
464 struct tsocket_address *srv_addr;
465 struct tsocket_address *cli_addr;
468 static void spoolss_handle_client(struct tevent_req *req);
470 static void spoolss_next_client(void *pvt)
472 struct tevent_req *req;
473 struct spoolss_children_data *data;
474 struct spoolss_new_client *next;
476 data = talloc_get_type_abort(pvt, struct spoolss_children_data);
478 if (data->pf->num_clients == 0) {
479 data->pf->status = PF_WORKER_IDLE;
482 if (data->pf->cmds == PF_SRV_MSG_EXIT) {
483 DEBUG(2, ("Parent process commands we terminate!\n"));
484 return;
487 if (data->listening ||
488 data->pf->num_clients >= data->pf->allowed_clients) {
489 /* nothing to do for now we are already listening
490 * or reached the number of clients we are allowed
491 * to handle in parallel */
492 return;
495 next = talloc_zero(data, struct spoolss_new_client);
496 if (!next) {
497 DEBUG(1, ("Out of memory!?\n"));
498 return;
500 next->data = data;
502 req = prefork_listen_send(next, data->ev_ctx, data->pf,
503 data->listen_fd_size,
504 data->listen_fds,
505 data->lock_fd);
506 if (!req) {
507 DEBUG(1, ("Failed to make listening request!?\n"));
508 talloc_free(next);
509 return;
511 tevent_req_set_callback(req, spoolss_handle_client, next);
513 data->listening = true;
516 static void spoolss_handle_client(struct tevent_req *req)
518 struct spoolss_children_data *data;
519 struct spoolss_new_client *client;
520 int ret;
521 int sd;
523 client = tevent_req_callback_data(req, struct spoolss_new_client);
524 data = client->data;
526 ret = prefork_listen_recv(req, client, &sd,
527 &client->srv_addr, &client->cli_addr);
529 /* this will free the request too */
530 talloc_free(client);
531 /* we are done listening */
532 data->listening = false;
534 if (ret > 0) {
535 DEBUG(1, ("Failed to accept client connection!\n"));
536 /* bail out if we are not serving any other client */
537 if (data->pf->num_clients == 0) {
538 data->pf->status = PF_WORKER_EXITING;
540 return;
543 if (ret == -2) {
544 DEBUG(1, ("Server asks us to die!\n"));
545 data->pf->status = PF_WORKER_EXITING;
546 return;
549 DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
550 (int)(data->pf->pid)));
552 named_pipe_accept_function(data->ev_ctx, data->msg_ctx,
553 SPOOLSS_PIPE_NAME, sd,
554 spoolss_client_terminated, data);
557 /* ==== Main Process Functions ==== */
559 extern pid_t background_lpq_updater_pid;
560 static char *bq_logfile;
562 static void check_updater_child(void)
564 int status;
565 pid_t pid;
567 if (background_lpq_updater_pid == -1) {
568 return;
571 pid = sys_waitpid(background_lpq_updater_pid, &status, WNOHANG);
572 if (pid > 0) {
573 DEBUG(2, ("The background queue child died... Restarting!\n"));
574 pid = start_background_queue(server_event_context(),
575 server_messaging_context(),
576 bq_logfile);
577 background_lpq_updater_pid = pid;
581 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
582 struct messaging_context *msg_ctx,
583 struct timeval current_time);
584 static void spoolssd_check_children(struct tevent_context *ev_ctx,
585 struct tevent_timer *te,
586 struct timeval current_time,
587 void *pvt);
589 static void spoolssd_sigchld_handler(struct tevent_context *ev_ctx,
590 struct prefork_pool *pfp,
591 void *pvt)
593 struct messaging_context *msg_ctx;
594 int active, total;
595 int n, r;
597 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
599 /* now check we do not descend below the minimum */
600 active = prefork_count_active_children(pfp, &total);
602 n = 0;
603 if (total < spoolss_min_children) {
604 n = total - spoolss_min_children;
605 } else if (total - active < (total / 4)) {
606 n = spoolss_min_children;
609 if (n > 0) {
610 r = prefork_add_children(ev_ctx, msg_ctx, pfp, n);
611 if (r < n) {
612 DEBUG(10, ("Tried to start %d children but only,"
613 "%d were actually started.!\n", n, r));
617 /* also check if the updater child is alive and well */
618 check_updater_child();
621 static bool spoolssd_setup_children_monitor(struct tevent_context *ev_ctx,
622 struct messaging_context *msg_ctx)
624 bool ok;
626 /* add our oun sigchld callback */
627 prefork_set_sigchld_callback(spoolss_pool,
628 spoolssd_sigchld_handler, msg_ctx);
630 ok = spoolssd_schedule_check(ev_ctx, msg_ctx,
631 tevent_timeval_current());
632 return ok;
635 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
636 struct messaging_context *msg_ctx,
637 struct timeval current_time)
639 struct tevent_timer *te;
640 struct timeval next_event;
642 /* check situation again in 10 seconds */
643 next_event = tevent_timeval_current_ofs(10, 0);
645 /* TODO: check when the socket becomes readable, so that children
646 * are checked only when there is some activity ? */
647 te = tevent_add_timer(ev_ctx, spoolss_pool, next_event,
648 spoolssd_check_children, msg_ctx);
649 if (!te) {
650 DEBUG(2, ("Failed to set up children monitoring!\n"));
651 return false;
654 return true;
657 static void spoolssd_check_children(struct tevent_context *ev_ctx,
658 struct tevent_timer *te,
659 struct timeval current_time,
660 void *pvt)
662 struct messaging_context *msg_ctx;
663 time_t now = time(NULL);
664 int active, total;
665 int ret, n;
667 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
669 if ((spoolss_prefork_status & SPOOLSS_NEW_MAX) &&
670 !(spoolss_prefork_status & SPOLLSS_ENOSPC)) {
671 ret = prefork_expand_pool(spoolss_pool, spoolss_max_children);
672 if (ret == ENOSPC) {
673 spoolss_prefork_status |= SPOLLSS_ENOSPC;
675 spoolss_prefork_status &= ~SPOOLSS_NEW_MAX;
678 active = prefork_count_active_children(spoolss_pool, &total);
680 if (total - active < spoolss_spawn_rate) {
681 n = prefork_add_children(ev_ctx, msg_ctx,
682 spoolss_pool, spoolss_spawn_rate);
683 if (n < spoolss_spawn_rate) {
684 DEBUG(10, ("Tried to start 5 children but only,"
685 "%d were actually started.!\n", n));
689 if (total - active > spoolss_min_children) {
690 if ((total - spoolss_min_children) >= spoolss_spawn_rate) {
691 prefork_retire_children(spoolss_pool,
692 spoolss_spawn_rate,
693 now - SPOOLSS_MIN_LIFE);
697 ret = spoolssd_schedule_check(ev_ctx, msg_ctx, current_time);
700 static void print_queue_forward(struct messaging_context *msg,
701 void *private_data,
702 uint32_t msg_type,
703 struct server_id server_id,
704 DATA_BLOB *data)
706 messaging_send_buf(msg, pid_to_procid(background_lpq_updater_pid),
707 MSG_PRINTER_UPDATE, data->data, data->length);
710 char *get_bq_logfile(void)
712 char *lfile = lp_logfile();
713 int rc;
715 if (lfile == NULL || lfile[0] == '\0') {
716 rc = asprintf(&lfile, "%s/log.%s.bq",
717 get_dyn_LOGFILEBASE(), DAEMON_NAME);
718 } else {
719 rc = asprintf(&lfile, "%s.bq", lp_logfile());
721 if (rc == -1) {
722 lfile = NULL;
724 return lfile;
727 pid_t start_spoolssd(struct tevent_context *ev_ctx,
728 struct messaging_context *msg_ctx)
730 struct rpc_srv_callbacks spoolss_cb;
731 struct dcerpc_binding_vector *v;
732 TALLOC_CTX *mem_ctx;
733 pid_t pid;
734 NTSTATUS status;
735 int listen_fd;
736 int ret;
737 bool ok;
739 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
742 * Block signals before forking child as it will have to
743 * set its own handlers. Child will re-enable SIGHUP as
744 * soon as the handlers are set up.
746 BlockSignals(true, SIGTERM);
747 BlockSignals(true, SIGHUP);
749 pid = sys_fork();
751 if (pid == -1) {
752 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
753 strerror(errno)));
756 /* parent or error */
757 if (pid != 0) {
759 /* Re-enable SIGHUP before returnig */
760 BlockSignals(false, SIGTERM);
761 BlockSignals(false, SIGHUP);
762 return pid;
765 /* child */
766 close_low_fds(false);
768 status = reinit_after_fork(msg_ctx,
769 ev_ctx,
770 procid_self(), true);
771 if (!NT_STATUS_IS_OK(status)) {
772 DEBUG(0,("reinit_after_fork() failed\n"));
773 smb_panic("reinit_after_fork() failed");
776 spoolss_reopen_logs(0);
777 spoolss_prefork_config();
779 spoolss_setup_sig_term_handler(ev_ctx);
780 spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
782 BlockSignals(false, SIGTERM);
783 BlockSignals(false, SIGHUP);
785 /* Publish nt printers, this requires a working winreg pipe */
786 pcap_cache_reload(ev_ctx, msg_ctx, &reload_printers);
788 /* always start the backgroundqueue listner in spoolssd */
789 bq_logfile = get_bq_logfile();
790 pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
791 if (pid > 0) {
792 background_lpq_updater_pid = pid;
795 /* the listening fd must be created before the children are actually
796 * forked out. */
797 listen_fd = create_named_pipe_socket(SPOOLSS_PIPE_NAME);
798 if (listen_fd == -1) {
799 exit(1);
802 ret = listen(listen_fd, spoolss_max_children);
803 if (ret == -1) {
804 DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
805 strerror(errno)));
806 exit(1);
809 /* start children before any more initialization is done */
810 ok = prefork_create_pool(ev_ctx, /* mem_ctx */
811 ev_ctx, msg_ctx,
812 1, &listen_fd,
813 spoolss_min_children,
814 spoolss_max_children,
815 &spoolss_children_main, NULL,
816 &spoolss_pool);
818 if (!serverid_register(procid_self(),
819 FLAG_MSG_GENERAL |
820 FLAG_MSG_SMBD |
821 FLAG_MSG_PRINT_NOTIFY |
822 FLAG_MSG_PRINT_GENERAL)) {
823 exit(1);
826 if (!locking_init()) {
827 exit(1);
830 messaging_register(msg_ctx, ev_ctx,
831 MSG_SMB_CONF_UPDATED, smb_conf_updated);
832 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
833 print_queue_forward);
834 messaging_register(msg_ctx, ev_ctx, MSG_PRINTER_PCAP,
835 pcap_updated);
837 mem_ctx = talloc_new(NULL);
838 if (mem_ctx == NULL) {
839 exit(1);
843 * Initialize spoolss with an init function to convert printers first.
844 * static_init_rpc will try to initialize the spoolss server too but you
845 * can't register it twice.
847 spoolss_cb.init = spoolss_init_cb;
848 spoolss_cb.shutdown = spoolss_shutdown_cb;
849 spoolss_cb.private_data = msg_ctx;
851 status = rpc_winreg_init(NULL);
852 if (!NT_STATUS_IS_OK(status)) {
853 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
854 nt_errstr(status)));
855 exit(1);
858 status = rpc_spoolss_init(&spoolss_cb);
859 if (!NT_STATUS_IS_OK(status)) {
860 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
861 nt_errstr(status)));
862 exit(1);
865 status = dcerpc_binding_vector_new(mem_ctx, &v);
866 if (!NT_STATUS_IS_OK(status)) {
867 DEBUG(0, ("Failed to create binding vector (%s)\n",
868 nt_errstr(status)));
869 exit(1);
872 status = dcerpc_binding_vector_add_np_default(&ndr_table_spoolss, v);
873 if (!NT_STATUS_IS_OK(status)) {
874 DEBUG(0, ("Failed to add np to binding vector (%s)\n",
875 nt_errstr(status)));
876 exit(1);
879 status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_spoolss, v);
880 if (!NT_STATUS_IS_OK(status)) {
881 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
882 nt_errstr(status)));
883 exit(1);
886 talloc_free(mem_ctx);
888 ok = spoolssd_setup_children_monitor(ev_ctx, msg_ctx);
889 if (!ok) {
890 DEBUG(0, ("Failed to setup children monitoring!\n"));
891 exit(1);
894 DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
896 /* loop forever */
897 ret = tevent_loop_wait(ev_ctx);
899 /* should not be reached */
900 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
901 ret, (ret == 0) ? "out of events" : strerror(errno)));
902 exit(1);