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
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.
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.
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/>.
26 #include "smbd/smbd.h"
27 #include "smbd/globals.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 "../librpc/gen_ndr/srv_fsrvp.h"
44 #include "../librpc/gen_ndr/srv_epmapper.h"
45 #include "printing/notify.h"
50 #include "../lib/util/pidfile.h"
51 #include "smbprofile.h"
52 #include "libcli/auth/netlogon_creds_cli.h"
53 #include "lib/gencache.h"
55 static struct files_struct
*log_writeable_file_fn(
56 struct files_struct
*fsp
, void *private_data
)
58 bool *found
= (bool *)private_data
;
61 if (!fsp
->can_write
) {
65 DEBUG(0, ("Writable files open at exit:\n"));
69 path
= talloc_asprintf(talloc_tos(), "%s/%s", fsp
->conn
->connectpath
,
70 smb_fname_str_dbg(fsp
->fsp_name
));
72 DEBUGADD(0, ("<NOMEM>\n"));
75 DEBUGADD(0, ("%s\n", path
));
81 /****************************************************************************
83 ****************************************************************************/
85 /* Reasons for shutting down a server process. */
86 enum server_exit_reason
{ SERVER_EXIT_NORMAL
, SERVER_EXIT_ABNORMAL
};
88 static void exit_server_common(enum server_exit_reason how
,
89 const char *reason
) _NORETURN_
;
91 static void exit_server_common(enum server_exit_reason how
,
94 struct smbXsrv_client
*client
= global_smbXsrv_client
;
95 struct smbXsrv_connection
*xconn
= NULL
;
96 struct smbd_server_connection
*sconn
= NULL
;
97 struct messaging_context
*msg_ctx
= global_messaging_context();
100 sconn
= client
->sconn
;
101 xconn
= client
->connections
;
106 exit_firsttime
= false;
108 change_to_root_user();
112 * Here we typically have just one connection
114 for (; xconn
!= NULL
; xconn
= xconn
->next
) {
116 * This is typically the disconnect for the only
117 * (or with multi-channel last) connection of the client
119 if (NT_STATUS_IS_OK(xconn
->transport
.status
)) {
121 case SERVER_EXIT_ABNORMAL
:
122 xconn
->transport
.status
= NT_STATUS_INTERNAL_ERROR
;
124 case SERVER_EXIT_NORMAL
:
125 xconn
->transport
.status
= NT_STATUS_LOCAL_DISCONNECT
;
129 DO_PROFILE_INC(disconnect
);
132 change_to_root_user();
135 if (lp_log_writeable_files_on_exit()) {
137 files_forall(sconn
, log_writeable_file_fn
, &found
);
141 change_to_root_user();
143 if (client
!= NULL
) {
147 * Note: this is a no-op for smb2 as
148 * conn->tcon_table is empty
150 status
= smb1srv_tcon_disconnect_all(client
);
151 if (!NT_STATUS_IS_OK(status
)) {
152 DEBUG(0,("Server exit (%s)\n",
153 (reason
? reason
: "normal exit")));
154 DEBUG(0, ("exit_server_common: "
155 "smb1srv_tcon_disconnect_all() failed (%s) - "
156 "triggering cleanup\n", nt_errstr(status
)));
159 status
= smbXsrv_session_logoff_all(client
);
160 if (!NT_STATUS_IS_OK(status
)) {
161 DEBUG(0,("Server exit (%s)\n",
162 (reason
? reason
: "normal exit")));
163 DEBUG(0, ("exit_server_common: "
164 "smbXsrv_session_logoff_all() failed (%s) - "
165 "triggering cleanup\n", nt_errstr(status
)));
169 change_to_root_user();
171 if (client
!= NULL
) {
172 struct smbXsrv_connection
*xconn_next
= NULL
;
174 for (xconn
= client
->connections
;
176 xconn
= xconn_next
) {
177 xconn_next
= xconn
->next
;
178 DLIST_REMOVE(client
->connections
, xconn
);
183 change_to_root_user();
185 /* 3 second timeout. */
186 print_notify_send_messages(msg_ctx
, 3);
189 /* Destroy Samba DMAPI session only if we are master smbd process */
191 if (!dmapi_destroy_session()) {
192 DEBUG(0,("Unable to close Samba DMAPI session\n"));
198 rpc_wkssvc_shutdown();
199 rpc_dssetup_shutdown();
201 rpc_rpcecho_shutdown();
203 rpc_netdfs_shutdown();
204 rpc_initshutdown_shutdown();
205 rpc_eventlog_shutdown();
206 rpc_ntsvcs_shutdown();
207 rpc_svcctl_shutdown();
208 rpc_spoolss_shutdown();
210 rpc_srvsvc_shutdown();
211 rpc_winreg_shutdown();
213 rpc_netlogon_shutdown();
215 rpc_lsarpc_shutdown();
217 rpc_FileServerVssAgent_shutdown();
219 rpc_epmapper_shutdown();
223 * we need to force the order of freeing the following,
224 * because smbd_msg_ctx is not a talloc child of smbd_server_conn.
226 if (client
!= NULL
) {
227 TALLOC_FREE(client
->sconn
);
232 netlogon_creds_cli_close_global_db();
233 TALLOC_FREE(global_smbXsrv_client
);
235 global_messaging_context_free();
236 global_event_context_free();
237 TALLOC_FREE(smbd_memcache_ctx
);
242 if (how
!= SERVER_EXIT_NORMAL
) {
249 DEBUG(3,("Server exit (%s)\n",
250 (reason
? reason
: "normal exit")));
252 pidfile_unlink(lp_pid_directory(), "smbd");
259 void smbd_exit_server(const char *const explanation
)
261 exit_server_common(SERVER_EXIT_ABNORMAL
, explanation
);
264 void smbd_exit_server_cleanly(const char *const explanation
)
266 exit_server_common(SERVER_EXIT_NORMAL
, explanation
);
270 * reinit_after_fork() wrapper that should be called when forking from
273 NTSTATUS
smbd_reinit_after_fork(struct messaging_context
*msg_ctx
,
274 struct tevent_context
*ev_ctx
,
275 bool parent_longlived
, const char *comment
)
279 ret
= reinit_after_fork(msg_ctx
, ev_ctx
, parent_longlived
, comment
);
280 initialize_password_db(true, ev_ctx
);