s3: Remove serverid_[de]register_self
[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/globals.h"
27 #include "librpc/gen_ndr/messaging.h"
28
29 static struct files_struct *log_writeable_file_fn(
30         struct files_struct *fsp, void *private_data)
31 {
32         bool *found = (bool *)private_data;
33         char *path;
34
35         if (!fsp->can_write) {
36                 return NULL;
37         }
38         if (!(*found)) {
39                 DEBUG(0, ("Writable files open at exit:\n"));
40                 *found = true;
41         }
42
43         path = talloc_asprintf(talloc_tos(), "%s/%s", fsp->conn->connectpath,
44                                smb_fname_str_dbg(fsp->fsp_name));
45         if (path == NULL) {
46                 DEBUGADD(0, ("<NOMEM>\n"));
47         }
48
49         DEBUGADD(0, ("%s\n", path));
50
51         TALLOC_FREE(path);
52         return NULL;
53 }
54
55 /****************************************************************************
56  Exit the server.
57 ****************************************************************************/
58
59 /* Reasons for shutting down a server process. */
60 enum server_exit_reason { SERVER_EXIT_NORMAL, SERVER_EXIT_ABNORMAL };
61
62 static void exit_server_common(enum server_exit_reason how,
63         const char *const reason) _NORETURN_;
64
65 static void exit_server_common(enum server_exit_reason how,
66         const char *const reason)
67 {
68         bool had_open_conn = false;
69         struct smbd_server_connection *sconn = smbd_server_conn;
70
71         if (!exit_firsttime)
72                 exit(0);
73         exit_firsttime = false;
74
75         change_to_root_user();
76
77         if (sconn && sconn->smb1.negprot.auth_context) {
78                 struct auth_context *a = sconn->smb1.negprot.auth_context;
79                 a->free(&sconn->smb1.negprot.auth_context);
80         }
81
82         if (lp_log_writeable_files_on_exit()) {
83                 bool found = false;
84                 files_forall(log_writeable_file_fn, &found);
85         }
86
87         if (sconn) {
88                 had_open_conn = conn_close_all(sconn);
89                 invalidate_all_vuids(sconn);
90         }
91
92         /* 3 second timeout. */
93         print_notify_send_messages(smbd_messaging_context(), 3);
94
95         /* delete our entry in the serverid database. */
96         if (am_parent) {
97                 /*
98                  * For children the parent takes care of cleaning up
99                  */
100                 serverid_deregister(procid_self());
101         }
102
103 #ifdef WITH_DFS
104         if (dcelogin_atmost_once) {
105                 dfs_unlogin();
106         }
107 #endif
108
109 #ifdef USE_DMAPI
110         /* Destroy Samba DMAPI session only if we are master smbd process */
111         if (am_parent) {
112                 if (!dmapi_destroy_session()) {
113                         DEBUG(0,("Unable to close Samba DMAPI session\n"));
114                 }
115         }
116 #endif
117
118         locking_end();
119         printing_end();
120
121         /*
122          * we need to force the order of freeing the following,
123          * because smbd_msg_ctx is not a talloc child of smbd_server_conn.
124          */
125         sconn = NULL;
126         TALLOC_FREE(smbd_server_conn);
127         server_messaging_context_free();
128         server_event_context_free();
129         TALLOC_FREE(smbd_memcache_ctx);
130
131         if (how != SERVER_EXIT_NORMAL) {
132                 int oldlevel = DEBUGLEVEL;
133
134                 DEBUGLEVEL = 10;
135
136                 DEBUGSEP(0);
137                 DEBUG(0,("Abnormal server exit: %s\n",
138                         reason ? reason : "no explanation provided"));
139                 DEBUGSEP(0);
140
141                 log_stack_trace();
142
143                 DEBUGLEVEL = oldlevel;
144                 dump_core();
145
146         } else {
147                 DEBUG(3,("Server exit (%s)\n",
148                         (reason ? reason : "normal exit")));
149                 if (am_parent) {
150                         pidfile_unlink();
151                 }
152                 gencache_stabilize();
153         }
154
155         /* if we had any open SMB connections when we exited then we
156            need to tell the parent smbd so that it can trigger a retry
157            of any locks we may have been holding or open files we were
158            blocking */
159         if (had_open_conn) {
160                 exit(1);
161         } else {
162                 exit(0);
163         }
164 }
165
166 void exit_server(const char *const explanation)
167 {
168         exit_server_common(SERVER_EXIT_ABNORMAL, explanation);
169 }
170
171 void exit_server_cleanly(const char *const explanation)
172 {
173         exit_server_common(SERVER_EXIT_NORMAL, explanation);
174 }
175
176 void exit_server_fault(void)
177 {
178         exit_server("critical server fault");
179 }