d1ff7417dec708f31fc2746dbb24333357bb6e1a
[ira/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 "librpc/gen_ndr/messaging.h"
29
30 #include "../librpc/gen_ndr/srv_dfs.h"
31 #include "../librpc/gen_ndr/srv_dssetup.h"
32 #include "../librpc/gen_ndr/srv_echo.h"
33 #include "../librpc/gen_ndr/srv_eventlog.h"
34 #include "../librpc/gen_ndr/srv_initshutdown.h"
35 #include "../librpc/gen_ndr/srv_lsa.h"
36 #include "../librpc/gen_ndr/srv_netlogon.h"
37 #include "../librpc/gen_ndr/srv_ntsvcs.h"
38 #include "../librpc/gen_ndr/srv_samr.h"
39 #include "../librpc/gen_ndr/srv_spoolss.h"
40 #include "../librpc/gen_ndr/srv_srvsvc.h"
41 #include "../librpc/gen_ndr/srv_svcctl.h"
42 #include "../librpc/gen_ndr/srv_winreg.h"
43 #include "../librpc/gen_ndr/srv_wkssvc.h"
44 #include "printing/notify.h"
45 #include "printing.h"
46 #include "serverid.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 *const reason) _NORETURN_;
83
84 static void exit_server_common(enum server_exit_reason how,
85         const char *const reason)
86 {
87         bool had_open_conn = false;
88         struct smbd_server_connection *sconn = smbd_server_conn;
89
90         if (!exit_firsttime)
91                 exit(0);
92         exit_firsttime = false;
93
94         change_to_root_user();
95
96         if (sconn && sconn->smb1.negprot.auth_context) {
97                 TALLOC_FREE(sconn->smb1.negprot.auth_context);
98         }
99
100         if (sconn) {
101                 if (lp_log_writeable_files_on_exit()) {
102                         bool found = false;
103                         files_forall(sconn, log_writeable_file_fn, &found);
104                 }
105                 had_open_conn = conn_close_all(sconn);
106                 invalidate_all_vuids(sconn);
107         }
108
109         /* 3 second timeout. */
110         print_notify_send_messages(sconn->msg_ctx, 3);
111
112         /* delete our entry in the serverid database. */
113         if (am_parent) {
114                 /*
115                  * For children the parent takes care of cleaning up
116                  */
117                 serverid_deregister(sconn_server_id(sconn));
118         }
119
120 #ifdef WITH_DFS
121         if (dcelogin_atmost_once) {
122                 dfs_unlogin();
123         }
124 #endif
125
126 #ifdef USE_DMAPI
127         /* Destroy Samba DMAPI session only if we are master smbd process */
128         if (am_parent) {
129                 if (!dmapi_destroy_session()) {
130                         DEBUG(0,("Unable to close Samba DMAPI session\n"));
131                 }
132         }
133 #endif
134
135         if (am_parent) {
136                 rpc_wkssvc_shutdown();
137                 rpc_dssetup_shutdown();
138 #ifdef DEVELOPER
139                 rpc_rpcecho_shutdown();
140 #endif
141                 rpc_netdfs_shutdown();
142                 rpc_initshutdown_shutdown();
143                 rpc_eventlog_shutdown();
144                 rpc_ntsvcs_shutdown();
145                 rpc_svcctl_shutdown();
146                 rpc_spoolss_shutdown();
147
148                 rpc_srvsvc_shutdown();
149                 rpc_winreg_shutdown();
150
151                 rpc_netlogon_shutdown();
152                 rpc_samr_shutdown();
153                 rpc_lsarpc_shutdown();
154         }
155
156         locking_end();
157         printing_end();
158
159         /*
160          * we need to force the order of freeing the following,
161          * because smbd_msg_ctx is not a talloc child of smbd_server_conn.
162          */
163         sconn = NULL;
164         TALLOC_FREE(smbd_server_conn);
165         server_messaging_context_free();
166         server_event_context_free();
167         TALLOC_FREE(smbd_memcache_ctx);
168
169         if (how != SERVER_EXIT_NORMAL) {
170                 DEBUGSEP(0);
171                 DEBUG(0,("Abnormal server exit: %s\n",
172                         reason ? reason : "no explanation provided"));
173                 DEBUGSEP(0);
174
175                 log_stack_trace();
176
177                 dump_core();
178
179         } else {
180                 DEBUG(3,("Server exit (%s)\n",
181                         (reason ? reason : "normal exit")));
182                 if (am_parent) {
183                         pidfile_unlink();
184                 }
185                 gencache_stabilize();
186         }
187
188         /* if we had any open SMB connections when we exited then we
189            need to tell the parent smbd so that it can trigger a retry
190            of any locks we may have been holding or open files we were
191            blocking */
192         if (had_open_conn) {
193                 exit(1);
194         } else {
195                 exit(0);
196         }
197 }
198
199 void exit_server(const char *const explanation)
200 {
201         exit_server_common(SERVER_EXIT_ABNORMAL, explanation);
202 }
203
204 void exit_server_cleanly(const char *const explanation)
205 {
206         exit_server_common(SERVER_EXIT_NORMAL, explanation);
207 }