s3:rpc_server: Shutdown registered enpoint servers on server exit
[asn/samba.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/smbd.h"
27 #include "smbd/globals.h"
28 #include "ntdomain.h"
29 #include "librpc/rpc/dcesrv_core.h"
30 #include "../librpc/gen_ndr/srv_dfs.h"
31 #include "../librpc/gen_ndr/srv_dssetup.h"
32 #include "../librpc/gen_ndr/srv_echo.h"
33 #include "../librpc/gen_ndr/srv_eventlog.h"
34 #include "../librpc/gen_ndr/srv_initshutdown.h"
35 #include "../librpc/gen_ndr/srv_lsa.h"
36 #include "../librpc/gen_ndr/srv_netlogon.h"
37 #include "../librpc/gen_ndr/srv_ntsvcs.h"
38 #include "../librpc/gen_ndr/srv_samr.h"
39 #include "../librpc/gen_ndr/srv_spoolss.h"
40 #include "../librpc/gen_ndr/srv_srvsvc.h"
41 #include "../librpc/gen_ndr/srv_svcctl.h"
42 #include "../librpc/gen_ndr/srv_winreg.h"
43 #include "../librpc/gen_ndr/srv_wkssvc.h"
44 #include "../librpc/gen_ndr/srv_fsrvp.h"
45 #include "../librpc/gen_ndr/srv_epmapper.h"
46 #include "printing/notify.h"
47 #include "printing.h"
48 #include "serverid.h"
49 #include "messages.h"
50 #include "passdb.h"
51 #include "../lib/util/pidfile.h"
52 #include "smbprofile.h"
53 #include "libcli/auth/netlogon_creds_cli.h"
54 #include "lib/gencache.h"
55 #include "rpc_server/rpc_config.h"
56
57 static struct files_struct *log_writeable_file_fn(
58         struct files_struct *fsp, void *private_data)
59 {
60         bool *found = (bool *)private_data;
61         char *path;
62
63         if (!fsp->can_write) {
64                 return NULL;
65         }
66         if (!(*found)) {
67                 DEBUG(0, ("Writable files open at exit:\n"));
68                 *found = true;
69         }
70
71         path = talloc_asprintf(talloc_tos(), "%s/%s", fsp->conn->connectpath,
72                                smb_fname_str_dbg(fsp->fsp_name));
73         if (path == NULL) {
74                 DEBUGADD(0, ("<NOMEM>\n"));
75         }
76
77         DEBUGADD(0, ("%s\n", path));
78
79         TALLOC_FREE(path);
80         return NULL;
81 }
82
83 /****************************************************************************
84  Exit the server.
85 ****************************************************************************/
86
87 /* Reasons for shutting down a server process. */
88 enum server_exit_reason { SERVER_EXIT_NORMAL, SERVER_EXIT_ABNORMAL };
89
90 static void exit_server_common(enum server_exit_reason how,
91         const char *reason) _NORETURN_;
92
93 static void exit_server_common(enum server_exit_reason how,
94         const char *reason)
95 {
96         struct smbXsrv_client *client = global_smbXsrv_client;
97         struct smbXsrv_connection *xconn = NULL;
98         struct smbd_server_connection *sconn = NULL;
99         struct messaging_context *msg_ctx = global_messaging_context();
100
101         if (client != NULL) {
102                 sconn = client->sconn;
103                 xconn = client->connections;
104         }
105
106         if (!exit_firsttime)
107                 exit(0);
108         exit_firsttime = false;
109
110         change_to_root_user();
111
112
113         /*
114          * Here we typically have just one connection
115          */
116         for (; xconn != NULL; xconn = xconn->next) {
117                 /*
118                  * This is typically the disconnect for the only
119                  * (or with multi-channel last) connection of the client
120                  */
121                 if (NT_STATUS_IS_OK(xconn->transport.status)) {
122                         switch (how) {
123                         case SERVER_EXIT_ABNORMAL:
124                                 xconn->transport.status = NT_STATUS_INTERNAL_ERROR;
125                                 break;
126                         case SERVER_EXIT_NORMAL:
127                                 xconn->transport.status = NT_STATUS_LOCAL_DISCONNECT;
128                                 break;
129                         }
130                 }
131                 DO_PROFILE_INC(disconnect);
132         }
133
134         change_to_root_user();
135
136         if (sconn != NULL) {
137                 if (lp_log_writeable_files_on_exit()) {
138                         bool found = false;
139                         files_forall(sconn, log_writeable_file_fn, &found);
140                 }
141         }
142
143         change_to_root_user();
144
145         if (client != NULL) {
146                 NTSTATUS status;
147
148                 /*
149                  * Note: this is a no-op for smb2 as
150                  * conn->tcon_table is empty
151                  */
152                 status = smb1srv_tcon_disconnect_all(client);
153                 if (!NT_STATUS_IS_OK(status)) {
154                         DEBUG(0,("Server exit (%s)\n",
155                                 (reason ? reason : "normal exit")));
156                         DEBUG(0, ("exit_server_common: "
157                                   "smb1srv_tcon_disconnect_all() failed (%s) - "
158                                   "triggering cleanup\n", nt_errstr(status)));
159                 }
160
161                 status = smbXsrv_session_logoff_all(client);
162                 if (!NT_STATUS_IS_OK(status)) {
163                         DEBUG(0,("Server exit (%s)\n",
164                                 (reason ? reason : "normal exit")));
165                         DEBUG(0, ("exit_server_common: "
166                                   "smbXsrv_session_logoff_all() failed (%s) - "
167                                   "triggering cleanup\n", nt_errstr(status)));
168                 }
169         }
170
171         change_to_root_user();
172
173         if (client != NULL) {
174                 struct smbXsrv_connection *xconn_next = NULL;
175
176                 for (xconn = client->connections;
177                      xconn != NULL;
178                      xconn = xconn_next) {
179                         xconn_next = xconn->next;
180                         DLIST_REMOVE(client->connections, xconn);
181                         TALLOC_FREE(xconn);
182                 }
183         }
184
185         change_to_root_user();
186
187         /* 3 second timeout. */
188         print_notify_send_messages(msg_ctx, 3);
189
190 #ifdef USE_DMAPI
191         /* Destroy Samba DMAPI session only if we are master smbd process */
192         if (am_parent) {
193                 if (!dmapi_destroy_session()) {
194                         DEBUG(0,("Unable to close Samba DMAPI session\n"));
195                 }
196         }
197 #endif
198
199         if (am_parent && sconn != NULL) {
200                 dcesrv_shutdown_registered_ep_servers(sconn->dce_ctx);
201
202                 rpc_wkssvc_shutdown();
203                 rpc_dssetup_shutdown();
204 #ifdef DEVELOPER
205                 rpc_rpcecho_shutdown();
206 #endif
207                 rpc_netdfs_shutdown();
208                 rpc_initshutdown_shutdown();
209                 rpc_eventlog_shutdown();
210                 rpc_ntsvcs_shutdown();
211                 rpc_svcctl_shutdown();
212                 rpc_spoolss_shutdown();
213
214                 rpc_srvsvc_shutdown();
215                 rpc_winreg_shutdown();
216
217                 rpc_netlogon_shutdown();
218                 rpc_samr_shutdown();
219                 rpc_lsarpc_shutdown();
220
221                 rpc_FileServerVssAgent_shutdown();
222
223                 rpc_epmapper_shutdown();
224
225                 global_dcesrv_context_free();
226         }
227
228         /*
229          * we need to force the order of freeing the following,
230          * because smbd_msg_ctx is not a talloc child of smbd_server_conn.
231          */
232         if (client != NULL) {
233                 TALLOC_FREE(client->sconn);
234         }
235         sconn = NULL;
236         xconn = NULL;
237         client = NULL;
238         netlogon_creds_cli_close_global_db();
239         TALLOC_FREE(global_smbXsrv_client);
240         smbprofile_dump();
241         global_messaging_context_free();
242         global_event_context_free();
243         TALLOC_FREE(smbd_memcache_ctx);
244
245         locking_end();
246         printing_end();
247
248         if (how != SERVER_EXIT_NORMAL) {
249
250                 smb_panic(reason);
251
252                 /* Notreached. */
253                 exit(1);
254         } else {
255                 DEBUG(3,("Server exit (%s)\n",
256                         (reason ? reason : "normal exit")));
257                 if (am_parent) {
258                         pidfile_unlink(lp_pid_directory(), "smbd");
259                 }
260         }
261
262         exit(0);
263 }
264
265 void smbd_exit_server(const char *const explanation)
266 {
267         exit_server_common(SERVER_EXIT_ABNORMAL, explanation);
268 }
269
270 void smbd_exit_server_cleanly(const char *const explanation)
271 {
272         exit_server_common(SERVER_EXIT_NORMAL, explanation);
273 }
274
275 /*
276  * reinit_after_fork() wrapper that should be called when forking from
277  * smbd.
278  */
279 NTSTATUS smbd_reinit_after_fork(struct messaging_context *msg_ctx,
280                                 struct tevent_context *ev_ctx,
281                                 bool parent_longlived, const char *comment)
282 {
283         NTSTATUS ret;
284         am_parent = NULL;
285         ret = reinit_after_fork(msg_ctx, ev_ctx, parent_longlived, comment);
286         initialize_password_db(true, ev_ctx);
287         return ret;
288 }