python/samba: Another object.next() to next(object) py2/py3 converstion
[Samba.git] / source3 / printing / spoolssd.c
blobca1eda918790dc22233031def09eb0d3ca6c1fd5
1 /*
2 Unix SMB/Netbios implementation.
3 SPOOLSS Daemon
4 Copyright (C) Simo Sorce <idra@samba.org> 2010-2011
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 "smbd/smbd.h"
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 "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/rpc_config.h"
34 #include "rpc_server/spoolss/srv_spoolss_nt.h"
35 #include "librpc/rpc/dcerpc_ep.h"
36 #include "lib/server_prefork.h"
37 #include "lib/server_prefork_util.h"
39 #define SPOOLSS_PIPE_NAME "spoolss"
40 #define DAEMON_NAME "spoolssd"
42 static struct server_id parent_id;
43 static struct prefork_pool *spoolss_pool = NULL;
44 static int spoolss_child_id = 0;
46 static struct pf_daemon_config default_pf_spoolss_cfg = {
47 .prefork_status = PFH_INIT,
48 .min_children = 5,
49 .max_children = 25,
50 .spawn_rate = 5,
51 .max_allowed_clients = 100,
52 .child_min_life = 60 /* 1 minute minimum life time */
54 static struct pf_daemon_config pf_spoolss_cfg = { 0 };
56 pid_t start_spoolssd(struct tevent_context *ev_ctx,
57 struct messaging_context *msg_ctx);
59 static void spoolss_reopen_logs(int child_id)
61 char *lfile = lp_logfile(talloc_tos());
62 char *ext;
63 int rc;
65 if (child_id) {
66 rc = asprintf(&ext, "%s.%d", DAEMON_NAME, child_id);
67 } else {
68 rc = asprintf(&ext, "%s", DAEMON_NAME);
71 if (rc == -1) {
72 return;
75 rc = 0;
76 if (lfile == NULL || lfile[0] == '\0') {
77 rc = asprintf(&lfile, "%s/log.%s",
78 get_dyn_LOGFILEBASE(), ext);
79 } else {
80 if (strstr(lfile, ext) == NULL) {
81 if (child_id) {
82 rc = asprintf(&lfile, "%s.%d",
83 lp_logfile(talloc_tos()),
84 child_id);
85 } else {
86 rc = asprintf(&lfile, "%s.%s",
87 lp_logfile(talloc_tos()),
88 ext);
93 if (rc > 0) {
94 lp_set_logfile(lfile);
95 SAFE_FREE(lfile);
98 SAFE_FREE(ext);
100 reopen_logs();
103 static void update_conf(struct tevent_context *ev,
104 struct messaging_context *msg)
106 change_to_root_user();
107 lp_load_global(get_dyn_CONFIGFILE());
108 load_printers();
110 spoolss_reopen_logs(spoolss_child_id);
111 if (spoolss_child_id == 0) {
112 pfh_daemon_config(DAEMON_NAME,
113 &pf_spoolss_cfg,
114 &default_pf_spoolss_cfg);
115 pfh_manage_pool(ev, msg, &pf_spoolss_cfg, spoolss_pool);
119 static void smb_conf_updated(struct messaging_context *msg,
120 void *private_data,
121 uint32_t msg_type,
122 struct server_id server_id,
123 DATA_BLOB *data)
125 struct tevent_context *ev_ctx = talloc_get_type_abort(private_data,
126 struct tevent_context);
128 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
129 update_conf(ev_ctx, msg);
132 static void spoolss_sig_term_handler(struct tevent_context *ev,
133 struct tevent_signal *se,
134 int signum,
135 int count,
136 void *siginfo,
137 void *private_data)
139 exit_server_cleanly("termination signal");
142 static void spoolss_setup_sig_term_handler(struct tevent_context *ev_ctx)
144 struct tevent_signal *se;
146 se = tevent_add_signal(ev_ctx,
147 ev_ctx,
148 SIGTERM, 0,
149 spoolss_sig_term_handler,
150 NULL);
151 if (!se) {
152 exit_server("failed to setup SIGTERM handler");
156 static void spoolss_sig_hup_handler(struct tevent_context *ev,
157 struct tevent_signal *se,
158 int signum,
159 int count,
160 void *siginfo,
161 void *pvt)
163 struct messaging_context *msg_ctx;
165 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
167 DEBUG(1,("Reloading printers after SIGHUP\n"));
168 update_conf(ev, msg_ctx);
170 /* relay to all children */
171 if (spoolss_pool) {
172 prefork_send_signal_to_all(spoolss_pool, SIGHUP);
176 static void spoolss_setup_sig_hup_handler(struct tevent_context *ev_ctx,
177 struct messaging_context *msg_ctx)
179 struct tevent_signal *se;
181 se = tevent_add_signal(ev_ctx,
182 ev_ctx,
183 SIGHUP, 0,
184 spoolss_sig_hup_handler,
185 msg_ctx);
186 if (!se) {
187 exit_server("failed to setup SIGHUP handler");
191 static bool spoolss_init_cb(void *ptr)
193 struct messaging_context *msg_ctx = talloc_get_type_abort(
194 ptr, struct messaging_context);
196 return nt_printing_tdb_migrate(msg_ctx);
199 static bool spoolss_shutdown_cb(void *ptr)
201 srv_spoolss_cleanup();
203 return true;
206 /* Children */
208 static void spoolss_chld_sig_hup_handler(struct tevent_context *ev,
209 struct tevent_signal *se,
210 int signum,
211 int count,
212 void *siginfo,
213 void *pvt)
215 change_to_root_user();
216 DEBUG(1,("Reloading printers after SIGHUP\n"));
217 load_printers();
218 spoolss_reopen_logs(spoolss_child_id);
221 static bool spoolss_setup_chld_hup_handler(struct tevent_context *ev_ctx,
222 struct messaging_context *msg_ctx,
223 struct pf_worker_data *pf)
225 struct tevent_signal *se;
227 se = tevent_add_signal(ev_ctx,
228 ev_ctx,
229 SIGHUP, 0,
230 spoolss_chld_sig_hup_handler,
231 msg_ctx);
232 if (!se) {
233 DEBUG(1, ("failed to setup SIGHUP handler"));
234 return false;
237 return true;
240 static void parent_ping(struct messaging_context *msg_ctx,
241 void *private_data,
242 uint32_t msg_type,
243 struct server_id server_id,
244 DATA_BLOB *data)
247 /* The fact we received this message is enough to let make the event
248 * loop if it was idle. spoolss_children_main will cycle through
249 * spoolss_next_client at least once. That function will take whatever
250 * action is necessary */
252 DEBUG(10, ("Got message that the parent changed status.\n"));
253 return;
256 static bool spoolss_child_init(struct tevent_context *ev_ctx,
257 int child_id, struct pf_worker_data *pf)
259 NTSTATUS status;
260 struct rpc_srv_callbacks spoolss_cb;
261 struct messaging_context *msg_ctx = server_messaging_context();
262 bool ok;
264 status = reinit_after_fork(msg_ctx, ev_ctx, true, "spoolssd-child");
265 if (!NT_STATUS_IS_OK(status)) {
266 DEBUG(0,("reinit_after_fork() failed\n"));
267 smb_panic("reinit_after_fork() failed");
270 spoolss_child_id = child_id;
271 spoolss_reopen_logs(child_id);
273 ok = spoolss_setup_chld_hup_handler(ev_ctx, msg_ctx, pf);
274 if (!ok) {
275 return false;
278 if (!locking_init()) {
279 return false;
282 messaging_register(msg_ctx, ev_ctx,
283 MSG_SMB_CONF_UPDATED, smb_conf_updated);
284 messaging_register(msg_ctx, ev_ctx,
285 MSG_PREFORK_PARENT_EVENT, parent_ping);
287 /* As soon as messaging is up check if pcap has been loaded already.
288 * If so then we probably missed a message and should load_printers()
289 * ourselves. If pcap has not been loaded yet, then ignore, we will get
290 * a message as soon as the bq process completes the reload. */
291 if (pcap_cache_loaded(NULL)) {
292 load_printers();
295 /* try to reinit rpc queues */
296 spoolss_cb.init = spoolss_init_cb;
297 spoolss_cb.shutdown = spoolss_shutdown_cb;
298 spoolss_cb.private_data = msg_ctx;
300 status = rpc_winreg_init(NULL);
301 if (!NT_STATUS_IS_OK(status)) {
302 DEBUG(0, ("Failed to register winreg rpc interface! (%s)\n",
303 nt_errstr(status)));
304 return false;
307 status = rpc_spoolss_init(&spoolss_cb);
308 if (!NT_STATUS_IS_OK(status)) {
309 DEBUG(0, ("Failed to register spoolss rpc interface! (%s)\n",
310 nt_errstr(status)));
311 return false;
314 return true;
317 struct spoolss_children_data {
318 struct tevent_context *ev_ctx;
319 struct messaging_context *msg_ctx;
320 struct pf_worker_data *pf;
321 int listen_fd_size;
322 int *listen_fds;
325 static void spoolss_next_client(void *pvt);
327 static int spoolss_children_main(struct tevent_context *ev_ctx,
328 struct messaging_context *msg_ctx,
329 struct pf_worker_data *pf,
330 int child_id,
331 int listen_fd_size,
332 int *listen_fds,
333 void *private_data)
335 struct spoolss_children_data *data;
336 bool ok;
337 int ret = 0;
339 ok = spoolss_child_init(ev_ctx, child_id, pf);
340 if (!ok) {
341 return 1;
344 data = talloc(ev_ctx, struct spoolss_children_data);
345 if (!data) {
346 return 1;
348 data->pf = pf;
349 data->ev_ctx = ev_ctx;
350 data->msg_ctx = msg_ctx;
351 data->listen_fd_size = listen_fd_size;
352 data->listen_fds = listen_fds;
354 /* loop until it is time to exit */
355 while (pf->status != PF_WORKER_EXITING) {
356 /* try to see if it is time to schedule the next client */
357 spoolss_next_client(data);
359 ret = tevent_loop_once(ev_ctx);
360 if (ret != 0) {
361 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
362 ret, strerror(errno)));
363 pf->status = PF_WORKER_EXITING;
367 return ret;
370 static void spoolss_client_terminated(void *pvt)
372 struct spoolss_children_data *data;
374 data = talloc_get_type_abort(pvt, struct spoolss_children_data);
376 pfh_client_terminated(data->pf);
378 spoolss_next_client(pvt);
381 struct spoolss_new_client {
382 struct spoolss_children_data *data;
383 struct tsocket_address *srv_addr;
384 struct tsocket_address *cli_addr;
387 static void spoolss_handle_client(struct tevent_req *req);
389 static void spoolss_next_client(void *pvt)
391 struct tevent_req *req;
392 struct spoolss_children_data *data;
393 struct spoolss_new_client *next;
395 data = talloc_get_type_abort(pvt, struct spoolss_children_data);
397 if (!pfh_child_allowed_to_accept(data->pf)) {
398 /* nothing to do for now we are already listening
399 * or we are not allowed to listen further */
400 return;
403 next = talloc_zero(data, struct spoolss_new_client);
404 if (!next) {
405 DEBUG(1, ("Out of memory!?\n"));
406 return;
408 next->data = data;
410 req = prefork_listen_send(next, data->ev_ctx, data->pf,
411 data->listen_fd_size,
412 data->listen_fds);
413 if (!req) {
414 DEBUG(1, ("Failed to make listening request!?\n"));
415 talloc_free(next);
416 return;
418 tevent_req_set_callback(req, spoolss_handle_client, next);
421 static void spoolss_handle_client(struct tevent_req *req)
423 struct spoolss_children_data *data;
424 struct spoolss_new_client *client;
425 const DATA_BLOB ping = data_blob_null;
426 int ret;
427 int sd;
429 client = tevent_req_callback_data(req, struct spoolss_new_client);
430 data = client->data;
432 ret = prefork_listen_recv(req, client, &sd,
433 &client->srv_addr, &client->cli_addr);
435 /* this will free the request too */
436 talloc_free(client);
438 if (ret != 0) {
439 DEBUG(6, ("No client connection was available after all!\n"));
440 return;
443 /* Warn parent that our status changed */
444 messaging_send(data->msg_ctx, parent_id,
445 MSG_PREFORK_CHILD_EVENT, &ping);
447 DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
448 (int)(data->pf->pid)));
450 named_pipe_accept_function(data->ev_ctx, data->msg_ctx,
451 SPOOLSS_PIPE_NAME, sd,
452 spoolss_client_terminated, data);
455 /* ==== Main Process Functions ==== */
457 extern pid_t background_lpq_updater_pid;
458 static char *bq_logfile;
460 static void check_updater_child(struct tevent_context *ev_ctx,
461 struct messaging_context *msg_ctx)
463 int status;
464 pid_t pid;
466 if (background_lpq_updater_pid == -1) {
467 return;
470 pid = waitpid(background_lpq_updater_pid, &status, WNOHANG);
471 if (pid > 0) {
472 DEBUG(2, ("The background queue child died... Restarting!\n"));
473 pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
474 background_lpq_updater_pid = pid;
478 static void child_ping(struct messaging_context *msg_ctx,
479 void *private_data,
480 uint32_t msg_type,
481 struct server_id server_id,
482 DATA_BLOB *data)
484 struct tevent_context *ev_ctx;
486 ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
488 DEBUG(10, ("Got message that a child changed status.\n"));
489 pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
492 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
493 struct messaging_context *msg_ctx,
494 struct timeval current_time);
495 static void spoolssd_check_children(struct tevent_context *ev_ctx,
496 struct tevent_timer *te,
497 struct timeval current_time,
498 void *pvt);
500 static void spoolssd_sigchld_handler(struct tevent_context *ev_ctx,
501 struct prefork_pool *pfp,
502 void *pvt)
504 struct messaging_context *msg_ctx;
506 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
508 /* run pool management so we can fork/retire or increase
509 * the allowed connections per child based on load */
510 pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
512 /* also check if the updater child is alive and well */
513 check_updater_child(ev_ctx, msg_ctx);
516 static bool spoolssd_setup_children_monitor(struct tevent_context *ev_ctx,
517 struct messaging_context *msg_ctx)
519 bool ok;
521 /* add our oun sigchld callback */
522 prefork_set_sigchld_callback(spoolss_pool,
523 spoolssd_sigchld_handler, msg_ctx);
525 ok = spoolssd_schedule_check(ev_ctx, msg_ctx,
526 tevent_timeval_current());
527 return ok;
530 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
531 struct messaging_context *msg_ctx,
532 struct timeval current_time)
534 struct tevent_timer *te;
535 struct timeval next_event;
537 /* check situation again in 10 seconds */
538 next_event = tevent_timeval_current_ofs(10, 0);
540 /* TODO: check when the socket becomes readable, so that children
541 * are checked only when there is some activity ? */
542 te = tevent_add_timer(ev_ctx, spoolss_pool, next_event,
543 spoolssd_check_children, msg_ctx);
544 if (!te) {
545 DEBUG(2, ("Failed to set up children monitoring!\n"));
546 return false;
549 return true;
552 static void spoolssd_check_children(struct tevent_context *ev_ctx,
553 struct tevent_timer *te,
554 struct timeval current_time,
555 void *pvt)
557 struct messaging_context *msg_ctx;
559 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
561 pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
563 spoolssd_schedule_check(ev_ctx, msg_ctx, current_time);
566 static void print_queue_forward(struct messaging_context *msg,
567 void *private_data,
568 uint32_t msg_type,
569 struct server_id server_id,
570 DATA_BLOB *data)
572 messaging_send_buf(msg, pid_to_procid(background_lpq_updater_pid),
573 MSG_PRINTER_UPDATE, data->data, data->length);
576 static char *get_bq_logfile(void)
578 char *lfile = lp_logfile(talloc_tos());
579 int rc;
581 if (lfile == NULL || lfile[0] == '\0') {
582 rc = asprintf(&lfile, "%s/log.%s.bq",
583 get_dyn_LOGFILEBASE(), DAEMON_NAME);
584 } else {
585 rc = asprintf(&lfile, "%s.bq", lp_logfile(talloc_tos()));
587 if (rc == -1) {
588 lfile = NULL;
590 return lfile;
593 pid_t start_spoolssd(struct tevent_context *ev_ctx,
594 struct messaging_context *msg_ctx)
596 enum rpc_service_mode_e epm_mode = rpc_epmapper_mode();
597 struct rpc_srv_callbacks spoolss_cb;
598 struct dcerpc_binding_vector *v;
599 TALLOC_CTX *mem_ctx;
600 pid_t pid;
601 NTSTATUS status;
602 int listen_fd;
603 int ret;
604 bool ok;
606 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
609 * Block signals before forking child as it will have to
610 * set its own handlers. Child will re-enable SIGHUP as
611 * soon as the handlers are set up.
613 BlockSignals(true, SIGTERM);
614 BlockSignals(true, SIGHUP);
616 pid = fork();
618 if (pid == -1) {
619 DEBUG(0, ("Failed to fork SPOOLSS [%s]\n",
620 strerror(errno)));
623 /* parent or error */
624 if (pid != 0) {
626 /* Re-enable SIGHUP before returnig */
627 BlockSignals(false, SIGTERM);
628 BlockSignals(false, SIGHUP);
629 return pid;
632 status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true,
633 "spoolssd-master");
634 if (!NT_STATUS_IS_OK(status)) {
635 DEBUG(0,("reinit_after_fork() failed\n"));
636 smb_panic("reinit_after_fork() failed");
639 /* save the parent process id so the children can use it later */
640 parent_id = messaging_server_id(msg_ctx);
642 spoolss_reopen_logs(0);
643 pfh_daemon_config(DAEMON_NAME,
644 &pf_spoolss_cfg,
645 &default_pf_spoolss_cfg);
647 spoolss_setup_sig_term_handler(ev_ctx);
648 spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
650 BlockSignals(false, SIGTERM);
651 BlockSignals(false, SIGHUP);
653 /* always start the backgroundqueue listner in spoolssd */
654 bq_logfile = get_bq_logfile();
655 pid = start_background_queue(ev_ctx, msg_ctx, bq_logfile);
656 if (pid > 0) {
657 background_lpq_updater_pid = pid;
660 /* the listening fd must be created before the children are actually
661 * forked out. */
662 listen_fd = create_named_pipe_socket(SPOOLSS_PIPE_NAME);
663 if (listen_fd == -1) {
664 exit(1);
667 ret = listen(listen_fd, pf_spoolss_cfg.max_allowed_clients);
668 if (ret == -1) {
669 DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
670 strerror(errno)));
671 exit(1);
674 /* start children before any more initialization is done */
675 ok = prefork_create_pool(ev_ctx, /* mem_ctx */
676 ev_ctx, msg_ctx,
677 1, &listen_fd,
678 pf_spoolss_cfg.min_children,
679 pf_spoolss_cfg.max_children,
680 &spoolss_children_main, NULL,
681 &spoolss_pool);
682 if (!ok) {
683 exit(1);
686 if (!locking_init()) {
687 exit(1);
690 messaging_register(msg_ctx, ev_ctx,
691 MSG_SMB_CONF_UPDATED, smb_conf_updated);
692 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
693 print_queue_forward);
694 messaging_register(msg_ctx, ev_ctx,
695 MSG_PREFORK_CHILD_EVENT, child_ping);
698 * As soon as messaging is up check if pcap has been loaded already.
699 * If pcap has not been loaded yet, then ignore, as we will reload on
700 * client enumeration anyway.
702 if (pcap_cache_loaded(NULL)) {
703 load_printers();
706 mem_ctx = talloc_new(NULL);
707 if (mem_ctx == NULL) {
708 exit(1);
712 * Initialize spoolss with an init function to convert printers first.
713 * static_init_rpc will try to initialize the spoolss server too but you
714 * can't register it twice.
716 spoolss_cb.init = spoolss_init_cb;
717 spoolss_cb.shutdown = spoolss_shutdown_cb;
718 spoolss_cb.private_data = msg_ctx;
720 status = rpc_winreg_init(NULL);
721 if (!NT_STATUS_IS_OK(status)) {
722 DEBUG(0, ("Failed to register winreg rpc interface! (%s)\n",
723 nt_errstr(status)));
724 exit(1);
727 status = rpc_spoolss_init(&spoolss_cb);
728 if (!NT_STATUS_IS_OK(status)) {
729 DEBUG(0, ("Failed to register spoolss rpc interface! (%s)\n",
730 nt_errstr(status)));
731 exit(1);
734 if (epm_mode != RPC_SERVICE_MODE_DISABLED &&
735 (lp_parm_bool(-1, "rpc_server", "register_embedded_np", false))) {
736 status = dcerpc_binding_vector_new(mem_ctx, &v);
737 if (!NT_STATUS_IS_OK(status)) {
738 DEBUG(0, ("Failed to create binding vector (%s)\n",
739 nt_errstr(status)));
740 exit(1);
743 status = dcerpc_binding_vector_add_np_default(&ndr_table_spoolss, v);
744 if (!NT_STATUS_IS_OK(status)) {
745 DEBUG(0, ("Failed to add np to binding vector (%s)\n",
746 nt_errstr(status)));
747 exit(1);
750 status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_spoolss, v);
751 if (!NT_STATUS_IS_OK(status)) {
752 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
753 nt_errstr(status)));
754 exit(1);
758 talloc_free(mem_ctx);
760 ok = spoolssd_setup_children_monitor(ev_ctx, msg_ctx);
761 if (!ok) {
762 DEBUG(0, ("Failed to setup children monitoring!\n"));
763 exit(1);
766 DEBUG(1, ("SPOOLSS Daemon Started (%u)\n", (unsigned int)getpid()));
768 pfh_manage_pool(ev_ctx, msg_ctx, &pf_spoolss_cfg, spoolss_pool);
770 /* loop forever */
771 ret = tevent_loop_wait(ev_ctx);
773 /* should not be reached */
774 DEBUG(0,("spoolssd tevent_loop_wait() exited with %d - %s\n",
775 ret, (ret == 0) ? "out of events" : strerror(errno)));
776 exit(1);