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