LCOV - code coverage report
Current view: top level - source3/smbd - server_exit.c (source / functions) Hit Total Coverage
Test: coverage report for master 6248eab5 Lines: 65 95 68.4 %
Date: 2021-08-25 13:27:56 Functions: 3 5 60.0 %

          Line data    Source code
       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 "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"
      42             : 
      43           0 : static struct files_struct *log_writeable_file_fn(
      44             :         struct files_struct *fsp, void *private_data)
      45             : {
      46           0 :         bool *found = (bool *)private_data;
      47             :         char *path;
      48             : 
      49           0 :         if (!fsp->fsp_flags.can_write) {
      50           0 :                 return NULL;
      51             :         }
      52           0 :         if (!(*found)) {
      53           0 :                 DEBUG(0, ("Writable files open at exit:\n"));
      54           0 :                 *found = true;
      55             :         }
      56             : 
      57           0 :         path = talloc_asprintf(talloc_tos(), "%s/%s", fsp->conn->connectpath,
      58           0 :                                smb_fname_str_dbg(fsp->fsp_name));
      59           0 :         if (path == NULL) {
      60           0 :                 DEBUGADD(0, ("<NOMEM>\n"));
      61             :         }
      62             : 
      63           0 :         DEBUGADD(0, ("%s\n", path));
      64             : 
      65           0 :         TALLOC_FREE(path);
      66           0 :         return NULL;
      67             : }
      68             : 
      69             : /****************************************************************************
      70             :  Exit the server.
      71             : ****************************************************************************/
      72             : 
      73             : /* Reasons for shutting down a server process. */
      74             : enum server_exit_reason { SERVER_EXIT_NORMAL, SERVER_EXIT_ABNORMAL };
      75             : 
      76             : static void exit_server_common(enum server_exit_reason how,
      77             :         const char *reason) _NORETURN_;
      78             : 
      79       27494 : static void exit_server_common(enum server_exit_reason how,
      80             :         const char *reason)
      81             : {
      82       27494 :         struct smbXsrv_client *client = global_smbXsrv_client;
      83       27494 :         struct smbXsrv_connection *xconn = NULL;
      84       27494 :         struct smbd_server_connection *sconn = NULL;
      85       27494 :         struct messaging_context *msg_ctx = global_messaging_context();
      86             :         NTSTATUS disconnect_status;
      87             : 
      88       27494 :         if (!exit_firsttime) {
      89           0 :                 exit(0);
      90             :         }
      91       27494 :         exit_firsttime = false;
      92             : 
      93       27494 :         switch (how) {
      94       27494 :         case SERVER_EXIT_NORMAL:
      95       27494 :                 disconnect_status = NT_STATUS_LOCAL_DISCONNECT;
      96       27494 :                 break;
      97           0 :         case SERVER_EXIT_ABNORMAL:
      98             :         default:
      99           0 :                 disconnect_status = NT_STATUS_INTERNAL_ERROR;
     100           0 :                 break;
     101             :         }
     102             : 
     103       27494 :         if (client != NULL) {
     104             :                 NTSTATUS status;
     105             : 
     106       27490 :                 sconn = client->sconn;
     107       27490 :                 xconn = client->connections;
     108             : 
     109             :                 /*
     110             :                  * Make sure we stop handling new multichannel
     111             :                  * connections early!
     112             :                  *
     113             :                  * From here, we're not able to handle them.
     114             :                  */
     115       27490 :                 status = smbXsrv_client_remove(client);
     116       27490 :                 if (!NT_STATUS_IS_OK(status)) {
     117           0 :                         D_ERR("Server exit (%s)\n",
     118             :                               (reason ? reason : "normal exit"));
     119           0 :                         DBG_ERR("smbXsrv_client_remove() failed (%s)\n",
     120             :                                 nt_errstr(status));
     121             :                 }
     122             :         }
     123             : 
     124       27494 :         change_to_root_user();
     125             : 
     126             : 
     127             :         /*
     128             :          * Here we typically have just one connection
     129             :          */
     130       55016 :         for (; xconn != NULL; xconn = xconn->next) {
     131             :                 /*
     132             :                  * This is typically the disconnect for the only
     133             :                  * (or with multi-channel last) connection of the client.
     134             :                  *
     135             :                  * smbXsrv_connection_disconnect_transport() might be called already,
     136             :                  * but calling it again is a no-op.
     137             :                  */
     138       27522 :                 smbXsrv_connection_disconnect_transport(xconn, disconnect_status);
     139             :         }
     140             : 
     141       27494 :         change_to_root_user();
     142             : 
     143       27494 :         if (sconn != NULL) {
     144       27490 :                 if (lp_log_writeable_files_on_exit()) {
     145           0 :                         bool found = false;
     146           0 :                         files_forall(sconn, log_writeable_file_fn, &found);
     147             :                 }
     148             :         }
     149             : 
     150       27494 :         change_to_root_user();
     151             : 
     152       27494 :         if (client != NULL) {
     153             :                 NTSTATUS status;
     154             : 
     155             :                 /*
     156             :                  * Note: this is a no-op for smb2 as
     157             :                  * conn->tcon_table is empty
     158             :                  */
     159       27490 :                 status = smb1srv_tcon_disconnect_all(client);
     160       27490 :                 if (!NT_STATUS_IS_OK(status)) {
     161           0 :                         DEBUG(0,("Server exit (%s)\n",
     162             :                                 (reason ? reason : "normal exit")));
     163           0 :                         DEBUG(0, ("exit_server_common: "
     164             :                                   "smb1srv_tcon_disconnect_all() failed (%s) - "
     165             :                                   "triggering cleanup\n", nt_errstr(status)));
     166             :                 }
     167             : 
     168       27490 :                 status = smbXsrv_session_logoff_all(client);
     169       27490 :                 if (!NT_STATUS_IS_OK(status)) {
     170          36 :                         DEBUG(0,("Server exit (%s)\n",
     171             :                                 (reason ? reason : "normal exit")));
     172          36 :                         DEBUG(0, ("exit_server_common: "
     173             :                                   "smbXsrv_session_logoff_all() failed (%s) - "
     174             :                                   "triggering cleanup\n", nt_errstr(status)));
     175             :                 }
     176             :         }
     177             : 
     178       27494 :         change_to_root_user();
     179             : 
     180       27494 :         if (client != NULL) {
     181       27490 :                 struct smbXsrv_connection *xconn_next = NULL;
     182             : 
     183       82502 :                 for (xconn = client->connections;
     184             :                      xconn != NULL;
     185       27522 :                      xconn = xconn_next) {
     186       27522 :                         xconn_next = xconn->next;
     187       27522 :                         DLIST_REMOVE(client->connections, xconn);
     188       27522 :                         TALLOC_FREE(xconn);
     189             :                 }
     190             :         }
     191             : 
     192       27494 :         change_to_root_user();
     193             : 
     194             :         /* 3 second timeout. */
     195       27494 :         print_notify_send_messages(msg_ctx, 3);
     196             : 
     197             : #ifdef USE_DMAPI
     198             :         /* Destroy Samba DMAPI session only if we are master smbd process */
     199             :         if (am_parent) {
     200             :                 if (!dmapi_destroy_session()) {
     201             :                         DEBUG(0,("Unable to close Samba DMAPI session\n"));
     202             :                 }
     203             :         }
     204             : #endif
     205             : 
     206       27494 :         if (am_parent && sconn != NULL) {
     207           0 :                 dcesrv_shutdown_registered_ep_servers(sconn->dce_ctx);
     208             : 
     209           0 :                 global_dcesrv_context_free();
     210             :         }
     211             : 
     212             :         /*
     213             :          * we need to force the order of freeing the following,
     214             :          * because smbd_msg_ctx is not a talloc child of smbd_server_conn.
     215             :          */
     216       27494 :         if (client != NULL) {
     217       27490 :                 TALLOC_FREE(client->sconn);
     218             :         }
     219       27494 :         sconn = NULL;
     220       27494 :         xconn = NULL;
     221       27494 :         client = NULL;
     222       27494 :         netlogon_creds_cli_close_global_db();
     223       27494 :         TALLOC_FREE(global_smbXsrv_client);
     224       27494 :         smbprofile_dump();
     225       27494 :         global_messaging_context_free();
     226       27494 :         global_event_context_free();
     227       27494 :         TALLOC_FREE(smbd_memcache_ctx);
     228             : 
     229       27494 :         locking_end();
     230       27494 :         printing_end();
     231             : 
     232       27494 :         if (how != SERVER_EXIT_NORMAL) {
     233             : 
     234           0 :                 smb_panic(reason);
     235             : 
     236             :                 /* Notreached. */
     237             :                 exit(1);
     238             :         } else {
     239       27494 :                 DEBUG(3,("Server exit (%s)\n",
     240             :                         (reason ? reason : "normal exit")));
     241       27494 :                 if (am_parent) {
     242           0 :                         pidfile_unlink(lp_pid_directory(), "smbd");
     243             :                 }
     244             :         }
     245             : 
     246       27494 :         exit(0);
     247             : }
     248             : 
     249           0 : void smbd_exit_server(const char *const explanation)
     250             : {
     251           0 :         exit_server_common(SERVER_EXIT_ABNORMAL, explanation);
     252             : }
     253             : 
     254       27494 : void smbd_exit_server_cleanly(const char *const explanation)
     255             : {
     256       27494 :         exit_server_common(SERVER_EXIT_NORMAL, explanation);
     257             : }
     258             : 
     259             : /*
     260             :  * reinit_after_fork() wrapper that should be called when forking from
     261             :  * smbd.
     262             :  */
     263       27526 : NTSTATUS smbd_reinit_after_fork(struct messaging_context *msg_ctx,
     264             :                                 struct tevent_context *ev_ctx,
     265             :                                 bool parent_longlived, const char *comment)
     266             : {
     267             :         NTSTATUS ret;
     268       27526 :         am_parent = NULL;
     269       27526 :         ret = reinit_after_fork(msg_ctx, ev_ctx, parent_longlived, comment);
     270       27526 :         initialize_password_db(true, ev_ctx);
     271       27526 :         return ret;
     272             : }

Generated by: LCOV version 1.13