s3:spoolssd Add spoolss own signal handlers
[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 static void spoolss_sig_term_handler(struct tevent_context *ev,
53                                      struct tevent_signal *se,
54                                      int signum,
55                                      int count,
56                                      void *siginfo,
57                                      void *private_data)
58 {
59         exit_server_cleanly("termination signal");
60 }
61
62 static void spoolss_setup_sig_term_handler(void)
63 {
64         struct tevent_signal *se;
65
66         se = tevent_add_signal(server_event_context(),
67                                server_event_context(),
68                                SIGTERM, 0,
69                                spoolss_sig_term_handler,
70                                NULL);
71         if (!se) {
72                 exit_server("failed to setup SIGTERM handler");
73         }
74 }
75
76 static void spoolss_sig_hup_handler(struct tevent_context *ev,
77                                     struct tevent_signal *se,
78                                     int signum,
79                                     int count,
80                                     void *siginfo,
81                                     void *private_data)
82 {
83         change_to_root_user();
84         DEBUG(1,("Reloading printers after SIGHUP\n"));
85         reload_printers(server_messaging_context());
86         spoolss_reopen_logs();
87 }
88
89 static void spoolss_setup_sig_hup_handler(void)
90 {
91         struct tevent_signal *se;
92
93         se = tevent_add_signal(server_event_context(),
94                                server_event_context(),
95                                SIGHUP, 0,
96                                spoolss_sig_hup_handler,
97                                NULL);
98         if (!se) {
99                 exit_server("failed to setup SIGHUP handler");
100         }
101 }
102
103 void start_spoolssd(void)
104 {
105         pid_t pid;
106         NTSTATUS status;
107         int ret;
108
109         DEBUG(1, ("Forking SPOOLSS Daemon\n"));
110
111         pid = sys_fork();
112
113         if (pid == -1) {
114                 DEBUG(0, ("Failed to fork SPOOLSS [%s], aborting ...\n",
115                            strerror(errno)));
116                 exit(1);
117         }
118
119         if (pid) {
120                 /* parent */
121                 return;
122         }
123
124         /* child */
125         close_low_fds(false);
126
127         status = reinit_after_fork(server_messaging_context(),
128                                    server_event_context(),
129                                    procid_self(), true);
130         if (!NT_STATUS_IS_OK(status)) {
131                 DEBUG(0,("reinit_after_fork() failed\n"));
132                 smb_panic("reinit_after_fork() failed");
133         }
134
135         spoolss_reopen_logs();
136
137         spoolss_setup_sig_term_handler();
138         spoolss_setup_sig_hup_handler();
139
140         if (!serverid_register(procid_self(),
141                                 FLAG_MSG_GENERAL|FLAG_MSG_SMBD
142                                 |FLAG_MSG_PRINT_GENERAL)) {
143                 exit(1);
144         }
145
146         if (!locking_init()) {
147                 exit(1);
148         }
149
150         messaging_register(server_messaging_context(), NULL,
151                            MSG_PRINTER_UPDATE, print_queue_receive);
152         messaging_register(server_messaging_context(), NULL,
153                            MSG_SMB_CONF_UPDATED, smb_conf_updated);
154
155         if (!setup_named_pipe_socket(SPOOLSS_PIPE_NAME, server_event_context())) {
156                 exit(1);
157         }
158
159         /* loop forever */
160         ret = tevent_loop_wait(server_event_context());
161
162         /* should not be reached */
163         DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
164                  ret, (ret == 0) ? "out of events" : strerror(errno)));
165         exit(1);
166 }