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