* [gentoo-commits] proj/openrc:master commit in: src/rc/, src/includes/
@ 2014-10-24 1:07 William Hubbs
0 siblings, 0 replies; 7+ messages in thread
From: William Hubbs @ 2014-10-24 1:07 UTC (permalink / raw
To: gentoo-commits
commit: 6ca79042b9aa9752e577346e6f355356ef8f2f9a
Author: Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Thu Oct 23 23:56:19 2014 +0000
Commit: William Hubbs <williamh <AT> gentoo <DOT> org>
CommitDate: Fri Oct 24 00:14:06 2014 +0000
URL: http://sources.gentoo.org/gitweb/?p=proj/openrc.git;a=commit;h=6ca79042
helpers.h, start-stop-daemon.c: remove uneeded macros
TAILQ_CONCAT, TAILQ_FOREACH_SAFE and LIST_FOREACH_SAFE are defined
in our bundled queue.h and are no longer required.
---
src/includes/helpers.h | 19 -------------------
src/rc/start-stop-daemon.c | 9 ---------
2 files changed, 28 deletions(-)
diff --git a/src/includes/helpers.h b/src/includes/helpers.h
index 94e59a8..4352858 100644
--- a/src/includes/helpers.h
+++ b/src/includes/helpers.h
@@ -48,25 +48,6 @@
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
-/* Some libc implemntations don't have these */
-#ifndef TAILQ_CONCAT
-#define TAILQ_CONCAT(head1, head2, field) do { \
- if (!TAILQ_EMPTY(head2)) { \
- *(head1)->tqh_last = (head2)->tqh_first; \
- (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
- (head1)->tqh_last = (head2)->tqh_last; \
- TAILQ_INIT((head2)); \
- } \
- } while (0)
-#endif
-
-#ifndef TAILQ_FOREACH_SAFE
-#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
- for ((var) = TAILQ_FIRST((head)); \
- (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
- (var) = (tvar))
-#endif
-
#ifdef __GLIBC__
# if ! defined (__UCLIBC__) && ! defined (__dietlibc__)
# define strlcpy(dst, src, size) snprintf(dst, size, "%s", src)
diff --git a/src/rc/start-stop-daemon.c b/src/rc/start-stop-daemon.c
index 1412449..6229bbf 100644
--- a/src/rc/start-stop-daemon.c
+++ b/src/rc/start-stop-daemon.c
@@ -78,15 +78,6 @@ static struct pam_conv conv = { NULL, NULL};
#include "rc.h"
#include "rc-misc.h"
-/* Some libc implementations don't define this */
-#ifndef LIST_FOREACH_SAFE
-#define LIST_FOREACH_SAFE(var, head, field, tvar) \
- for ((var) = LIST_FIRST((head)); \
- (var) && ((tvar) = LIST_NEXT((var), field), 1); \
- (var) = (tvar))
-#endif
-
-
typedef struct scheduleitem
{
enum
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [gentoo-commits] proj/openrc:master commit in: src/rc/, src/includes/
@ 2016-01-14 18:55 William Hubbs
0 siblings, 0 replies; 7+ messages in thread
From: William Hubbs @ 2016-01-14 18:55 UTC (permalink / raw
To: gentoo-commits
commit: f5e06bc55ab1717ddff8c21f00f48cfcba8cb4a7
Author: William Hubbs <w.d.hubbs <AT> gmail <DOT> com>
AuthorDate: Thu Jan 14 18:52:21 2016 +0000
Commit: William Hubbs <williamh <AT> gentoo <DOT> org>
CommitDate: Thu Jan 14 18:52:32 2016 +0000
URL: https://gitweb.gentoo.org/proj/openrc.git/commit/?id=f5e06bc5
include rc.h properly in source files
We were attempting to include rc.h in rc-misc.h instead of the source
files where it should be included.
src/includes/rc-misc.h | 1 -
src/rc/_usage.c | 1 +
src/rc/checkpath.c | 1 +
src/rc/do_value.c | 1 +
src/rc/is_newer_than.c | 1 +
src/rc/is_older_than.c | 1 +
src/rc/swclock.c | 1 +
7 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/src/includes/rc-misc.h b/src/includes/rc-misc.h
index 9616634..f2449cd 100644
--- a/src/includes/rc-misc.h
+++ b/src/includes/rc-misc.h
@@ -26,7 +26,6 @@
#include <unistd.h>
#include "helpers.h"
-#include "rc.h"
#define RC_LEVEL_BOOT "boot"
#define RC_LEVEL_DEFAULT "default"
diff --git a/src/rc/_usage.c b/src/rc/_usage.c
index f0c7f0f..9fc3e5d 100644
--- a/src/rc/_usage.c
+++ b/src/rc/_usage.c
@@ -13,6 +13,7 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
+#include "rc.h"
#include "rc-misc.h"
#include "_usage.h"
#include "version.h"
diff --git a/src/rc/checkpath.c b/src/rc/checkpath.c
index c44ffe8..b150994 100644
--- a/src/rc/checkpath.c
+++ b/src/rc/checkpath.c
@@ -30,6 +30,7 @@
#include <unistd.h>
#include "einfo.h"
+#include "rc.h"
#include "rc-misc.h"
#include "rc-selinux.h"
#include "_usage.h"
diff --git a/src/rc/do_value.c b/src/rc/do_value.c
index a511afd..e6aa3ef 100644
--- a/src/rc/do_value.c
+++ b/src/rc/do_value.c
@@ -28,6 +28,7 @@
#include <unistd.h>
#include "einfo.h"
+#include "rc.h"
#include "rc-misc.h"
const char *applet = NULL;
diff --git a/src/rc/is_newer_than.c b/src/rc/is_newer_than.c
index f1aa9d6..4093ea8 100644
--- a/src/rc/is_newer_than.c
+++ b/src/rc/is_newer_than.c
@@ -15,6 +15,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include "rc.h"
#include "rc-misc.h"
int main(int argc, char **argv)
diff --git a/src/rc/is_older_than.c b/src/rc/is_older_than.c
index 87d5641..aea5377 100644
--- a/src/rc/is_older_than.c
+++ b/src/rc/is_older_than.c
@@ -15,6 +15,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include "rc.h"
#include "rc-misc.h"
int main(int argc, char **argv)
diff --git a/src/rc/swclock.c b/src/rc/swclock.c
index 4b62a46..8245f11 100644
--- a/src/rc/swclock.c
+++ b/src/rc/swclock.c
@@ -30,6 +30,7 @@
#include <utime.h>
#include "einfo.h"
+#include "rc.h"
#include "rc-misc.h"
#include "_usage.h"
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [gentoo-commits] proj/openrc:master commit in: src/rc/, src/includes/
@ 2016-01-19 6:12 William Hubbs
0 siblings, 0 replies; 7+ messages in thread
From: William Hubbs @ 2016-01-19 6:12 UTC (permalink / raw
To: gentoo-commits
commit: f79a7a7be18d68bf264efc60d82838d03048da6b
Author: William Hubbs <w.d.hubbs <AT> gmail <DOT> com>
AuthorDate: Mon Jan 18 21:50:50 2016 +0000
Commit: William Hubbs <williamh <AT> gentoo <DOT> org>
CommitDate: Mon Jan 18 21:50:59 2016 +0000
URL: https://gitweb.gentoo.org/proj/openrc.git/commit/?id=f79a7a7b
rc: make get_systype similar to the old rc_sys function
src/includes/rc-misc.h | 5 +-
src/rc/_usage.c | 5 +-
src/rc/rc-misc.c | 333 +++++++++++++++++++++++++------------------------
src/rc/rc.c | 19 +--
4 files changed, 176 insertions(+), 186 deletions(-)
diff --git a/src/includes/rc-misc.h b/src/includes/rc-misc.h
index f2449cd..de73c5c 100644
--- a/src/includes/rc-misc.h
+++ b/src/includes/rc-misc.h
@@ -43,6 +43,7 @@
char *rc_conf_value(const char *var);
bool rc_conf_yesno(const char *var);
+char *get_systype(void);
void env_filter(void);
void env_config(void);
int signal_setup(int sig, void (*handler)(int));
@@ -60,10 +61,6 @@ int is_writable(const char *);
#define service_stop(service) exec_service(service, "stop");
int parse_mode(mode_t *, char *);
-const char *detect_prefix(void);
-const char *get_systype(void);
-const char *detect_container(void);
-const char *detect_vm(void);
/* Handy function so we can wrap einfo around our deptree */
RC_DEPTREE *_rc_deptree_load (int, int *);
diff --git a/src/rc/_usage.c b/src/rc/_usage.c
index 9fc3e5d..40d8142 100644
--- a/src/rc/_usage.c
+++ b/src/rc/_usage.c
@@ -44,12 +44,9 @@ void set_quiet_options(void)
_noreturn void show_version(void)
{
- const char *systype = NULL;
+ char *systype = get_systype();
printf("%s (OpenRC", applet);
- systype = detect_container();
- if (!systype)
- systype = detect_vm();
if (systype)
printf(" [%s]", systype);
printf(") %s", VERSION);
diff --git a/src/rc/rc-misc.c b/src/rc/rc-misc.c
index 2c6c388..e0e6e2c 100644
--- a/src/rc/rc-misc.c
+++ b/src/rc/rc-misc.c
@@ -50,6 +50,172 @@ rc_conf_yesno(const char *setting)
return rc_yesno(rc_conf_value (setting));
}
+/*
+ * static bool is_container(const char *systype)
+ * {
+ * FreeBSD: RC_SYS_JAIL
+ * Linux: RC_SYS_UML RC_SYS_VSERVER RC_SYS_OPENVZ RC_SYS_LXC
+ * RC_SYS_RKT RC_SYS_SYSTEMD_NSPAWN RC_SYS_DOCKER
+ * }
+ *
+ * static bool is_vm(const char *systype)
+ * {
+ * NetBSD: RC_SYS_XEN0 RC_SYS_XENU
+ * Linux: RC_SYS_XEN0 RC_SYS_XENU
+ * }
+ */
+
+static bool file_regex(const char *file, const char *regex)
+{
+ FILE *fp;
+ char *line = NULL;
+ size_t len = 0;
+ regex_t re;
+ bool retval = true;
+ int result;
+
+ if (!(fp = fopen(file, "r")))
+ return false;
+
+ if ((result = regcomp(&re, regex, REG_EXTENDED | REG_NOSUB)) != 0) {
+ fclose(fp);
+ line = xmalloc(sizeof(char) * BUFSIZ);
+ regerror(result, &re, line, BUFSIZ);
+ fprintf(stderr, "file_regex: %s", line);
+ free(line);
+ return false;
+ }
+
+ while ((rc_getline(&line, &len, fp))) {
+ char *str = line;
+ /* some /proc files have \0 separated content so we have to
+ loop through the 'line' */
+ do {
+ if (regexec(&re, str, 0, NULL, 0) == 0)
+ goto found;
+ str += strlen(str) + 1;
+ /* len is the size of allocated buffer and we don't
+ want call regexec BUFSIZE times. find next str */
+ while (str < line + len && *str == '\0')
+ str++;
+ } while (str < line + len);
+ }
+ retval = false;
+found:
+ fclose(fp);
+ free(line);
+ regfree(&re);
+
+ return retval;
+}
+
+char *get_systype(void)
+{
+ char *systype = rc_conf_value("rc_sys");
+ char *s;
+
+ if (systype) {
+ if (*systype == '\0') {
+ free(systype);
+ return NULL;
+ }
+ s = systype;
+ /* Convert to uppercase */
+ while (s && *s) {
+ if (islower((unsigned char) *s))
+ *s = toupper((unsigned char) *s);
+ s++;
+ }
+ if (strcmp(systype,RC_SYS_PREFIX) == 0)
+ return systype;
+
+#ifdef __FreeBSD__
+ if (strcmp(systype, RC_SYS_JAIL) == 0)
+ return systype;
+#endif
+
+#ifdef __linux__
+ if (strcmp(systype, RC_SYS_UML) == 0)
+ return systype;
+ else if (strcmp(systype, RC_SYS_VSERVER) == 0)
+ return systype;
+ else if (strcmp(systype, RC_SYS_OPENVZ) == 0)
+ return systype;
+ else if (strcmp(systype, RC_SYS_LXC) == 0)
+ return systype;
+ else if (strcmp(systype, RC_SYS_RKT) == 0)
+ return systype;
+ else if (strcmp(systype, RC_SYS_SYSTEMD_NSPAWN) == 0)
+ return systype;
+ else if (strcmp(systype, RC_SYS_DOCKER) == 0)
+ return systype;
+ else if (strcmp(systype, RC_SYS_XEN0) == 0)
+ return systype;
+ else if (strcmp(systype, RC_SYS_XENU) == 0)
+ return systype;
+#endif
+
+#ifdef __NetBSD__
+ if(strcmp(systype, RC_SYS_XEN0) == 0)
+ return systype;
+ else if (strcmp(systype, RC_SYS_XENU) == 0)
+ return systype;
+#endif
+ }
+
+ free(systype);
+ systype = NULL;
+
+#ifdef PREFIX
+ systype = xstrdup(RC_SYS_PREFIX);
+ return systype;
+#endif
+
+#ifdef __FreeBSD__
+ int jailed = 0;
+ size_t len = sizeof(jailed);
+
+ if (sysctlbyname("security.jail.jailed", &jailed, &len, NULL, 0) == 0)
+ if (jailed == 1)
+ systype = xstrdup(RC_SYS_JAIL);
+#endif
+
+#ifdef __linux__
+ if (file_regex("/proc/cpuinfo", "UML"))
+ systype = xstrdup(RC_SYS_UML);
+ else if (file_regex("/proc/self/status",
+ "(s_context|VxID):[[:space:]]*[1-9]"))
+ systype = xstrdup(RC_SYS_VSERVER);
+ else if (exists("/proc/vz/veinfo") && !exists("/proc/vz/version"))
+ systype = xstrdup(RC_SYS_OPENVZ);
+ else if (file_regex("/proc/self/status",
+ "envID:[[:space:]]*[1-9]"))
+ systype = xstrdup(RC_SYS_OPENVZ); /* old test */
+ else if (file_regex("/proc/1/environ", "container=lxc"))
+ systype = xstrdup(RC_SYS_LXC);
+ else if (file_regex("/proc/1/environ", "container=rkt"))
+ systype = xstrdup(RC_SYS_RKT);
+ else if (file_regex("/proc/1/environ", "container=systemd-nspawn"))
+ systype = xstrdup(RC_SYS_SYSTEMD_NSPAWN);
+ else if (file_regex("/proc/1/environ", "container=docker"))
+ systype = xstrdup(RC_SYS_DOCKER);
+ else if (exists("/proc/xen")) {
+ if (file_regex("/proc/xen/capabilities", "control_d"))
+ systype = xstrdup(RC_SYS_XEN0);
+ systype = xstrdup(RC_SYS_XENU);
+ }
+#endif
+
+#ifdef __NetBSD__
+ if (exists("/kern/xen/privcmd"))
+ systype = xstrdup(RC_SYS_XEN0);
+ else if (exists("/kern/xen"))
+ systype = xstrdup(RC_SYS_XENU);
+#endif
+
+ return systype;
+}
+
static const char *const env_whitelist[] = {
"EERROR_QUIET", "EINFO_QUIET",
"IN_BACKGROUND", "IN_HOTPLUG",
@@ -126,7 +292,7 @@ env_config(void)
char *np;
char *npp;
char *tok;
- const char *sys = NULL;
+ char *sys = get_systype();
char buffer[PATH_MAX];
/* Ensure our PATH is prefixed with the system locations first
@@ -177,12 +343,10 @@ env_config(void)
} else
setenv("RC_DEFAULTLEVEL", RC_LEVEL_DEFAULT, 1);
- sys = detect_container();
- if (!sys)
- sys = detect_vm();
-
- if (sys)
+ if (sys) {
setenv("RC_SYS", sys, 1);
+ free(sys);
+ }
#ifdef PREFIX
setenv("RC_PREFIX", RC_PREFIX, 1);
@@ -338,163 +502,6 @@ is_writable(const char *path)
return 0;
}
-static bool file_regex(const char *file, const char *regex)
-{
- FILE *fp;
- char *line = NULL;
- size_t len = 0;
- regex_t re;
- bool retval = true;
- int result;
-
- if (!(fp = fopen(file, "r")))
- return false;
-
- if ((result = regcomp(&re, regex, REG_EXTENDED | REG_NOSUB)) != 0) {
- fclose(fp);
- line = xmalloc(sizeof(char) * BUFSIZ);
- regerror(result, &re, line, BUFSIZ);
- fprintf(stderr, "file_regex: %s", line);
- free(line);
- return false;
- }
-
- while ((rc_getline(&line, &len, fp))) {
- char *str = line;
- /* some /proc files have \0 separated content so we have to
- loop through the 'line' */
- do {
- if (regexec(&re, str, 0, NULL, 0) == 0)
- goto found;
- str += strlen(str) + 1;
- /* len is the size of allocated buffer and we don't
- want call regexec BUFSIZE times. find next str */
- while (str < line + len && *str == '\0')
- str++;
- } while (str < line + len);
- }
- retval = false;
-found:
- fclose(fp);
- free(line);
- regfree(&re);
-
- return retval;
-}
-
-const char *detect_prefix(void)
-{
-#ifdef PREFIX
- return RC_SYS_PREFIX;
-#else
- return NULL;
-#endif
-}
-
-const char *get_systype(void)
-{
- char *systype = rc_conf_value("rc_sys");
- if (systype) {
- char *s = systype;
- /* Convert to uppercase */
- while (s && *s) {
- if (islower((unsigned char) *s))
- *s = toupper((unsigned char) *s);
- s++;
- }
- }
- return systype;
-}
-
-const char *detect_container(void)
-{
- const char *systype = get_systype();
-
-#ifdef __FreeBSD__
- if (systype && strcmp(systype, RC_SYS_JAIL) == 0)
- return RC_SYS_JAIL;
- int jailed = 0;
- size_t len = sizeof(jailed);
-
- if (sysctlbyname("security.jail.jailed", &jailed, &len, NULL, 0) == 0)
- if (jailed == 1)
- return RC_SYS_JAIL;
-#endif
-
-#ifdef __linux__
- if (systype) {
- if (strcmp(systype, RC_SYS_UML) == 0)
- return RC_SYS_UML;
- if (strcmp(systype, RC_SYS_VSERVER) == 0)
- return RC_SYS_VSERVER;
- if (strcmp(systype, RC_SYS_OPENVZ) == 0)
- return RC_SYS_OPENVZ;
- if (strcmp(systype, RC_SYS_LXC) == 0)
- return RC_SYS_LXC;
- if (strcmp(systype, RC_SYS_RKT) == 0)
- return RC_SYS_RKT;
- if (strcmp(systype, RC_SYS_SYSTEMD_NSPAWN) == 0)
- return RC_SYS_SYSTEMD_NSPAWN;
- if (strcmp(systype, RC_SYS_DOCKER) == 0)
- return RC_SYS_DOCKER;
- }
- if (file_regex("/proc/cpuinfo", "UML"))
- return RC_SYS_UML;
- else if (file_regex("/proc/self/status",
- "(s_context|VxID):[[:space:]]*[1-9]"))
- return RC_SYS_VSERVER;
- else if (exists("/proc/vz/veinfo") && !exists("/proc/vz/version"))
- return RC_SYS_OPENVZ;
- else if (file_regex("/proc/self/status",
- "envID:[[:space:]]*[1-9]"))
- return RC_SYS_OPENVZ; /* old test */
- else if (file_regex("/proc/1/environ", "container=lxc"))
- return RC_SYS_LXC;
- else if (file_regex("/proc/1/environ", "container=rkt"))
- return RC_SYS_RKT;
- else if (file_regex("/proc/1/environ", "container=systemd-nspawn"))
- return RC_SYS_SYSTEMD_NSPAWN;
- else if (file_regex("/proc/1/environ", "container=docker"))
- return RC_SYS_DOCKER;
-#endif
-
- return NULL;
-}
-
-const char *detect_vm(void)
-{
- const char *systype = get_systype();
-
-#ifdef __NetBSD__
- if (systype) {
- if(strcmp(systype, RC_SYS_XEN0) == 0)
- return RC_SYS_XEN0;
- if (strcmp(systype, RC_SYS_XENU) == 0)
- return RC_SYS_XENU;
- }
- if (exists("/kern/xen/privcmd"))
- return RC_SYS_XEN0;
- if (exists("/kern/xen"))
- return RC_SYS_XENU;
-#endif
-
-#ifdef __linux__
- if (systype) {
- if (strcmp(systype, RC_SYS_XEN0) == 0)
- return RC_SYS_XEN0;
- if (strcmp(systype, RC_SYS_XENU) == 0)
- return RC_SYS_XENU;
- }
- if (exists("/proc/xen")) {
- if (file_regex("/proc/xen/capabilities", "control_d"))
- return RC_SYS_XEN0;
- return RC_SYS_XENU;
- }
-#endif
-
- return NULL;
-}
-
RC_DEPTREE * _rc_deptree_load(int force, int *regen)
{
int fd;
diff --git a/src/rc/rc.c b/src/rc/rc.c
index 87c4913..0a970b4 100644
--- a/src/rc/rc.c
+++ b/src/rc/rc.c
@@ -281,12 +281,8 @@ open_shell(void)
struct passwd *pw;
#ifdef __linux__
- const char *sys = NULL;
+ char *sys = get_systype();
- sys = detect_container();
- if (!sys)
- sys = detect_vm();
-
/* VSERVER and OPENVZ systems cannot really drop to shells */
if (sys &&
(strcmp(sys, "VSERVER") == 0 || strcmp(sys, "OPENVZ") == 0))
@@ -470,7 +466,7 @@ static void
do_sysinit()
{
struct utsname uts;
- const char *sys;
+ char *sys = get_systype();
/* exec init-early.sh if it exists
* This should just setup the console to use the correct
@@ -491,9 +487,6 @@ do_sysinit()
uts.machine);
#endif
- sys = detect_container();
- if (!sys)
- sys = detect_vm();
if (sys)
printf(" [%s]", sys);
@@ -509,9 +502,7 @@ do_sysinit()
/* init may have mounted /proc so we can now detect or real
* sys */
- sys = detect_container();
- if (!sys)
- sys = detect_vm();
+ sys = get_systype();
if (sys)
setenv("RC_SYS", sys, 1);
}
@@ -832,9 +823,7 @@ int main(int argc, char **argv)
eerrorx("%s: %s", applet, strerror(errno));
/* NOTREACHED */
case 'S':
- systype = detect_container();
- if (!systype)
- systype = detect_vm();
+ systype = get_systype();
if (systype)
printf("%s\n", systype);
exit(EXIT_SUCCESS);
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [gentoo-commits] proj/openrc:master commit in: src/rc/, src/includes/
@ 2017-05-12 2:42 William Hubbs
0 siblings, 0 replies; 7+ messages in thread
From: William Hubbs @ 2017-05-12 2:42 UTC (permalink / raw
To: gentoo-commits
commit: 6b4050ab9cf9d678a1d6b7af7af7494f8533dbca
Author: William Hubbs <w.d.hubbs <AT> gmail <DOT> com>
AuthorDate: Thu May 11 21:18:41 2017 +0000
Commit: William Hubbs <williamh <AT> gentoo <DOT> org>
CommitDate: Thu May 11 21:22:12 2017 +0000
URL: https://gitweb.gentoo.org/proj/openrc.git/commit/?id=6b4050ab
fix from_time_t function
src/includes/rc-misc.h | 2 +-
src/rc/rc-misc.c | 5 +----
src/rc/supervise-daemon.c | 4 +++-
3 files changed, 5 insertions(+), 6 deletions(-)
diff --git a/src/includes/rc-misc.h b/src/includes/rc-misc.h
index 384328e7..95ccbc33 100644
--- a/src/includes/rc-misc.h
+++ b/src/includes/rc-misc.h
@@ -69,7 +69,7 @@ RC_DEPTREE *_rc_deptree_load (int, int *);
bool _rc_can_find_pids(void);
RC_SERVICE lookup_service_state(const char *service);
-char *from_time_t(time_t tv);
+void from_time_t(char *time_string, time_t tv);
time_t to_time_t(char *timestring);
#endif
diff --git a/src/rc/rc-misc.c b/src/rc/rc-misc.c
index b75c3b4e..1a98537c 100644
--- a/src/rc/rc-misc.c
+++ b/src/rc/rc-misc.c
@@ -443,12 +443,9 @@ RC_SERVICE lookup_service_state(const char *service)
return 0;
}
-char *from_time_t(time_t tv)
+void from_time_t(char *time_string, time_t tv)
{
- char time_string[20];
-
strftime(time_string, 20, "%Y-%m-%d %H:%M:%S", localtime(&tv));
- return time_string;
}
time_t to_time_t(char *timestring)
diff --git a/src/rc/supervise-daemon.c b/src/rc/supervise-daemon.c
index a971e6a3..f3335fa2 100644
--- a/src/rc/supervise-daemon.c
+++ b/src/rc/supervise-daemon.c
@@ -186,6 +186,7 @@ static void child_process(char *exec, char **argv, char *svcname,
char cmdline[PATH_MAX];
time_t start_time;
char start_count_string[20];
+ char start_time_string[20];
#ifdef HAVE_PAM
pam_handle_t *pamh = NULL;
@@ -343,7 +344,8 @@ static void child_process(char *exec, char **argv, char *svcname,
syslog(LOG_INFO, "Running command line: %s", cmdline);
if (svcname) {
start_time = time(NULL);
- rc_service_value_set(svcname, "start_time", from_time_t(start_time));
+from_time_t(start_time_string, start_time);
+ rc_service_value_set(svcname, "start_time", start_time_string);
sprintf(start_count_string, "%i", start_count);
rc_service_value_set(svcname, "start_count", start_count_string);
}
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [gentoo-commits] proj/openrc:master commit in: src/rc/, src/includes/
@ 2017-05-12 2:42 William Hubbs
0 siblings, 0 replies; 7+ messages in thread
From: William Hubbs @ 2017-05-12 2:42 UTC (permalink / raw
To: gentoo-commits
commit: cf5e9aa2bbcdf1783fadeab26586c1134929d928
Author: William Hubbs <w.d.hubbs <AT> gmail <DOT> com>
AuthorDate: Thu May 11 21:06:12 2017 +0000
Commit: William Hubbs <williamh <AT> gentoo <DOT> org>
CommitDate: Thu May 11 21:06:12 2017 +0000
URL: https://gitweb.gentoo.org/proj/openrc.git/commit/?id=cf5e9aa2
Move time_t conversions to rc-misc.c so they can be shared
src/includes/rc-misc.h | 3 +++
src/rc/rc-misc.c | 34 ++++++++++++++++++++++++++++++++++
src/rc/supervise-daemon.c | 4 +---
3 files changed, 38 insertions(+), 3 deletions(-)
diff --git a/src/includes/rc-misc.h b/src/includes/rc-misc.h
index 684ace59..384328e7 100644
--- a/src/includes/rc-misc.h
+++ b/src/includes/rc-misc.h
@@ -23,6 +23,7 @@
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
+#include <time.h>
#include <unistd.h>
#include "helpers.h"
@@ -68,5 +69,7 @@ RC_DEPTREE *_rc_deptree_load (int, int *);
bool _rc_can_find_pids(void);
RC_SERVICE lookup_service_state(const char *service);
+char *from_time_t(time_t tv);
+time_t to_time_t(char *timestring);
#endif
diff --git a/src/rc/rc-misc.c b/src/rc/rc-misc.c
index 8afff0a2..b75c3b4e 100644
--- a/src/rc/rc-misc.c
+++ b/src/rc/rc-misc.c
@@ -442,3 +442,37 @@ RC_SERVICE lookup_service_state(const char *service)
return service_bits[i].bit;
return 0;
}
+
+char *from_time_t(time_t tv)
+{
+ char time_string[20];
+
+ strftime(time_string, 20, "%Y-%m-%d %H:%M:%S", localtime(&tv));
+ return time_string;
+}
+
+time_t to_time_t(char *timestring)
+{
+ int check = 0;
+ int year = 0;
+ int month = 0;
+ int day = 0;
+ int hour = 0;
+ int min = 0;
+ int sec = 0;
+ struct tm breakdown = {0};
+ time_t result = -1;
+
+ check = sscanf(timestring, "%4d-%2d-%2d %2d:%2d:%2d",
+ &year, &month, &day, &hour, &min, &sec);
+ if (check == 6) {
+ breakdown.tm_year = year - 1900; /* years since 1900 */
+ breakdown.tm_mon = month - 1;
+ breakdown.tm_mday = day;
+ breakdown.tm_hour = hour;
+ breakdown.tm_min = min;
+ breakdown.tm_sec = sec;
+ result = mktime(&breakdown);
+ }
+ return result;
+}
diff --git a/src/rc/supervise-daemon.c b/src/rc/supervise-daemon.c
index bc5fd3d0..a971e6a3 100644
--- a/src/rc/supervise-daemon.c
+++ b/src/rc/supervise-daemon.c
@@ -185,7 +185,6 @@ static void child_process(char *exec, char **argv, char *svcname,
char **c;
char cmdline[PATH_MAX];
time_t start_time;
- char start_time_string[20];
char start_count_string[20];
#ifdef HAVE_PAM
@@ -344,8 +343,7 @@ static void child_process(char *exec, char **argv, char *svcname,
syslog(LOG_INFO, "Running command line: %s", cmdline);
if (svcname) {
start_time = time(NULL);
-strftime(start_time_string, 20, "%Y-%m-%d %H:%M:%S", localtime(&start_time));
- rc_service_value_set(svcname, "start_time", start_time_string);
+ rc_service_value_set(svcname, "start_time", from_time_t(start_time));
sprintf(start_count_string, "%i", start_count);
rc_service_value_set(svcname, "start_count", start_count_string);
}
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [gentoo-commits] proj/openrc:master commit in: src/rc/, src/includes/
@ 2017-06-05 17:18 William Hubbs
0 siblings, 0 replies; 7+ messages in thread
From: William Hubbs @ 2017-06-05 17:18 UTC (permalink / raw
To: gentoo-commits
commit: 7689106aa10f7852b707b4c21ec080ccb2767280
Author: William Hubbs <w.d.hubbs <AT> gmail <DOT> com>
AuthorDate: Fri Jun 2 19:07:40 2017 +0000
Commit: William Hubbs <williamh <AT> gentoo <DOT> org>
CommitDate: Mon Jun 5 01:56:03 2017 +0000
URL: https://gitweb.gentoo.org/proj/openrc.git/commit/?id=7689106a
add support for writing reboot and shutdown records to wtmp
src/includes/rc-wtmp.h | 26 +++++++++++++++++++++
src/rc/Makefile | 6 ++---
src/rc/openrc-init.c | 2 ++
src/rc/openrc-shutdown.c | 61 +++++++++++++++++++++++++++++++-----------------
src/rc/rc-wtmp.c | 50 +++++++++++++++++++++++++++++++++++++++
5 files changed, 121 insertions(+), 24 deletions(-)
diff --git a/src/includes/rc-wtmp.h b/src/includes/rc-wtmp.h
new file mode 100644
index 00000000..6645774b
--- /dev/null
+++ b/src/includes/rc-wtmp.h
@@ -0,0 +1,26 @@
+/*
+ * rc-wtmp.h
+ * This is private to us and not for user consumption
+*/
+
+/*
+ * Copyright (c) 2017 The OpenRC Authors.
+ * See the Authors file at the top-level directory of this distribution and
+ * https://github.com/OpenRC/openrc/blob/master/AUTHORS
+ *
+ * This file is part of OpenRC. It is subject to the license terms in
+ * the LICENSE file found in the top-level directory of this
+ * distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE
+ * This file may not be copied, modified, propagated, or distributed
+ * except according to the terms contained in the LICENSE file.
+ */
+
+#ifndef __RC_WTMP_H__
+#define __RC_WTMP_H__
+
+#include <utmp.h>
+
+void log_wtmp(const char *user, const char *id, pid_t pid, int type,
+ const char *line);
+
+#endif
diff --git a/src/rc/Makefile b/src/rc/Makefile
index 5874ed17..19adcafb 100644
--- a/src/rc/Makefile
+++ b/src/rc/Makefile
@@ -14,7 +14,7 @@ SRCS+= rc-selinux.c
endif
ifeq (${OS},Linux)
-SRCS+= kill_all.c openrc-init.c openrc-shutdown.c
+SRCS+= kill_all.c openrc-init.c openrc-shutdown.c rc-wtmp.c
endif
CLEANFILES= version.h rc-selinux.o
@@ -111,7 +111,7 @@ veinfo vewarn vebegin veend vewend veindent veoutdent: do_e.o rc-misc.o
fstabinfo: fstabinfo.o _usage.o rc-misc.o
${CC} ${LOCAL_CFLAGS} ${LOCAL_LDFLAGS} ${CFLAGS} ${LDFLAGS} -o $@ $^ ${LDADD}
-openrc-init: openrc-init.o
+openrc-init: openrc-init.o rc-wtmp.o
${CC} ${LOCAL_CFLAGS} ${LOCAL_LDFLAGS} ${CFLAGS} ${LDFLAGS} -o $@ $^ ${LDADD}
is_newer_than: is_newer_than.o rc-misc.o
@@ -132,7 +132,7 @@ mountinfo: mountinfo.o _usage.o rc-misc.o
openrc rc: rc.o rc-logger.o rc-misc.o rc-plugin.o _usage.o
${CC} ${LOCAL_CFLAGS} ${LOCAL_LDFLAGS} ${CFLAGS} ${LDFLAGS} -o $@ $^ ${LDADD}
-openrc-shutdown: openrc-shutdown.o _usage.o
+openrc-shutdown: openrc-shutdown.o _usage.o rc-wtmp.o
${CC} ${LOCAL_CFLAGS} ${LOCAL_LDFLAGS} ${CFLAGS} ${LDFLAGS} -o $@ $^ ${LDADD}
openrc-run runscript: openrc-run.o _usage.o rc-misc.o rc-plugin.o
diff --git a/src/rc/openrc-init.c b/src/rc/openrc-init.c
index 003ce31f..eb346f59 100644
--- a/src/rc/openrc-init.c
+++ b/src/rc/openrc-init.c
@@ -32,6 +32,7 @@
#include "helpers.h"
#include "rc.h"
+#include "rc-wtmp.h"
#include "version.h"
static const char *rc_default_runlevel = "default";
@@ -82,6 +83,7 @@ static void init(const char *default_runlevel)
}
pid = do_openrc(runlevel);
waitpid(pid, NULL, 0);
+ log_wtmp("reboot", "~~", 0, RUN_LVL, "~~");
}
static void handle_reexec(char *my_name)
diff --git a/src/rc/openrc-shutdown.c b/src/rc/openrc-shutdown.c
index ecb251a8..4ba619c5 100644
--- a/src/rc/openrc-shutdown.c
+++ b/src/rc/openrc-shutdown.c
@@ -27,46 +27,63 @@
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
+#include <sys/utsname.h>
#include "einfo.h"
#include "rc.h"
#include "helpers.h"
#include "_usage.h"
+#include "rc-wtmp.h"
const char *applet = NULL;
const char *extraopts = NULL;
-const char *getoptstring = "dHkpRr" getoptstring_COMMON;
+const char *getoptstring = "dDHKpRrw" getoptstring_COMMON;
const struct option longopts[] = {
- { "dry-run", no_argument, NULL, 'd'},
+ { "no-write", no_argument, NULL, 'd'},
+ { "dry-run", no_argument, NULL, 'D'},
{ "halt", no_argument, NULL, 'H'},
- { "kexec", no_argument, NULL, 'k'},
+ { "kexec", no_argument, NULL, 'K'},
{ "poweroff", no_argument, NULL, 'p'},
{ "reexec", no_argument, NULL, 'R'},
{ "reboot", no_argument, NULL, 'r'},
+ { "write-only", no_argument, NULL, 'w'},
longopts_COMMON
};
const char * const longopts_help[] = {
+ "do not write wtmp record",
"print actions instead of executing them",
"halt the system",
"reboot the system using kexec",
"power off the system",
"re-execute init (use after upgrading)",
"reboot the system",
+ "write wtmp boot record and exit",
longopts_help_COMMON
};
const char *usagestring = NULL;
const char *exclusive = "Select one of "
"--halt, --kexec, --poweroff, --reexec or --reboot";
-static void send_cmd(const char *cmd, bool dryrun)
+static bool do_dryrun = false;
+static bool do_halt = false;
+static bool do_kexec = false;
+static bool do_poweroff = false;
+static bool do_reboot = false;
+static bool do_reexec = false;
+static bool do_wtmp = true;
+static bool do_wtmp_only = false;
+
+static void send_cmd(const char *cmd)
{
FILE *fifo;
size_t ignored;
- if (dryrun) {
+ if (do_dryrun) {
einfo("Would send %s to init", cmd);
return;
}
+ if (do_wtmp && (do_halt || do_kexec || do_reboot || do_poweroff))
+ log_wtmp("shutdown", "~~", 0, RUN_LVL, "~~");
fifo = fopen(RC_INIT_FIFO, "w");
if (!fifo) {
perror("fopen");
@@ -83,26 +100,23 @@ int main(int argc, char **argv)
{
int opt;
int cmd_count = 0;
- bool do_dryrun = false;
- bool do_halt = false;
- bool do_kexec = false;
- bool do_poweroff = false;
- bool do_reboot = false;
- bool do_reexec = false;
applet = basename_c(argv[0]);
while ((opt = getopt_long(argc, argv, getoptstring,
longopts, (int *) 0)) != -1)
{
switch (opt) {
- case 'd':
+ case 'd':
+ do_wtmp = false;
+ break;
+ case 'D':
do_dryrun = true;
break;
case 'H':
do_halt = true;
cmd_count++;
break;
- case 'k':
+ case 'K':
do_kexec = true;
cmd_count++;
break;
@@ -118,26 +132,31 @@ int main(int argc, char **argv)
do_reboot = true;
cmd_count++;
break;
+ case 'w':
+ do_wtmp_only = true;
+ break;
case_RC_COMMON_GETOPT
}
}
-if (geteuid() != 0 && ! do_dryrun)
- eerrorx("%s: you must be root\n", applet);
+ if (geteuid() != 0 && ! do_dryrun)
+ eerrorx("%s: you must be root\n", applet);
if (cmd_count > 1) {
eerror("%s: %s\n", applet, exclusive);
usage(EXIT_FAILURE);
}
if (do_halt)
- send_cmd("halt", do_dryrun);
+ send_cmd("halt");
else if (do_kexec)
- send_cmd("kexec", do_dryrun);
+ send_cmd("kexec");
else if (do_poweroff)
- send_cmd("poweroff", do_dryrun);
+ send_cmd("poweroff");
else if (do_reboot)
- send_cmd("reboot", do_dryrun);
+ send_cmd("reboot");
else if (do_reexec)
- send_cmd("reexec", do_dryrun);
+ send_cmd("reexec");
+ else if (do_wtmp_only)
+ log_wtmp("shutdown", "~~", 0, RUN_LVL, "~~");
else
- send_cmd("single", do_dryrun);
+ send_cmd("single");
return 0;
}
diff --git a/src/rc/rc-wtmp.c b/src/rc/rc-wtmp.c
new file mode 100644
index 00000000..913fa06b
--- /dev/null
+++ b/src/rc/rc-wtmp.c
@@ -0,0 +1,50 @@
+/*
+ * rc-wtmp.c
+ * This file contains routines to deal with the wtmp file.
+ */
+
+/*
+ * Copyright 2017 The OpenRC Authors.
+ * See the Authors file at the top-level directory of this distribution and
+ * https://github.com/OpenRC/openrc/blob/master/AUTHORS
+ *
+ * This file is part of OpenRC. It is subject to the license terms in
+ * the LICENSE file found in the top-level directory of this
+ * distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE
+ * This file may not be copied, modified, propagated, or distributed
+ * except according to the terms contained in the LICENSE file.
+ */
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/utsname.h>
+
+#include "rc-wtmp.h"
+
+void log_wtmp(const char *user, const char *id, pid_t pid, int type,
+ const char *line)
+{
+ struct timeval tv;
+ struct utmp utmp;
+ struct utsname uname_buf;
+
+ memset(&utmp, 0, sizeof(utmp));
+ gettimeofday(&tv, NULL);
+ utmp.ut_tv.tv_sec = tv.tv_sec;
+ utmp.ut_tv.tv_usec = tv.tv_usec;
+ utmp.ut_pid = pid;
+ utmp.ut_type = type;
+ strncpy(utmp.ut_name, user, sizeof(utmp.ut_name));
+ strncpy(utmp.ut_id , id , sizeof(utmp.ut_id ));
+ strncpy(utmp.ut_line, line, sizeof(utmp.ut_line));
+
+ /* Put the OS version in place of the hostname */
+ if (uname(&uname_buf) == 0)
+ strncpy(utmp.ut_host, uname_buf.release, sizeof(utmp.ut_host));
+
+ updwtmp(WTMP_FILE, &utmp);
+}
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [gentoo-commits] proj/openrc:master commit in: src/rc/, src/includes/
@ 2017-10-24 15:33 William Hubbs
0 siblings, 0 replies; 7+ messages in thread
From: William Hubbs @ 2017-10-24 15:33 UTC (permalink / raw
To: gentoo-commits
commit: fdce4769f2e0f4175163ffa181c7b3b2192f7b22
Author: William Hubbs <w.d.hubbs <AT> gmail <DOT> com>
AuthorDate: Tue Oct 24 15:26:18 2017 +0000
Commit: William Hubbs <williamh <AT> gentoo <DOT> org>
CommitDate: Tue Oct 24 15:26:18 2017 +0000
URL: https://gitweb.gentoo.org/proj/openrc.git/commit/?id=fdce4769
supervise-daemon: multiple fixes
- Harden against dying by handling all signals that would terminate the
program and adding --reexec support
- factor the supervisor into its own function
- fix test for whether we are already running
src/includes/rc-misc.h | 1 +
src/rc/rc-misc.c | 12 +
src/rc/supervise-daemon.c | 570 +++++++++++++++++++++++++++-------------------
3 files changed, 343 insertions(+), 240 deletions(-)
diff --git a/src/includes/rc-misc.h b/src/includes/rc-misc.h
index 9a55c413..e6789911 100644
--- a/src/includes/rc-misc.h
+++ b/src/includes/rc-misc.h
@@ -47,6 +47,7 @@ bool rc_conf_yesno(const char *var);
void env_filter(void);
void env_config(void);
int signal_setup(int sig, void (*handler)(int));
+int signal_setup_restart(int sig, void (*handler)(int));
int svc_lock(const char *);
int svc_unlock(const char *, int);
pid_t exec_service(const char *, const char *);
diff --git a/src/rc/rc-misc.c b/src/rc/rc-misc.c
index d43f1274..33a17b35 100644
--- a/src/rc/rc-misc.c
+++ b/src/rc/rc-misc.c
@@ -218,6 +218,18 @@ signal_setup(int sig, void (*handler)(int))
}
int
+signal_setup_restart(int sig, void (*handler)(int))
+{
+ struct sigaction sa;
+
+ memset(&sa, 0, sizeof (sa));
+ sigemptyset(&sa.sa_mask);
+ sa.sa_handler = handler;
+ sa.sa_flags = SA_RESTART;
+ return sigaction(sig, &sa, NULL);
+}
+
+int
svc_lock(const char *applet)
{
char file[PATH_MAX];
diff --git a/src/rc/supervise-daemon.c b/src/rc/supervise-daemon.c
index f1e8ea16..df59eb67 100644
--- a/src/rc/supervise-daemon.c
+++ b/src/rc/supervise-daemon.c
@@ -67,7 +67,7 @@ static struct pam_conv conv = { NULL, NULL};
const char *applet = NULL;
const char *extraopts = NULL;
-const char *getoptstring = "D:d:e:g:I:Kk:m:N:p:R:r:Su:1:2:" \
+const char *getoptstring = "D:d:e:g:I:Kk:m:N:p:R:r:Su:1:2:3" \
getoptstring_COMMON;
const struct option longopts[] = {
{ "respawn-delay", 1, NULL, 'D'},
@@ -87,6 +87,7 @@ const struct option longopts[] = {
{ "user", 1, NULL, 'u'},
{ "stdout", 1, NULL, '1'},
{ "stderr", 1, NULL, '2'},
+ { "reexec", 0, NULL, '3'},
longopts_COMMON
};
const char * const longopts_help[] = {
@@ -107,6 +108,7 @@ const char * const longopts_help[] = {
"Change the process user",
"Redirect stdout to file",
"Redirect stderr to file",
+ "reexec (used internally)",
longopts_help_COMMON
};
const char *usagestring = NULL;
@@ -127,6 +129,13 @@ static bool exiting = false;
#ifdef TIOCNOTTY
static int tty_fd = -1;
#endif
+static pid_t child_pid;
+static int respawn_count = 0;
+static int respawn_delay = 0;
+static int respawn_max = 10;
+static int respawn_period = 5;
+static char *pidfile = NULL;
+static char *svcname = NULL;
extern char **environ;
@@ -150,8 +159,71 @@ static void cleanup(void)
free(changeuser);
}
-static void child_process(char *exec, char **argv, char *svcname,
- int start_count)
+static void re_exec(void)
+{
+ syslog(LOG_WARNING, "Re-executing supervise-daemon");
+ execlp("supervise-daemon", "supervise-daemon", "--reexec", (char *) NULL);
+ syslog(LOG_ERR, "Unable to execute supervise-daemon: %s",
+ strerror(errno));
+ exit(EXIT_FAILURE);
+}
+
+static void handle_signal(int sig)
+{
+ int serrno = errno;
+
+ syslog(LOG_WARNING, "caught signal %d", sig);
+
+ if (sig == SIGTERM)
+ exiting = true;
+ /* Restore errno */
+ errno = serrno;
+ if (! exiting)
+ re_exec();
+}
+
+static char * expand_home(const char *home, const char *path)
+{
+ char *opath, *ppath, *p, *nh;
+ size_t len;
+ struct passwd *pw;
+
+ if (!path || *path != '~')
+ return xstrdup(path);
+
+ opath = ppath = xstrdup(path);
+ if (ppath[1] != '/' && ppath[1] != '\0') {
+ p = strchr(ppath + 1, '/');
+ if (p)
+ *p = '\0';
+ pw = getpwnam(ppath + 1);
+ if (pw) {
+ home = pw->pw_dir;
+ ppath = p;
+ if (ppath)
+ *ppath = '/';
+ } else
+ home = NULL;
+ } else
+ ppath++;
+
+ if (!home) {
+ free(opath);
+ return xstrdup(path);
+ }
+ if (!ppath) {
+ free(opath);
+ return xstrdup(home);
+ }
+
+ len = strlen(ppath) + strlen(home) + 1;
+ nh = xmalloc(len);
+ snprintf(nh, len, "%s%s", home, ppath);
+ free(opath);
+ return nh;
+}
+
+static void child_process(char *exec, char **argv)
{
RC_STRINGLIST *env_list;
RC_STRING *env;
@@ -176,11 +248,13 @@ static void child_process(char *exec, char **argv, char *svcname,
setsid();
if (svcname) {
-start_time = time(NULL);
-from_time_t(start_time_string, start_time);
+ start_time = time(NULL);
+ from_time_t(start_time_string, start_time);
rc_service_value_set(svcname, "start_time", start_time_string);
-sprintf(start_count_string, "%i", start_count);
+ sprintf(start_count_string, "%i", respawn_count);
rc_service_value_set(svcname, "start_count", start_count_string);
+ sprintf(start_count_string, "%d", getpid());
+ rc_service_value_set(svcname, "child_pid", start_count_string);
}
if (nicelevel) {
@@ -323,7 +397,7 @@ sprintf(start_count_string, "%i", start_count);
*cmdline = '\0';
c = argv;
- while (*c) {
+ while (c && *c) {
strcat(cmdline, *c);
strcat(cmdline, " ");
c++;
@@ -338,108 +412,152 @@ sprintf(start_count_string, "%i", start_count);
eerrorx("%s: failed to exec `%s': %s", applet, exec,strerror(errno));
}
-static void handle_signal(int sig)
+static void supervisor(char *exec, char **argv)
{
- int serrno = errno;
- char signame[10] = { '\0' };
-
- switch (sig) {
- case SIGINT:
- snprintf(signame, sizeof(signame), "SIGINT");
- break;
- case SIGTERM:
- snprintf(signame, sizeof(signame), "SIGTERM");
- break;
- case SIGQUIT:
- snprintf(signame, sizeof(signame), "SIGQUIT");
- break;
- }
-
- if (*signame != 0) {
- syslog(LOG_INFO, "%s: caught signal %s, exiting", applet, signame);
- exiting = true;
- } else
- syslog(LOG_INFO, "%s: caught unknown signal %d", applet, sig);
+ FILE *fp;
+ int i;
+ int nkilled;
+ time_t respawn_now= 0;
+ time_t first_spawn= 0;
- /* Restore errno */
- errno = serrno;
-}
+ openlog(applet, LOG_PID, LOG_DAEMON);
+#ifndef RC_DEBUG
+ signal_setup_restart(SIGHUP, handle_signal);
+ signal_setup_restart(SIGINT, handle_signal);
+ signal_setup_restart(SIGQUIT, handle_signal);
+ signal_setup_restart(SIGILL, handle_signal);
+ signal_setup_restart(SIGABRT, handle_signal);
+ signal_setup_restart(SIGFPE, handle_signal);
+ signal_setup_restart(SIGSEGV, handle_signal);
+ signal_setup_restart(SIGPIPE, handle_signal);
+ signal_setup_restart(SIGALRM, handle_signal);
+ signal_setup(SIGTERM, handle_signal);
+ signal_setup_restart(SIGUSR1, handle_signal);
+ signal_setup_restart(SIGUSR2, handle_signal);
+ signal_setup_restart(SIGBUS, handle_signal);
+ signal_setup_restart(SIGPOLL, handle_signal);
+ signal_setup_restart(SIGPROF, handle_signal);
+ signal_setup_restart(SIGSYS, handle_signal);
+ signal_setup_restart(SIGTRAP, handle_signal);
+ signal_setup_restart(SIGVTALRM, handle_signal);
+ signal_setup_restart(SIGXCPU, handle_signal);
+ signal_setup_restart(SIGXFSZ, handle_signal);
+#ifdef SIGEMT
+ signal_setup_restart(SIGEMT, handle_signal);
+#endif
+ signal_setup_restart(SIGIO, handle_signal);
+ signal_setup_restart(SIGPWR, handle_signal);
+ signal_setup_restart(SIGUNUSED, handle_signal);
+#ifdef SIGRTMIN
+ for (i = SIGRTMIN; i <= SIGRTMAX; i++)
+ signal_setup_restart(i, handle_signal);
+#endif
+#endif
-static char * expand_home(const char *home, const char *path)
-{
- char *opath, *ppath, *p, *nh;
- size_t len;
- struct passwd *pw;
+ fp = fopen(pidfile, "w");
+ if (! fp)
+ eerrorx("%s: fopen `%s': %s", applet, pidfile, strerror(errno));
+ fprintf(fp, "%d\n", getpid());
+ fclose(fp);
- if (!path || *path != '~')
- return xstrdup(path);
+ if (svcname)
+ rc_service_daemon_set(svcname, exec, (const char * const *) argv,
+ pidfile, true);
- opath = ppath = xstrdup(path);
- if (ppath[1] != '/' && ppath[1] != '\0') {
- p = strchr(ppath + 1, '/');
- if (p)
- *p = '\0';
- pw = getpwnam(ppath + 1);
- if (pw) {
- home = pw->pw_dir;
- ppath = p;
- if (ppath)
- *ppath = '/';
- } else
- home = NULL;
- } else
- ppath++;
+ /* remove the controlling tty */
+#ifdef TIOCNOTTY
+ ioctl(tty_fd, TIOCNOTTY, 0);
+ close(tty_fd);
+#endif
- if (!home) {
- free(opath);
- return xstrdup(path);
- }
- if (!ppath) {
- free(opath);
- return xstrdup(home);
+ /*
+ * Supervisor main loop
+ */
+ i = 0;
+ while (!exiting) {
+ wait(&i);
+ if (exiting) {
+ signal_setup(SIGCHLD, SIG_IGN);
+ syslog(LOG_INFO, "stopping %s, pid %d", exec, child_pid);
+ nkilled = run_stop_schedule(applet, exec, NULL, child_pid, 0,
+ false, false, true);
+ if (nkilled > 0)
+ syslog(LOG_INFO, "killed %d processes", nkilled);
+ } else {
+ sleep(respawn_delay);
+ if (respawn_max > 0 && respawn_period > 0) {
+ respawn_now = time(NULL);
+ if (first_spawn == 0)
+ first_spawn = respawn_now;
+ if (respawn_now - first_spawn > respawn_period) {
+ respawn_count = 0;
+ first_spawn = 0;
+ } else
+ respawn_count++;
+ if (respawn_count >= respawn_max) {
+ syslog(LOG_WARNING,
+ "respawned \"%s\" too many times, exiting", exec);
+ exiting = true;
+ continue;
+ }
+ }
+ if (WIFEXITED(i))
+ syslog(LOG_WARNING, "%s, pid %d, exited with return code %d",
+ exec, child_pid, WEXITSTATUS(i));
+ else if (WIFSIGNALED(i))
+ syslog(LOG_WARNING, "%s, pid %d, terminated by signal %d",
+ exec, child_pid, WTERMSIG(i));
+ child_pid = fork();
+ if (child_pid == -1)
+ eerrorx("%s: fork: %s", applet, strerror(errno));
+ if (child_pid == 0)
+ child_process(exec, argv);
+ }
}
- len = strlen(ppath) + strlen(home) + 1;
- nh = xmalloc(len);
- snprintf(nh, len, "%s%s", home, ppath);
- free(opath);
- return nh;
+ if (pidfile && exists(pidfile))
+ unlink(pidfile);
+ if (svcname) {
+ rc_service_daemon_set(svcname, exec, (const char *const *)argv,
+ pidfile, false);
+ rc_service_mark(svcname, RC_SERVICE_STOPPED);
+ }
+ exit(EXIT_SUCCESS);
}
int main(int argc, char **argv)
{
int opt;
+ char **c;
+ int x;
bool start = false;
bool stop = false;
+ bool reexec = false;
char *exec = NULL;
- char *pidfile = NULL;
char *retry = NULL;
- int nkilled;
int sig = SIGTERM;
char *home = NULL;
int tid = 0;
- pid_t child_pid, pid;
- char *svcname = getenv("RC_SVCNAME");
+ pid_t pid;
char *tmp;
char *p;
char *token;
int i;
int n;
char exec_file[PATH_MAX];
- int respawn_count = 0;
- int respawn_delay = 0;
- int respawn_max = 10;
- int respawn_period = 5;
- time_t respawn_now= 0;
- time_t first_spawn= 0;
+ char name[PATH_MAX];
struct timespec ts;
struct passwd *pw;
struct group *gr;
FILE *fp;
mode_t numask = 022;
+ int child_argc = 0;
+ char **child_argv = NULL;
+ char *str = NULL;
applet = basename_c(argv[0]);
atexit(cleanup);
+ svcname = getenv("RC_SVCNAME");
if ((tmp = getenv("SSD_NICELEVEL")))
if (sscanf(tmp, "%d", &nicelevel) != 1)
@@ -493,8 +611,8 @@ int main(int argc, char **argv)
case 'P': /* --respawn-period time */
n = sscanf(optarg, "%d", &respawn_period);
- if (n != 1 || respawn_delay < 1)
- eerrorx("Invalid respawn-delay value '%s'", optarg);
+ if (n != 1 || respawn_period < 1)
+ eerrorx("Invalid respawn-period value '%s'", optarg);
break;
case 'S': /* --start */
@@ -590,11 +708,14 @@ int main(int argc, char **argv)
case '2': /* --stderr /path/to/stderr.logfile */
redirect_stderr = optarg;
break;
+ case '3': /* --reexec */
+ reexec = true;
+ break;
case_RC_COMMON_GETOPT
}
- if (!pidfile)
+ if (!pidfile && !reexec)
eerrorx("%s: --pidfile must be specified", applet);
endpwent();
@@ -602,195 +723,165 @@ int main(int argc, char **argv)
argv += optind;
exec = *argv;
- if (start) {
- if (!exec)
- eerrorx("%s: nothing to start", applet);
- if (respawn_delay * respawn_max > respawn_period) {
- ewarn("%s: Please increase the value of --respawn-period to more "
- "than %d to avoid infinite respawning", applet,
- respawn_delay * respawn_max);
- }
- if (retry)
- parse_schedule(applet, retry, sig);
- else
- parse_schedule(applet, NULL, sig);
- }
-
/* Expand ~ */
if (ch_dir && *ch_dir == '~')
ch_dir = expand_home(home, ch_dir);
if (ch_root && *ch_root == '~')
ch_root = expand_home(home, ch_root);
- if (exec) {
- if (*exec == '~')
- exec = expand_home(home, exec);
-
- /* Validate that the binary exists if we are starting */
- if (*exec == '/' || *exec == '.') {
- /* Full or relative path */
- if (ch_root)
- snprintf(exec_file, sizeof(exec_file),
- "%s/%s", ch_root, exec);
- else
- snprintf(exec_file, sizeof(exec_file),
- "%s", exec);
- } else {
- /* Something in $PATH */
- p = tmp = xstrdup(getenv("PATH"));
- *exec_file = '\0';
- while ((token = strsep(&p, ":"))) {
+
+ umask(numask);
+
+ if (reexec) {
+ str = rc_service_value_get(svcname, "argc");
+ sscanf(str, "%d", &child_argc);
+ child_argv = xmalloc((child_argc + 1) * sizeof(char *));
+ memset(child_argv, 0, (child_argc + 1) * sizeof(char *));
+ for (x = 0; x < child_argc; x++) {
+ sprintf(name, "argv_%d", x);
+ str = rc_service_value_get(svcname, name);
+ child_argv[x] = str;
+ }
+ free(str);
+ str = rc_service_value_get(svcname, "child_pid");
+ sscanf(str, "%d", &child_pid);
+ free(str);
+ exec = rc_service_value_get(svcname, "exec");
+ pidfile = rc_service_value_get(svcname, "pidfile");
+ retry = rc_service_value_get(svcname, "retry");
+ if (retry) {
+ parse_schedule(applet, retry, sig);
+ rc_service_value_set(svcname, "retry", retry);
+ } else
+ parse_schedule(applet, NULL, sig);
+
+ str = rc_service_value_get(svcname, "respawn_delay");
+ sscanf(str, "%d", &respawn_delay);
+ str = rc_service_value_get(svcname, "respawn_max");
+ sscanf(str, "%d", &respawn_max);
+ supervisor(exec, child_argv);
+ } else if (start) {
+ if (exec) {
+ if (*exec == '~')
+ exec = expand_home(home, exec);
+
+ /* Validate that the binary exists if we are starting */
+ if (*exec == '/' || *exec == '.') {
+ /* Full or relative path */
if (ch_root)
snprintf(exec_file, sizeof(exec_file),
- "%s/%s/%s",
- ch_root, token, exec);
+ "%s/%s", ch_root, exec);
else
snprintf(exec_file, sizeof(exec_file),
- "%s/%s", token, exec);
- if (exists(exec_file))
- break;
+ "%s", exec);
+ } else {
+ /* Something in $PATH */
+ p = tmp = xstrdup(getenv("PATH"));
*exec_file = '\0';
+ while ((token = strsep(&p, ":"))) {
+ if (ch_root)
+ snprintf(exec_file, sizeof(exec_file),
+ "%s/%s/%s",
+ ch_root, token, exec);
+ else
+ snprintf(exec_file, sizeof(exec_file),
+ "%s/%s", token, exec);
+ if (exists(exec_file))
+ break;
+ *exec_file = '\0';
+ }
+ free(tmp);
}
- free(tmp);
- }
- }
- if (start && !exists(exec_file))
- eerrorx("%s: %s does not exist", applet,
- *exec_file ? exec_file : exec);
+ if ( !exists(exec_file))
+ eerrorx("%s: %s does not exist", applet,
+ *exec_file ? exec_file : exec);
+ } else
+ eerrorx("%s: nothing to start", applet);
- if (stop) {
pid = get_pid(applet, pidfile);
- if (pid != -1) {
- i = kill(pid, SIGTERM);
- if (i != 0)
- /* We failed to send the signal */
- exit(EXIT_FAILURE);
-
- /* wait for the supervisor to go down */
- while (kill(pid, 0) == 0) {
- ts.tv_sec = 0;
- ts.tv_nsec = 1;
- nanosleep(&ts, NULL);
- }
- }
-
- /* Even if we have not actually killed anything, we should
- * remove information about it as it may have unexpectedly
- * crashed out. We should also return success as the end
- * result would be the same. */
- if (pidfile && exists(pidfile))
- unlink(pidfile);
- if (svcname) {
- rc_service_daemon_set(svcname, exec,
- (const char *const *)argv,
- pidfile, false);
- rc_service_mark(svcname, RC_SERVICE_STOPPED);
- }
- exit(EXIT_SUCCESS);
- }
-
- pid = get_pid(applet, pidfile);
- if (pid != -1)
- if (kill(pid, 0) == 0)
- eerrorx("%s: %s is already running", applet, exec);
-
- einfov("Detaching to start `%s'", exec);
- eindentv();
-
- /* Remove existing pidfile */
- if (pidfile)
- unlink(pidfile);
-
- /*
- * Make sure we can write a pid file
- */
- fp = fopen(pidfile, "w");
- if (! fp)
- eerrorx("%s: fopen `%s': %s", applet, pidfile, strerror(errno));
- fclose(fp);
+ if (pid != -1)
+ if (do_stop(applet, exec, (const char * const *)argv, pid, uid,
+ 0, false, true) > 0)
+ eerrorx("%s: %s is already running", applet, exec);
- child_pid = fork();
- if (child_pid == -1)
- eerrorx("%s: fork: %s", applet, strerror(errno));
+ if (respawn_delay * respawn_max > respawn_period)
+ ewarn("%s: Please increase the value of --respawn-period to more "
+ "than %d to avoid infinite respawning", applet,
+ respawn_delay * respawn_max);
- /* first parent process, do nothing. */
- if (child_pid != 0)
- exit(EXIT_SUCCESS);
+ if (retry) {
+ parse_schedule(applet, retry, sig);
+ rc_service_value_set(svcname, "retry", retry);
+ } else
+ parse_schedule(applet, NULL, sig);
-#ifdef TIOCNOTTY
- tty_fd = open("/dev/tty", O_RDWR);
-#endif
- devnull_fd = open("/dev/null", O_RDWR);
- child_pid = fork();
- if (child_pid == -1)
- eerrorx("%s: fork: %s", applet, strerror(errno));
+ einfov("Detaching to start `%s'", exec);
- if (child_pid != 0) {
- /* this is the supervisor */
- umask(numask);
- openlog(applet, LOG_PID, LOG_DAEMON);
- signal_setup(SIGTERM, handle_signal);
+ /* Remove existing pidfile */
+ if (pidfile)
+ unlink(pidfile);
+ /* Make sure we can write a pid file */
fp = fopen(pidfile, "w");
if (! fp)
eerrorx("%s: fopen `%s': %s", applet, pidfile, strerror(errno));
- fprintf(fp, "%d\n", getpid());
fclose(fp);
- if (svcname)
- rc_service_daemon_set(svcname, exec,
- (const char * const *) argv, pidfile, true);
-
- /* remove the controlling tty */
+ rc_service_value_set(svcname, "pidfile", pidfile);
+ sprintf(name, "%i", respawn_delay);
+ rc_service_value_set(svcname, "respawn_delay", name);
+ sprintf(name, "%i", respawn_max);
+ rc_service_value_set(svcname, "respawn_max", name);
+ sprintf(name, "%i", respawn_period);
+ rc_service_value_set(svcname, "respawn_period", name);
+ child_pid = fork();
+ if (child_pid == -1)
+ eerrorx("%s: fork: %s", applet, strerror(errno));
+ if (child_pid != 0)
+ /* first parent process, do nothing. */
+ exit(EXIT_SUCCESS);
#ifdef TIOCNOTTY
- ioctl(tty_fd, TIOCNOTTY, 0);
- close(tty_fd);
+ tty_fd = open("/dev/tty", O_RDWR);
#endif
-
- /*
- * Supervisor main loop
- */
- i = 0;
- while (!exiting) {
- wait(&i);
- if (exiting) {
- signal_setup(SIGCHLD, SIG_IGN);
- syslog(LOG_INFO, "stopping %s, pid %d", exec, child_pid);
- nkilled = run_stop_schedule(applet, exec, NULL, child_pid,
- 0, false, false, true);
- if (nkilled > 0)
- syslog(LOG_INFO, "killed %d processes", nkilled);
- } else {
- sleep(respawn_delay);
- if (respawn_max > 0 && respawn_period > 0) {
- respawn_now = time(NULL);
- if (first_spawn == 0)
- first_spawn = respawn_now;
- if (respawn_now - first_spawn > respawn_period) {
- respawn_count = 0;
- first_spawn = 0;
- } else
- respawn_count++;
- if (respawn_count >= respawn_max) {
- syslog(LOG_WARNING, "respawned \"%s\" too many times, "
- "exiting", exec);
- exiting = true;
- continue;
- }
+ devnull_fd = open("/dev/null", O_RDWR);
+ child_pid = fork();
+ if (child_pid == -1)
+ eerrorx("%s: fork: %s", applet, strerror(errno));
+ else if (child_pid != 0) {
+ c = argv;
+ x = 0;
+ while (c && *c) {
+ snprintf(name, sizeof(name), "argv_%-d",x);
+ rc_service_value_set(svcname, name, *c);
+ x++;
+ c++;
+ }
+ sprintf(name, "%d", x);
+ rc_service_value_set(svcname, "argc", name);
+ rc_service_value_set(svcname, "exec", exec);
+ supervisor(exec, argv);
+ } else
+ child_process(exec, argv);
+ } else if (stop) {
+ pid = get_pid(applet, pidfile);
+ if (pid != -1) {
+ i = kill(pid, SIGTERM);
+ if (i != 0)
+ /* We failed to send the signal */
+ ewarn("Unable to shut down the supervisor");
+ else {
+ /* wait for the supervisor to go down */
+ while (kill(pid, 0) == 0) {
+ ts.tv_sec = 0;
+ ts.tv_nsec = 1;
+ nanosleep(&ts, NULL);
}
- if (WIFEXITED(i))
- syslog(LOG_WARNING, "%s, pid %d, exited with return code %d",
- exec, child_pid, WEXITSTATUS(i));
- else if (WIFSIGNALED(i))
- syslog(LOG_WARNING, "%s, pid %d, terminated by signal %d",
- exec, child_pid, WTERMSIG(i));
- child_pid = fork();
- if (child_pid == -1)
- eerrorx("%s: fork: %s", applet, strerror(errno));
- if (child_pid == 0)
- child_process(exec, argv, svcname, respawn_count);
}
}
+ /* Even if we have not actually killed anything, we should
+ * remove information about it as it may have unexpectedly
+ * crashed out. We should also return success as the end
+ * result would be the same. */
if (pidfile && exists(pidfile))
unlink(pidfile);
if (svcname) {
@@ -800,6 +891,5 @@ int main(int argc, char **argv)
rc_service_mark(svcname, RC_SERVICE_STOPPED);
}
exit(EXIT_SUCCESS);
- } else if (child_pid == 0)
- child_process(exec, argv, svcname, respawn_count);
+ }
}
^ permalink raw reply related [flat|nested] 7+ messages in thread
end of thread, other threads:[~2017-10-24 15:33 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-06-05 17:18 [gentoo-commits] proj/openrc:master commit in: src/rc/, src/includes/ William Hubbs
-- strict thread matches above, loose matches on Subject: below --
2017-10-24 15:33 William Hubbs
2017-05-12 2:42 William Hubbs
2017-05-12 2:42 William Hubbs
2016-01-19 6:12 William Hubbs
2016-01-14 18:55 William Hubbs
2014-10-24 1:07 William Hubbs
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox