server.c revision 67607dda05e36c4d0be4647160ad376b89d89c51
/*
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/>.
*/
#define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>
#include "ldb.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) {
DEBUG(0,("Didn't get file descriptor %d\n",i));
return;
}
}
#endif
}
/**
Become a daemon, discarding the controlling terminal.
**/
void become_daemon(bool Fork)
{
int ret;
if (Fork) {
if (fork()) {
_exit(0);
}
}
/* detach from the terminal */
setsid();
/* chdir to / to be sure we're not on a remote filesystem */
errno = 0;
DEBUG(0, ("Cannot change directory (%d [%s])\n",
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) {
if (ret > 0) {
/* 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;
}
return err;
}
return 0;
}
{
return;
}
{
#if HAVE_GETPGRP
static int done_sigterm;
DEBUG(0,("SIGTERM: killing children\n"));
done_sigterm = 1;
}
#endif
exit(0);
}
#ifndef HAVE_PRCTL
static void sig_segv_abrt(int sig)
{
#if HAVE_GETPGRP
static int done;
done = 1;
}
#endif /* HAVE_GETPGRP */
exit(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);
#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 recieve 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;
if (read(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 tevent_signal *se,
int signum,
int count,
void *siginfo,
void *private_data)
{
}
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;
struct tevent_signal *tes;
if (!debug_prg_name) {
return ENOMEM;
}
/* 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) {
DEBUG(0, ("Error creating pidfile! (%d [%s])\n",
return ret;
}
}
/* Set up locale */
/* the event context is the top level structure.
* Everything else should hang off that */
DEBUG(0,("The event context initialiaziton failed\n"));
return 1;
}
DEBUG(0,("Out of memory, aborting!\n"));
return ENOMEM;
}
DEBUG(0,("Out of memory, aborting!\n"));
return ENOMEM;
}
DEBUG(0,("The confdb initialization failed\n"));
return ret;
}
/* set debug level if any in conf_entry */
DEBUG(0, ("Error reading from confdb (%d) [%s]\n",
return ret;
}
/* same for debug timestamps */
dt = (debug_timestamps != 0);
DEBUG(0, ("Error reading from confdb (%d) [%s]\n",
return ret;
}
else debug_timestamps = 0;
/* same for debug to file */
dl = (debug_to_file != 0);
DEBUG(0, ("Error reading from confdb (%d) [%s]\n",
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();
DEBUG(0, ("Error setting up logging (%d) [%s]\n",
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 */
}