1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-24 21:34:56 +03:00
samba-mirror/lib/util/become_daemon.c

152 lines
4.0 KiB
C
Raw Normal View History

/*
Unix SMB/CIFS implementation.
Samba utility functions
Copyright (C) Andrew Tridgell 1992-1998
Copyright (C) Jeremy Allison 2001-2002
Copyright (C) Simo Sorce 2001
Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003.
Copyright (C) James J Myers 2003
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "replace.h"
#include "system/filesys.h"
#include "system/locale.h"
#if defined(HAVE_LIBSYSTEMD_DAEMON) || defined(HAVE_LIBSYSTEMD)
#include <systemd/sd-daemon.h>
#endif
#include "close_low_fd.h"
#include "debug.h"
#include "become_daemon.h"
daemons: Do not notify systemd in child processes started by main samba When samba runs as ADDC only the main 'samba' daemon have to notify its status to systemd because our systemd unit files contains implied NotifyAccess=main since commit d1740fb3d5a72cb49e30b330bb0b01e7ef3e09cc. This commit adds a function to disable the systemd notification in the smbd and winbinddd child processes started by the main 'samba' daemon in AD DC mode to avoid warnings like: systemd[1]: samba-ad-dc.service: Got notification message from PID 26194, but reception only permitted for main PID 26187 systemd[1]: samba-ad-dc.service: Got notification message from PID 26222, but reception only permitted for main PID 26187 $ pstree -p ... ├─samba(26187)─┬─tfork(26189)(26188)───s3fs[master](26189)───tfork(26194)(26193)───smbd(26194)─┬─cleanupd(+ │ │ ├─lpqd(2623+ │ │ └─smbd-noti+ │ ├─tfork(26191)(26190)───rpc[master](26191)─┬─tfork(26198)(26195)───rpc(0)(26198) │ │ ├─tfork(26200)(26199)───rpc(1)(26200) │ │ ├─tfork(26206)(26201)───rpc(2)(26206) │ │ └─tfork(26212)(26207)───rpc(3)(26212) │ ├─tfork(26196)(26192)───nbt[master](26196) │ ├─tfork(26202)(26197)───wrepl[master](26202) │ ├─tfork(26204)(26203)───ldap[master](26204)─┬─tfork(26242)(26241)───ldap(0)(26242) │ │ ├─tfork(26244)(26243)───ldap(1)(26244) │ │ ├─tfork(26246)(26245)───ldap(2)(26246) │ │ └─tfork(26248)(26247)───ldap(3)(26248) │ ├─tfork(26208)(26205)───cldap[master](26208) │ ├─tfork(26210)(26209)───kdc[master](26210)───tfork(26218)(26215)───krb5kdc(26218) │ ├─tfork(26213)(26211)───drepl[master](26213) │ ├─tfork(26216)(26214)───winbindd[master(26216)───tfork(26222)(26219)───winbindd(26222)───wi+ │ ├─tfork(26220)(26217)───ntp_signd[maste(26220) │ ├─tfork(26223)(26221)───kcc[master](26223) │ ├─tfork(26225)(26224)───dnsupdate[maste(26225) │ └─tfork(26227)(26226)───dns[master](26227) Signed-off-by: Samuel Cabrero <scabrero@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2021-02-26 12:36:02 +03:00
static bool sd_notifications = true;
/*******************************************************************
Enable or disable daemon status systemd notifications
********************************************************************/
void daemon_sd_notifications(bool enable)
{
sd_notifications = enable;
DBG_DEBUG("Daemon status systemd notifications %s\n",
sd_notifications ? "enabled" : "disabled");
}
/****************************************************************************
Become a daemon, discarding the controlling terminal.
****************************************************************************/
void become_daemon(bool do_fork, bool no_session, bool log_stdout)
{
pid_t newpid;
if (do_fork) {
newpid = fork();
if (newpid == -1) {
exit_daemon("Fork failed", errno);
}
if (newpid) {
_exit(0);
}
#if defined(HAVE_LIBSYSTEMD_DAEMON) || defined(HAVE_LIBSYSTEMD)
daemons: Do not notify systemd in child processes started by main samba When samba runs as ADDC only the main 'samba' daemon have to notify its status to systemd because our systemd unit files contains implied NotifyAccess=main since commit d1740fb3d5a72cb49e30b330bb0b01e7ef3e09cc. This commit adds a function to disable the systemd notification in the smbd and winbinddd child processes started by the main 'samba' daemon in AD DC mode to avoid warnings like: systemd[1]: samba-ad-dc.service: Got notification message from PID 26194, but reception only permitted for main PID 26187 systemd[1]: samba-ad-dc.service: Got notification message from PID 26222, but reception only permitted for main PID 26187 $ pstree -p ... ├─samba(26187)─┬─tfork(26189)(26188)───s3fs[master](26189)───tfork(26194)(26193)───smbd(26194)─┬─cleanupd(+ │ │ ├─lpqd(2623+ │ │ └─smbd-noti+ │ ├─tfork(26191)(26190)───rpc[master](26191)─┬─tfork(26198)(26195)───rpc(0)(26198) │ │ ├─tfork(26200)(26199)───rpc(1)(26200) │ │ ├─tfork(26206)(26201)───rpc(2)(26206) │ │ └─tfork(26212)(26207)───rpc(3)(26212) │ ├─tfork(26196)(26192)───nbt[master](26196) │ ├─tfork(26202)(26197)───wrepl[master](26202) │ ├─tfork(26204)(26203)───ldap[master](26204)─┬─tfork(26242)(26241)───ldap(0)(26242) │ │ ├─tfork(26244)(26243)───ldap(1)(26244) │ │ ├─tfork(26246)(26245)───ldap(2)(26246) │ │ └─tfork(26248)(26247)───ldap(3)(26248) │ ├─tfork(26208)(26205)───cldap[master](26208) │ ├─tfork(26210)(26209)───kdc[master](26210)───tfork(26218)(26215)───krb5kdc(26218) │ ├─tfork(26213)(26211)───drepl[master](26213) │ ├─tfork(26216)(26214)───winbindd[master(26216)───tfork(26222)(26219)───winbindd(26222)───wi+ │ ├─tfork(26220)(26217)───ntp_signd[maste(26220) │ ├─tfork(26223)(26221)───kcc[master](26223) │ ├─tfork(26225)(26224)───dnsupdate[maste(26225) │ └─tfork(26227)(26226)───dns[master](26227) Signed-off-by: Samuel Cabrero <scabrero@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2021-02-26 12:36:02 +03:00
} else if (sd_notifications) {
sd_notify(0, "STATUS=Starting process...");
#endif
}
/* detach from the terminal */
#ifdef HAVE_SETSID
if (!no_session) {
int ret = setsid();
if (ret == -1) {
exit_daemon("Failed to create session", errno);
}
}
#elif defined(TIOCNOTTY)
if (!no_session) {
2012-03-28 05:48:00 +04:00
int i = open("/dev/tty", O_RDWR, 0);
if (i != -1) {
ioctl(i, (int) TIOCNOTTY, (char *)0);
close(i);
}
}
#endif /* HAVE_SETSID */
/* Close fd's 0,1,2 as appropriate. Needed if started by rsh. */
/* stdin must be open if we do not fork, for monitoring for
* close. stdout must be open if we are logging there, and we
* never close stderr (but debug might dup it onto a log file) */
if (do_fork) {
int ret = close_low_fd(0);
if (ret != 0) {
exit_daemon("close_low_fd(0) failed: %s\n", errno);
}
}
if (!log_stdout) {
int ret = close_low_fd(1);
if (ret != 0) {
exit_daemon("close_low_fd(1) failed: %s\n", errno);
}
}
}
void exit_daemon(const char *msg, int error)
{
if (msg == NULL) {
msg = strerror(error);
}
#if defined(HAVE_LIBSYSTEMD_DAEMON) || defined(HAVE_LIBSYSTEMD)
daemons: Do not notify systemd in child processes started by main samba When samba runs as ADDC only the main 'samba' daemon have to notify its status to systemd because our systemd unit files contains implied NotifyAccess=main since commit d1740fb3d5a72cb49e30b330bb0b01e7ef3e09cc. This commit adds a function to disable the systemd notification in the smbd and winbinddd child processes started by the main 'samba' daemon in AD DC mode to avoid warnings like: systemd[1]: samba-ad-dc.service: Got notification message from PID 26194, but reception only permitted for main PID 26187 systemd[1]: samba-ad-dc.service: Got notification message from PID 26222, but reception only permitted for main PID 26187 $ pstree -p ... ├─samba(26187)─┬─tfork(26189)(26188)───s3fs[master](26189)───tfork(26194)(26193)───smbd(26194)─┬─cleanupd(+ │ │ ├─lpqd(2623+ │ │ └─smbd-noti+ │ ├─tfork(26191)(26190)───rpc[master](26191)─┬─tfork(26198)(26195)───rpc(0)(26198) │ │ ├─tfork(26200)(26199)───rpc(1)(26200) │ │ ├─tfork(26206)(26201)───rpc(2)(26206) │ │ └─tfork(26212)(26207)───rpc(3)(26212) │ ├─tfork(26196)(26192)───nbt[master](26196) │ ├─tfork(26202)(26197)───wrepl[master](26202) │ ├─tfork(26204)(26203)───ldap[master](26204)─┬─tfork(26242)(26241)───ldap(0)(26242) │ │ ├─tfork(26244)(26243)───ldap(1)(26244) │ │ ├─tfork(26246)(26245)───ldap(2)(26246) │ │ └─tfork(26248)(26247)───ldap(3)(26248) │ ├─tfork(26208)(26205)───cldap[master](26208) │ ├─tfork(26210)(26209)───kdc[master](26210)───tfork(26218)(26215)───krb5kdc(26218) │ ├─tfork(26213)(26211)───drepl[master](26213) │ ├─tfork(26216)(26214)───winbindd[master(26216)───tfork(26222)(26219)───winbindd(26222)───wi+ │ ├─tfork(26220)(26217)───ntp_signd[maste(26220) │ ├─tfork(26223)(26221)───kcc[master](26223) │ ├─tfork(26225)(26224)───dnsupdate[maste(26225) │ └─tfork(26227)(26226)───dns[master](26227) Signed-off-by: Samuel Cabrero <scabrero@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2021-02-26 12:36:02 +03:00
if (sd_notifications) {
sd_notifyf(0, "STATUS=daemon failed to start: %s\n"
"ERRNO=%i",
msg,
error);
daemons: Do not notify systemd in child processes started by main samba When samba runs as ADDC only the main 'samba' daemon have to notify its status to systemd because our systemd unit files contains implied NotifyAccess=main since commit d1740fb3d5a72cb49e30b330bb0b01e7ef3e09cc. This commit adds a function to disable the systemd notification in the smbd and winbinddd child processes started by the main 'samba' daemon in AD DC mode to avoid warnings like: systemd[1]: samba-ad-dc.service: Got notification message from PID 26194, but reception only permitted for main PID 26187 systemd[1]: samba-ad-dc.service: Got notification message from PID 26222, but reception only permitted for main PID 26187 $ pstree -p ... ├─samba(26187)─┬─tfork(26189)(26188)───s3fs[master](26189)───tfork(26194)(26193)───smbd(26194)─┬─cleanupd(+ │ │ ├─lpqd(2623+ │ │ └─smbd-noti+ │ ├─tfork(26191)(26190)───rpc[master](26191)─┬─tfork(26198)(26195)───rpc(0)(26198) │ │ ├─tfork(26200)(26199)───rpc(1)(26200) │ │ ├─tfork(26206)(26201)───rpc(2)(26206) │ │ └─tfork(26212)(26207)───rpc(3)(26212) │ ├─tfork(26196)(26192)───nbt[master](26196) │ ├─tfork(26202)(26197)───wrepl[master](26202) │ ├─tfork(26204)(26203)───ldap[master](26204)─┬─tfork(26242)(26241)───ldap(0)(26242) │ │ ├─tfork(26244)(26243)───ldap(1)(26244) │ │ ├─tfork(26246)(26245)───ldap(2)(26246) │ │ └─tfork(26248)(26247)───ldap(3)(26248) │ ├─tfork(26208)(26205)───cldap[master](26208) │ ├─tfork(26210)(26209)───kdc[master](26210)───tfork(26218)(26215)───krb5kdc(26218) │ ├─tfork(26213)(26211)───drepl[master](26213) │ ├─tfork(26216)(26214)───winbindd[master(26216)───tfork(26222)(26219)───winbindd(26222)───wi+ │ ├─tfork(26220)(26217)───ntp_signd[maste(26220) │ ├─tfork(26223)(26221)───kcc[master](26223) │ ├─tfork(26225)(26224)───dnsupdate[maste(26225) │ └─tfork(26227)(26226)───dns[master](26227) Signed-off-by: Samuel Cabrero <scabrero@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2021-02-26 12:36:02 +03:00
}
#endif
DBG_ERR("daemon failed to start: %s, error code %d\n",
msg, error);
exit(1);
}
void daemon_ready(const char *daemon)
{
if (daemon == NULL) {
daemon = "Samba";
}
#if defined(HAVE_LIBSYSTEMD_DAEMON) || defined(HAVE_LIBSYSTEMD)
daemons: Do not notify systemd in child processes started by main samba When samba runs as ADDC only the main 'samba' daemon have to notify its status to systemd because our systemd unit files contains implied NotifyAccess=main since commit d1740fb3d5a72cb49e30b330bb0b01e7ef3e09cc. This commit adds a function to disable the systemd notification in the smbd and winbinddd child processes started by the main 'samba' daemon in AD DC mode to avoid warnings like: systemd[1]: samba-ad-dc.service: Got notification message from PID 26194, but reception only permitted for main PID 26187 systemd[1]: samba-ad-dc.service: Got notification message from PID 26222, but reception only permitted for main PID 26187 $ pstree -p ... ├─samba(26187)─┬─tfork(26189)(26188)───s3fs[master](26189)───tfork(26194)(26193)───smbd(26194)─┬─cleanupd(+ │ │ ├─lpqd(2623+ │ │ └─smbd-noti+ │ ├─tfork(26191)(26190)───rpc[master](26191)─┬─tfork(26198)(26195)───rpc(0)(26198) │ │ ├─tfork(26200)(26199)───rpc(1)(26200) │ │ ├─tfork(26206)(26201)───rpc(2)(26206) │ │ └─tfork(26212)(26207)───rpc(3)(26212) │ ├─tfork(26196)(26192)───nbt[master](26196) │ ├─tfork(26202)(26197)───wrepl[master](26202) │ ├─tfork(26204)(26203)───ldap[master](26204)─┬─tfork(26242)(26241)───ldap(0)(26242) │ │ ├─tfork(26244)(26243)───ldap(1)(26244) │ │ ├─tfork(26246)(26245)───ldap(2)(26246) │ │ └─tfork(26248)(26247)───ldap(3)(26248) │ ├─tfork(26208)(26205)───cldap[master](26208) │ ├─tfork(26210)(26209)───kdc[master](26210)───tfork(26218)(26215)───krb5kdc(26218) │ ├─tfork(26213)(26211)───drepl[master](26213) │ ├─tfork(26216)(26214)───winbindd[master(26216)───tfork(26222)(26219)───winbindd(26222)───wi+ │ ├─tfork(26220)(26217)───ntp_signd[maste(26220) │ ├─tfork(26223)(26221)───kcc[master](26223) │ ├─tfork(26225)(26224)───dnsupdate[maste(26225) │ └─tfork(26227)(26226)───dns[master](26227) Signed-off-by: Samuel Cabrero <scabrero@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2021-02-26 12:36:02 +03:00
if (sd_notifications) {
sd_notifyf(0,
"READY=1\nSTATUS=%s: ready to serve connections...",
daemon);
}
#endif
DBG_INFO("daemon '%s' finished starting up and ready to serve "
"connections\n", daemon);
}
void daemon_status(const char *daemon, const char *msg)
{
if (daemon == NULL) {
daemon = "Samba";
}
#if defined(HAVE_LIBSYSTEMD_DAEMON) || defined(HAVE_LIBSYSTEMD)
daemons: Do not notify systemd in child processes started by main samba When samba runs as ADDC only the main 'samba' daemon have to notify its status to systemd because our systemd unit files contains implied NotifyAccess=main since commit d1740fb3d5a72cb49e30b330bb0b01e7ef3e09cc. This commit adds a function to disable the systemd notification in the smbd and winbinddd child processes started by the main 'samba' daemon in AD DC mode to avoid warnings like: systemd[1]: samba-ad-dc.service: Got notification message from PID 26194, but reception only permitted for main PID 26187 systemd[1]: samba-ad-dc.service: Got notification message from PID 26222, but reception only permitted for main PID 26187 $ pstree -p ... ├─samba(26187)─┬─tfork(26189)(26188)───s3fs[master](26189)───tfork(26194)(26193)───smbd(26194)─┬─cleanupd(+ │ │ ├─lpqd(2623+ │ │ └─smbd-noti+ │ ├─tfork(26191)(26190)───rpc[master](26191)─┬─tfork(26198)(26195)───rpc(0)(26198) │ │ ├─tfork(26200)(26199)───rpc(1)(26200) │ │ ├─tfork(26206)(26201)───rpc(2)(26206) │ │ └─tfork(26212)(26207)───rpc(3)(26212) │ ├─tfork(26196)(26192)───nbt[master](26196) │ ├─tfork(26202)(26197)───wrepl[master](26202) │ ├─tfork(26204)(26203)───ldap[master](26204)─┬─tfork(26242)(26241)───ldap(0)(26242) │ │ ├─tfork(26244)(26243)───ldap(1)(26244) │ │ ├─tfork(26246)(26245)───ldap(2)(26246) │ │ └─tfork(26248)(26247)───ldap(3)(26248) │ ├─tfork(26208)(26205)───cldap[master](26208) │ ├─tfork(26210)(26209)───kdc[master](26210)───tfork(26218)(26215)───krb5kdc(26218) │ ├─tfork(26213)(26211)───drepl[master](26213) │ ├─tfork(26216)(26214)───winbindd[master(26216)───tfork(26222)(26219)───winbindd(26222)───wi+ │ ├─tfork(26220)(26217)───ntp_signd[maste(26220) │ ├─tfork(26223)(26221)───kcc[master](26223) │ ├─tfork(26225)(26224)───dnsupdate[maste(26225) │ └─tfork(26227)(26226)───dns[master](26227) Signed-off-by: Samuel Cabrero <scabrero@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2021-02-26 12:36:02 +03:00
if (sd_notifications) {
sd_notifyf(0, "STATUS=%s: %s", daemon, msg);
}
#endif
DBG_ERR("daemon '%s' : %s\n", daemon, msg);
}