07b8432bff75ad9b46724c82c275b1864ca0f776
[kai/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
48 static struct files_struct *log_writeable_file_fn(
49         struct files_struct *fsp, void *private_data)
50 {
51         bool *found = (bool *)private_data;
52         char *path;
53
54         if (!fsp->can_write) {
55                 return NULL;
56         }
57         if (!(*found)) {
58                 DEBUG(0, ("Writable files open at exit:\n"));
59                 *found = true;
60         }
61
62         path = talloc_asprintf(talloc_tos(), "%s/%s", fsp->conn->connectpath,
63                                smb_fname_str_dbg(fsp->fsp_name));
64         if (path == NULL) {
65                 DEBUGADD(0, ("<NOMEM>\n"));
66         }
67
68         DEBUGADD(0, ("%s\n", path));
69
70         TALLOC_FREE(path);
71         return NULL;
72 }
73
74 /****************************************************************************
75  Exit the server.
76 ****************************************************************************/
77
78 /* Reasons for shutting down a server process. */
79 enum server_exit_reason { SERVER_EXIT_NORMAL, SERVER_EXIT_ABNORMAL };
80
81 static void exit_server_common(enum server_exit_reason how,
82         const char *reason) _NORETURN_;
83
84 static void exit_server_common(enum server_exit_reason how,
85         const char *reason)
86 {
87         struct smbXsrv_connection *conn = global_smbXsrv_connection;
88         struct smbd_server_connection *sconn = NULL;
89         struct messaging_context *msg_ctx = server_messaging_context();
90
91         if (conn != NULL) {
92                 sconn = conn->sconn;
93         }
94
95         if (!exit_firsttime)
96                 exit(0);
97         exit_firsttime = false;
98
99         change_to_root_user();
100
101         if (sconn && sconn->smb1.negprot.auth_context) {
102                 TALLOC_FREE(sconn->smb1.negprot.auth_context);
103         }
104
105         if (sconn) {
106                 NTSTATUS status;
107
108                 if (lp_log_writeable_files_on_exit()) {
109                         bool found = false;
110                         files_forall(sconn, log_writeable_file_fn, &found);
111                 }
112
113                 /*
114                  * Note: this is a no-op for smb2 as
115                  * conn->tcon_table is empty
116                  */
117                 status = smb1srv_tcon_disconnect_all(conn);
118                 if (!NT_STATUS_IS_OK(status)) {
119                         DEBUG(0,("Server exit (%s)\n",
120                                 (reason ? reason : "normal exit")));
121                         DEBUG(0, ("exit_server_common: "
122                                   "smb1srv_tcon_disconnect_all() failed (%s) - "
123                                   "triggering cleanup\n", nt_errstr(status)));
124                         how = SERVER_EXIT_ABNORMAL;
125                         reason = "smb1srv_tcon_disconnect_all failed";
126                 }
127
128                 status = smbXsrv_session_logoff_all(conn);
129                 if (!NT_STATUS_IS_OK(status)) {
130                         DEBUG(0,("Server exit (%s)\n",
131                                 (reason ? reason : "normal exit")));
132                         DEBUG(0, ("exit_server_common: "
133                                   "smbXsrv_session_logoff_all() failed (%s) - "
134                                   "triggering cleanup\n", nt_errstr(status)));
135                         how = SERVER_EXIT_ABNORMAL;
136                         reason = "smbXsrv_session_logoff_all failed";
137                 }
138
139                 change_to_root_user();
140         }
141
142         /* 3 second timeout. */
143         print_notify_send_messages(msg_ctx, 3);
144
145         /* delete our entry in the serverid database. */
146         if (am_parent) {
147                 /*
148                  * For children the parent takes care of cleaning up
149                  */
150                 serverid_deregister(messaging_server_id(msg_ctx));
151         }
152
153 #ifdef WITH_DFS
154         if (dcelogin_atmost_once) {
155                 dfs_unlogin();
156         }
157 #endif
158
159 #ifdef USE_DMAPI
160         /* Destroy Samba DMAPI session only if we are master smbd process */
161         if (am_parent) {
162                 if (!dmapi_destroy_session()) {
163                         DEBUG(0,("Unable to close Samba DMAPI session\n"));
164                 }
165         }
166 #endif
167
168         if (am_parent) {
169                 rpc_wkssvc_shutdown();
170                 rpc_dssetup_shutdown();
171 #ifdef DEVELOPER
172                 rpc_rpcecho_shutdown();
173 #endif
174                 rpc_netdfs_shutdown();
175                 rpc_initshutdown_shutdown();
176                 rpc_eventlog_shutdown();
177                 rpc_ntsvcs_shutdown();
178                 rpc_svcctl_shutdown();
179                 rpc_spoolss_shutdown();
180
181                 rpc_srvsvc_shutdown();
182                 rpc_winreg_shutdown();
183
184                 rpc_netlogon_shutdown();
185                 rpc_samr_shutdown();
186                 rpc_lsarpc_shutdown();
187         }
188
189         /*
190          * we need to force the order of freeing the following,
191          * because smbd_msg_ctx is not a talloc child of smbd_server_conn.
192          */
193         sconn = NULL;
194         conn = NULL;
195         TALLOC_FREE(global_smbXsrv_connection);
196         server_messaging_context_free();
197         server_event_context_free();
198         TALLOC_FREE(smbd_memcache_ctx);
199
200         locking_end();
201         printing_end();
202
203         if (how != SERVER_EXIT_NORMAL) {
204                 DEBUGSEP(0);
205                 DEBUG(0,("Abnormal server exit: %s\n",
206                         reason ? reason : "no explanation provided"));
207                 DEBUGSEP(0);
208
209                 log_stack_trace();
210
211                 dump_core();
212
213                 /* Notreached. */
214                 exit(1);
215         } else {
216                 DEBUG(3,("Server exit (%s)\n",
217                         (reason ? reason : "normal exit")));
218                 if (am_parent) {
219                         pidfile_unlink();
220                 }
221                 gencache_stabilize();
222         }
223
224         exit(0);
225 }
226
227 void exit_server(const char *const explanation)
228 {
229         exit_server_common(SERVER_EXIT_ABNORMAL, explanation);
230 }
231
232 void exit_server_cleanly(const char *const explanation)
233 {
234         exit_server_common(SERVER_EXIT_NORMAL, explanation);
235 }