aboutsummaryrefslogtreecommitdiffstats
path: root/coreupdown.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-06-02 09:10:17 +0200
committerMattias Andrée <maandree@kth.se>2024-06-02 09:10:17 +0200
commitfbe33dce137994314af97aea220bad2e8b984c36 (patch)
tree093b7e3c14f3186bf68a04d151bdaf1e07b8898e /coreupdown.c
parentBold names in Synopsis in man pages (diff)
downloadcoreupdown-fbe33dce137994314af97aea220bad2e8b984c36.tar.gz
coreupdown-fbe33dce137994314af97aea220bad2e8b984c36.tar.bz2
coreupdown-fbe33dce137994314af97aea220bad2e8b984c36.tar.xz
coreupdownd: add -X and -x1.1
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--coreupdown.c74
1 files changed, 65 insertions, 9 deletions
diff --git a/coreupdown.c b/coreupdown.c
index 500839f..283a1de 100644
--- a/coreupdown.c
+++ b/coreupdown.c
@@ -19,11 +19,11 @@
char *argv0;
static volatile sig_atomic_t caught_sighup = 0;
-#define coreup() coreupdown(ULONG_MAX)
-#define coredown() coreupdown(1)
+#define coreup(POSTCOMMAND) coreupdown(ULONG_MAX, (POSTCOMMAND))
+#define coredown(POSTCOMMAND) coreupdown(1, (POSTCOMMAND))
static int
-coreupdown(unsigned long int count)
+coreupdown(unsigned long int count, const char *postcommand)
{
char path[sizeof("/sys/devices/system/cpu/cpu/online") + 3 * sizeof(count)];
unsigned long int i;
@@ -49,7 +49,14 @@ coreupdown(unsigned long int count)
}
close(fd);
}
- return -(have_failure && !have_success);
+
+ if (have_failure && !have_success)
+ return -1;
+
+ if (postcommand)
+ system(postcommand);
+
+ return 0;
}
static void
@@ -70,6 +77,8 @@ daemon_usage(const char *command)
"[-t coredown-cpu-usage-time-consistency-threshold] "
"[-U coreup-cpu-usage-threshold] "
"[-u coredown-cpu-usage-threshold] "
+ "[-X post-coreup-command] "
+ "[-x post-coredown-command] "
"[-ef]\n", command);
}
@@ -99,7 +108,7 @@ oneshot_main(int argc, char **argv, unsigned long int count)
argc--;
}
- return -coreupdown(count);
+ return -coreupdown(count, NULL);
}
static int
@@ -506,9 +515,11 @@ load_configurations(const char *file, int optional,
unsigned int *coreup_threshold_cpu, unsigned int *coredown_threshold_cpu,
unsigned int *coreup_threshold_time, unsigned int *coredown_threshold_time,
unsigned int *coreup_cooldown, unsigned int *coredown_cooldown,
- unsigned int *daemon_interval_sec, unsigned long int *daemon_interval_nsec)
+ unsigned int *daemon_interval_sec, unsigned long int *daemon_interval_nsec,
+ char **post_coreup_command, char **post_coredown_command)
{
unsigned int *uintp;
+ char **strp;
unsigned long int tmp;
int fd;
FILE *f;
@@ -582,8 +593,38 @@ load_configurations(const char *file, int optional,
*uintp = (unsigned int)tmp;
continue;
+
not_uint:
+ if (OPTION("post-coreup-command"))
+ strp = post_coreup_command;
+ else if (OPTION("post-coredown-command"))
+ strp = post_coredown_command;
+ else
+ goto not_str;
+ s = &s[n];
+ while (isspace(*s))
+ s++;
+ if (*s != '=')
+ goto invalid;
+ s++;
+ while (isspace(*s))
+ s++;
+ p = rstrip(s);
+ pc = *p;
+ *p = 0;
+
+ if (strp) {
+ *strp = strdup(s);
+ if (!*strp) {
+ fprintf(stderr, "%s: strdup: %s\n", argv0, strerror(errno));
+ exit(1);
+ }
+ }
+
+ continue;
+
+ not_str:
if (!OPTION("check-interval"))
goto invalid;
@@ -672,6 +713,7 @@ daemon_main(int argc, char **argv, int orig_argc, char **orig_argv, int reexeced
unsigned int coreup_cooldown, coredown_cooldown;
unsigned int daemon_interval_sec, *uintp;
unsigned long int daemon_interval_nsec, tmp;
+ char *post_coreup_command = NULL, *post_coredown_command = NULL;
int have_coreup_threshold_cpu = 0, have_coredown_threshold_cpu = 0;
int have_coreup_threshold_time = 0, have_coredown_threshold_time = 0;
int have_coreup_cooldown = 0, have_coredown_cooldown = 0;
@@ -721,6 +763,18 @@ restart:
have_daemon_interval = 1;
break;
+ case 'X':
+ post_coreup_command = ARGNULL();
+ if (!post_coreup_command)
+ goto usage;
+ break;
+
+ case 'x':
+ post_coredown_command = ARGNULL();
+ if (!post_coredown_command)
+ goto usage;
+ break;
+
case 'e':
keep_stderr = 1;
break;
@@ -788,7 +842,9 @@ restart:
have_coreup_cooldown ? NULL : &coreup_cooldown,
have_coredown_cooldown ? NULL : &coredown_cooldown,
have_daemon_interval ? NULL : &daemon_interval_sec,
- have_daemon_interval ? NULL : &daemon_interval_nsec);
+ have_daemon_interval ? NULL : &daemon_interval_nsec,
+ post_coreup_command ? NULL : &post_coreup_command,
+ post_coredown_command ? NULL : &post_coredown_command);
if (coreup_threshold_cpu > 100) {
fprintf(stderr, "%s: CPU usage threshold for coreup was above 100, setting to 100\n", argv0);
@@ -860,7 +916,7 @@ restart:
down_time = 0;
if (up_time++ >= coreup_threshold_time) {
cooldown = coreup_cooldown;
- if (coreup())
+ if (coreup(post_coreup_command))
return 1;
up_time = 0;
}
@@ -868,7 +924,7 @@ restart:
up_time = 0;
if (down_time++ >= coredown_threshold_time) {
cooldown = coredown_cooldown;
- if (coredown())
+ if (coredown(post_coredown_command))
return 1;
down_time = 0;
}