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