s3:spoolssd handle smb.conf reloads directly
[mat/samba.git] / source3 / printing / spoolssd.c
1 /*
2    Unix SMB/Netbios implementation.
3    SPOOLSS Daemon
4    Copyright (C) Simo Sorce 2010
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 "librpc/gen_ndr/messaging.h"
21 #include "include/printing.h"
22 #include "rpc_server/rpc_server.h"
23
24 #define SPOOLSS_PIPE_NAME "spoolss"
25
26
27 static void spoolss_reopen_logs(void)
28 {
29         char *lfile = NULL;
30         int ret;
31
32         ret = asprintf(&lfile, "%s.spoolssd", lp_logfile());
33         if (ret > 0) {
34                 lp_set_logfile(lfile);
35                 SAFE_FREE(lfile);
36         }
37         reopen_logs();
38 }
39
40 static void smb_conf_updated(struct messaging_context *msg,
41                              void *private_data,
42                              uint32_t msg_type,
43                              struct server_id server_id,
44                              DATA_BLOB *data)
45 {
46         DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
47         change_to_root_user();
48         reload_printers(msg);
49         spoolss_reopen_logs();
50 }
51
52 void start_spoolssd(void)
53 {
54         pid_t pid;
55         NTSTATUS status;
56         int ret;
57
58         DEBUG(1, ("Forking SPOOLSS Daemon\n"));
59
60         pid = sys_fork();
61
62         if (pid == -1) {
63                 DEBUG(0, ("Failed to fork SPOOLSS [%s], aborting ...\n",
64                            strerror(errno)));
65                 exit(1);
66         }
67
68         if (pid) {
69                 /* parent */
70                 return;
71         }
72
73         /* child */
74         close_low_fds(false);
75
76         status = reinit_after_fork(server_messaging_context(),
77                                    server_event_context(),
78                                    procid_self(), true);
79         if (!NT_STATUS_IS_OK(status)) {
80                 DEBUG(0,("reinit_after_fork() failed\n"));
81                 smb_panic("reinit_after_fork() failed");
82         }
83
84         spoolss_reopen_logs();
85
86         smbd_setup_sig_term_handler();
87         smbd_setup_sig_hup_handler();
88
89         if (!serverid_register(procid_self(),
90                                 FLAG_MSG_GENERAL|FLAG_MSG_SMBD
91                                 |FLAG_MSG_PRINT_GENERAL)) {
92                 exit(1);
93         }
94
95         if (!locking_init()) {
96                 exit(1);
97         }
98
99         messaging_register(server_messaging_context(), NULL,
100                            MSG_PRINTER_UPDATE, print_queue_receive);
101         messaging_register(server_messaging_context(), NULL,
102                            MSG_SMB_CONF_UPDATED, smb_conf_updated);
103
104         if (!setup_named_pipe_socket(SPOOLSS_PIPE_NAME, server_event_context())) {
105                 exit(1);
106         }
107
108         /* loop forever */
109         ret = tevent_loop_wait(server_event_context());
110
111         /* should not be reached */
112         DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
113                  ret, (ret == 0) ? "out of events" : strerror(errno)));
114         exit(1);
115 }