diff --git a/CMakeLists.txt b/CMakeLists.txt
index eb627f1..5c868c7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -96,6 +96,8 @@ set(CMAKE_LIBRARY_PATH "")
set(CMAKE_INCLUDE_PATH "")
include_directories(AFTER ${GLIB2_INCLUDE_DIRS})
-add_subdirectory(./lib)
+add_definitions(-DLOG_DOMAIN="gridinit")
+add_definitions(-DGRIDINIT_DOMAIN="gridinit")
+
add_subdirectory(./main)
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
deleted file mode 100644
index dd03f5f..0000000
--- a/lib/CMakeLists.txt
+++ /dev/null
@@ -1,24 +0,0 @@
-
-include_directories(BEFORE .)
-
-add_library(gridinit-utils SHARED
- uid.c
- common.c
- limits.c
- command.c
- children.c
- gridinit-utils.h
- gridinit-internals.h)
-
-target_link_libraries(gridinit-utils
- ${GLIB2_LIBRARIES})
-
-set_target_properties(gridinit-utils PROPERTIES
- PUBLIC_HEADER "gridinit-utils.h"
- SOVERSION ${ABI_VERSION})
-
-install(TARGETS gridinit-utils
- LIBRARY DESTINATION ${LD_LIBDIR}
- PUBLIC_HEADER DESTINATION include
- RUNTIME DESTINATION bin)
-
diff --git a/lib/command.c b/lib/command.c
deleted file mode 100644
index 972818d..0000000
--- a/lib/command.c
+++ /dev/null
@@ -1,103 +0,0 @@
-/*
-gridinit-utils, a helper library for gridinit.
-Copyright (C) 2013 AtoS Worldline, original work aside of Redcurrant
-Copyright (C) 2015 OpenIO, modified for OpenIO Software Defined Storage
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as
-published by the Free Software Foundation, either version 3 of the
-License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU Affero General Public License for more details.
-
-You should have received a copy of the GNU Affero General Public License
-along with this program. If not, see .
-*/
-
-#ifdef HAVE_CONFIG_H
-# include "../config.h"
-#endif
-
-#include
-#include
-#include
-#include
-#include
-
-#include
-#include "./gridinit-utils.h"
-
-#define IDX_IN 0
-#define IDX_OUT 1
-
-static void
-dup2_or_exit(int fd1, int fd2)
-{
- if (-1 == dup2(fd1, fd2)) {
- /*ERROR("dup2(%d,%d) error : %s", fd1, fd2, strerror(errno));*/
- exit(1);
- }
-}
-
-static void
-run_command(int fd_out, const char *cmd)
-{
- gint argc = 0;
- gchar **argv = NULL;
-
- if (!g_shell_parse_argv(cmd, &argc, &argv, NULL))
- exit(1);
-
- dup2_or_exit(fd_out, fileno(stdout));
- dup2_or_exit(fd_out, fileno(stderr));
-
- const char *real_cmd = argv[0];
- if (!g_path_is_absolute(real_cmd))
- real_cmd = g_find_program_in_path(real_cmd);
- execv(real_cmd, argv);
- g_strfreev(argv);
- exit(1);
-}
-
-int
-command_get_pipe(const gchar *str_cmd)
-{
- typeof(errno) errsav;
- int fd[2];
-
- if (!str_cmd) {
- errno = EINVAL;
- return -1;
- }
-
- if (0 != pipe(fd)) {
- return -1;
- }
-
- /*TRACE("pipe opened (IN=%d,OUT=%d)", fd[IDX_IN], fd[IDX_OUT]);*/
-
- switch (fork()) {
-
- case -1: /* ERROR */
- errsav = errno;
- close(fd[IDX_IN]);
- close(fd[IDX_OUT]);
- errno = errsav;
- return -1;
-
- case 0: /* CHILD */
- close(fd[IDX_IN]);
- /*TRACE("Child writing in fd=%d", fd[IDX_OUT]);*/
- run_command(fd[IDX_OUT], str_cmd); /*never returns on success*/
- return -1;/* makes everybody happy*/
-
- default: /* FATHER */
- close(fd[IDX_OUT]);
- /*TRACE("Father reading from fd=%d", fd[IDX_IN]);*/
- return fd[IDX_IN];
- }
-}
-
diff --git a/lib/gridinit-internals.h b/lib/gridinit-internals.h
deleted file mode 100644
index 46b73c9..0000000
--- a/lib/gridinit-internals.h
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
-gridinit-utils, a helper library for gridinit.
-Copyright (C) 2013 AtoS Worldline, original work aside of Redcurrant
-Copyright (C) 2015 OpenIO, modified for OpenIO Software Defined Storage
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as
-published by the Free Software Foundation, either version 3 of the
-License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU Affero General Public License for more details.
-
-You should have received a copy of the GNU Affero General Public License
-along with this program. If not, see .
-*/
-
-#ifndef __GRID_SUPERVISOR_INTERNALS_H__
-# define __GRID_SUPERVISOR_INTERNALS_H__
-# include
-
-#ifndef GRIDINIT_DOMAIN
-# define GRIDINIT_DOMAIN "gridinit"
-#endif
-
-# define GRID_LOGLVL_TRACE (32 << G_LOG_LEVEL_USER_SHIFT)
-# define GRID_LOGLVL_DEBUG (16 << G_LOG_LEVEL_USER_SHIFT)
-# define GRID_LOGLVL_INFO (8 << G_LOG_LEVEL_USER_SHIFT)
-# define GRID_LOGLVL_NOTICE (4 << G_LOG_LEVEL_USER_SHIFT)
-# define GRID_LOGLVL_WARN (2 << G_LOG_LEVEL_USER_SHIFT)
-# define GRID_LOGLVL_ERROR (1 << G_LOG_LEVEL_USER_SHIFT)
-
-# define FATAL(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_ERROR, Format, ##__VA_ARGS__)
-# define ALERT(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_ERROR, Format, ##__VA_ARGS__)
-# define CRIT(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_ERROR, Format, ##__VA_ARGS__)
-# define ERROR(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_ERROR, Format, ##__VA_ARGS__)
-# define WARN(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_WARN, Format, ##__VA_ARGS__)
-# define NOTICE(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_NOTICE, Format, ##__VA_ARGS__)
-# define INFO(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_INFO, Format, ##__VA_ARGS__)
-# define DEBUG(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_DEBUG, Format, ##__VA_ARGS__)
-# define TRACE(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_TRACE, Format, ##__VA_ARGS__)
-
-GError* g_error_printf(const char *dom, int code, const char *fmt, ...);
-
-#endif
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt
index 812a4d3..18f9887 100644
--- a/main/CMakeLists.txt
+++ b/main/CMakeLists.txt
@@ -1,35 +1,24 @@
-
-include_directories(BEFORE .
- ../lib)
-
-add_library(gridinit-internals STATIC
- utils.c
- cnx.c
- gridinit_internals.h)
-target_link_libraries(gridinit-internals
- ${GLIB2_LIBRARIES})
+include_directories(BEFORE .)
add_executable(gridinit
+ uid.c
+ common.c
+ limits.c
+ children.c
gridinit.c
- alerting.c
- gridinit_alerts.h)
+ utils.c
+ cnx.c)
target_link_libraries(gridinit
- gridinit-utils gridinit-internals
${GLIB2_LIBRARIES} ${LIBEVENT_LIBRARIES})
add_executable(gridinit_cmd
gridinit_cmd.c
- format_output.c)
+ format_output.c
+ utils.c
+ cnx.c)
target_link_libraries(gridinit_cmd
- gridinit-internals
${GLIB2_LIBRARIES} ${LIBEVENT_LIBRARIES})
-add_executable(gridinit_testcmd
- test_cmd.c)
-target_link_libraries(gridinit_testcmd
- gridinit-utils gridinit-internals
- ${GLIB2_LIBRARIES})
-
install(TARGETS gridinit gridinit_cmd
LIBRARY DESTINATION ${LD_LIBDIR}
PUBLIC_HEADER DESTINATION include
diff --git a/main/alerting.c b/main/alerting.c
deleted file mode 100644
index e063fd1..0000000
--- a/main/alerting.c
+++ /dev/null
@@ -1,117 +0,0 @@
-/*
-gridinit, a monitor for non-daemon processes.
-Copyright (C) 2013 AtoS Worldline, original work aside of Redcurrant
-Copyright (C) 2015 OpenIO, modified for OpenIO Software Defined Storage
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as
-published by the Free Software Foundation, either version 3 of the
-License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU Affero General Public License for more details.
-
-You should have received a copy of the GNU Affero General Public License
-along with this program. If not, see .
-*/
-
-#ifdef HAVE_CONFIG_H
-# include "../config.h"
-#endif
-#ifndef LOG_DOMAIN
-# define LOG_DOMAIN "gridinit"
-#endif
-
-#include
-#include
-#include
-
-#include
-#include "./gridinit_internals.h"
-#include "./gridinit_alerts.h"
-#include "../lib/gridinit-internals.h"
-
-static GModule *module = NULL;
-static struct gridinit_alert_handle_s *handle = NULL;
-
-/* ------------------------------------------------------------------------- */
-
-gboolean
-gridinit_alerting_configure(const gchar *path, const gchar *symbol,
- GHashTable *ht_params, GError **err)
-{
- TRACE("trying to configure the alerting with the module [%s] and the symbol [%s]",
- path, symbol);
- if (!symbol || !path) {
- if (err) {
- *err = g_error_printf(LOG_DOMAIN, 500, "Invalid parameter");
- }
- return FALSE;
- }
- if (module != NULL) {
- if (err) {
- *err = g_error_printf(LOG_DOMAIN, 500, "Module already loaded");
- }
- return FALSE;
- }
-
- /* Open the module and locate the exported symbol */
- if (NULL == (module = g_module_open (path, 0))) {
- if (err) {
- *err = g_error_printf(LOG_DOMAIN, 500,
- "Cannot load the plug-in from file %s (%s)",
- path, g_module_error());
- }
- return FALSE;
- }
-
- gpointer pointer = NULL;
- if (!g_module_symbol(module, symbol, &pointer) || !pointer) {
- if (err) {
- *err = g_error_printf(LOG_DOMAIN, 500,
- "Cannot get the exported structure (%s) from the plug-in %p (%s)",
- symbol, (void*)module, g_module_error());
- }
- return FALSE;
- }
-
- handle = pointer;
- if (handle->init) {
- handle->init(handle->module_data, ht_params);
- }
-
- return TRUE;
-}
-
-
-void
-gridinit_alerting_send(int event, const char *msg)
-{
- if (event == GRIDINIT_EVENT_BROKEN) {
- ERROR("Process alert: %s", msg);
- } else if (event == GRIDINIT_EVENT_RESTARTED) {
- WARN("Process alert: %s", msg);
- } else {
- NOTICE("Process alert: %s", msg);
- }
-
- if (!module || !handle || !handle->send)
- return;
-
- handle->send(handle->module_data, event, msg);
-}
-
-void
-gridinit_alerting_close(void)
-{
- if (handle && handle->fini)
- handle->fini(handle->module_data);
- if (module)
- g_module_close(module);
-
- module = NULL;
- handle = NULL;
-}
-
diff --git a/lib/children.c b/main/children.c
similarity index 96%
rename from lib/children.c
rename to main/children.c
index f996d91..2e845c5 100644
--- a/lib/children.c
+++ b/main/children.c
@@ -1,7 +1,7 @@
/*
gridinit-utils, a helper library for gridinit.
Copyright (C) 2013 AtoS Worldline, original work aside of Redcurrant
-Copyright (C) 2015 OpenIO, modified for OpenIO Software Defined Storage
+Copyright (C) 2015-2018 OpenIO SAS
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
@@ -17,10 +17,6 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
-#ifdef HAVE_CONFIG_H
-# include "../config.h"
-#endif
-
#include
#include
#include
@@ -33,7 +29,6 @@ along with this program. If not, see .
#include
#include "./gridinit-utils.h"
-#include "./gridinit-internals.h"
time_t supervisor_default_delay_KILL = SUPERVISOR_DEFAULT_TIMEOUT_KILL;
@@ -666,29 +661,48 @@ supervisor_children_disable_obsolete(void)
return count;
}
+static inline int
+_is_dead(const pid_t needle, pid_t *pincushion, register const int max)
+{
+ for (register int i=0; i 0) {
- FOREACH_CHILD(sd) {
- if (sd->pid == pid_dead) {
- count++;
- _child_notify_death(sd);
- if (cb) {
- _child_get_info(sd, &ci);
- cb(udata, &ci);
- }
- sd->pid = -1;
- break;
- }
- }
+ g_assert_nonnull(cb);
+
+ /* Consume a batch of dead children */
+ while (pids_idx < 1024 && (pid = waitpid(-1, NULL, WNOHANG)) > 0)
+ pids[pids_idx++] = pid;
+ if (!pids_idx)
+ return 0;
+
+ /* Locate the concerned structures, for each dead child */
+ FOREACH_CHILD(sd) {
+ if (!_is_dead(sd->pid, pids, pids_idx))
+ continue;
+
+ count++;
+ _child_notify_death(sd);
+
+ struct child_info_s ci = {};
+ _child_get_info(sd, &ci);
+ cb(udata, &ci);
+
+ sd->pid = -1;
}
+
return count;
}
diff --git a/main/cnx.c b/main/cnx.c
index bca43cb..08ea573 100644
--- a/main/cnx.c
+++ b/main/cnx.c
@@ -1,7 +1,7 @@
/*
gridinit, a monitor for non-daemon processes.
Copyright (C) 2013 AtoS Worldline, original work aside of Redcurrant
-Copyright (C) 2015 OpenIO, modified for OpenIO Software Defined Storage
+Copyright (C) 2015-2018 OpenIO SAS
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
@@ -17,13 +17,6 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
-#ifdef HAVE_CONFIG_H
-# include "../config.h"
-#endif
-#ifndef LOG_DOMAIN
-# define LOG_DOMAIN "gridinit"
-#endif
-
#include
#include
#include
@@ -31,9 +24,6 @@ along with this program. If not, see .
#include
#include
#include
-#include
-#include
-#include
#include
#include
#include
@@ -41,49 +31,14 @@ along with this program. If not, see .
#include
-#include "./gridinit_internals.h"
-#include "../lib/gridinit-internals.h"
-
-static volatile int backlog_unix = 65536;
-static volatile int backlog_tcp = 4096;
-
-static int
-__addr_split(gchar *url_wrk, gchar ** host, gchar ** port)
-{
- int len;
- gchar *last_semicolon;
-
- len = strlen(url_wrk);
-
- if (*url_wrk == '[') { /*[IP]:PORT */
-
- last_semicolon = g_strrstr(url_wrk, ":");
- if (!last_semicolon || last_semicolon - url_wrk >= len)
- return 0;
-
- *(last_semicolon - 1) = '\0';
- *port = &(last_semicolon[1]);
- *host = &(url_wrk[1]);
- return 1;
- }
-
- last_semicolon = g_strrstr(url_wrk, ":");
- if (!last_semicolon || last_semicolon - url_wrk >= len)
- return 0;
-
- *last_semicolon = '\0';
- *port = &(last_semicolon[1]);
- *host = &(url_wrk[0]);
- return 1;
-}
+#include "gridinit_internals.h"
int
__open_unix_client(const char *path)
{
int sock;
- struct sockaddr_un local;
+ struct sockaddr_un local = {};
- memset(&local, 0x00, sizeof(local));
if (!path || strlen(path) >= sizeof(local.sun_path)) {
errno = EINVAL;
return -1;
@@ -94,12 +49,6 @@ __open_unix_client(const char *path)
if (sock < 0)
return -1;
-#if 0
- /* Got to non-blocking mode */
- if (-1 == fcntl(sock, F_SETFL, O_NONBLOCK))
- goto label_error;
-#endif
-
/* Bind to file */
local.sun_family = AF_UNIX;
g_strlcpy(local.sun_path, path, sizeof(local.sun_path)-1);
@@ -120,112 +69,3 @@ __open_unix_client(const char *path)
return -1;
}
-int
-__open_unix_server(const char *path)
-{
- int sock;
- struct sockaddr_un local;
-
- memset(&local, 0x00, sizeof(local));
- if (!path || strlen(path) >= sizeof(local.sun_path)) {
- errno = EINVAL;
- return -1;
- }
-
- /* Create ressources to monitor */
-#ifdef SOCK_CLOEXEC
-# define SOCK_FLAGS SOCK_CLOEXEC
-#else
-# define SOCK_FLAGS 0
-#endif
-
- sock = socket(PF_UNIX, SOCK_STREAM | SOCK_FLAGS, 0);
- if (sock < 0)
- return -1;
-
-#ifndef SOCK_CLOEXEC
-# ifdef FD_CLOEXEC
- (void) fcntl(sock, F_SETFD, fcntl(sock, F_GETFD)|FD_CLOEXEC);
-# endif
-#endif
-
- /* Bind to file */
- local.sun_family = AF_UNIX;
- g_strlcpy(local.sun_path, path, sizeof(local.sun_path)-1);
-
- if (-1 == bind(sock, (struct sockaddr *)&local, sizeof(local)))
- goto label_error;
-
- /* Listen on that socket */
- if (-1 == listen(sock, backlog_unix))
- goto label_error;
-
- errno = 0;
- return sock;
-
-label_error:
- if (sock >= 0) {
- typeof(errno) errsav;
- errsav = errno;
- close(sock);
- errno = errsav;
- }
- return -1;
-}
-
-int
-__open_inet_server(const char *url)
-{
- gchar url_wrk[512];
-
- int sock = -1;
- int i_opt = 1;
- struct sockaddr_in sin;
- gchar *host=NULL, *port=NULL;
-
- memset(url_wrk, 0x00, sizeof(url_wrk));
- g_strlcpy(url_wrk, url, sizeof(url_wrk)-1);
-
- if (!__addr_split(url_wrk, &host, &port)) {
- errno = EINVAL;
- return -1;
- }
-
- sock = socket(PF_INET, SOCK_STREAM, 0);
- if (sock < 0) {
- /* transmit the errno as is */
- return -1;
- }
-
- /* SO_REUSEADDR */
- i_opt = 1;
- if (0 != setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (void*) &i_opt, sizeof(i_opt)))
- WARN("Cannot set SO_REUSEADDR flag on socket %d (%s)", sock, strerror(errno));
-
- /* bind on the given URL then wait for incoming connections */
- memset(&sin, 0x00, sizeof(sin));
- sin.sin_family = AF_INET;
- sin.sin_port = htons(atoi(port));
- if (!inet_aton(host, &(sin.sin_addr)))
- goto label_error;
-
- if (-1 == bind(sock, (struct sockaddr *)&sin, sizeof(sin)))
- goto label_error;
-
- if (-1 == listen(sock, backlog_tcp))
- goto label_error;
-
- errno = 0;
- return sock;
-
-label_error:
- if (sock >= 0) {
- typeof(errno) errsav;
- errsav = errno;
- close(sock);
- errno = errsav;
- }
- return -1;
-}
-
-
diff --git a/lib/common.c b/main/common.c
similarity index 87%
rename from lib/common.c
rename to main/common.c
index b58495e..8754bb3 100644
--- a/lib/common.c
+++ b/main/common.c
@@ -1,7 +1,7 @@
/*
gridinit-utils, a helper library for gridinit.
Copyright (C) 2013 AtoS Worldline, original work aside of Redcurrant
-Copyright (C) 2015 OpenIO, modified for OpenIO Software Defined Storage
+Copyright (C) 2015-2018 OpenIO SAS
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
@@ -17,16 +17,11 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
-#ifdef HAVE_CONFIG_H
-# include "../config.h"
-#endif
-
-#include "./gridinit-utils.h"
-#include "./gridinit-internals.h"
-
#include
#include
+#include "./gridinit-utils.h"
+
GError*
g_error_printf(const char *dom, int code, const char *fmt, ...)
{
diff --git a/lib/gridinit-utils.h b/main/gridinit-utils.h
similarity index 80%
rename from lib/gridinit-utils.h
rename to main/gridinit-utils.h
index 6d354f0..e1b39d2 100644
--- a/lib/gridinit-utils.h
+++ b/main/gridinit-utils.h
@@ -1,7 +1,7 @@
/*
gridinit-utils, a helper library for gridinit.
Copyright (C) 2013 AtoS Worldline, original work aside of Redcurrant
-Copyright (C) 2015 OpenIO, modified for OpenIO Software Defined Storage
+Copyright (C) 2015-2018 OpenIO SAS
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
@@ -32,6 +32,25 @@ along with this program. If not, see .
# include
# include
+# define GRID_LOGLVL_TRACE (32 << G_LOG_LEVEL_USER_SHIFT)
+# define GRID_LOGLVL_DEBUG (16 << G_LOG_LEVEL_USER_SHIFT)
+# define GRID_LOGLVL_INFO (8 << G_LOG_LEVEL_USER_SHIFT)
+# define GRID_LOGLVL_NOTICE (4 << G_LOG_LEVEL_USER_SHIFT)
+# define GRID_LOGLVL_WARN (2 << G_LOG_LEVEL_USER_SHIFT)
+# define GRID_LOGLVL_ERROR (1 << G_LOG_LEVEL_USER_SHIFT)
+
+# define FATAL(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_ERROR, Format, ##__VA_ARGS__)
+# define ALERT(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_ERROR, Format, ##__VA_ARGS__)
+# define CRIT(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_ERROR, Format, ##__VA_ARGS__)
+# define ERROR(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_ERROR, Format, ##__VA_ARGS__)
+# define WARN(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_WARN, Format, ##__VA_ARGS__)
+# define NOTICE(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_NOTICE, Format, ##__VA_ARGS__)
+# define INFO(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_INFO, Format, ##__VA_ARGS__)
+# define DEBUG(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_DEBUG, Format, ##__VA_ARGS__)
+# define TRACE(Format,...) g_log(GRIDINIT_DOMAIN, GRID_LOGLVL_TRACE, Format, ##__VA_ARGS__)
+
+GError* g_error_printf(const char *dom, int code, const char *fmt, ...);
+
extern time_t supervisor_default_delay_KILL;
/* Children monitoring ----------------------------------------------------- */
@@ -153,10 +172,6 @@ int supervisor_children_set_ids(const gchar *key, gint32 uid, gint32 gid);
int supervisor_children_set_delay_sigkill(const char *key, time_t delay);
-/* Fork and pipe ----------------------------------------------------------- */
-
-int command_get_pipe(const gchar *str_cmd);
-
/* Privileges -------------------------------------------------------------- */
gboolean supervisor_rights_init(const char *user_name, const char *group_name,
diff --git a/main/gridinit.c b/main/gridinit.c
index 83ce9cf..5fba4e5 100644
--- a/main/gridinit.c
+++ b/main/gridinit.c
@@ -1,7 +1,7 @@
/*
gridinit, a monitor for non-daemon processes.
Copyright (C) 2013 AtoS Worldline, original work aside of Redcurrant
-Copyright (C) 2015 OpenIO, modified for OpenIO Software Defined Storage
+Copyright (C) 2015-2018 OpenIO SAS
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
@@ -17,13 +17,6 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
-#ifdef HAVE_CONFIG_H
-# include "../config.h"
-#endif
-#ifndef LOG_DOMAIN
-# define LOG_DOMAIN "gridinit.main"
-#endif
-
#include
#include
#include
@@ -51,8 +44,6 @@ along with this program. If not, see .
#include
#include "./gridinit_internals.h"
-#include "./gridinit_alerts.h"
-#include "../lib/gridinit-internals.h"
#define USERFLAG_PROCESS_DIED 0x00000002
#define USERFLAG_PROCESS_RESTARTED 0x00000004
@@ -108,8 +99,6 @@ static GHashTable *default_env = NULL;
static gboolean _cfg_reload(gboolean services_only, GError **err);
-static void servers_ensure(void);
-
static GOptionEntry entries[] = {
{"daemonize", 'd', 0, G_OPTION_ARG_NONE, (gboolean *)&flag_daemon,
"Detaches then daemonizes the gridinit", NULL},
@@ -232,20 +221,17 @@ static void
alert_send_deferred(void *udata, struct child_info_s *ci)
{
(void) udata;
- gchar buff[1024];
/* Handle the alerting of broken services */
if ((ci->user_flags & USERFLAG_PROCESS_DIED) && ci->broken) {
supervisor_children_del_user_flags(ci->key, USERFLAG_PROCESS_DIED);
- g_snprintf(buff, sizeof(buff), "Process broken [%s] %s", ci->key, ci->cmd);
- gridinit_alerting_send(GRIDINIT_EVENT_BROKEN, buff);
+ ERROR("Process broken [%s] %s", ci->key, ci->cmd);
}
/* Handle the alerting of successfully restarted services */
if (!(ci->user_flags & USERFLAG_PROCESS_DIED) && (ci->user_flags & USERFLAG_PROCESS_RESTARTED)) {
supervisor_children_del_user_flags(ci->key, USERFLAG_PROCESS_RESTARTED);
- g_snprintf(buff, sizeof(buff), "Process restarted [%s] %s", ci->key, ci->cmd);
- gridinit_alerting_send(GRIDINIT_EVENT_RESTARTED, buff);
+ NOTICE("Process restarted [%s] %s", ci->key, ci->cmd);
}
}
@@ -544,9 +530,7 @@ supervisor_signal_handler(int s, short flags, void *udata)
case SIGUSR1:
flag_more_verbose = ~0;
return;
- case SIGUSR2:
- flag_check_socket = ~0;
- return;
+ case SIGUSR2: /* ignored */
case SIGPIPE: /* ignored */
return;
case SIGINT:
@@ -864,6 +848,57 @@ servers_save_fd(int fd, const char *url)
return TRUE;
}
+static int
+__open_unix_server(const char *path)
+{
+ struct sockaddr_un local = {};
+
+ if (!path || strlen(path) >= sizeof(local.sun_path)) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ /* Create ressources to monitor */
+#ifdef SOCK_CLOEXEC
+# define SOCK_FLAGS SOCK_CLOEXEC
+#else
+# define SOCK_FLAGS 0
+#endif
+
+ int sock = socket(PF_UNIX, SOCK_STREAM | SOCK_FLAGS, 0);
+ if (sock < 0)
+ return -1;
+
+#ifndef SOCK_CLOEXEC
+# ifdef FD_CLOEXEC
+ (void) fcntl(sock, F_SETFD, fcntl(sock, F_GETFD)|FD_CLOEXEC);
+# endif
+#endif
+
+ /* Bind to file */
+ local.sun_family = AF_UNIX;
+ g_strlcpy(local.sun_path, path, sizeof(local.sun_path)-1);
+
+ if (-1 == bind(sock, (struct sockaddr *)&local, sizeof(local)))
+ goto label_error;
+
+ /* Listen on that socket */
+ if (-1 == listen(sock, 65536))
+ goto label_error;
+
+ errno = 0;
+ return sock;
+
+label_error:
+ if (sock >= 0) {
+ typeof(errno) errsav;
+ errsav = errno;
+ close(sock);
+ errno = errsav;
+ }
+ return -1;
+}
+
/**
* Opens a UNIX server socket then manage a server based on it
*/
@@ -919,42 +954,6 @@ servers_clean(void)
list_of_servers = NULL;
}
-/**
- * Reopens all the UNIX server sockets bond on paths that changed.
- */
-static void
-servers_ensure(void)
-{
- GList *l;
-
- flag_check_socket = 0;
- TRACE("About to ensure the server sockets");
-
- for (l=list_of_servers; l ; l=l->next) {
- struct server_sock_s *p_server = l->data;
-
- NOTICE("Ensuring socket fd=%d bond to [%s]", p_server->fd, p_server->url);
-
- if (servers_is_unix(p_server) && !servers_is_the_same(p_server)) {
-
- /* close */
- servers_unmonitor_one(p_server);
-
- /* reopen */
- p_server->fd = __open_unix_server(p_server->url);
- if (p_server->fd < 0) {
- WARN("unix: failed to reopen a server bond to [%s] : %s",
- p_server->url, strerror(errno));
- }
- else if (!servers_monitor_one(p_server)) {
- WARN("unix: failed to monitor a server bond to [%s] : %s",
- p_server->url, strerror(errno));
- servers_unmonitor_one(p_server);
- }
- }
- }
-}
-
/* Signals management ------------------------------------------------------ */
static void
@@ -1319,60 +1318,6 @@ _cfg_section_service(GKeyFile *kf, const gchar *section, GError **err)
return rc;
}
-static gboolean
-_cfg_section_alert(GKeyFile *kf, const gchar *section, GError **err)
-{
- gchar cfg_plugin[1024], cfg_symbol[128];
- gchar **p_key, **keys;
-
- bzero(cfg_plugin, sizeof(cfg_plugin));
- bzero(cfg_symbol, sizeof(cfg_symbol));
-
- keys = g_key_file_get_keys(kf, section, NULL, err);
- if (!keys)
- return FALSE;
-
- for (p_key=keys; *p_key ;p_key++) {
- gchar *str;
-
- str = g_key_file_get_string(kf, section, *p_key, NULL);
-
- if (!g_ascii_strcasecmp(*p_key, "plugin")) {
- if (*cfg_plugin)
- NOTICE("Alerting plugin already known : plugin=[%s]", cfg_plugin);
- else
- g_strlcpy(cfg_plugin, str, sizeof(cfg_plugin)-1);
- }
- else if (!g_ascii_strcasecmp(*p_key, "symbol")) {
- if (*cfg_symbol)
- NOTICE("Alerting symbol already known : symbol=[%s]", cfg_symbol);
- else
- g_strlcpy(cfg_symbol, str, sizeof(cfg_symbol)-1);
- }
-
- g_free(str);
- }
-
- g_strfreev(keys);
-
- if (!*cfg_symbol || !*cfg_plugin) {
- WARN("Missing configuration keys : both \"plugin\" and \"symbol\""
- " must be present in section [%s]", section);
- return FALSE;
- }
- else {
- GHashTable *ht_params;
- gboolean rc;
- ht_params = _cfg_extract_parameters(kf, section, "config.", err);
- rc = gridinit_alerting_configure(cfg_plugin, cfg_symbol, ht_params, err);
- g_hash_table_destroy(ht_params);
- if (!rc)
- return FALSE;
- }
-
- return TRUE;
-}
-
static gboolean
_cfg_section_default(GKeyFile *kf, const gchar *section, GError **err)
{
@@ -1537,13 +1482,6 @@ _cfg_reload_file(GKeyFile *kf, gboolean services_only, GError **err)
goto label_exit;
}
}
- else if (!services_only && !g_ascii_strcasecmp(*p_group, "alerts")) {
- INFO("reconfigure : loading alerting parameters from section [%s]", *p_group);
- if (!_cfg_section_alert(kf, *p_group, err)) {
- WARN("Invalid alerts section");
- goto label_exit;
- }
- }
else {
INFO("reconfigure : ignoring section [%s]", *p_group);
}
@@ -1984,8 +1922,6 @@ main(int argc, char ** args)
if (!flag_running)
break;
- if (flag_check_socket)
- servers_ensure();
if (flag_more_verbose) {
NOTICE("Increasing verbosity for 15 minutes");
logger_verbose();
@@ -2036,7 +1972,6 @@ main(int argc, char ** args)
signals_clean();
g_free(config_path);
- gridinit_alerting_close();
closelog();
return rc;
}
diff --git a/main/gridinit_alerts.h b/main/gridinit_alerts.h
deleted file mode 100644
index 79bdb93..0000000
--- a/main/gridinit_alerts.h
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
-gridinit, a monitor for non-daemon processes.
-Copyright (C) 2013 AtoS Worldline, original work aside of Redcurrant
-Copyright (C) 2015 OpenIO, modified for OpenIO Software Defined Storage
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as
-published by the Free Software Foundation, either version 3 of the
-License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU Affero General Public License for more details.
-
-You should have received a copy of the GNU Affero General Public License
-along with this program. If not, see .
-*/
-
-#ifndef __GRIDINIT_ALERTS_H__
-# define __GRIDINIT_ALERTS_H__
-# include
-# define GRIDINIT_EVENT_STARTED 1
-# define GRIDINIT_EVENT_RESTARTED 2
-# define GRIDINIT_EVENT_BROKEN 3
-
-/**
- * @param udata the user data provided in the exported structure
- */
-typedef void (*gridinit_alert_handler_f) (void *udata, int event, const char *msg);
-
-/**
- * @param udata the user data provided in the exported structure
- */
-typedef void (*gridinit_alert_init_f) (void *udata, GHashTable *params);
-
-/**
- * @param udata the user data provided in the exported structure
- */
-typedef void (*gridinit_alert_fini_f) (void *udata);
-
-/**
- * The type of structure that must be exported by the module
- * under the name MODULE_HANDLER_gridnit_alert;
- */
-struct gridinit_alert_handle_s {
- void *module_data;
- gridinit_alert_init_f init;
- gridinit_alert_fini_f fini;
- gridinit_alert_handler_f send;
-};
-
-#endif
diff --git a/main/gridinit_cmd.c b/main/gridinit_cmd.c
index ac194f3..2584d55 100644
--- a/main/gridinit_cmd.c
+++ b/main/gridinit_cmd.c
@@ -1,7 +1,7 @@
/*
gridinit, a monitor for non-daemon processes.
Copyright (C) 2013 AtoS Worldline, original work aside of Redcurrant
-Copyright (C) 2015 OpenIO, modified for OpenIO Software Defined Storage
+Copyright (C) 2015-2018 OpenIO SAS
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
@@ -17,13 +17,6 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
-#ifdef HAVE_CONFIG_H
-# include "../config.h"
-#endif
-#ifndef LOG_DOMAIN
-# define LOG_DOMAIN "gridinit_cmd"
-#endif
-
#include
#include
#include
@@ -38,7 +31,6 @@ along with this program. If not, see .
#include
#include "./gridinit_internals.h"
-#include "../lib/gridinit-internals.h"
#define MINI 0
#define MEDIUM 1
diff --git a/main/gridinit_internals.h b/main/gridinit_internals.h
index 7fce86d..8cb8a71 100644
--- a/main/gridinit_internals.h
+++ b/main/gridinit_internals.h
@@ -1,7 +1,7 @@
/*
gridinit, a monitor for non-daemon processes.
Copyright (C) 2013 AtoS Worldline, original work aside of Redcurrant
-Copyright (C) 2015 OpenIO, modified for OpenIO Software Defined Storage
+Copyright (C) 2015-2018 OpenIO SAS
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
@@ -80,20 +80,8 @@ along with this program. If not, see .
# define CFG_KEY_INHERIT "inherit_env"
#endif
-int __open_unix_server(const char *path);
-
int __open_unix_client(const char *path);
-int __open_inet_server(const char *url);
-
-/* Alerting */
-
-gboolean gridinit_alerting_configure(const gchar *path, const gchar *symbol, GHashTable *ht, GError **err);
-
-void gridinit_alerting_send(int event, const char *msg);
-
-void gridinit_alerting_close(void);
-
/* Groups matching */
gboolean gridinit_group_in_set(const gchar *group, const gchar *set);
diff --git a/lib/limits.c b/main/limits.c
similarity index 95%
rename from lib/limits.c
rename to main/limits.c
index d76cb7e..c63a551 100644
--- a/lib/limits.c
+++ b/main/limits.c
@@ -1,7 +1,7 @@
/*
gridinit-utils, a helper library for gridinit.
Copyright (C) 2013 AtoS Worldline, original work aside of Redcurrant
-Copyright (C) 2015 OpenIO, modified for OpenIO Software Defined Storage
+Copyright (C) 2015-2018 OpenIO SAS
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
@@ -17,13 +17,6 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
-#ifdef HAVE_CONFIG_H
-# include "../config.h"
-#endif
-
-#include "./gridinit-utils.h"
-#include "./gridinit-internals.h"
-
#include
#include
#include
@@ -31,6 +24,8 @@ along with this program. If not, see .
#include
#include
+#include "./gridinit-utils.h"
+
static const char*
get_rlimit_name(enum supervisor_limit_e what)
{
diff --git a/main/test_cmd.c b/main/test_cmd.c
deleted file mode 100644
index 8488616..0000000
--- a/main/test_cmd.c
+++ /dev/null
@@ -1,114 +0,0 @@
-/*
-gridinit, a monitor for non-daemon processes.
-Copyright (C) 2013 AtoS Worldline, original work aside of Redcurrant
-Copyright (C) 2015 OpenIO, modified for OpenIO Software Defined Storage
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as
-published by the Free Software Foundation, either version 3 of the
-License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU Affero General Public License for more details.
-
-You should have received a copy of the GNU Affero General Public License
-along with this program. If not, see .
-*/
-
-#include
-#include
-#include
-#include
-
-#include
-
-#include "../lib/gridinit-utils.h"
-
-static void
-dump_limits(void)
-{
- gint64 limit;
-
- g_print("\nLIMITS:\n");
- supervisor_limit_get(SUPERV_LIMIT_THREAD_STACK, &limit);
- g_print("\tSUPERV_LIMIT_THREAD_STACK = %"G_GINT64_FORMAT"\n", limit);
-
- supervisor_limit_get(SUPERV_LIMIT_MAX_FILES, &limit);
- g_print("\tSUPERV_LIMIT_MAX_FILES = %"G_GINT64_FORMAT"\n", limit);
-
- supervisor_limit_get(SUPERV_LIMIT_CORE_SIZE, &limit);
- g_print("\tSUPERV_LIMIT_CORE_SIZE = %"G_GINT64_FORMAT"\n", limit);
-}
-
-static void
-dump_environment(void)
-{
- gchar **env, **p_env;
- const gchar *value;
-
- g_print("\nENVIRONMENT:\n");
- if (!(env = g_listenv()))
- return;
-
- for (p_env=env ; *p_env ; p_env++) {
- value = g_getenv(*p_env);
- g_print("\t%s=%s\n", *p_env, value);
- }
-
- g_strfreev(env);
-}
-
-static void
-dump_cwd(void)
-{
- gchar *dir;
-
- dir = g_get_current_dir();
- g_print("Current directory: %s\n", dir);
- g_free(dir);
-}
-
-static void
-my_sleep(gdouble sleep_time)
-{
- GTimer *timer;
-
- g_print("\nSleeping %f seconds\n", sleep_time);
-
- timer = g_timer_new();
- while (g_timer_elapsed(timer, NULL) < sleep_time)
- sleep(1);
-
- g_print("Sleeped %f seconds\n", g_timer_elapsed(timer, NULL));
- g_timer_destroy(timer);
-}
-
-static void
-reopen_output(const gchar *path)
-{
- if (!freopen(path, "a", stderr))
- g_printerr("freopen(%s, \"a\", stderr) : %s\n", path, strerror(errno));
- if (!freopen(path, "a", stdout))
- g_printerr("freopen(%s, \"a\", stdout) : %s\n", path, strerror(errno));
-}
-
-int
-main(int argc, char **args)
-{
- gdouble sleep_time = 1.0;
-
- if (argc > 1)
- reopen_output(args[1]);
- if (argc > 2)
- sleep_time = g_ascii_strtod(args[2], NULL);
-
- dump_cwd();
- dump_limits();
- dump_environment();
- my_sleep(sleep_time);
-
- return 0;
-}
-
diff --git a/lib/uid.c b/main/uid.c
similarity index 93%
rename from lib/uid.c
rename to main/uid.c
index b11f72d..1dc6e71 100644
--- a/lib/uid.c
+++ b/main/uid.c
@@ -1,7 +1,7 @@
/*
gridinit-utils, a helper library for gridinit.
Copyright (C) 2013 AtoS Worldline, original work aside of Redcurrant
-Copyright (C) 2015 OpenIO, modified for OpenIO Software Defined Storage
+Copyright (C) 2015-2018 OpenIO SAS
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
@@ -17,13 +17,6 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
-#ifdef HAVE_CONFIG_H
-# include "../config.h"
-#endif
-
-#include "./gridinit-utils.h"
-#include "./gridinit-internals.h"
-
#include
#include
#include
@@ -31,6 +24,8 @@ along with this program. If not, see .
#include
#include
+#include "./gridinit-utils.h"
+
static volatile uid_t effective_uid = 0;
static volatile uid_t effective_gid = 0;
diff --git a/main/utils.c b/main/utils.c
index 68ca298..0394eea 100644
--- a/main/utils.c
+++ b/main/utils.c
@@ -1,7 +1,7 @@
/*
gridinit, a monitor for non-daemon processes.
Copyright (C) 2013 AtoS Worldline, original work aside of Redcurrant
-Copyright (C) 2015 OpenIO, modified for OpenIO Software Defined Storage
+Copyright (C) 2015-2018 OpenIO SAS
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
@@ -17,42 +17,21 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
-#ifdef HAVE_CONFIG_H
-# include "../config.h"
-#endif
-#ifndef LOG_DOMAIN
-# define LOG_DOMAIN "gridinit.utils"
-#endif
-
#include
-#include "./gridinit_internals.h"
-
-#define STR_SKIP_SPACES(s) do {\
- register gchar c;\
- for (; (c = *s) && g_ascii_isspace(c) ;++s);\
-} while (0)
-#define STR_TRIM_TRAILING_SPACES(s) do { \
- register gchar c, *end; \
- for (end = s; *end ;++end); \
- -- end; \
- for (; end > s && (c = *end) && g_ascii_isspace(c) ;--end) *end = '\0'; \
-} while (0)
+#include "./gridinit_internals.h"
gboolean
gridinit_group_in_set(const gchar *group, const gchar *set)
{
- gchar **tokens, **token, *g;
-
- tokens = g_strsplit_set(set, ",", -1);
+ gchar **tokens = g_strsplit_set(set, ",", -1);
if (!tokens)
return 0;
- for (token=tokens; (g = *token) ;token++) {
- STR_SKIP_SPACES(g);
- STR_TRIM_TRAILING_SPACES(g);
+ for (gchar **ptoken=tokens; *ptoken ;ptoken++) {
+ gchar *g = *ptoken;
if (!*g)
continue;
- if (0 == g_ascii_strcasecmp(g, group)) {
+ if (0 == g_ascii_strcasecmp(g_strstrip(g), group)) {
g_strfreev(tokens);
return TRUE;
}