2 Unix SMB/Netbios implementation.
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/>.
21 #include "smbd/smbd.h"
24 #include "include/printing.h"
25 #include "printing/nt_printing_migrate_internal.h"
27 #include "librpc/gen_ndr/srv_winreg.h"
28 #include "librpc/gen_ndr/srv_spoolss.h"
29 #include "rpc_server/rpc_server.h"
30 #include "rpc_server/rpc_ep_register.h"
31 #include "rpc_server/spoolss/srv_spoolss_nt.h"
32 #include "librpc/rpc/dcerpc_ep.h"
33 #include "lib/server_prefork.h"
35 #define SPOOLSS_PIPE_NAME "spoolss"
36 #define DAEMON_NAME "spoolssd"
38 #define SPOOLSS_MIN_CHILDREN 5
39 #define SPOOLSS_MAX_CHILDREN 25
40 #define SPOOLSS_SPAWN_RATE 5
41 #define SPOOLSS_MIN_LIFE 60 /* 1 minute minimum life time */
43 static int spoolss_min_children;
44 static int spoolss_max_children;
45 static int spoolss_spawn_rate;
47 static void spoolss_prefork_config(void)
49 const char *prefork_str;
51 bool use_defaults = false;
54 prefork_str = lp_parm_const_string(GLOBAL_SECTION_SNUM,
55 "spoolssd", "prefork", "none");
56 if (strcmp(prefork_str, "none") == 0) {
59 ret = sscanf(prefork_str, "%d:%d:%d", &min, &max, &rate);
61 DEBUG(0, ("invalid format for spoolssd:prefork!\n"));
67 spoolss_min_children = SPOOLSS_MIN_CHILDREN;
68 spoolss_max_children = SPOOLSS_MAX_CHILDREN;
69 spoolss_spawn_rate = SPOOLSS_SPAWN_RATE;
71 spoolss_min_children = min;
72 spoolss_max_children = max;
73 spoolss_spawn_rate = rate;
77 void start_spoolssd(struct tevent_context *ev_ctx,
78 struct messaging_context *msg_ctx);
80 static void spoolss_reopen_logs(void)
82 char *lfile = lp_logfile();
85 if (lfile == NULL || lfile[0] == '\0') {
86 rc = asprintf(&lfile, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME);
88 lp_set_logfile(lfile);
92 if (strstr(lfile, DAEMON_NAME) == NULL) {
93 rc = asprintf(&lfile, "%s.%s", lp_logfile(), DAEMON_NAME);
95 lp_set_logfile(lfile);
104 static void smb_conf_updated(struct messaging_context *msg,
107 struct server_id server_id,
110 struct tevent_context *ev_ctx = talloc_get_type_abort(private_data,
111 struct tevent_context);
113 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
114 change_to_root_user();
115 reload_printers(ev_ctx, msg);
116 spoolss_reopen_logs();
117 spoolss_prefork_config();
120 static void spoolss_sig_term_handler(struct tevent_context *ev,
121 struct tevent_signal *se,
127 exit_server_cleanly("termination signal");
130 static void spoolss_setup_sig_term_handler(struct tevent_context *ev_ctx)
132 struct tevent_signal *se;
134 se = tevent_add_signal(ev_ctx,
137 spoolss_sig_term_handler,
140 exit_server("failed to setup SIGTERM handler");
144 static void spoolss_sig_hup_handler(struct tevent_context *ev,
145 struct tevent_signal *se,
151 struct messaging_context *msg_ctx;
153 msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
155 change_to_root_user();
156 DEBUG(1,("Reloading printers after SIGHUP\n"));
157 reload_printers(ev, msg_ctx);
158 spoolss_reopen_logs();
161 static void spoolss_setup_sig_hup_handler(struct tevent_context *ev_ctx,
162 struct messaging_context *msg_ctx)
164 struct tevent_signal *se;
166 se = tevent_add_signal(ev_ctx,
169 spoolss_sig_hup_handler,
172 exit_server("failed to setup SIGHUP handler");
176 static bool spoolss_init_cb(void *ptr)
178 struct messaging_context *msg_ctx = talloc_get_type_abort(
179 ptr, struct messaging_context);
181 return nt_printing_tdb_migrate(msg_ctx);
184 static bool spoolss_shutdown_cb(void *ptr)
186 srv_spoolss_cleanup();
193 struct spoolss_chld_sig_hup_ctx {
194 struct messaging_context *msg_ctx;
195 struct pf_worker_data *pf;
198 static void spoolss_chld_sig_hup_handler(struct tevent_context *ev,
199 struct tevent_signal *se,
205 struct spoolss_chld_sig_hup_ctx *shc;
207 shc = talloc_get_type_abort(pvt, struct spoolss_chld_sig_hup_ctx);
209 /* avoid wasting CPU cycles if we are going to exit soon anyways */
210 if (shc->pf != NULL &&
211 shc->pf->cmds == PF_SRV_MSG_EXIT) {
215 change_to_root_user();
216 DEBUG(1,("Reloading printers after SIGHUP\n"));
217 reload_printers(ev, shc->msg_ctx);
218 spoolss_reopen_logs();
221 static bool spoolss_setup_chld_hup_handler(struct tevent_context *ev_ctx,
222 struct pf_worker_data *pf,
223 struct messaging_context *msg_ctx)
225 struct spoolss_chld_sig_hup_ctx *shc;
226 struct tevent_signal *se;
228 shc = talloc(ev_ctx, struct spoolss_chld_sig_hup_ctx);
230 DEBUG(1, ("failed to setup SIGHUP handler"));
234 shc->msg_ctx = msg_ctx;
236 se = tevent_add_signal(ev_ctx,
239 spoolss_chld_sig_hup_handler,
242 DEBUG(1, ("failed to setup SIGHUP handler"));
249 static bool spoolss_child_init(struct tevent_context *ev_ctx,
250 struct pf_worker_data *pf)
253 struct rpc_srv_callbacks spoolss_cb;
254 struct messaging_context *msg_ctx = server_messaging_context();
257 status = reinit_after_fork(msg_ctx, ev_ctx,
258 procid_self(), true);
259 if (!NT_STATUS_IS_OK(status)) {
260 DEBUG(0,("reinit_after_fork() failed\n"));
261 smb_panic("reinit_after_fork() failed");
264 spoolss_reopen_logs();
266 ok = spoolss_setup_chld_hup_handler(ev_ctx, pf, msg_ctx);
271 if (!serverid_register(procid_self(), FLAG_MSG_GENERAL)) {
275 if (!locking_init()) {
279 messaging_register(msg_ctx, ev_ctx,
280 MSG_SMB_CONF_UPDATED, smb_conf_updated);
282 /* try to reinit rpc queues */
283 spoolss_cb.init = spoolss_init_cb;
284 spoolss_cb.shutdown = spoolss_shutdown_cb;
285 spoolss_cb.private_data = msg_ctx;
287 status = rpc_winreg_init(NULL);
288 if (!NT_STATUS_IS_OK(status)) {
289 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
294 status = rpc_spoolss_init(&spoolss_cb);
295 if (!NT_STATUS_IS_OK(status)) {
296 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
301 reload_printers(ev_ctx, msg_ctx);
306 struct spoolss_children_data {
307 struct tevent_context *ev_ctx;
308 struct messaging_context *msg_ctx;
309 struct pf_worker_data *pf;
316 static void spoolss_next_client(void *pvt);
318 static int spoolss_children_main(struct tevent_context *ev_ctx,
319 struct pf_worker_data *pf,
320 int listen_fd, int lock_fd,
323 struct messaging_context *msg_ctx = server_messaging_context();
324 struct spoolss_children_data *data;
328 ok = spoolss_child_init(ev_ctx, pf);
333 data = talloc(ev_ctx, struct spoolss_children_data);
338 data->ev_ctx = ev_ctx;
339 data->msg_ctx = msg_ctx;
340 data->lock_fd = lock_fd;
341 data->listen_fd = listen_fd;
342 data->listening = false;
344 /* loop until it is time to exit */
345 while (pf->status != PF_WORKER_EXITING) {
346 /* try to see if it is time to schedule the next client */
347 spoolss_next_client(data);
349 ret = tevent_loop_once(ev_ctx);
351 DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
352 ret, strerror(errno)));
353 pf->status = PF_WORKER_EXITING;
360 static void spoolss_client_terminated(void *pvt)
362 struct spoolss_children_data *data;
364 data = talloc_get_type_abort(pvt, struct spoolss_children_data);
366 if (data->pf->num_clients) {
367 data->pf->num_clients--;
369 DEBUG(2, ("Invalid num clients, aborting!\n"));
370 data->pf->status = PF_WORKER_EXITING;
374 spoolss_next_client(pvt);
377 struct spoolss_new_client {
378 struct spoolss_children_data *data;
379 struct sockaddr_un sunaddr;
383 static void spoolss_handle_client(struct tevent_req *req);
385 static void spoolss_next_client(void *pvt)
387 struct tevent_req *req;
388 struct spoolss_children_data *data;
389 struct spoolss_new_client *next;
391 data = talloc_get_type_abort(pvt, struct spoolss_children_data);
393 if (data->pf->num_clients == 0) {
394 data->pf->status = PF_WORKER_IDLE;
397 if (data->pf->cmds == PF_SRV_MSG_EXIT) {
398 DEBUG(2, ("Parent process commands we terminate!\n"));
402 if (data->listening ||
403 data->pf->num_clients >= data->pf->allowed_clients) {
404 /* nothing to do for now we are already listening
405 * or reached the number of clients we are allowed
406 * to handle in parallel */
410 next = talloc_zero(data, struct spoolss_new_client);
412 DEBUG(1, ("Out of memory!?\n"));
416 next->addrlen = sizeof(next->sunaddr);
418 req = prefork_listen_send(next, data->ev_ctx, data->pf,
419 data->lock_fd, data->listen_fd,
420 (struct sockaddr *)&next->sunaddr,
423 DEBUG(1, ("Failed to make listening request!?\n"));
427 tevent_req_set_callback(req, spoolss_handle_client, next);
429 data->listening = true;
432 static void spoolss_handle_client(struct tevent_req *req)
434 struct spoolss_children_data *data;
435 struct spoolss_new_client *client;
439 client = tevent_req_callback_data(req, struct spoolss_new_client);
442 ret = prefork_listen_recv(req, &sd);
444 /* this will free the request too */
446 /* we are done listening */
447 data->listening = false;
450 DEBUG(1, ("Failed to accept client connection!\n"));
451 /* bail out if we are not serving any other client */
452 if (data->pf->num_clients == 0) {
453 data->pf->status = PF_WORKER_EXITING;
459 DEBUG(1, ("Server asks us to die!\n"));
460 data->pf->status = PF_WORKER_EXITING;
464 DEBUG(2, ("Spoolss preforked child %d got client connection!\n",
465 (int)(data->pf->pid)));
467 named_pipe_accept_function(data->ev_ctx, data->msg_ctx,
468 SPOOLSS_PIPE_NAME, sd,
469 spoolss_client_terminated, data);
472 /* ==== Main Process Functions ==== */
474 static void spoolssd_sig_chld_handler(struct tevent_context *ev_ctx,
475 struct tevent_signal *se,
476 int signum, int count,
477 void *siginfo, void *pvt)
479 struct prefork_pool *pfp;
486 pfp = talloc_get_type_abort(pvt, struct prefork_pool);
488 while ((pid = sys_waitpid(-1, &status, WNOHANG)) > 0) {
489 ok = prefork_mark_pid_dead(pfp, pid);
491 DEBUG(1, ("Pid %d was not found in children pool!\n",
496 /* now check we do not descent below the minimum */
497 active = prefork_count_active_children(pfp, &total);
500 if (total < spoolss_min_children) {
501 n = total - spoolss_min_children;
502 } else if (total - active < (total / 4)) {
503 n = spoolss_min_children;
507 r = prefork_add_children(ev_ctx, pfp, n);
509 DEBUG(10, ("Tried to start %d children but only,"
510 "%d were actually started.!\n", n, r));
517 static bool spoolssd_setup_sig_chld_handler(struct tevent_context *ev_ctx,
518 struct prefork_pool *pfp)
520 struct tevent_signal *se;
522 se = tevent_add_signal(ev_ctx, ev_ctx, SIGCHLD, 0,
523 spoolssd_sig_chld_handler, pfp);
525 DEBUG(0, ("Failed to setup SIGCHLD handler!\n"));
532 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
533 struct prefork_pool *pfp,
534 struct timeval current_time);
535 static void spoolssd_check_children(struct tevent_context *ev_ctx,
536 struct tevent_timer *te,
537 struct timeval current_time,
540 static bool spoolssd_setup_children_monitor(struct tevent_context *ev_ctx,
541 struct prefork_pool *pfp)
545 ok = spoolssd_setup_sig_chld_handler(ev_ctx, pfp);
550 ok = spoolssd_schedule_check(ev_ctx, pfp, tevent_timeval_current());
554 static bool spoolssd_schedule_check(struct tevent_context *ev_ctx,
555 struct prefork_pool *pfp,
556 struct timeval current_time)
558 struct tevent_timer *te;
559 struct timeval next_event;
561 /* check situation again in 10 seconds */
562 next_event = tevent_timeval_current_ofs(10, 0);
564 /* check when the socket becomes readable, so that children
565 * are checked only when there is some activity */
566 te = tevent_add_timer(ev_ctx, pfp, next_event,
567 spoolssd_check_children, pfp);
569 DEBUG(2, ("Failed to set up children monitoring!\n"));
576 static void spoolssd_check_children(struct tevent_context *ev_ctx,
577 struct tevent_timer *te,
578 struct timeval current_time,
581 struct prefork_pool *pfp;
585 pfp = talloc_get_type_abort(pvt, struct prefork_pool);
587 active = prefork_count_active_children(pfp, &total);
589 if (total - active < spoolss_spawn_rate) {
590 n = prefork_add_children(ev_ctx, pfp, spoolss_spawn_rate);
591 if (n < spoolss_spawn_rate) {
592 DEBUG(10, ("Tried to start 5 children but only,"
593 "%d were actually started.!\n", n));
597 if (total - active > spoolss_min_children) {
598 if ((total - spoolss_min_children) >= spoolss_spawn_rate) {
599 prefork_retire_children(pfp, spoolss_spawn_rate,
600 time(NULL) - SPOOLSS_MIN_LIFE);
604 ret = spoolssd_schedule_check(ev_ctx, pfp, current_time);
607 void start_spoolssd(struct tevent_context *ev_ctx,
608 struct messaging_context *msg_ctx)
610 struct prefork_pool *pool;
611 struct rpc_srv_callbacks spoolss_cb;
612 struct dcerpc_binding_vector *v;
620 DEBUG(1, ("Forking SPOOLSS Daemon\n"));
625 DEBUG(0, ("Failed to fork SPOOLSS [%s], aborting ...\n",
636 close_low_fds(false);
638 status = reinit_after_fork(msg_ctx,
640 procid_self(), true);
641 if (!NT_STATUS_IS_OK(status)) {
642 DEBUG(0,("reinit_after_fork() failed\n"));
643 smb_panic("reinit_after_fork() failed");
646 spoolss_reopen_logs();
647 spoolss_prefork_config();
649 /* the listening fd must be created before the children are actually
651 listen_fd = create_named_pipe_socket(SPOOLSS_PIPE_NAME);
652 if (listen_fd == -1) {
656 ret = listen(listen_fd, spoolss_max_children);
658 DEBUG(0, ("Failed to listen on spoolss pipe - %s\n",
664 /* start children before any more initialization is done */
665 ok = prefork_create_pool(ev_ctx, ev_ctx, listen_fd,
666 spoolss_min_children,
667 spoolss_max_children,
668 &spoolss_children_main, NULL,
671 spoolss_setup_sig_term_handler(ev_ctx);
672 spoolss_setup_sig_hup_handler(ev_ctx, msg_ctx);
674 if (!serverid_register(procid_self(),
675 FLAG_MSG_GENERAL|FLAG_MSG_SMBD
676 |FLAG_MSG_PRINT_GENERAL)) {
680 if (!locking_init()) {
684 messaging_register(msg_ctx, NULL,
685 MSG_PRINTER_UPDATE, print_queue_receive);
686 messaging_register(msg_ctx, ev_ctx,
687 MSG_SMB_CONF_UPDATED, smb_conf_updated);
689 mem_ctx = talloc_new(NULL);
690 if (mem_ctx == NULL) {
695 * Initialize spoolss with an init function to convert printers first.
696 * static_init_rpc will try to initialize the spoolss server too but you
697 * can't register it twice.
699 spoolss_cb.init = spoolss_init_cb;
700 spoolss_cb.shutdown = spoolss_shutdown_cb;
701 spoolss_cb.private_data = msg_ctx;
703 status = rpc_winreg_init(NULL);
704 if (!NT_STATUS_IS_OK(status)) {
705 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
710 status = rpc_spoolss_init(&spoolss_cb);
711 if (!NT_STATUS_IS_OK(status)) {
712 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
717 status = dcerpc_binding_vector_new(mem_ctx, &v);
718 if (!NT_STATUS_IS_OK(status)) {
719 DEBUG(0, ("Failed to create binding vector (%s)\n",
724 status = dcerpc_binding_vector_add_np_default(&ndr_table_spoolss, v);
725 if (!NT_STATUS_IS_OK(status)) {
726 DEBUG(0, ("Failed to add np to binding vector (%s)\n",
731 status = rpc_ep_register(ev_ctx, msg_ctx, &ndr_table_spoolss, v);
732 if (!NT_STATUS_IS_OK(status)) {
733 DEBUG(0, ("Failed to register spoolss endpoint! (%s)\n",
738 talloc_free(mem_ctx);
740 ok = spoolssd_setup_children_monitor(ev_ctx, pool);
742 DEBUG(0, ("Failed to setup children monitoring!\n"));
746 reload_printers(ev_ctx, msg_ctx);
748 DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
751 ret = tevent_loop_wait(ev_ctx);
753 /* should not be reached */
754 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
755 ret, (ret == 0) ? "out of events" : strerror(errno)));