server.c revision a3c8390d19593b1e5277d95bfb4ab206d4785150
/*
SSSD
Servers setup routines
Copyright (C) Andrew Tridgell 1992-2005
Copyright (C) Martin Pool 2002
Copyright (C) Jelmer Vernooij 2002
Copyright (C) James J Myers 2003 <myersjj@samba.org>
Copyright (C) Simo Sorce 2008
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 <fcntl.h>
#include <unistd.h>
#include <ldb.h>
#include "monitor/monitor_interfaces.h"
#ifdef HAVE_PRCTL
#endif
/*******************************************************************
********************************************************************/
static void close_low_fds(void)
{
#ifndef VALGRIND
int fd;
int i;
close(0);
close(1);
close(2);
/* try and use up these file descriptors, so silly
library routines writing to stdout etc won't cause havoc */
for (i = 0; i < 3; i++) {
if (fd < 0)
if (fd < 0) {
return;
}
if (fd != i) {
return;
}
}
#endif
}
static void deamon_parent_sigterm(int sig)
{
_exit(0);
}
/**
Become a daemon, discarding the controlling terminal.
**/
void become_daemon(bool Fork)
{
int status;
if (Fork) {
if (pid != 0) {
/* Terminate parent process on demand so we can hold systemd
* or initd from starting next service until sssd in initialized.
* We use signals directly here because we don't have a tevent
* context yet. */
/* or exit when sssd monitor is terminated */
do {
errno = 0;
if (cpid == 1) {
/* An error occurred while waiting */
"Error [%d][%s] while waiting for child\n",
/* Forcibly kill this child */
ret = 1;
}
}
error = 0;
/* return error if we didn't exited normally */
ret = 1;
/* but return our exit code otherwise */
}
}
}
/* detach from the terminal */
setsid();
/* chdir to / to be sure we're not on a remote filesystem */
errno = 0;
return;
}
/* Close fd's 0,1,2. Needed if started by rsh */
}
{
char pid_str[32];
char *file;
int fd;
if (!file) {
return ENOMEM;
}
if (fd != -1) {
errno = 0;
if (len == -1) {
return EINVAL;
}
/* Ensure NULL-termination */
/* let's check the pid */
if (pid != 0) {
errno = 0;
/* succeeded in signaling the process -> another sssd process */
if (ret == 0) {
return EEXIST;
}
return err;
}
}
/* nothing in the file or no process */
} else {
return err;
}
}
if (fd == -1) {
return err;
}
errno = 0;
if (written == -1) {
return err;
}
return EIO;
}
return 0;
}
void orderly_shutdown(int status)
{
#if HAVE_GETPGRP
static int sent_sigterm;
sent_sigterm = 1;
}
#endif
}
struct tevent_signal *se,
int signum,
int count,
void *siginfo,
void *private_data)
{
orderly_shutdown(0);
}
#ifndef HAVE_PRCTL
static void sig_segv_abrt(int sig)
{
orderly_shutdown(1);
}
#endif /* HAVE_PRCTL */
/*
setup signal masks
*/
static void setup_signals(void)
{
/* we are never interested in SIGPIPE */
BlockSignals(true, SIGPIPE);
#if defined(SIGFPE)
/* we are never interested in SIGFPE */
BlockSignals(true, SIGFPE);
#endif
/* We are no longer interested in USR1 */
BlockSignals(true, SIGUSR1);
/* We are no longer interested in SIGINT except for monitor */
BlockSignals(true, SIGINT);
#if defined(SIGUSR2)
/* We are no longer interested in USR2 */
BlockSignals(true, SIGUSR2);
#endif
/* POSIX demands that signals are inherited. If the invoking process has
* these signals masked, we will have problems, as we won't receive them. */
BlockSignals(false, SIGHUP);
BlockSignals(false, SIGTERM);
#ifndef HAVE_PRCTL
/* If prctl is not defined on the system, try to handle
* some common termination signals gracefully */
#endif
}
/*
handle io on stdin
*/
{
const char *binary_name = (const char *)private;
uint8_t c;
errno = 0;
if (sss_atomic_read_s(0, &c, 1) == 0) {
#if HAVE_GETPGRP
}
#endif
exit(0);
}
}
/*
main server helpers.
*/
int die_if_parent_died(void)
{
#ifdef HAVE_PRCTL
int ret;
errno = 0;
if (ret != 0) {
return ret;
}
#endif
return EOK;
}
struct logrotate_ctx {
struct confdb_ctx *confdb;
const char *confdb_path;
};
struct tevent_signal *se,
int signum,
int count,
void *siginfo,
void *private_data)
{
struct logrotate_ctx *lctx =
}
}
const char *conf_entry,
struct main_context **main_ctx)
{
struct tevent_context *event_ctx;
struct main_context *ctx;
char *conf_db;
bool dt;
bool dl;
bool dm;
struct tevent_signal *tes;
struct logrotate_ctx *lctx;
if (!debug_prg_name) {
return ENOMEM;
}
/* To make sure the domain cannot be set from the environment, unset the
* variable explicitly when setting up any server. Backends later set the
* value after reading domain from the configuration */
if (ret != 0) {
"logging mightnot work as expected\n");
}
/* we want default permissions on created files to be very strict,
so set our umask to 0177 */
umask(0177);
if (flags & FLAGS_DAEMON) {
become_daemon(true);
}
if (flags & FLAGS_PID_FILE) {
return ret;
}
}
/* Set up locale */
/* the event context is the top level structure.
* Everything else should hang off that */
"The event context initialiaziton failed\n");
return 1;
}
/* Set up an event handler for a SIGINT */
default_quit, NULL);
return EIO;
}
/* Set up an event handler for a SIGTERM */
default_quit, NULL);
return EIO;
}
return ENOMEM;
}
return ENOMEM;
}
return ret;
}
if (debug_level == SSSDBG_UNRESOLVED) {
/* set debug level if any in conf_entry */
&debug_level);
return ret;
}
}
/* same for debug timestamps */
if (debug_timestamps == SSSDBG_TIMESTAMP_UNRESOLVED) {
&dt);
return ret;
}
else debug_timestamps = 0;
}
/* same for debug microseconds */
&dm);
return ret;
}
else debug_microseconds = 0;
}
/* same for debug to file */
dl = (debug_to_file != 0);
return ret;
}
/* before opening the log file set up log rotation */
return EIO;
}
/* open log file if told so */
if (debug_to_file) {
ret = open_debug_file();
return ret;
}
}
if (flags & FLAGS_INTERACTIVE) {
/* terminate when stdin goes away */
} else {
/* stay alive forever */
stdin_event_flags = 0;
}
/* catch EOF on stdin */
#ifdef SIGTTIN
#endif
return EOK;
}
{
/* wait for events - this is where the server sits for most of its
life */
/* as everything hangs off this event context, freeing it
should initiate a clean shutdown of all services */
}