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