s3-spoolssd: Fixed reopening of logs.
[ddiss/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 "printing/nt_printing_migrate.h"
23 #include "librpc/gen_ndr/srv_winreg.h"
24 #include "librpc/gen_ndr/srv_spoolss.h"
25 #include "rpc_server/rpc_server.h"
26
27 #define SPOOLSS_PIPE_NAME "spoolss"
28 #define DAEMON_NAME "spoolssd"
29
30 static void spoolss_reopen_logs(void)
31 {
32         char *lfile = lp_logfile();
33         int rc;
34
35         if (strstr(lfile, DAEMON_NAME) == NULL) {
36                 rc = asprintf(&lfile, "%s.%s", lp_logfile(), DAEMON_NAME);
37                 if (rc > 0) {
38                         lp_set_logfile(lfile);
39                         SAFE_FREE(lfile);
40                 }
41         }
42         reopen_logs();
43 }
44
45 static void smb_conf_updated(struct messaging_context *msg,
46                              void *private_data,
47                              uint32_t msg_type,
48                              struct server_id server_id,
49                              DATA_BLOB *data)
50 {
51         DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
52         change_to_root_user();
53         reload_printers(msg);
54         spoolss_reopen_logs();
55 }
56
57 static void spoolss_sig_term_handler(struct tevent_context *ev,
58                                      struct tevent_signal *se,
59                                      int signum,
60                                      int count,
61                                      void *siginfo,
62                                      void *private_data)
63 {
64         exit_server_cleanly("termination signal");
65 }
66
67 static void spoolss_setup_sig_term_handler(void)
68 {
69         struct tevent_signal *se;
70
71         se = tevent_add_signal(server_event_context(),
72                                server_event_context(),
73                                SIGTERM, 0,
74                                spoolss_sig_term_handler,
75                                NULL);
76         if (!se) {
77                 exit_server("failed to setup SIGTERM handler");
78         }
79 }
80
81 static void spoolss_sig_hup_handler(struct tevent_context *ev,
82                                     struct tevent_signal *se,
83                                     int signum,
84                                     int count,
85                                     void *siginfo,
86                                     void *private_data)
87 {
88         change_to_root_user();
89         DEBUG(1,("Reloading printers after SIGHUP\n"));
90         reload_printers(server_messaging_context());
91         spoolss_reopen_logs();
92 }
93
94 static void spoolss_setup_sig_hup_handler(void)
95 {
96         struct tevent_signal *se;
97
98         se = tevent_add_signal(server_event_context(),
99                                server_event_context(),
100                                SIGHUP, 0,
101                                spoolss_sig_hup_handler,
102                                NULL);
103         if (!se) {
104                 exit_server("failed to setup SIGHUP handler");
105         }
106 }
107
108 static bool spoolss_init_cb(void *ptr)
109 {
110         return nt_printing_tdb_migrate(server_messaging_context());
111 }
112
113 void start_spoolssd(void)
114 {
115         struct rpc_srv_callbacks spoolss_cb;
116         pid_t pid;
117         NTSTATUS status;
118         int ret;
119
120         DEBUG(1, ("Forking SPOOLSS Daemon\n"));
121
122         pid = sys_fork();
123
124         if (pid == -1) {
125                 DEBUG(0, ("Failed to fork SPOOLSS [%s], aborting ...\n",
126                            strerror(errno)));
127                 exit(1);
128         }
129
130         if (pid) {
131                 /* parent */
132                 return;
133         }
134
135         /* child */
136         close_low_fds(false);
137
138         status = reinit_after_fork(server_messaging_context(),
139                                    server_event_context(),
140                                    procid_self(), true);
141         if (!NT_STATUS_IS_OK(status)) {
142                 DEBUG(0,("reinit_after_fork() failed\n"));
143                 smb_panic("reinit_after_fork() failed");
144         }
145
146         spoolss_reopen_logs();
147
148         spoolss_setup_sig_term_handler();
149         spoolss_setup_sig_hup_handler();
150
151         if (!serverid_register(procid_self(),
152                                 FLAG_MSG_GENERAL|FLAG_MSG_SMBD
153                                 |FLAG_MSG_PRINT_GENERAL)) {
154                 exit(1);
155         }
156
157         if (!locking_init()) {
158                 exit(1);
159         }
160
161         messaging_register(server_messaging_context(), NULL,
162                            MSG_PRINTER_UPDATE, print_queue_receive);
163         messaging_register(server_messaging_context(), NULL,
164                            MSG_SMB_CONF_UPDATED, smb_conf_updated);
165
166         /*
167          * Initialize spoolss with an init function to convert printers first.
168          * static_init_rpc will try to initialize the spoolss server too but you
169          * can't register it twice.
170          */
171         spoolss_cb.init = spoolss_init_cb;
172         spoolss_cb.shutdown = NULL;
173
174         status = rpc_winreg_init(NULL);
175         if (!NT_STATUS_IS_OK(status)) {
176                 DEBUG(0, ("Failed to register winreg rpc inteface! (%s)\n",
177                           nt_errstr(status)));
178                 exit(1);
179         }
180
181         status = rpc_spoolss_init(&spoolss_cb);
182         if (!NT_STATUS_IS_OK(status)) {
183                 DEBUG(0, ("Failed to register spoolss rpc inteface! (%s)\n",
184                           nt_errstr(status)));
185                 exit(1);
186         }
187
188         if (!setup_named_pipe_socket(SPOOLSS_PIPE_NAME, server_event_context())) {
189                 exit(1);
190         }
191
192         DEBUG(1, ("SPOOLSS Daemon Started (%d)\n", getpid()));
193
194         /* loop forever */
195         ret = tevent_loop_wait(server_event_context());
196
197         /* should not be reached */
198         DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
199                  ret, (ret == 0) ? "out of events" : strerror(errno)));
200         exit(1);
201 }