summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <mzfuryy@0pointer.net>2008-04-18 05:32:15 +0200
committerLennart Poettering <mzfuryy@0pointer.net>2008-04-18 05:32:15 +0200
commit152265bda9bf908fedc9e6e1813e3c9a853b1efd (patch)
tree9d599096ba28b5204c882a508d250f5b8c959bcc
parent1741f3d650a00bc75071f78590f454e2625e3c31 (diff)
Add getopt() options.
Implement a --verbose switch
-rw-r--r--rtwatch.c70
1 files changed, 50 insertions, 20 deletions
diff --git a/rtwatch.c b/rtwatch.c
index ee34406..7a05dd4 100644
--- a/rtwatch.c
+++ b/rtwatch.c
@@ -16,6 +16,7 @@
#include <sys/mount.h>
#include <sys/wait.h>
#include <inttypes.h>
+#include <getopt.h>
#define TOP_PATH "/var/run/rtwatch"
#define PERIOD_USEC_FILE
@@ -23,6 +24,8 @@
#define CPU_LOAD_MAX 50 /* 80% CPU load at max */
#define RTPRIO_MAX 18
+static int verbose = 0;
+
static int write_long(const char *fn, unsigned long u) {
int fd;
ssize_t k;
@@ -184,7 +187,8 @@ static int wait_for_kid(pid_t pid, int timeout, sigset_t *ss, int *ret, int *ret
}
if (WIFSIGNALED(status)) {
- fprintf(stderr, "Warning: child process terminated with signal %i.\n", WTERMSIG(status));
+ if (verbose)
+ fprintf(stderr, "Warning: child process terminated with signal %i.\n", WTERMSIG(status));
*ret_signal = WTERMSIG(status);
return 0;
}
@@ -197,7 +201,8 @@ static int wait_for_kid(pid_t pid, int timeout, sigset_t *ss, int *ret, int *ret
static int safe_kill(pid_t pid, sigset_t *ss, int *ret, int *ret_signal) {
int r;
- fprintf(stderr, "Killing child %lu with signal %i\n", (unsigned long) pid, SIGTERM);
+ if (verbose)
+ fprintf(stderr, "Killing child %lu with signal %i\n", (unsigned long) pid, SIGTERM);
if (kill(pid, SIGTERM) < 0) {
if (errno == ESRCH)
@@ -210,7 +215,8 @@ static int safe_kill(pid_t pid, sigset_t *ss, int *ret, int *ret_signal) {
if ((r = wait_for_kid(pid, 1, ss, ret, ret_signal)) <= 0)
return r;
- fprintf(stderr, "Killing child %lu with signal %i\n", (unsigned long) pid, SIGKILL);
+ if (verbose)
+ fprintf(stderr, "Killing child %lu with signal %i\n", (unsigned long) pid, SIGKILL);
if (kill(pid, SIGKILL) < 0) {
if (errno == ESRCH)
@@ -223,18 +229,21 @@ static int safe_kill(pid_t pid, sigset_t *ss, int *ret, int *ret_signal) {
if ((r = wait_for_kid(pid, 1, ss, ret, ret_signal)) <= 0)
return r;
- fprintf(stderr, "Client didn't react.\n");
+ fprintf(stderr, "Client didn't react on termination signals.\n");
return -1;
}
static void help(const char *argv0, FILE *f) {
fprintf(f,
- "%s <percentage> <program> [arguments...]\n\n"
- "\truntime: available runtime in percent.\n"
- "\tprogram: the program to run\n\n"
- "Example:\n"
- "\t%s 1.5 foobar\n",
+ "%s <percentage> -- <program> [arguments...]\n\n"
+ "\t-v\tVerbose\n"
+ "\t-h\tHelp\n\n"
+ "\t<percentage>: CPU time to assign in percent.\n"
+ "\t<program>: the program to run\n\n"
+ "Example:\n\n"
+ "\t%s 1.5 -- foobar\n\n"
+ "\tRuns 'foobar' and assigns it 1.5%% of the available CPU time.\n",
argv0, argv0);
}
@@ -247,6 +256,12 @@ int main(int argc, char *argv[]) {
float cpu_percentage;
unsigned long runtime_usec, period_usec, u;
int ret_signal = -1;
+ int o;
+ static const struct option long_options[] = {
+ { "help", no_argument, NULL, 'h' },
+ { "verbose", no_argument, NULL, 'v' },
+ { NULL, 0, NULL, 0 }
+ };
if (argc < 0)
argv0 = "rtwatch";
@@ -255,18 +270,29 @@ int main(int argc, char *argv[]) {
else
argv0 = argv[0];
- if (argc == 1) {
- help(argv0, stdout);
- ret = 0;
- goto finish;
+ while ((o = getopt_long(argc, argv, "hv", long_options, NULL)) >= 0) {
+
+ switch (o) {
+
+ case 'h':
+ help(argv0, stdout);
+ break;
+
+ case 'v':
+ verbose = 1;
+ break;
+
+ default:
+ goto finish;
+ }
}
- if (argc < 3) {
+ if (optind+2 > argc) {
help(argv0, stderr);
goto finish;
}
- cpu_percentage = atof(argv[1]);
+ cpu_percentage = atof(argv[optind]);
if (cpu_percentage <= 0 || cpu_percentage > 100) {
fprintf(stderr, "Failed to parse period argument: %s\n", argv[1]);
@@ -287,10 +313,11 @@ int main(int argc, char *argv[]) {
goto finish;
}
- fprintf(stderr, "period=%0.2fms runtime=%0.2fms max_cpu_load=%0.1f%%\n",
- (double) period_usec / 1000,
- (double) runtime_usec / 1000,
- (double) (runtime_usec*100/period_usec));
+ if (verbose)
+ fprintf(stderr, "period=%0.2fms runtime=%0.2fms max_cpu_load=%0.1f%%\n",
+ (double) period_usec / 1000,
+ (double) runtime_usec / 1000,
+ (double) (runtime_usec*100/period_usec));
if (geteuid() != 0) {
fprintf(stderr, "%s needs to be run as root.\n", argv0);
@@ -369,7 +396,10 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "fork(): %s\n", strerror(errno));
goto finish_remove_cgroup_path;
} else if (!pid)
- _exit(child(runtime_usec, cgroup_path, argv + 2));
+ _exit(child(runtime_usec, cgroup_path, argv + optind + 1));
+
+ if (verbose)
+ fprintf(stderr, "Child process has PID %lu.\n", (unsigned long) pid);
if (wait_for_kid(pid, 0, &ss, &ret, &ret_signal) < 0)
goto finish_kill;