s3-printing: only include printing where really needed.
[bbaumbach/samba-autobuild/.git] / source3 / smbd / server_exit.c
1 /*
2    Unix SMB/CIFS implementation.
3    Main SMB server routines
4    Copyright (C) Andrew Tridgell                1992-1998
5    Copyright (C) Martin Pool                    2002
6    Copyright (C) Jelmer Vernooij                2002-2003
7    Copyright (C) Volker Lendecke                1993-2007
8    Copyright (C) Jeremy Allison                 1993-2007
9    Copyright (C) Andrew Bartlett                2010
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "smbd/globals.h"
27 #include "librpc/gen_ndr/messaging.h"
28
29 #include "../librpc/gen_ndr/srv_dfs.h"
30 #include "../librpc/gen_ndr/srv_dssetup.h"
31 #include "../librpc/gen_ndr/srv_echo.h"
32 #include "../librpc/gen_ndr/srv_eventlog.h"
33 #include "../librpc/gen_ndr/srv_initshutdown.h"
34 #include "../librpc/gen_ndr/srv_lsa.h"
35 #include "../librpc/gen_ndr/srv_netlogon.h"
36 #include "../librpc/gen_ndr/srv_ntsvcs.h"
37 #include "../librpc/gen_ndr/srv_samr.h"
38 #include "../librpc/gen_ndr/srv_spoolss.h"
39 #include "../librpc/gen_ndr/srv_srvsvc.h"
40 #include "../librpc/gen_ndr/srv_svcctl.h"
41 #include "../librpc/gen_ndr/srv_winreg.h"
42 #include "../librpc/gen_ndr/srv_wkssvc.h"
43 #include "printing/notify.h"
44 #include "printing.h"
45
46 static struct files_struct *log_writeable_file_fn(
47         struct files_struct *fsp, void *private_data)
48 {
49         bool *found = (bool *)private_data;
50         char *path;
51
52         if (!fsp->can_write) {
53                 return NULL;
54         }
55         if (!(*found)) {
56                 DEBUG(0, ("Writable files open at exit:\n"));
57                 *found = true;
58         }
59
60         path = talloc_asprintf(talloc_tos(), "%s/%s", fsp->conn->connectpath,
61                                smb_fname_str_dbg(fsp->fsp_name));
62         if (path == NULL) {
63                 DEBUGADD(0, ("<NOMEM>\n"));
64         }
65
66         DEBUGADD(0, ("%s\n", path));
67
68         TALLOC_FREE(path);
69         return NULL;
70 }
71
72 /****************************************************************************
73  Exit the server.
74 ****************************************************************************/
75
76 /* Reasons for shutting down a server process. */
77 enum server_exit_reason { SERVER_EXIT_NORMAL, SERVER_EXIT_ABNORMAL };
78
79 static void exit_server_common(enum server_exit_reason how,
80         const char *const reason) _NORETURN_;
81
82 static void exit_server_common(enum server_exit_reason how,
83         const char *const reason)
84 {
85         bool had_open_conn = false;
86         struct smbd_server_connection *sconn = smbd_server_conn;
87
88         if (!exit_firsttime)
89                 exit(0);
90         exit_firsttime = false;
91
92         change_to_root_user();
93
94         if (sconn && sconn->smb1.negprot.auth_context) {
95                 TALLOC_FREE(sconn->smb1.negprot.auth_context);
96         }
97
98         if (sconn) {
99                 if (lp_log_writeable_files_on_exit()) {
100                         bool found = false;
101                         files_forall(sconn, log_writeable_file_fn, &found);
102                 }
103                 had_open_conn = conn_close_all(sconn);
104                 invalidate_all_vuids(sconn);
105         }
106
107         /* 3 second timeout. */
108         print_notify_send_messages(sconn->msg_ctx, 3);
109
110         /* delete our entry in the serverid database. */
111         if (am_parent) {
112                 /*
113                  * For children the parent takes care of cleaning up
114                  */
115                 serverid_deregister(sconn_server_id(sconn));
116         }
117
118 #ifdef WITH_DFS
119         if (dcelogin_atmost_once) {
120                 dfs_unlogin();
121         }
122 #endif
123
124 #ifdef USE_DMAPI
125         /* Destroy Samba DMAPI session only if we are master smbd process */
126         if (am_parent) {
127                 if (!dmapi_destroy_session()) {
128                         DEBUG(0,("Unable to close Samba DMAPI session\n"));
129                 }
130         }
131 #endif
132
133         if (am_parent) {
134                 rpc_wkssvc_shutdown();
135                 rpc_dssetup_shutdown();
136 #ifdef DEVELOPER
137                 rpc_rpcecho_shutdown();
138 #endif
139                 rpc_netdfs_shutdown();
140                 rpc_initshutdown_shutdown();
141                 rpc_eventlog_shutdown();
142                 rpc_ntsvcs_shutdown();
143                 rpc_svcctl_shutdown();
144                 rpc_spoolss_shutdown();
145
146                 rpc_srvsvc_shutdown();
147                 rpc_winreg_shutdown();
148
149                 rpc_netlogon_shutdown();
150                 rpc_samr_shutdown();
151                 rpc_lsarpc_shutdown();
152         }
153
154         locking_end();
155         printing_end();
156
157         /*
158          * we need to force the order of freeing the following,
159          * because smbd_msg_ctx is not a talloc child of smbd_server_conn.
160          */
161         sconn = NULL;
162         TALLOC_FREE(smbd_server_conn);
163         server_messaging_context_free();
164         server_event_context_free();
165         TALLOC_FREE(smbd_memcache_ctx);
166
167         if (how != SERVER_EXIT_NORMAL) {
168                 DEBUGSEP(0);
169                 DEBUG(0,("Abnormal server exit: %s\n",
170                         reason ? reason : "no explanation provided"));
171                 DEBUGSEP(0);
172
173                 log_stack_trace();
174
175                 dump_core();
176
177         } else {
178                 DEBUG(3,("Server exit (%s)\n",
179                         (reason ? reason : "normal exit")));
180                 if (am_parent) {
181                         pidfile_unlink();
182                 }
183                 gencache_stabilize();
184         }
185
186         /* if we had any open SMB connections when we exited then we
187            need to tell the parent smbd so that it can trigger a retry
188            of any locks we may have been holding or open files we were
189            blocking */
190         if (had_open_conn) {
191                 exit(1);
192         } else {
193                 exit(0);
194         }
195 }
196
197 void exit_server(const char *const explanation)
198 {
199         exit_server_common(SERVER_EXIT_ABNORMAL, explanation);
200 }
201
202 void exit_server_cleanly(const char *const explanation)
203 {
204         exit_server_common(SERVER_EXIT_NORMAL, explanation);
205 }
206
207 void exit_server_fault(void)
208 {
209         exit_server("critical server fault");
210 }