nsswitch: Fix getting data out of pam_get_data()
[Samba.git] / source3 / smbd / server_exit.c
blob5e0c386e090b79e03af3dad145d4304898065c8c
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
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/>.
25 #include "includes.h"
26 #include "locking/share_mode_lock.h"
27 #include "smbd/smbd.h"
28 #include "smbd/globals.h"
29 #include "ntdomain.h"
30 #include "librpc/rpc/dcesrv_core.h"
31 #include "printing/notify.h"
32 #include "printing.h"
33 #include "serverid.h"
34 #include "messages.h"
35 #include "passdb.h"
36 #include "../lib/util/pidfile.h"
37 #include "smbprofile.h"
38 #include "libcli/auth/netlogon_creds_cli.h"
39 #include "lib/gencache.h"
40 #include "rpc_server/rpc_config.h"
41 #include "lib/global_contexts.h"
43 static struct files_struct *log_writeable_file_fn(
44 struct files_struct *fsp, void *private_data)
46 bool *found = (bool *)private_data;
47 char *path;
49 if (!fsp->fsp_flags.can_write) {
50 return NULL;
52 if (!(*found)) {
53 DEBUG(0, ("Writable files open at exit:\n"));
54 *found = true;
57 path = talloc_asprintf(talloc_tos(), "%s/%s", fsp->conn->connectpath,
58 smb_fname_str_dbg(fsp->fsp_name));
59 if (path == NULL) {
60 DEBUGADD(0, ("<NOMEM>\n"));
63 DEBUGADD(0, ("%s\n", path));
65 TALLOC_FREE(path);
66 return NULL;
69 /****************************************************************************
70 Exit the server.
71 ****************************************************************************/
73 /* Reasons for shutting down a server process. */
74 enum server_exit_reason { SERVER_EXIT_NORMAL, SERVER_EXIT_ABNORMAL };
76 static void exit_server_common(enum server_exit_reason how,
77 const char *reason) _NORETURN_;
79 static void exit_server_common(enum server_exit_reason how,
80 const char *reason)
82 struct smbXsrv_client *client = global_smbXsrv_client;
83 struct smbXsrv_connection *xconn = NULL;
84 struct smbd_server_connection *sconn = NULL;
85 NTSTATUS disconnect_status;
87 if (!exit_firsttime) {
88 exit(0);
90 exit_firsttime = false;
92 switch (how) {
93 case SERVER_EXIT_NORMAL:
94 disconnect_status = NT_STATUS_LOCAL_DISCONNECT;
95 break;
96 case SERVER_EXIT_ABNORMAL:
97 default:
98 disconnect_status = NT_STATUS_INTERNAL_ERROR;
99 break;
102 if (client != NULL) {
103 NTSTATUS status;
105 sconn = client->sconn;
106 xconn = client->connections;
109 * Make sure we stop handling new multichannel
110 * connections early!
112 * From here, we're not able to handle them.
114 status = smbXsrv_client_remove(client);
115 if (!NT_STATUS_IS_OK(status)) {
116 D_ERR("Server exit (%s)\n",
117 (reason ? reason : "normal exit"));
118 DBG_ERR("smbXsrv_client_remove() failed (%s)\n",
119 nt_errstr(status));
123 change_to_root_user();
127 * Here we typically have just one connection
129 for (; xconn != NULL; xconn = xconn->next) {
131 * This is typically the disconnect for the only
132 * (or with multi-channel last) connection of the client.
134 * smbXsrv_connection_disconnect_transport() might be called already,
135 * but calling it again is a no-op.
137 smbXsrv_connection_disconnect_transport(xconn, disconnect_status);
140 change_to_root_user();
142 if (sconn != NULL) {
143 if (lp_log_writeable_files_on_exit()) {
144 bool found = false;
145 files_forall(sconn, log_writeable_file_fn, &found);
149 change_to_root_user();
151 if (client != NULL) {
152 NTSTATUS status;
155 * Note: this is a no-op for smb2 as
156 * conn->tcon_table is empty
158 status = smb1srv_tcon_disconnect_all(client);
159 if (!NT_STATUS_IS_OK(status)) {
160 DEBUG(0,("Server exit (%s)\n",
161 (reason ? reason : "normal exit")));
162 DEBUG(0, ("exit_server_common: "
163 "smb1srv_tcon_disconnect_all() failed (%s) - "
164 "triggering cleanup\n", nt_errstr(status)));
167 status = smbXsrv_session_logoff_all(client);
168 if (!NT_STATUS_IS_OK(status)) {
169 DEBUG(0,("Server exit (%s)\n",
170 (reason ? reason : "normal exit")));
171 DEBUG(0, ("exit_server_common: "
172 "smbXsrv_session_logoff_all() failed (%s) - "
173 "triggering cleanup\n", nt_errstr(status)));
177 change_to_root_user();
179 if (client != NULL) {
180 struct smbXsrv_connection *xconn_next = NULL;
182 for (xconn = client->connections;
183 xconn != NULL;
184 xconn = xconn_next) {
185 xconn_next = xconn->next;
186 DLIST_REMOVE(client->connections, xconn);
187 TALLOC_FREE(xconn);
191 change_to_root_user();
193 #ifdef USE_DMAPI
194 /* Destroy Samba DMAPI session only if we are master smbd process */
195 if (am_parent) {
196 if (!dmapi_destroy_session()) {
197 DEBUG(0,("Unable to close Samba DMAPI session\n"));
200 #endif
204 * we need to force the order of freeing the following,
205 * because smbd_msg_ctx is not a talloc child of smbd_server_conn.
207 if (client != NULL) {
208 TALLOC_FREE(client->sconn);
210 sconn = NULL;
211 xconn = NULL;
212 client = NULL;
213 netlogon_creds_cli_close_global_db();
214 TALLOC_FREE(global_smbXsrv_client);
215 smbprofile_dump();
216 global_messaging_context_free();
217 global_event_context_free();
218 TALLOC_FREE(smbd_memcache_ctx);
220 locking_end();
222 if (how != SERVER_EXIT_NORMAL) {
224 smb_panic(reason);
226 /* Notreached. */
227 exit(1);
228 } else {
229 DEBUG(3,("Server exit (%s)\n",
230 (reason ? reason : "normal exit")));
231 if (am_parent) {
232 pidfile_unlink(lp_pid_directory(), "smbd");
236 exit(0);
239 void smbd_exit_server(const char *const explanation)
241 exit_server_common(SERVER_EXIT_ABNORMAL, explanation);
244 void smbd_exit_server_cleanly(const char *const explanation)
246 exit_server_common(SERVER_EXIT_NORMAL, explanation);
250 * reinit_after_fork() wrapper that should be called when forking from
251 * smbd.
253 NTSTATUS smbd_reinit_after_fork(struct messaging_context *msg_ctx,
254 struct tevent_context *ev_ctx,
255 bool parent_longlived)
257 NTSTATUS ret;
258 am_parent = NULL;
259 ret = reinit_after_fork(msg_ctx, ev_ctx, parent_longlived);
260 initialize_password_db(true, ev_ctx);
261 return ret;