monitor.c revision 0ae402cee3485fb5830ce0ffcf9e566c38fa287f
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/*
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher SSSD
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher Service monitor
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek Copyright (C) Simo Sorce 2008
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek This program is free software; you can redistribute it and/or modify
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher it under the terms of the GNU General Public License as published by
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher the Free Software Foundation; either version 3 of the License, or
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher (at your option) any later version.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek This program is distributed in the hope that it will be useful,
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher GNU General Public License for more details.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher You should have received a copy of the GNU General Public License
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher along with this program. If not, see <http://www.gnu.org/licenses/>.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher*/
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "util/util.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "util/child_common.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <sys/types.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <sys/wait.h>
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek#include <sys/time.h>
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek#include <sys/param.h>
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek#include <time.h>
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek#include <string.h>
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek#ifdef HAVE_SYS_INOTIFY_H
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher#include <sys/inotify.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#endif
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher#include <sys/types.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <sys/stat.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <unistd.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <fcntl.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher/* Needed for res_init() */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <netinet/in.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <arpa/nameser.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <resolv.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "popt.h"
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher#include "tevent.h"
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek#include "confdb/confdb.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "confdb/confdb_setup.h"
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher#include "collection.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "ini_config.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "db/sysdb.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "monitor/monitor.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "dbus/dbus.h"
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher#include "sbus/sssd_dbus.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "monitor/monitor_interfaces.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "responder/common/responder_sbus.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#ifdef USE_KEYRING
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <keyutils.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#endif
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* ping time cannot be less then once every few seconds or the
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher * monitor will get crazy hammering children with messages */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher#define MONITOR_DEF_PING_TIME 10
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher/* terminate the child after this interval by default if it
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * doesn't shutdown on receiving SIGTERM */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define MONITOR_DEF_FORCE_TIME 60
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* TODO: get the restart related values from config */
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek#define MONITOR_RESTART_CNT_INTERVAL_RESET 30
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek/* maximum allowed number of service restarts if the restarts
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek * were less than MONITOR_RESTART_CNT_INTERVAL_RESET apart, which would
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek * indicate a crash after startup or after every request */
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek#define MONITOR_MAX_SVC_RESTARTS 2
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher/* The services are restarted with a delay in case the restart was
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * hitting a race condition where the DP is not ready yet either.
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher * The MONITOR_MAX_RESTART_DELAY defines the maximum delay between
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * restarts.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define MONITOR_MAX_RESTART_DELAY 4
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* name of the monitor server instance */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define MONITOR_NAME "sssd"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define SSSD_PIDFILE_PATH PID_PATH"/"MONITOR_NAME".pid"
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher/* Special value to leave the Kerberos Replay Cache set to use
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * the libkrb5 defaults
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define KRB5_RCACHE_DIR_DISABLE "__LIBKRB5_DEFAULTS__"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherint cmdline_debug_level;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozekint cmdline_debug_timestamps;
ea929f1b022fc2cb77dec89b0e12accef983ec85Jakub Hrozekint cmdline_debug_microseconds;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstruct svc_spy;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherenum mt_svc_type {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher MT_SVC_SERVICE,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher MT_SVC_PROVIDER
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher};
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstruct mt_svc {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct mt_svc *prev;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct mt_svc *next;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher enum mt_svc_type type;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct sbus_connection *conn;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct svc_spy *conn_spy;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_ctx *mt_ctx;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *provider;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher char *command;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher char *name;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher char *identity;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher pid_t pid;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int ping_time;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int kill_time;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher bool svc_started;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher int restarts;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher time_t last_restart;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int failed_pongs;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DBusPendingCall *pending;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int debug_level;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct tevent_timer *ping_ev;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct sss_child_ctx *child_ctx;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct tevent_timer *sigkill_ev;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher};
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstruct config_file_callback {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher int wd;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int retries;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher monitor_reconf_fn fn;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *filename;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek time_t modified;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct config_file_callback *next;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct config_file_callback *prev;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher};
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstruct config_file_ctx {
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek TALLOC_CTX *parent_ctx;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct tevent_timer *timer;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher bool needs_update;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_ctx *mt_ctx;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct config_file_callback *callbacks;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher};
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstruct mt_ctx {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct tevent_context *ev;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct confdb_ctx *cdb;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher TALLOC_CTX *domain_ctx; /* Memory context for domain list */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct sss_domain_info *domains;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher TALLOC_CTX *service_ctx; /* Memory context for services */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher char **services;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int num_services;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int started_services;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_svc *svc_list;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct sbus_connection *sbus_srv;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct config_file_ctx *file_ctx;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int inotify_fd;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int service_id_timeout;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher bool check_children;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher bool services_started;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct netlink_ctx *nlctx;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher const char *conf_path;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct sss_sigchild_ctx *sigchld_ctx;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher bool is_daemon;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher pid_t parent_pid;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher};
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int start_service(struct mt_svc *mt_svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int monitor_service_init(struct sbus_connection *conn, void *data);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int service_send_ping(struct mt_svc *svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int service_signal_reset_offline(struct mt_svc *svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void ping_check(DBusPendingCall *pending, void *data);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstatic void set_tasks_checker(struct mt_svc *srv);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstatic int monitor_kill_service (struct mt_svc *svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int get_service_config(struct mt_ctx *ctx, const char *name,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_svc **svc_cfg);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int get_provider_config(struct mt_ctx *ctx, const char *name,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_svc **svc_cfg);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int add_new_service(struct mt_ctx *ctx,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher const char *name,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int restarts);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstatic int add_new_provider(struct mt_ctx *ctx,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher const char *name,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int restarts);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int mark_service_as_started(struct mt_svc *svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int monitor_cleanup(void);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void network_status_change_cb(void *cb_data)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_svc *iter;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher struct mt_ctx *ctx = (struct mt_ctx *) cb_data;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_TRACE_INTERNAL, ("A networking status change detected "
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "signaling providers to reset offline status\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (iter = ctx->svc_list; iter; iter = iter->next) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* Don't signal services, only providers */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (iter->provider) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher service_signal_reset_offline(iter);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* dbus_get_monitor_version
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Return the monitor version over D-BUS */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstatic int get_monitor_version(DBusMessage *message,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct sbus_connection *conn)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_uint16_t version = MONITOR_VERSION;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher DBusMessage *reply;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_bool_t ret;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher reply = dbus_message_new_method_return(message);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!reply) return ENOMEM;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher ret = dbus_message_append_args(reply,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DBUS_TYPE_UINT16, &version,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DBUS_TYPE_INVALID);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!ret) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_message_unref(reply);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return EIO;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* send reply back */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher sbus_conn_send_reply(conn, reply);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher dbus_message_unref(reply);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return EOK;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagherstruct mon_init_conn {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_ctx *ctx;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct sbus_connection *conn;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct tevent_timer *timeout;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher};
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstatic int add_svc_conn_spy(struct mt_svc *svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* registers a new client.
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher * if operation is successful also sends back the Monitor version */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int client_registration(DBusMessage *message,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct sbus_connection *conn)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_uint16_t version = MONITOR_VERSION;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher struct mon_init_conn *mini;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_svc *svc;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher void *data;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DBusMessage *reply;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DBusError dbus_error;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_uint16_t svc_ver;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher char *svc_name;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher dbus_bool_t dbret;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher int ret;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher data = sbus_conn_get_private_data(conn);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher mini = talloc_get_type(data, struct mon_init_conn);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!mini) {
bf9abef629707167d39fcc92ec9c18a6244b27b8Jakub Hrozek DEBUG(0, ("Connection holds no valid init data\n"));
bf9abef629707167d39fcc92ec9c18a6244b27b8Jakub Hrozek return EINVAL;
bf9abef629707167d39fcc92ec9c18a6244b27b8Jakub Hrozek }
bf9abef629707167d39fcc92ec9c18a6244b27b8Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* First thing, cancel the timeout */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_zfree(mini->timeout);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_error_init(&dbus_error);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbret = dbus_message_get_args(message, &dbus_error,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DBUS_TYPE_STRING, &svc_name,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DBUS_TYPE_UINT16, &svc_ver,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DBUS_TYPE_INVALID);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!dbret) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(1, ("Failed to parse message, killing connection\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (dbus_error_is_set(&dbus_error)) dbus_error_free(&dbus_error);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher sbus_disconnect(conn);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* FIXME: should we just talloc_zfree(conn) ? */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto done;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(4, ("Received ID registration: (%s,%d)\n", svc_name, svc_ver));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* search this service in the list */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc = mini->ctx->svc_list;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher while (svc) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = strcasecmp(svc->identity, svc_name);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (ret == 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher svc = svc->next;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!svc) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0, ("Unable to find peer [%s] in list of services,"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher " killing connection!\n", svc_name));
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek sbus_disconnect(conn);
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek /* FIXME: should we just talloc_zfree(conn) ? */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto done;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* Fill in svc structure with connection data */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->conn = mini->conn;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = mark_service_as_started(svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ret) {
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek DEBUG(1, ("Failed to mark service [%s]!\n", svc_name));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto done;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* reply that all is ok */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher reply = dbus_message_new_method_return(message);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!reply) return ENOMEM;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbret = dbus_message_append_args(reply,
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek DBUS_TYPE_UINT16, &version,
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek DBUS_TYPE_INVALID);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek if (!dbret) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_message_unref(reply);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return EIO;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* send reply back */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher sbus_conn_send_reply(conn, reply);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_message_unref(reply);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekdone:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* init complete, get rid of temp init context */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek talloc_zfree(mini);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return EOK;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek}
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekstruct svc_spy {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct mt_svc *svc;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek};
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekstatic int svc_destructor(void *mem)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek{
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct mt_svc *svc = talloc_get_type(mem, struct mt_svc);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (!svc) {
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek /* ?!?!? */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return 0;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* try to delist service */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (svc->mt_ctx) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DLIST_REMOVE(svc->mt_ctx->svc_list, svc);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* Cancel any pending pings */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (svc->pending) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_pending_call_cancel(svc->pending);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* svc is beeing freed, neutralize the spy */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (svc->conn_spy) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek talloc_set_destructor((TALLOC_CTX *)svc->conn_spy, NULL);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek talloc_zfree(svc->conn_spy);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (svc->type == MT_SVC_SERVICE && svc->svc_started
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek && svc->mt_ctx != NULL && svc->mt_ctx->started_services > 0) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek svc->mt_ctx->started_services--;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return 0;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek}
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekstatic int svc_spy_destructor(void *mem)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek{
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct svc_spy *spy = talloc_get_type(mem, struct svc_spy);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!spy) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* ?!?!? */
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher return 0;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher /* svc->conn has been freed, NULL the pointer in svc */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher spy->svc->conn_spy = NULL;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher spy->svc->conn = NULL;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int add_svc_conn_spy(struct mt_svc *svc)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct svc_spy *spy;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher spy = talloc(svc->conn, struct svc_spy);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!spy) return ENOMEM;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher spy->svc = svc;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_set_destructor((TALLOC_CTX *)spy, svc_spy_destructor);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher svc->conn_spy = spy;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return EOK;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int mark_service_as_started(struct mt_svc *svc)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_ctx *ctx = svc->mt_ctx;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_svc *iter;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int i;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(5, ("Marking %s as started.\n", svc->name));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher svc->svc_started = true;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* we need to attach a spy to the connection structure so that if some code
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * frees it we can zero it out in the service structure. Otherwise we may
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * try to access or even free, freed memory. */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = add_svc_conn_spy(svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ret) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0, ("Failed to attch spy\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto done;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!ctx->services_started) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* check if all providers are up */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (iter = ctx->svc_list; iter; iter = iter->next) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (iter->provider && !iter->svc_started) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(5, ("Still waiting on %s provider.\n", iter->name));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (iter) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* there are still unstarted providers */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto done;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ctx->services_started = true;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(4, ("Now starting services!\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* then start all services */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (i = 0; ctx->services[i]; i++) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher add_new_service(ctx, ctx->services[i], 0);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (svc->type == MT_SVC_SERVICE) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ctx->started_services++;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ctx->started_services == ctx->num_services) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Initialization is complete, terminate parent process if in daemon
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher * mode. Make sure we send the signal to the right process */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (ctx->is_daemon) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ctx->parent_pid <= 1 || ctx->parent_pid != getppid()) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* the parent process was already terminated */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(SSSDBG_MINOR_FAILURE, ("Invalid parent pid: %d\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ctx->parent_pid));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto done;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(SSSDBG_TRACE_FUNC, ("SSSD is initialized, "
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher "terminating parent process\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher errno = 0;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ret = kill(ctx->parent_pid, SIGTERM);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek if (ret != 0) {
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek ret = errno;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, ("Unable to terminate parent "
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek "process [%d]: %s\n", ret, strerror(ret)));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek }
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek }
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozekdone:
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek return ret;
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek}
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozekstatic void services_startup_timeout(struct tevent_context *ev,
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek struct tevent_timer *te,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct timeval t, void *ptr)
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek{
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek struct mt_ctx *ctx = talloc_get_type(ptr, struct mt_ctx);
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek int i;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek DEBUG(6, ("Handling timeout\n"));
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (!ctx->services_started) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek DEBUG(1, ("Providers did not start in time, "
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek "forcing services startup!\n"));
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek ctx->services_started = true;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(4, ("Now starting services!\n"));
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher /* then start all services */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (i = 0; ctx->services[i]; i++) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher add_new_service(ctx, ctx->services[i], 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int add_services_startup_timeout(struct mt_ctx *ctx)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct tevent_timer *to;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct timeval tv;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* 5 seconds should be plenty */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher tv = tevent_timeval_current_ofs(5, 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher to = tevent_add_timer(ctx->ev, ctx, tv, services_startup_timeout, ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!to) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(0,("Out of memory?!\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ENOMEM;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return EOK;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstruct sbus_method monitor_methods[] = {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher { MON_SRV_METHOD_VERSION, get_monitor_version },
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher { MON_SRV_METHOD_REGISTER, client_registration },
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher { NULL, NULL }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher};
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstruct sbus_interface monitor_server_interface = {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher MON_SRV_INTERFACE,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher MON_SRV_PATH,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher SBUS_DEFAULT_VTABLE,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek monitor_methods,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher NULL
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher};
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* monitor_dbus_init
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * Set up the monitor service as a D-BUS Server */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int monitor_dbus_init(struct mt_ctx *ctx)
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *monitor_address;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int ret;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = monitor_get_sbus_address(ctx, &monitor_address);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (ret != EOK) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = sbus_new_server(ctx, ctx->ev,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher monitor_address, &monitor_server_interface,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher false, &ctx->sbus_srv, monitor_service_init, ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek talloc_free(monitor_address);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekstatic void tasks_check_handler(struct tevent_context *ev,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct tevent_timer *te,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct timeval t, void *ptr)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek{
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct mt_svc *svc = talloc_get_type(ptr, struct mt_svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int ret;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = service_send_ping(svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher switch (ret) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek case EOK:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* all fine */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher case ENXIO:
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(1,("Child (%s) not responding! (yet)\n", svc->name));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher default:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* TODO: should we tear it down ? */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(1,("Sending a message to service (%s) failed!!\n", svc->name));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek break;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek if (svc->failed_pongs >= 3) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* too long since we last heard of this process */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ("Killing service [%s], not responding to pings!\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->name));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher /* Kill the service. The SIGCHLD handler will restart it */
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher monitor_kill_service(svc);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher /* all fine, set up the task checker again */
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher set_tasks_checker(svc);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher}
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagherstatic void set_tasks_checker(struct mt_svc *svc)
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher{
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct tevent_timer *te = NULL;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher struct timeval tv;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher gettimeofday(&tv, NULL);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher tv.tv_sec += svc->ping_time;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek tv.tv_usec = 0;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher te = tevent_add_timer(svc->mt_ctx->ev, svc, tv, tasks_check_handler, svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (te == NULL) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0, ("failed to add event, monitor offline for [%s]!\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->name));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* FIXME: shutdown ? */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher svc->ping_ev = te;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher}
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstatic void mt_svc_sigkill(struct tevent_context *ev,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct tevent_timer *te,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct timeval t, void *ptr);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstatic int monitor_kill_service (struct mt_svc *svc)
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher{
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek int ret;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct timeval tv;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ret = kill(svc->pid, SIGTERM);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (ret != EOK) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE,
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher ("Sending signal to child (%s:%d) failed! "
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek "Ignore and pretend child is dead.\n",
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher svc->name, svc->pid));
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher talloc_free(svc);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher /* Set up a timer to send SIGKILL if this process
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher * doesn't exit within sixty seconds
7797e361155f7ce937085fd98e360469d7baf1b6Jakub Hrozek */
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher tv = tevent_timeval_current_ofs(svc->kill_time, 0);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek svc->sigkill_ev = tevent_add_timer(svc->mt_ctx->ev, svc, tv,
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek mt_svc_sigkill, svc);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher return ret;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek}
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekstatic void mt_svc_sigkill(struct tevent_context *ev,
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek struct tevent_timer *te,
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek struct timeval t, void *ptr)
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek{
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek int ret;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct mt_svc *svc = talloc_get_type(ptr, struct mt_svc);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE,
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ("[%s][%d] is not responding to SIGTERM. Sending SIGKILL.\n",
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek svc->name, svc->pid));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ret = kill(svc->pid, SIGKILL);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (ret != EOK) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE,
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ("Sending signal to child (%s:%d) failed! "
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek "Ignore and pretend child is dead.\n",
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek svc->name, svc->pid));
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek talloc_free(svc);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void reload_reply(DBusPendingCall *pending, void *data)
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DBusMessage *reply;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_svc *svc = talloc_get_type(data, struct mt_svc);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher reply = dbus_pending_call_steal_reply(pending);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!reply) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* reply should never be null. This function shouldn't be called
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher * until reply is valid or timeout has occurred. If reply is NULL
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher * here, something is seriously wrong and we should bail out.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0, ("A reply callback was called but no reply was received"
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek " and no timeout occurred\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Destroy this connection */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher sbus_disconnect(svc->conn);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_pending_call_unref(pending);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* TODO: Handle cases where the call has timed out or returned
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * with an error.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher dbus_pending_call_unref(pending);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_message_unref(reply);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int service_signal_dns_reload(struct mt_svc *svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstatic int monitor_update_resolv(struct config_file_ctx *file_ctx,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher const char *filename)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek int ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_svc *cur_svc;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(2, ("Resolv.conf has been updated. Reloading.\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = res_init();
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if(ret != 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return EIO;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Signal all services to reload their DNS configuration */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for(cur_svc = file_ctx->mt_ctx->svc_list; cur_svc; cur_svc = cur_svc->next) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher service_signal_dns_reload(cur_svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return EOK;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekstatic int service_signal(struct mt_svc *svc, const char *svc_signal)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DBusMessage *msg;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (svc->provider && strcasecmp(svc->provider, "local") == 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* The local provider requires no signaling */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return EOK;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (!svc->conn) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* Avoid a race condition where we are trying to
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher * order a service to reload that hasn't started
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * yet.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(1,("Could not signal service [%s].\n", svc->name));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return EIO;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher msg = dbus_message_new_method_call(NULL,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher MONITOR_PATH,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher MONITOR_INTERFACE,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher svc_signal);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (!msg) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(0,("Out of memory?!\n"));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher monitor_kill_service(svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_free(svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ENOMEM;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ret = sbus_conn_send(svc->conn, msg,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->mt_ctx->service_id_timeout,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher reload_reply, svc, NULL);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_message_unref(msg);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekstatic int service_signal_dns_reload(struct mt_svc *svc)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return service_signal(svc, MON_CLI_METHOD_RES_INIT);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int service_signal_offline(struct mt_svc *svc)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return service_signal(svc, MON_CLI_METHOD_OFFLINE);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher}
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstatic int service_signal_reset_offline(struct mt_svc *svc)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek return service_signal(svc, MON_CLI_METHOD_RESET_OFFLINE);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int service_signal_rotate(struct mt_svc *svc)
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return service_signal(svc, MON_CLI_METHOD_ROTATE);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekstatic int service_signal_clear_memcache(struct mt_svc *svc)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return service_signal(svc, MON_CLI_METHOD_CLEAR_MEMCACHE);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int service_signal_clear_enum_cache(struct mt_svc *svc)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return service_signal(svc, MON_CLI_METHOD_CLEAR_ENUM_CACHE);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int check_domain_ranges(struct sss_domain_info *domains)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct sss_domain_info *dom = domains, *other = NULL;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher uint32_t id_min, id_max;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher while (dom) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher other = get_next_domain(dom, false);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (dom->id_max && dom->id_min > dom->id_max) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ("Domain '%s' does not have a valid ID range\n", dom->name));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return EINVAL;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher while (other) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek id_min = MAX(dom->id_min, other->id_min);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher id_max = MIN((dom->id_max ? dom->id_max : UINT32_MAX),
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher (other->id_max ? other->id_max : UINT32_MAX));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (id_min <= id_max) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ("Domains '%s' and '%s' overlap in range %u - %u\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dom->name, other->name, id_min, id_max));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher other = get_next_domain(other, false);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher dom = get_next_domain(dom, false);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return EOK;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek}
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekstatic int check_local_domain_unique(struct sss_domain_info *domains)
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek{
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek uint8_t count = 0;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek struct sss_domain_info *dom = domains;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek while (dom) {
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek if (strcasecmp(dom->provider, "local") == 0) {
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek count++;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (count > 1) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek dom = get_next_domain(dom, false);
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek if (count > 1) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return EINVAL;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek }
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek return EOK;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek}
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozekstatic errno_t add_implicit_services(struct confdb_ctx *cdb, TALLOC_CTX *mem_ctx,
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek char ***_services)
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek{
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek int ret;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek char **domain_names;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek TALLOC_CTX *tmp_ctx;
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek size_t c;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek char *conf_path;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek char *id_provider;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek bool add_pac = false;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
486237ee009f1d84fc4c85665dce80ade76f7079Stephen Gallagher tmp_ctx = talloc_new(NULL);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (tmp_ctx == NULL) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher DEBUG(SSSDBG_OP_FAILURE, ("talloc_new failed.\n"));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return ENOMEM;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek }
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek ret = confdb_get_string_as_list(cdb, tmp_ctx,
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek CONFDB_MONITOR_CONF_ENTRY,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek CONFDB_MONITOR_ACTIVE_DOMAINS,
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek &domain_names);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek if (ret == ENOENT) {
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek DEBUG(SSSDBG_OP_FAILURE, ("No domains configured!\n"));
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek goto done;
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek }
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek for (c = 0; domain_names[c] != NULL; c++) {
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek conf_path = talloc_asprintf(tmp_ctx, CONFDB_DOMAIN_PATH_TMPL,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek domain_names[c]);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek if (conf_path == NULL) {
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek DEBUG(SSSDBG_OP_FAILURE, ("talloc_asprintf failed.\n"));
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek ret = ENOMEM;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek goto done;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ret = confdb_get_string(cdb, tmp_ctx, conf_path,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher CONFDB_DOMAIN_ID_PROVIDER, NULL, &id_provider);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ret == EOK) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (id_provider == NULL) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_OP_FAILURE, ("id_provider is not set for "
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "domain [%s], trying next domain.\n", domain_names[c]));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher continue;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (strcasecmp(id_provider, "IPA") == 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher add_pac = true;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher } else {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_OP_FAILURE, ("Failed to get id_provider for " \
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher "domain [%s], trying next domain.\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher domain_names[c]));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (BUILD_WITH_PAC_RESPONDER && add_pac &&
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher !string_in_list("pac", *_services, false)) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = add_string_to_list(mem_ctx, "pac", _services);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (ret != EOK) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(SSSDBG_OP_FAILURE, ("add_string_to_list failed.\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto done;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = EOK;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherdone:
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher talloc_free(tmp_ctx);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic char *check_services(char **services)
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher const char *known_services[] = { "nss", "pam", "sudo", "autofs", "ssh",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "pac", NULL };
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek int i;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int ii;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Check if services we are about to start are in the list if known */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (i = 0; services[i]; i++) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher for (ii=0; known_services[ii]; ii++) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (strcasecmp(services[i], known_services[ii]) == 0) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (known_services[ii] == NULL) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return services[i];
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return NULL;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherint get_monitor_config(struct mt_ctx *ctx)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int ret;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher int timeout_seconds;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *badsrv = NULL;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int i;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = confdb_get_int(ctx->cdb,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher CONFDB_MONITOR_CONF_ENTRY,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher CONFDB_MONITOR_SBUS_TIMEOUT,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher 10, &timeout_seconds);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (ret != EOK) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ctx->service_id_timeout = timeout_seconds * 1000; /* service_id_timeout is in ms */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ctx->service_ctx = talloc_new(ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if(!ctx->service_ctx) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return ENOMEM;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ret = confdb_get_string_as_list(ctx->cdb, ctx->service_ctx,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher CONFDB_MONITOR_CONF_ENTRY,
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek CONFDB_MONITOR_ACTIVE_SERVICES,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek &ctx->services);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (ret != EOK) {
486237ee009f1d84fc4c85665dce80ade76f7079Stephen Gallagher DEBUG(0, ("No services configured!\n"));
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return EINVAL;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher ret = add_implicit_services(ctx->cdb, ctx->service_ctx, &ctx->services);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (ret != EOK) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek DEBUG(SSSDBG_OP_FAILURE, ("Failed to add implicit configured " \
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek "services. Some functionality might " \
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher "be missing"));
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek badsrv = check_services(ctx->services);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (badsrv != NULL) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher DEBUG(0, ("Invalid service %s\n", badsrv));
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return EINVAL;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher ctx->started_services = 0;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ctx->num_services = 0;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher for (i = 0; ctx->services[i] != NULL; i++) {
486237ee009f1d84fc4c85665dce80ade76f7079Stephen Gallagher ctx->num_services++;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ctx->domain_ctx = talloc_new(ctx);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if(!ctx->domain_ctx) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return ENOMEM;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher ret = confdb_get_domains(ctx->cdb, &ctx->domains);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (ret != EOK) {
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher DEBUG(0, ("No domains configured.\n"));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return ret;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher ret = check_local_domain_unique(ctx->domains);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (ret != EOK) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(0, ("More than one local domain configured.\n"));
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher return ret;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher /* Check UID/GID overlaps */
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher ret = check_domain_ranges(ctx->domains);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (ret != EOK) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return ret;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher return EOK;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic errno_t get_ping_config(struct mt_ctx *ctx, const char *path,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct mt_svc *svc)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher errno_t ret;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = confdb_get_int(ctx->cdb, path,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher CONFDB_DOMAIN_TIMEOUT,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher MONITOR_DEF_PING_TIME, &svc->ping_time);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ret != EOK) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ("Failed to get ping timeout for '%s'\n", svc->name));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* 'timeout = 0' should be translated to the default */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (svc->ping_time == 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->ping_time = MONITOR_DEF_PING_TIME;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_CONF_SETTINGS,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ("Time between service pings for [%s]: [%d]\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->name, svc->ping_time));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ret = confdb_get_int(ctx->cdb, path,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher CONFDB_SERVICE_FORCE_TIMEOUT,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher MONITOR_DEF_FORCE_TIME, &svc->kill_time);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ret != EOK) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ("Failed to get kill timeout for %s\n", svc->name));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* 'force_timeout = 0' should be translated to the default */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (svc->kill_time == 0) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher svc->kill_time = MONITOR_DEF_FORCE_TIME;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(SSSDBG_CONF_SETTINGS,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ("Time between SIGTERM and SIGKILL for [%s]: [%d]\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->name, svc->kill_time));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return EOK;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int get_service_config(struct mt_ctx *ctx, const char *name,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct mt_svc **svc_cfg)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher int ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *path;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_svc *svc;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek time_t now = time(NULL);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *svc_cfg = NULL;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc = talloc_zero(ctx, struct mt_svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (!svc) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return ENOMEM;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->mt_ctx = ctx;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek svc->type = MT_SVC_SERVICE;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_set_destructor((TALLOC_CTX *)svc, svc_destructor);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher svc->name = talloc_strdup(svc, name);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!svc->name) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher talloc_free(svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return ENOMEM;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->identity = talloc_strdup(svc, name);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (!svc->identity) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_free(svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return ENOMEM;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek path = talloc_asprintf(svc, CONFDB_SERVICE_PATH_TMPL, svc->name);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!path) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher talloc_free(svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ENOMEM;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = confdb_get_string(ctx->cdb, svc, path,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher CONFDB_SERVICE_COMMAND,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher NULL, &svc->command);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ret != EOK) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(0,("Failed to start service '%s'\n", svc->name));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher talloc_free(svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!svc->command) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher svc->command = talloc_asprintf(
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc, "%s/sssd_%s", SSSD_LIBEXEC_PATH, svc->name
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher );
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (!svc->command) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_free(svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return ENOMEM;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (cmdline_debug_level != SSSDBG_UNRESOLVED) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->command = talloc_asprintf_append(
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher svc->command, " -d %#.4x", cmdline_debug_level
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher );
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!svc->command) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek talloc_free(svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ENOMEM;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (cmdline_debug_timestamps != SSSDBG_TIMESTAMP_UNRESOLVED) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->command = talloc_asprintf_append(
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher svc->command, " --debug-timestamps=%d", cmdline_debug_timestamps
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher );
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!svc->command) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek talloc_free(svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ENOMEM;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (cmdline_debug_microseconds != SSSDBG_MICROSECONDS_UNRESOLVED) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->command = talloc_asprintf_append(
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher svc->command, " --debug-microseconds=%d",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cmdline_debug_microseconds
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher );
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (!svc->command) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_free(svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ENOMEM;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (debug_to_file) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher svc->command = talloc_strdup_append(
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->command, " --debug-to-files"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher );
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (!svc->command) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_free(svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ENOMEM;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ret = get_ping_config(ctx, path, svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (ret != EOK) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ("Failed to get ping timeouts for %s\n", svc->name));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_free(svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ret;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher svc->last_restart = now;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher *svc_cfg = svc;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek talloc_free(path);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return EOK;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekstatic int add_new_service(struct mt_ctx *ctx,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher const char *name,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int restarts)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_svc *svc;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ret = get_service_config(ctx, name, &svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ret != EOK) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return ret;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher svc->restarts = restarts;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher ret = start_service(svc);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (ret != EOK) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher DEBUG(0,("Failed to start service '%s'\n", svc->name));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_free(svc);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return ret;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher}
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagherstatic int get_provider_config(struct mt_ctx *ctx, const char *name,
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher struct mt_svc **svc_cfg)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek{
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher int ret;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher char *path;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_svc *svc;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher time_t now = time(NULL);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
056302a92862fda16351d7192600746746f38e5dStephen Gallagher *svc_cfg = NULL;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
056302a92862fda16351d7192600746746f38e5dStephen Gallagher svc = talloc_zero(ctx, struct mt_svc);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher if (!svc) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return ENOMEM;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher svc->mt_ctx = ctx;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher svc->type = MT_SVC_PROVIDER;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek talloc_set_destructor((TALLOC_CTX *)svc, svc_destructor);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher svc->name = talloc_strdup(svc, name);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher if (!svc->name) {
056302a92862fda16351d7192600746746f38e5dStephen Gallagher talloc_free(svc);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return ENOMEM;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher }
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
056302a92862fda16351d7192600746746f38e5dStephen Gallagher svc->identity = talloc_asprintf(svc, "%%BE_%s", svc->name);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher if (!svc->identity) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher talloc_free(svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return ENOMEM;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher }
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek path = talloc_asprintf(svc, CONFDB_DOMAIN_PATH_TMPL, name);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher if (!path) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher talloc_free(svc);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher return ENOMEM;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
056302a92862fda16351d7192600746746f38e5dStephen Gallagher ret = confdb_get_string(ctx->cdb, svc, path,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher CONFDB_DOMAIN_ID_PROVIDER,
056302a92862fda16351d7192600746746f38e5dStephen Gallagher NULL, &svc->provider);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher if (ret != EOK) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(0, ("Failed to find ID provider from [%s] configuration\n", name));
056302a92862fda16351d7192600746746f38e5dStephen Gallagher talloc_free(svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return ret;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher }
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ret = confdb_get_string(ctx->cdb, svc, path,
056302a92862fda16351d7192600746746f38e5dStephen Gallagher CONFDB_DOMAIN_COMMAND,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher NULL, &svc->command);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (ret != EOK) {
056302a92862fda16351d7192600746746f38e5dStephen Gallagher DEBUG(0, ("Failed to find command from [%s] configuration\n", name));
056302a92862fda16351d7192600746746f38e5dStephen Gallagher talloc_free(svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return ret;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
056302a92862fda16351d7192600746746f38e5dStephen Gallagher ret = get_ping_config(ctx, path, svc);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (ret != EOK) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE,
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher ("Failed to get ping timeouts for %s\n", svc->name));
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek talloc_free(svc);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return ret;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher talloc_free(path);
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher /* if no provider is present do not run the domain */
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (!svc->provider) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek talloc_free(svc);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return EIO;
7797e361155f7ce937085fd98e360469d7baf1b6Jakub Hrozek }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher /* if there are no custom commands, build a default one */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (!svc->command) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher svc->command = talloc_asprintf(
45db68ae27147955a4be4c2c772041824c0dc00fStephen Gallagher svc, "%s/sssd_be --domain %s", SSSD_LIBEXEC_PATH, svc->name
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher );
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (!svc->command) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher talloc_free(svc);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek return ENOMEM;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (cmdline_debug_level != SSSDBG_UNRESOLVED) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek svc->command = talloc_asprintf_append(
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek svc->command, " -d %#.4x", cmdline_debug_level
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek );
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (!svc->command) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek talloc_free(svc);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek return ENOMEM;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (cmdline_debug_timestamps != SSSDBG_TIMESTAMP_UNRESOLVED) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek svc->command = talloc_asprintf_append(
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek svc->command, " --debug-timestamps=%d", cmdline_debug_timestamps
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek );
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (!svc->command) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek talloc_free(svc);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher return ENOMEM;
7797e361155f7ce937085fd98e360469d7baf1b6Jakub Hrozek }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (cmdline_debug_microseconds != SSSDBG_MICROSECONDS_UNRESOLVED) {
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher svc->command = talloc_asprintf_append(
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher svc->command, " --debug-microseconds=%d",
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher cmdline_debug_microseconds
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher );
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (!svc->command) {
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher talloc_free(svc);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher return ENOMEM;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (debug_to_file) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek svc->command = talloc_strdup_append(
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher svc->command, " --debug-to-files"
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher );
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (!svc->command) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek talloc_free(svc);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return ENOMEM;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher svc->last_restart = now;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher *svc_cfg = svc;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return EOK;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek}
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagherstatic int add_new_provider(struct mt_ctx *ctx,
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek const char *name,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek int restarts)
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek{
7797e361155f7ce937085fd98e360469d7baf1b6Jakub Hrozek int ret;
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek struct mt_svc *svc;
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ret = get_provider_config(ctx, name, &svc);
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek if (ret != EOK) {
7797e361155f7ce937085fd98e360469d7baf1b6Jakub Hrozek DEBUG(0, ("Could not get provider configuration for [%s]\n",
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek name));
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek return ret;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek svc->restarts = restarts;
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek if (strcasecmp(svc->provider, "local") == 0) {
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek /* The LOCAL provider requires no back-end currently
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek * We'll add it to the service list, but we don't need
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek * to poll it.
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek svc->svc_started = true;
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek DLIST_ADD(ctx->svc_list, svc);
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek return ENOENT;
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek }
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ret = start_service(svc);
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek if (ret != EOK) {
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek DEBUG(0,("Failed to start service '%s'\n", svc->name));
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek talloc_free(svc);
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek }
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek return ret;
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek}
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekstatic void monitor_hup(struct tevent_context *ev,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct tevent_signal *se,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher int signum,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int count,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher void *siginfo,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek void *private_data)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct mt_ctx *ctx = talloc_get_type(private_data, struct mt_ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_svc *cur_svc;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(1, ("Received SIGHUP.\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Send D-Bus message to other services to rotate their logs.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * NSS service receives also message to clear memory caches. */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for(cur_svc = ctx->svc_list; cur_svc; cur_svc = cur_svc->next) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher service_signal_rotate(cur_svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (!strcmp(NSS_SBUS_SERVICE_NAME, cur_svc->name)) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher service_signal_clear_memcache(cur_svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!strcmp(SSS_AUTOFS_SBUS_SERVICE_NAME, cur_svc->name)) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher service_signal_clear_enum_cache(cur_svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstatic int monitor_cleanup(void)
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek errno = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = unlink(SSSD_PIDFILE_PATH);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (ret == -1) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = errno;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_FATAL_FAILURE,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ("Error removing pidfile! (%d [%s])\n", ret, strerror(ret)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ret;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return EOK;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void monitor_quit(struct mt_ctx *mt_ctx, int ret)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_svc *svc;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher pid_t pid;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher int status;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher errno_t error;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int kret;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek bool killed;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(SSSDBG_IMPORTANT_INFO, ("Returned with: %d\n", ret));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Kill all of our known children manually */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DLIST_FOR_EACH(svc, mt_ctx->svc_list) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (svc->pid == 0) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* The local provider has no PID */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher continue;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek killed = false;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(1, ("Terminating [%s][%d]\n", svc->name, svc->pid));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher do {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher errno = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher kret = kill(svc->pid, SIGTERM);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (kret < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher error = errno;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(1, ("Couldn't kill [%s][%d]: [%s]\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->name, svc->pid, strerror(error)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek error = 0;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek do {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek errno = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher pid = waitpid(svc->pid, &status, WNOHANG);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (pid == -1) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* An error occurred while waiting */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek error = errno;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (error == ECHILD) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher killed = true;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher } else if (error != EINTR) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0, ("[%d][%s] while waiting for [%s]\n",
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher error, strerror(error), svc->name));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* Forcibly kill this child */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher kill(svc->pid, SIGKILL);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher } else if (pid != 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher error = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if WIFEXITED(status) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(1, ("Child [%s] exited gracefully\n", svc->name));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher } else if WIFSIGNALED(status) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(1, ("Child [%s] terminated with a signal\n", svc->name));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher } else {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(0, ("Child [%s] did not exit cleanly\n", svc->name));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* Forcibly kill this child */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher kill(svc->pid, SIGKILL);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek killed = true;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek } while (error == EINTR);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (!killed) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek /* Sleep 10ms and try again */
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek usleep(10000);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher } while (!killed);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher#if HAVE_GETPGRP
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Kill any remaining children in our process group, just in case
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * we have any leftover children we don't expect. For example, if
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * a krb5_child or ldap_child is running at the same moment.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher error = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (getpgrp() == getpid()) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher kill(-getpgrp(), SIGTERM);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher do {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher errno = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher pid = waitpid(0, &status, 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (pid == -1) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek error = errno;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher } while (error == EINTR || pid > 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#endif
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
056302a92862fda16351d7192600746746f38e5dStephen Gallagher monitor_cleanup();
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
056302a92862fda16351d7192600746746f38e5dStephen Gallagher exit(ret);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher}
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
056302a92862fda16351d7192600746746f38e5dStephen Gallagherstatic void monitor_quit_signal(struct tevent_context *ev,
056302a92862fda16351d7192600746746f38e5dStephen Gallagher struct tevent_signal *se,
056302a92862fda16351d7192600746746f38e5dStephen Gallagher int signum,
056302a92862fda16351d7192600746746f38e5dStephen Gallagher int count,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher void *siginfo,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher void *private_data)
056302a92862fda16351d7192600746746f38e5dStephen Gallagher{
056302a92862fda16351d7192600746746f38e5dStephen Gallagher struct mt_ctx *mt_ctx = talloc_get_type(private_data, struct mt_ctx);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(SSSDBG_TRACE_INTERNAL, ("Received shutdown command\n"));
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(SSSDBG_IMPORTANT_INFO, ("Monitor received %s: terminating "
056302a92862fda16351d7192600746746f38e5dStephen Gallagher "children\n", strsignal(signum)));
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek monitor_quit(mt_ctx, 0);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher}
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
056302a92862fda16351d7192600746746f38e5dStephen Gallagherstatic void signal_res_init(struct mt_ctx *monitor)
056302a92862fda16351d7192600746746f38e5dStephen Gallagher{
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct mt_svc *cur_svc;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher int ret;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher DEBUG(SSSDBG_OP_FAILURE, ("Reloading Resolv.conf.\n"));
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
056302a92862fda16351d7192600746746f38e5dStephen Gallagher ret = res_init();
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (ret == 0) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher for(cur_svc = monitor->svc_list; cur_svc; cur_svc = cur_svc->next) {
056302a92862fda16351d7192600746746f38e5dStephen Gallagher service_signal_dns_reload(cur_svc);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
056302a92862fda16351d7192600746746f38e5dStephen Gallagher}
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
056302a92862fda16351d7192600746746f38e5dStephen Gallagherstatic void signal_offline(struct tevent_context *ev,
056302a92862fda16351d7192600746746f38e5dStephen Gallagher struct tevent_signal *se,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek int signum,
056302a92862fda16351d7192600746746f38e5dStephen Gallagher int count,
056302a92862fda16351d7192600746746f38e5dStephen Gallagher void *siginfo,
056302a92862fda16351d7192600746746f38e5dStephen Gallagher void *private_data)
056302a92862fda16351d7192600746746f38e5dStephen Gallagher{
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct mt_ctx *monitor;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct mt_svc *cur_svc;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
056302a92862fda16351d7192600746746f38e5dStephen Gallagher monitor = talloc_get_type(private_data, struct mt_ctx);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
056302a92862fda16351d7192600746746f38e5dStephen Gallagher DEBUG(SSSDBG_TRACE_INTERNAL,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ("Signaling providers to go offline immediately.\n"));
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
056302a92862fda16351d7192600746746f38e5dStephen Gallagher /* Signal all providers to immediately go offline */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek for(cur_svc = monitor->svc_list; cur_svc; cur_svc = cur_svc->next) {
056302a92862fda16351d7192600746746f38e5dStephen Gallagher /* Don't signal services, only providers */
056302a92862fda16351d7192600746746f38e5dStephen Gallagher if (cur_svc->provider) {
056302a92862fda16351d7192600746746f38e5dStephen Gallagher service_signal_offline(cur_svc);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher}
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
056302a92862fda16351d7192600746746f38e5dStephen Gallagherstatic void signal_offline_reset(struct tevent_context *ev,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct tevent_signal *se,
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek int signum,
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek int count,
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek void *siginfo,
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek void *private_data)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek{
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek struct mt_ctx *monitor;
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek struct mt_svc *cur_svc;
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek monitor = talloc_get_type(private_data, struct mt_ctx);
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek
64a424ec1b268427822c646f7781e26e56c197f6Jakub Hrozek DEBUG(SSSDBG_TRACE_INTERNAL,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ("Signaling providers to reset offline immediately.\n"));
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek
bf9abef629707167d39fcc92ec9c18a6244b27b8Jakub Hrozek for(cur_svc = monitor->svc_list; cur_svc; cur_svc = cur_svc->next) {
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek if (cur_svc->provider) {
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek service_signal_reset_offline(cur_svc);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek }
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek signal_res_init(monitor);
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek}
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozekstatic int monitor_ctx_destructor(void *mem)
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek{
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct mt_ctx *mon = talloc_get_type(mem, struct mt_ctx);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct mt_svc *svc;
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* zero out references in svcs so that they don't try
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * to access the monitor context on process shutdown */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek for (svc = mon->svc_list; svc; svc = svc->next) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek svc->mt_ctx = NULL;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return 0;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek}
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekstatic errno_t load_configuration(TALLOC_CTX *mem_ctx,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek const char *config_file,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct mt_ctx **monitor)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek{
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek errno_t ret;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct mt_ctx *ctx;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek char *cdb_file = NULL;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ctx = talloc_zero(mem_ctx, struct mt_ctx);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if(!ctx) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return ENOMEM;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_set_destructor((TALLOC_CTX *)ctx, monitor_ctx_destructor);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek cdb_file = talloc_asprintf(ctx, "%s/%s", DB_PATH, CONFDB_FILE);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (cdb_file == NULL) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0,("Out of memory, aborting!\n"));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ret = ENOMEM;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher goto done;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ret = confdb_init(ctx, &ctx->cdb, cdb_file);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ret != EOK) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0,("The confdb initialization failed\n"));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher goto done;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* Initialize the CDB from the configuration file */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = confdb_test(ctx->cdb);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (ret == ENOENT) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* First-time setup */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* Purge any existing confdb in case an old
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * misconfiguration gets in the way
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_zfree(ctx->cdb);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher unlink(cdb_file);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = confdb_init(ctx, &ctx->cdb, cdb_file);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (ret != EOK) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(0,("The confdb initialization failed\n"));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher goto done;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* Load special entries */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = confdb_create_base(ctx->cdb);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (ret != EOK) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0, ("Unable to load special entries into confdb\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto done;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher } else if (ret != EOK) {
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek DEBUG(0, ("Fatal error initializing confdb\n"));
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher goto done;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek talloc_zfree(cdb_file);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher ret = confdb_init_db(config_file, ctx->cdb);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (ret != EOK) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher DEBUG(0, ("ConfDB initialization has failed [%s]\n",
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher strerror(ret)));
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher goto done;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* Validate the configuration in the database */
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher /* Read in the monitor's configuration */
486237ee009f1d84fc4c85665dce80ade76f7079Stephen Gallagher ret = get_monitor_config(ctx);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (ret != EOK) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher goto done;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher *monitor = ctx;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = EOK;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozekdone:
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek if (ret != EOK) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_free(ctx);
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek }
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek return ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek#ifdef HAVE_SYS_INOTIFY_H
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozekstatic void process_config_file(struct tevent_context *ev,
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek struct tevent_timer *te,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct timeval t, void *ptr);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekstatic void config_file_changed(struct tevent_context *ev,
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek struct tevent_fd *fde,
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek uint16_t flags, void *data)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct tevent_timer *te = NULL;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct timeval tv;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek struct config_file_ctx *file_ctx;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek file_ctx = talloc_get_type(data, struct config_file_ctx);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek if (file_ctx->needs_update) {
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek /* Skip updating. It's already queued for update.
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek */
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek return;
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek }
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek /* We will queue the file for update in one second.
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * This way, if there is a script writing to the file
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * repeatedly, we won't be attempting to update multiple
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek * times.
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek */
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek gettimeofday(&tv, NULL);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek tv.tv_sec += 1;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek te = tevent_add_timer(ev, ev, tv, process_config_file, file_ctx);
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek if (!te) {
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek DEBUG(0, ("Unable to queue config file update! Exiting.\n"));
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek kill(getpid(), SIGTERM);
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek return;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek }
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek file_ctx->needs_update = 1;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek}
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozekstruct rewatch_ctx {
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek struct config_file_callback *cb;
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek struct config_file_ctx *file_ctx;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek};
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozekstatic void rewatch_config_file(struct tevent_context *ev,
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek struct tevent_timer *te,
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek struct timeval t, void *ptr);
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozekstatic void process_config_file(struct tevent_context *ev,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct tevent_timer *te,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct timeval t, void *ptr)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher TALLOC_CTX *tmp_ctx;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct inotify_event *in_event;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *buf;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *name;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ssize_t len;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher ssize_t event_size;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher struct config_file_ctx *file_ctx;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher struct config_file_callback *cb;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher struct rewatch_ctx *rw_ctx;
486237ee009f1d84fc4c85665dce80ade76f7079Stephen Gallagher errno_t ret;
486237ee009f1d84fc4c85665dce80ade76f7079Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher event_size = sizeof(struct inotify_event);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher file_ctx = talloc_get_type(ptr, struct config_file_ctx);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(1, ("Processing config file changes\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tmp_ctx = talloc_new(NULL);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!tmp_ctx) return;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher buf = talloc_size(tmp_ctx, event_size);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (!buf) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher goto done;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher errno = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher len = sss_atomic_read_s(file_ctx->mt_ctx->inotify_fd, buf, event_size);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (len == -1) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ret = errno;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE,
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ("Critical error reading inotify file descriptor [%d]: %s\n",
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek ret, strerror(ret)));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek goto done;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek in_event = (struct inotify_event *)buf;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (in_event->len > 0) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek /* Read in the name, even though we don't use it,
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek * so that read ptr is in the right place
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek */
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek name = talloc_size(tmp_ctx, in_event->len);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (!name) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek goto done;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher errno = 0;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher len = sss_atomic_read_s(file_ctx->mt_ctx->inotify_fd, name, in_event->len);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (len == -1) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = errno;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ("Critical error reading inotify file descriptor [%d]: %s\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret, strerror(ret)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto done;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (cb = file_ctx->callbacks; cb; cb = cb->next) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (cb->wd == in_event->wd) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!cb) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0, ("Unknown watch descriptor\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto done;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (in_event->mask & IN_IGNORED) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Some text editors will move a new file on top of the
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * existing one instead of modifying it. In this case,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * the kernel will send us an IN_IGNORE signal.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * We will try to open a new watch descriptor on the
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * new file.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct timeval tv;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct tevent_timer *tev;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher tv.tv_sec = t.tv_sec+5;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher tv.tv_usec = t.tv_usec;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(5, ("Restoring inotify watch.\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cb->retries = 0;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek rw_ctx = talloc(file_ctx, struct rewatch_ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if(!rw_ctx) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0, ("Could not restore inotify watch. Quitting!\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher close(file_ctx->mt_ctx->inotify_fd);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher kill(getpid(), SIGTERM);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto done;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek rw_ctx->cb = cb;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher rw_ctx->file_ctx = file_ctx;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tev = tevent_add_timer(ev, rw_ctx, tv, rewatch_config_file, rw_ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (tev == NULL) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(0, ("Could not restore inotify watch. Quitting!\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher close(file_ctx->mt_ctx->inotify_fd);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher kill(getpid(), SIGTERM);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto done;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Tell the monitor to signal the children */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cb->fn(file_ctx, cb->filename);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek file_ctx->needs_update = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherdone:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_free(tmp_ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghererrno_t monitor_config_file_fallback(TALLOC_CTX *mem_ctx,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_ctx *ctx,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher const char *file,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher monitor_reconf_fn fn,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher bool ignore_missing);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstatic void rewatch_config_file(struct tevent_context *ev,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct tevent_timer *te,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct timeval t, void *ptr)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int err;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct tevent_timer *tev = NULL;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher struct timeval tv;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher struct config_file_callback *cb;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher struct rewatch_ctx *rw_ctx;
486237ee009f1d84fc4c85665dce80ade76f7079Stephen Gallagher struct config_file_ctx *file_ctx;
486237ee009f1d84fc4c85665dce80ade76f7079Stephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher rw_ctx = talloc_get_type(ptr, struct rewatch_ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek cb = rw_ctx->cb;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher file_ctx = rw_ctx->file_ctx;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Retry six times at five-second intervals before giving up */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cb->retries++;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (cb->retries > 6) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_FATAL_FAILURE,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ("Could not restore inotify watch. Switching to polling!\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher close(file_ctx->mt_ctx->inotify_fd);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher err = monitor_config_file_fallback(file_ctx->parent_ctx,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher file_ctx->mt_ctx,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cb->filename,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher cb->fn,true);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (err != EOK)
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher kill(getpid(), SIGTERM);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cb->fn(file_ctx, cb->filename);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_free(rw_ctx);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* A new callback was created in monitor_config_file_fallback()*/
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DLIST_REMOVE(file_ctx->callbacks, cb);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_free(cb);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cb->wd = inotify_add_watch(file_ctx->mt_ctx->inotify_fd,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cb->filename, IN_MODIFY);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (cb->wd < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher err = errno;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tv.tv_sec = t.tv_sec+5;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tv.tv_usec = t.tv_usec;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ("Could not add inotify watch for file [%s]. Error [%d:%s]\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cb->filename, err, strerror(err)));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tev = tevent_add_timer(ev, ev, tv, rewatch_config_file, rw_ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (tev == NULL) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_FATAL_FAILURE,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ("Could not restore inotify watch. Quitting!\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher close(file_ctx->mt_ctx->inotify_fd);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher kill(getpid(), SIGTERM);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek cb->retries = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Tell the monitor to signal the children */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cb->fn(file_ctx, cb->filename);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_free(rw_ctx);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher file_ctx->needs_update = 0;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher}
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher#endif
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void poll_config_file(struct tevent_context *ev,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct tevent_timer *te,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct timeval t, void *ptr)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int ret, err;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct stat file_stat;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct timeval tv;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct config_file_ctx *file_ctx;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct config_file_callback *cb;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher file_ctx = talloc_get_type(ptr,struct config_file_ctx);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (cb = file_ctx->callbacks; cb; cb = cb->next) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = stat(cb->filename, &file_stat);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (ret < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher err = errno;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0, ("Could not stat file [%s]. Error [%d:%s]\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cb->filename, err, strerror(err)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* TODO: If the config file is missing, should we shut down? */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (file_stat.st_mtime != cb->modified) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* Parse the configuration file and signal the children */
056302a92862fda16351d7192600746746f38e5dStephen Gallagher /* Note: this will fire if the modification time changes into the past
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher * as well as the future.
056302a92862fda16351d7192600746746f38e5dStephen Gallagher */
056302a92862fda16351d7192600746746f38e5dStephen Gallagher DEBUG(1, ("Config file changed\n"));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek cb->modified = file_stat.st_mtime;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
056302a92862fda16351d7192600746746f38e5dStephen Gallagher /* Tell the monitor to signal the children */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher cb->fn(file_ctx, cb->filename);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
056302a92862fda16351d7192600746746f38e5dStephen Gallagher }
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek gettimeofday(&tv, NULL);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher tv.tv_sec += CONFIG_FILE_POLL_INTERVAL;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher tv.tv_usec = 0;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher file_ctx->timer = tevent_add_timer(ev, file_ctx->parent_ctx, tv,
056302a92862fda16351d7192600746746f38e5dStephen Gallagher poll_config_file, file_ctx);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher if (!file_ctx->timer) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(0, ("Error: Config file no longer monitored for changes!\n"));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher}
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
056302a92862fda16351d7192600746746f38e5dStephen Gallagherstatic int try_inotify(struct config_file_ctx *file_ctx, const char *filename,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek monitor_reconf_fn fn)
056302a92862fda16351d7192600746746f38e5dStephen Gallagher{
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher#ifdef HAVE_SYS_INOTIFY_H
056302a92862fda16351d7192600746746f38e5dStephen Gallagher int err, fd_args, ret;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher struct tevent_fd *tfd;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct config_file_callback *cb;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
056302a92862fda16351d7192600746746f38e5dStephen Gallagher /* Monitoring the file descriptor should be global */
056302a92862fda16351d7192600746746f38e5dStephen Gallagher if (!file_ctx->mt_ctx->inotify_fd) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* Set up inotify to monitor the config file for changes */
056302a92862fda16351d7192600746746f38e5dStephen Gallagher file_ctx->mt_ctx->inotify_fd = inotify_init();
056302a92862fda16351d7192600746746f38e5dStephen Gallagher if (file_ctx->mt_ctx->inotify_fd < 0) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek err = errno;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek DEBUG(0, ("Could not initialize inotify, error [%d:%s]\n",
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek err, strerror(err)));
056302a92862fda16351d7192600746746f38e5dStephen Gallagher return err;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
056302a92862fda16351d7192600746746f38e5dStephen Gallagher fd_args = fcntl(file_ctx->mt_ctx->inotify_fd, F_GETFL, NULL);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek if (fd_args < 0) {
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek /* Could not set nonblocking */
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek close(file_ctx->mt_ctx->inotify_fd);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher return EINVAL;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher }
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek fd_args |= O_NONBLOCK;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher ret = fcntl(file_ctx->mt_ctx->inotify_fd, F_SETFL, fd_args);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek if (ret < 0) {
056302a92862fda16351d7192600746746f38e5dStephen Gallagher /* Could not set nonblocking */
056302a92862fda16351d7192600746746f38e5dStephen Gallagher close(file_ctx->mt_ctx->inotify_fd);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher return EINVAL;
056302a92862fda16351d7192600746746f38e5dStephen Gallagher }
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* Add the inotify file descriptor to the TEvent context */
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek tfd = tevent_add_fd(file_ctx->mt_ctx->ev, file_ctx,
056302a92862fda16351d7192600746746f38e5dStephen Gallagher file_ctx->mt_ctx->inotify_fd,
056302a92862fda16351d7192600746746f38e5dStephen Gallagher TEVENT_FD_READ, config_file_changed,
056302a92862fda16351d7192600746746f38e5dStephen Gallagher file_ctx);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (!tfd) {
056302a92862fda16351d7192600746746f38e5dStephen Gallagher close(file_ctx->mt_ctx->inotify_fd);
056302a92862fda16351d7192600746746f38e5dStephen Gallagher return EIO;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek }
056302a92862fda16351d7192600746746f38e5dStephen Gallagher }
056302a92862fda16351d7192600746746f38e5dStephen Gallagher
056302a92862fda16351d7192600746746f38e5dStephen Gallagher cb = talloc_zero(file_ctx, struct config_file_callback);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if(!cb) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher close(file_ctx->mt_ctx->inotify_fd);
486237ee009f1d84fc4c85665dce80ade76f7079Stephen Gallagher return EIO;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek cb->filename = talloc_strdup(cb, filename);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (!cb->filename) {
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek close(file_ctx->mt_ctx->inotify_fd);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return ENOMEM;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher cb->wd = inotify_add_watch(file_ctx->mt_ctx->inotify_fd,
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher cb->filename, IN_MODIFY);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (cb->wd < 0) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher err = errno;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher DEBUG(0, ("Could not add inotify watch for file [%s]. Error [%d:%s]\n",
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher cb->filename, err, strerror(err)));
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher close(file_ctx->mt_ctx->inotify_fd);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return err;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher cb->fn = fn;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher DLIST_ADD(file_ctx->callbacks, cb);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return EOK;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher#else
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return EINVAL;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher#endif
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher}
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagherstatic int monitor_config_file(TALLOC_CTX *mem_ctx,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct mt_ctx *ctx,
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher const char *file,
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher monitor_reconf_fn fn,
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher bool ignore_missing)
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher{
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher int ret, err;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher bool use_inotify;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher struct stat file_stat;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher ret = stat(file, &file_stat);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (ret < 0) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher err = errno;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (err == ENOENT && ignore_missing) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher DEBUG(SSSDBG_MINOR_FAILURE,
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher ("file [%s] is missing. Will not update online status "
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher "based on watching the file\n", file));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return EOK;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher } else {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher DEBUG(SSSDBG_FATAL_FAILURE,
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher ("Could not stat file [%s]. Error [%d:%s]\n",
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher file, err, strerror(err)));
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return err;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (!ctx->file_ctx) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher ctx->file_ctx = talloc_zero(mem_ctx, struct config_file_ctx);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (!ctx->file_ctx) return ENOMEM;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ctx->file_ctx->parent_ctx = mem_ctx;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher ctx->file_ctx->mt_ctx = ctx;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher ret = confdb_get_bool(ctx->cdb,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek CONFDB_MONITOR_CONF_ENTRY,
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher CONFDB_MONITOR_TRY_INOTIFY,
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher true, &use_inotify);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (ret != EOK) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher talloc_free(ctx->file_ctx);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return ret;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (use_inotify) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ret = try_inotify(ctx->file_ctx, file, fn);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (ret != EOK) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher use_inotify = false;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (!use_inotify) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher /* Could not monitor file with inotify, fall back to polling */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ret = monitor_config_file_fallback(mem_ctx, ctx, file, fn, true);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return ret;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher}
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallaghererrno_t monitor_config_file_fallback(TALLOC_CTX *mem_ctx,
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher struct mt_ctx *ctx,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek const char *file,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek monitor_reconf_fn fn,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek bool ignore_missing)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek{
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher struct config_file_callback *cb = NULL;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher struct stat file_stat;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher int ret, err;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct timeval tv;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek ret = stat(file, &file_stat);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek if (ret < 0) {
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek err = errno;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek if (err == ENOENT && ignore_missing) {
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek DEBUG(SSSDBG_MINOR_FAILURE,
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek ("file [%s] is missing. Will not update online status "
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek "based on watching the file\n", file));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return EOK;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek } else {
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE,
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek ("Could not stat file [%s]. Error [%d:%s]\n",
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek file, err, strerror(err)));
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek return err;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek }
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek cb = talloc_zero(ctx->file_ctx, struct config_file_callback);
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek if (!cb) {
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek talloc_free(ctx->file_ctx);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek return ENOMEM;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek cb->filename = talloc_strdup(cb, file);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek if (!cb->filename) {
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek talloc_free(ctx->file_ctx);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek return ENOMEM;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek }
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek cb->fn = fn;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek cb->modified = file_stat.st_mtime;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher DLIST_ADD(ctx->file_ctx->callbacks, cb);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if(!ctx->file_ctx->timer) {
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher gettimeofday(&tv, NULL);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher tv.tv_sec += CONFIG_FILE_POLL_INTERVAL;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher tv.tv_usec = 0;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher ctx->file_ctx->timer = tevent_add_timer(ctx->ev, mem_ctx, tv,
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher poll_config_file, ctx->file_ctx);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher if (!ctx->file_ctx->timer) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek talloc_free(ctx->file_ctx);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher return EIO;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher }
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher return EOK;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher}
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekint monitor_process_init(struct mt_ctx *ctx,
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher const char *config_file)
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher{
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher TALLOC_CTX *tmp_ctx;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher struct tevent_signal *tes;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher struct sss_domain_info *dom;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher char *rcachedir;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher int num_providers;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher int ret;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek int error;
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher /* Set up the environment variable for the Kerberos Replay Cache */
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher ret = confdb_get_string(ctx->cdb, ctx,
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher CONFDB_MONITOR_CONF_ENTRY,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek CONFDB_MONITOR_KRB5_RCACHEDIR,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher KRB5_RCACHE_DIR,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher &rcachedir);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ret != EOK) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ret;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (strcmp(rcachedir, KRB5_RCACHE_DIR_DISABLE) != 0)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher errno = 0;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ret = setenv("KRB5RCACHEDIR", rcachedir, 1);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (ret < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher error = errno;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(1,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ("Unable to set KRB5RCACHEDIR: %s."
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "Will attempt to use libkrb5 defaults\n",
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher strerror(error)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_zfree(rcachedir);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Set up an event handler for a SIGHUP */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher tes = tevent_add_signal(ctx->ev, ctx, SIGHUP, 0,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher monitor_hup, ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (tes == NULL) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return EIO;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* Set up an event handler for a SIGINT */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher BlockSignals(false, SIGINT);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tes = tevent_add_signal(ctx->ev, ctx, SIGINT, 0,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek monitor_quit_signal, ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (tes == NULL) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return EIO;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Set up an event handler for a SIGTERM */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek tes = tevent_add_signal(ctx->ev, ctx, SIGTERM, 0,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher monitor_quit_signal, ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (tes == NULL) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return EIO;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* Handle SIGUSR1 (tell all providers to go offline) */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher BlockSignals(false, SIGUSR1);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher tes = tevent_add_signal(ctx->ev, ctx, SIGUSR1, 0,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher signal_offline, ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (tes == NULL) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return EIO;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Handle SIGUSR2 (tell all providers to go reset offline) */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher BlockSignals(false, SIGUSR2);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek tes = tevent_add_signal(ctx->ev, ctx, SIGUSR2, 0,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher signal_offline_reset, ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (tes == NULL) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return EIO;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* Set up the SIGCHLD handler */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ret = sss_sigchld_init(ctx, ctx->ev, &ctx->sigchld_ctx);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (ret != EOK) return ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#if 0
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek This feature is incomplete and can leave the SSSD in a bad state if the
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher config file is changed while the SSSD is running.
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher Uncomment this once the backends are honoring reloadConfig()
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* Watch for changes to the confdb config file */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = monitor_config_file(ctx, ctx, config_file, monitor_signal_reconf,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher true);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ret != EOK) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ret;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher#endif
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Watch for changes to the DNS resolv.conf */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = monitor_config_file(ctx, ctx, RESOLV_CONF_PATH,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek monitor_update_resolv,true);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ret != EOK) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* Avoid a startup race condition between process.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * We need to handle DB upgrades or DB creation only
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher * in one process before all other start.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tmp_ctx = talloc_new(NULL);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (!tmp_ctx) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ENOMEM;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = sysdb_init(tmp_ctx, ctx->domains, true);
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher if (ret != EOK) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek SYSDB_VERSION_ERROR_DAEMON(ret);
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher return ret;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher talloc_zfree(tmp_ctx);
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* Initialize D-BUS Server
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher * The monitor will act as a D-BUS server for all
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher * SSSD processes */
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher ret = monitor_dbus_init(ctx);
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher if (ret != EOK) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return ret;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher ret = setup_netlink(ctx, ctx->ev, network_status_change_cb,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ctx, &ctx->nlctx);
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher if (ret != EOK) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(2, ("Cannot set up listening for network notifications\n"));
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher return ret;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher /* start providers */
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher num_providers = 0;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher for (dom = ctx->domains; dom; dom = get_next_domain(dom, false)) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher ret = add_new_provider(ctx, dom->name, 0);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (ret != EOK && ret != ENOENT) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher return ret;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (ret != ENOENT) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher num_providers++;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher if (num_providers > 0) {
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* now set the services stratup timeout *
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher * (responders will be started automatically when all
486237ee009f1d84fc4c85665dce80ade76f7079Stephen Gallagher * providers are up and running or when the tomeout
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher * expires) */
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek ret = add_services_startup_timeout(ctx);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek if (ret != EOK) {
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek return ret;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek }
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek } else {
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek int i;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek ctx->services_started = true;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek /* No providers start services immediately
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher * Normally this means only LOCAL is configured */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek for (i = 0; ctx->services[i]; i++) {
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher add_new_service(ctx, ctx->services[i], 0);
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher }
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek }
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek return EOK;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek}
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagherstatic void init_timeout(struct tevent_context *ev,
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher struct tevent_timer *te,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct timeval t, void *ptr)
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher{
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher struct mon_init_conn *mini;
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek DEBUG(2, ("Client timed out before Identification!\n"));
e59e09b5010f262228bbdeb92a79b733bf5854b3Stephen Gallagher
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher mini = talloc_get_type(ptr, struct mon_init_conn);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher sbus_disconnect(mini->conn);
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek talloc_zfree(mini);
dd3ba5c5b7d2a9d109963ae9e6c94fff34872221Stephen Gallagher}
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek/*
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * monitor_service_init
0172959f117b545c8a6b1893f5f56818d82dd624Jakub Hrozek * Set up a timeout function and temporary connection structure.
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * If the client does not identify before the timeout kicks in,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * the client is forcibly disconnected.
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekstatic int monitor_service_init(struct sbus_connection *conn, void *data)
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek{
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct mt_ctx *ctx;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct mon_init_conn *mini;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct timeval tv;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_TRACE_FUNC, ("Initializing D-BUS Service\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ctx = talloc_get_type(data, struct mt_ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher mini = talloc(conn, struct mon_init_conn);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (!mini) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(0,("Out of memory?!\n"));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher talloc_zfree(conn);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ENOMEM;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek mini->ctx = ctx;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher mini->conn = conn;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* 10 seconds should be plenty */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tv = tevent_timeval_current_ofs(10, 0);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher mini->timeout = tevent_add_timer(ctx->ev, mini, tv, init_timeout, mini);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (!mini->timeout) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0,("Out of memory?!\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_zfree(conn);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return ENOMEM;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher sbus_conn_set_private_data(conn, mini);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return EOK;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* service_send_ping
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * this function send a dbus ping to a service.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * It returns EOK if all is fine or ENXIO if the connection is
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher * not available (either not yet set up or teared down).
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Returns e generic error in other cases.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozekstatic int service_send_ping(struct mt_svc *svc)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DBusMessage *msg;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!svc->conn) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(8, ("Service not yet initialized\n"));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return ENXIO;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(4,("Pinging %s\n", svc->name));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /*
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Set up identity request
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * This should be a well-known path and method
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * for all services
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher msg = dbus_message_new_method_call(NULL,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher MONITOR_PATH,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher MONITOR_INTERFACE,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek MON_CLI_METHOD_PING);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!msg) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(0,("Out of memory?!\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_zfree(svc->conn);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return ENOMEM;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = sbus_conn_send(svc->conn, msg,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->ping_time * 1000, /* milliseconds */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ping_check, svc, &svc->pending);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_message_unref(msg);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return ret;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher}
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void ping_check(DBusPendingCall *pending, void *data)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct mt_svc *svc;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DBusMessage *reply;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher const char *dbus_error_name;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher size_t len;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int type;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc = talloc_get_type(data, struct mt_svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (!svc) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* The connection probably went down before the callback fired.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Not much we can do. */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek DEBUG(SSSDBG_CRIT_FAILURE, ("Invalid service pointer.\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->pending = NULL;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek reply = dbus_pending_call_steal_reply(pending);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!reply) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* reply should never be null. This function shouldn't be called
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * until reply is valid or timeout has occurred. If reply is NULL
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * here, something is seriously wrong and we should bail out.
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0, ("A reply callback was called but no reply was received"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher " and no timeout occurred\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Destroy this connection */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher sbus_disconnect(svc->conn);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher goto done;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek type = dbus_message_get_type(reply);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher switch (type) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher case DBUS_MESSAGE_TYPE_METHOD_RETURN:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* ok peer replied,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * make sure we reset the failure counter in the service structure */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(4,("Service %s replied to ping\n", svc->name));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->failed_pongs = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher case DBUS_MESSAGE_TYPE_ERROR:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_error_name = dbus_message_get_error_name(reply);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!dbus_error_name) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher dbus_error_name = "<UNKNOWN>";
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher len = strlen(DBUS_ERROR_NO_REPLY);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Increase failed pong count */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (strnlen(dbus_error_name, len + 1) == len
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher && strncmp(dbus_error_name, DBUS_ERROR_NO_REPLY, len) == 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek ("A service PING timed out on [%s]. "
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "Attempt [%d]\n",
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher svc->name, svc->failed_pongs));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->failed_pongs++;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_FATAL_FAILURE,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ("A service PING returned an error [%s], closing connection.\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_error_name));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* Falling through to default intentionally*/
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher default:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /*
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Timeout or other error occurred or something
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek * unexpected happened.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * It doesn't matter which, because either way we
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher * know that this connection isn't trustworthy.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * We'll destroy it now.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek sbus_disconnect(svc->conn);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherdone:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_pending_call_unref(pending);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher dbus_message_unref(reply);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher}
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstatic void service_startup_handler(struct tevent_context *ev,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct tevent_timer *te,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct timeval t, void *ptr);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int start_service(struct mt_svc *svc)
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct tevent_timer *te;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct timeval tv;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(4,("Queueing service %s for startup\n", svc->name));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tv = tevent_timeval_current();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek /* Add a timed event to start up the service.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * We have to do this in order to avoid a race
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * condition where the service being started forks
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * and attempts to connect to the SBUS before
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * the monitor is serving it.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher te = tevent_add_timer(svc->mt_ctx->ev, svc, tv,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher service_startup_handler, svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (te == NULL) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(0, ("Unable to queue service %s for startup\n", svc->name));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return ENOMEM;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return EOK;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstatic void mt_svc_exit_handler(int pid, int wait_status, void *pvt);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void service_startup_handler(struct tevent_context *ev,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct tevent_timer *te,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek struct timeval t, void *ptr)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher errno_t ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_svc *mt_svc;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char **args;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher mt_svc = talloc_get_type(ptr, struct mt_svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (mt_svc == NULL) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher mt_svc->pid = fork();
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (mt_svc->pid != 0) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (mt_svc->pid == -1) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0, ("Could not fork child to start service [%s]. "
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "Continuing.\n", mt_svc->name));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek return;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Parent */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher mt_svc->mt_ctx->check_children = true;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek mt_svc->failed_pongs = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* Handle process exit */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret = sss_child_register(mt_svc,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher mt_svc->mt_ctx->sigchld_ctx,
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek mt_svc->pid,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher mt_svc_exit_handler,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher mt_svc,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher &mt_svc->child_ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ret != EOK) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_FATAL_FAILURE,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ("Could not register sigchld handler.\n"));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* Should we exit here? For now, we'll hope this
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher * child never dies, because we can't restart it.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DLIST_ADD(mt_svc->mt_ctx->svc_list, mt_svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher set_tasks_checker(mt_svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return;
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek }
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* child */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher args = parse_args(mt_svc->command);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher execvp(args[0], args);
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* If we are here, exec() has failed
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Print errno and abort quickly */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0,("Could not exec %s, reason: %s\n", mt_svc->command, strerror(errno)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* We have to call _exit() instead of exit() here
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * because a bug in D-BUS will cause the server to
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * close its socket at exit() */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher _exit(1);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic void mt_svc_restart(struct tevent_context *ev,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct tevent_timer *te,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct timeval t, void *ptr)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_svc *svc;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc = talloc_get_type(ptr, struct mt_svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (svc == NULL) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_TRACE_FUNC, ("Scheduling service %s for restart %d\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->name, svc->restarts+1));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (svc->type == MT_SVC_SERVICE) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher add_new_service(svc->mt_ctx, svc->name, svc->restarts + 1);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher } else if (svc->type == MT_SVC_PROVIDER) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher add_new_provider(svc->mt_ctx, svc->name, svc->restarts + 1);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher } else {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* Invalid type? */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE,
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ("BUG: Invalid child process type [%d]\n", svc->type));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* Free the old service (which will also remove it
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher * from the child list)
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher talloc_free(svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher}
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagherstatic void mt_svc_exit_handler(int pid, int wait_status, void *pvt)
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher{
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct mt_svc *svc = talloc_get_type(pvt, struct mt_svc);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct mt_ctx *mt_ctx = svc->mt_ctx;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher time_t now = time(NULL);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct tevent_timer *te;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher struct timeval tv;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher int restart_delay;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if WIFEXITED(wait_status) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(SSSDBG_OP_FAILURE,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ("Child [%s] exited with code [%d]\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->name, WEXITSTATUS(wait_status)));
b20208b80e99abb79c00d5ec526caa9465859c52Jakub Hrozek } else if WIFSIGNALED(wait_status) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_OP_FAILURE,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ("Child [%s] terminated with signal [%d]\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->name, WTERMSIG(wait_status)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher } else {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(0, ("Child [%s] did not exit cleanly\n", svc->name));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Forcibly kill this child, just in case */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher kill(svc->pid, SIGKILL);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* Return and let us get caught by another
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * call to the SIGCHLD handler
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if ((now - svc->last_restart) > MONITOR_RESTART_CNT_INTERVAL_RESET) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher svc->restarts = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Restart the service */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (svc->restarts > MONITOR_MAX_SVC_RESTARTS) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_FATAL_FAILURE,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ("Process [%s], definitely stopped!\n", svc->name));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_free(svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher /* exit with error */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher monitor_quit(mt_ctx, 1);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher return;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* restarts are schedule after 0, 2, 4 seconds */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher restart_delay = svc->restarts << 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (restart_delay > MONITOR_MAX_RESTART_DELAY) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher restart_delay = MONITOR_MAX_RESTART_DELAY;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tv = tevent_timeval_current_ofs(restart_delay, 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher te = tevent_add_timer(svc->mt_ctx->ev, svc, tv, mt_svc_restart, svc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!te) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Nothing much we can do */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE, ("Out of memory?!\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherint main(int argc, const char *argv[])
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher int opt;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher poptContext pc;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher int opt_daemon = 0;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher int opt_interactive = 0;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher int opt_version = 0;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher char *opt_config_file = NULL;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher char *config_file = NULL;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int flags = 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct main_context *main_ctx;
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek TALLOC_CTX *tmp_ctx;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct mt_ctx *monitor;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int ret;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher uid_t uid;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct poptOption long_options[] = {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher POPT_AUTOHELP
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek SSSD_MAIN_OPTS
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek {"daemon", 'D', POPT_ARG_NONE, &opt_daemon, 0, \
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher _("Become a daemon (default)"), NULL }, \
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher {"interactive", 'i', POPT_ARG_NONE, &opt_interactive, 0, \
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher _("Run interactive (not a daemon)"), NULL}, \
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher {"config", 'c', POPT_ARG_STRING, &opt_config_file, 0, \
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher _("Specify a non-default config file"), NULL}, \
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher {"version", '\0', POPT_ARG_NONE, &opt_version, 0, \
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher _("Print version number and exit"), NULL }, \
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek POPT_TABLEEND
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek };
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Set debug level to invalid value so we can deside if -d 0 was used. */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher debug_level = SSSDBG_INVALID;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher pc = poptGetContext(argv[0], argc, argv, long_options, 0);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher while((opt = poptGetNextOpt(pc)) != -1) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher switch(opt) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher default:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fprintf(stderr, "\nInvalid option %s: %s\n\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher poptBadOption(pc, 0), poptStrerror(opt));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher poptPrintUsage(pc, stderr, 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher DEBUG_INIT(debug_level);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (opt_version) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher puts(VERSION""DISTRO_VERSION""PRERELEASE_VERSION);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return EXIT_SUCCESS;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* If the level or timestamps was passed at the command-line, we want
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher * to save it and pass it to the children later.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cmdline_debug_level = debug_level;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher cmdline_debug_timestamps = debug_timestamps;
65a9065538fd85e6ead925d344e6b421900eb8c2Jakub Hrozek cmdline_debug_microseconds = debug_microseconds;
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (opt_daemon && opt_interactive) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fprintf(stderr, "Option -i|--interactive is not allowed together with -D|--daemon\n");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher poptPrintUsage(pc, stderr, 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!opt_daemon && !opt_interactive) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher opt_daemon = 1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher poptFreeContext(pc);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher uid = getuid();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (uid != 0) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher DEBUG(SSSDBG_FATAL_FAILURE, ("Running under %d, must be root\n", uid));
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher sss_log(SSS_LOG_ALERT, "sssd must be run as root");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return 8;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher tmp_ctx = talloc_new(NULL);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (!tmp_ctx) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return 7;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (opt_daemon) flags |= FLAGS_DAEMON;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (opt_interactive) flags |= FLAGS_INTERACTIVE;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (opt_config_file) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher config_file = talloc_strdup(tmp_ctx, opt_config_file);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher } else {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher config_file = talloc_strdup(tmp_ctx, CONFDB_DEFAULT_CONFIG_FILE);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!config_file) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return 6;
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher }
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher /* we want a pid file check */
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher flags |= FLAGS_PID_FILE;
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher /* Open before server_setup() does to have logging
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher * during configuration checking */
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher if (debug_to_file) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ret = open_debug_file();
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher if (ret) {
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher return 7;
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher }
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher#ifdef USE_KEYRING
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher /* Do this before all the forks, it sets the session key ring so all
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher * keys are private to the daemon and cannot be read by any other process
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher * tree */
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher /* make a new session */
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher ret = keyctl_join_session_keyring(NULL);
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher if (ret == -1) {
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher sss_log(SSS_LOG_ALERT,
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher "Could not create private keyring session. "
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher "If you store password there they may be easily accessible "
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher "to the root user. (%d, %s)", errno, strerror(errno));
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher ret = keyctl_setperm(KEY_SPEC_SESSION_KEYRING, KEY_POS_ALL);
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher if (ret == -1) {
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher sss_log(SSS_LOG_ALERT,
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher "Could not set permissions on private keyring. "
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher "If you store password there they may be easily accessible "
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher "to the root user. (%d, %s)", errno, strerror(errno));
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher }
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher#endif
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher /* Warn if nscd seems to be running */
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher ret = check_file(NSCD_SOCKET_PATH, -1, -1, -1, CHECK_SOCK, NULL, false);
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher if (ret == EOK) {
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher sss_log(SSS_LOG_NOTICE,
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher "nscd socket was detected. Nscd caching capabilities "
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher "may conflict with SSSD for users and groups. It is "
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher "recommended not to run nscd in parallel with SSSD, unless "
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher "nscd is configured not to cache the passwd, group and "
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher "netgroup nsswitch maps.");
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher }
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher /* Parse config file, fail if cannot be done */
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher ret = load_configuration(tmp_ctx, config_file, &monitor);
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher if (ret != EOK) {
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher switch (ret) {
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher case EPERM:
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher case EACCES:
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE,
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher ("Insufficient permissions to read configuration file.\n"));
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher break;
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher default:
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE,
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher ("SSSD couldn't load the configuration database.\n"));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher sss_log(SSS_LOG_ALERT,
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek "SSSD couldn't load the configuration database [%d]: %s.\n",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ret, strerror(ret));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher break;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return 4;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher /* set up things like debug , signals, daemonization, etc... */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher monitor->conf_path = CONFDB_MONITOR_CONF_ENTRY;
2ea6196484055397cc4bc011c5960f790431fa9dStephen Gallagher ret = server_setup(MONITOR_NAME, flags, monitor->conf_path, &main_ctx);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ret != EOK) return 2;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher monitor->is_daemon = !opt_interactive;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher monitor->parent_pid = main_ctx->parent_pid;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher monitor->ev = main_ctx->event_ctx;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher talloc_steal(main_ctx, monitor);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek ret = monitor_process_init(monitor,
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek config_file);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek if (ret != EOK) return 3;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek talloc_free(tmp_ctx);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
e5c33e0bd03a2deb8e5011deeb3ae93f960910eeJakub Hrozek /* loop on main */
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek server_loop(main_ctx);
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek ret = monitor_cleanup();
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek if (ret != EOK) return 5;
6463ed1dcdd45416468b3fa178bd856b5a9ed2c3Jakub Hrozek
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek return 0;
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek}
524ceecc11f3d458eb3c1cf1489c3ff6ccb22226Jakub Hrozek