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