sin: add version and quiet flags
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I53f8463f4926cbd6aeaf31447cdb5a7c6a6a6964
This commit is contained in:
parent
c55096fca4
commit
faef5bccdc
1 changed files with 22 additions and 6 deletions
28
main.c
28
main.c
|
|
@ -15,6 +15,8 @@
|
||||||
|
|
||||||
#include <systemd/sd-bus.h>
|
#include <systemd/sd-bus.h>
|
||||||
|
|
||||||
|
#define VERSION "1.0.0"
|
||||||
|
|
||||||
#define EXIT_OK 0
|
#define EXIT_OK 0
|
||||||
#define EXIT_ARGS 1
|
#define EXIT_ARGS 1
|
||||||
#define EXIT_INIT 2
|
#define EXIT_INIT 2
|
||||||
|
|
@ -42,6 +44,7 @@ static const char *valid_mode[] = {"block", "delay", NULL};
|
||||||
|
|
||||||
static volatile sig_atomic_t running = 1;
|
static volatile sig_atomic_t running = 1;
|
||||||
static volatile sig_atomic_t cleanup_requested = 0;
|
static volatile sig_atomic_t cleanup_requested = 0;
|
||||||
|
static int quiet = 0;
|
||||||
|
|
||||||
static void handle_sig(int sig) {
|
static void handle_sig(int sig) {
|
||||||
(void)sig;
|
(void)sig;
|
||||||
|
|
@ -344,7 +347,7 @@ static void usage(FILE *f, const char *prog) {
|
||||||
fprintf(f,
|
fprintf(f,
|
||||||
"Usage: %s -n <pattern> [-n <pattern> ...] "
|
"Usage: %s -n <pattern> [-n <pattern> ...] "
|
||||||
"[--what=WHAT] [--mode=MODE]\n"
|
"[--what=WHAT] [--mode=MODE]\n"
|
||||||
" [--who=WHO] [--why=WHY] [--poll=SECONDS]\n"
|
" [--who=WHO] [--why=WHY] [--poll=SECONDS] [-q]\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Options:\n"
|
"Options:\n"
|
||||||
" -n, --name PATTERN Process command-line substring to watch "
|
" -n, --name PATTERN Process command-line substring to watch "
|
||||||
|
|
@ -362,7 +365,9 @@ static void usage(FILE *f, const char *prog) {
|
||||||
"system idle...)\n"
|
"system idle...)\n"
|
||||||
" --poll SECONDS Poll interval in seconds (%.1f-%.1f, "
|
" --poll SECONDS Poll interval in seconds (%.1f-%.1f, "
|
||||||
"default: %.1f)\n"
|
"default: %.1f)\n"
|
||||||
" -h, --help Show this help\n",
|
" -q, --quiet Suppress informational output\n"
|
||||||
|
" -h, --help Show this help\n"
|
||||||
|
" -v, --version Show version\n",
|
||||||
prog, MIN_POLL_INTERVAL, MAX_POLL_INTERVAL, DEFAULT_POLL_INTERVAL);
|
prog, MIN_POLL_INTERVAL, MAX_POLL_INTERVAL, DEFAULT_POLL_INTERVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -483,6 +488,15 @@ int main(int argc, char **argv) {
|
||||||
free(patterns);
|
free(patterns);
|
||||||
return EXIT_OK;
|
return EXIT_OK;
|
||||||
|
|
||||||
|
} else if (strcmp(argv[i], "--version") == 0 ||
|
||||||
|
strcmp(argv[i], "-v") == 0) {
|
||||||
|
printf("sin %s\n", VERSION);
|
||||||
|
free(patterns);
|
||||||
|
return EXIT_OK;
|
||||||
|
|
||||||
|
} else if (strcmp(argv[i], "--quiet") == 0 || strcmp(argv[i], "-q") == 0) {
|
||||||
|
quiet = 1;
|
||||||
|
|
||||||
} else if (argv[i][0] == '-' && argv[i][1] == 'n' && argv[i][2] != '\0') {
|
} else if (argv[i][0] == '-' && argv[i][1] == 'n' && argv[i][2] != '\0') {
|
||||||
// Support "-npattern" form
|
// Support "-npattern" form
|
||||||
if (npats >= MAX_PATTERNS) {
|
if (npats >= MAX_PATTERNS) {
|
||||||
|
|
@ -548,13 +562,14 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
if (found) {
|
if (found) {
|
||||||
if (inh.fd < 0) {
|
if (inh.fd < 0) {
|
||||||
fprintf(stderr, "Target process found - acquiring inhibitor\n");
|
if (!quiet)
|
||||||
|
fprintf(stderr, "Target process found - acquiring inhibitor\n");
|
||||||
if (inhibitor_acquire(&inh) < 0) {
|
if (inhibitor_acquire(&inh) < 0) {
|
||||||
fprintf(
|
fprintf(
|
||||||
stderr,
|
stderr,
|
||||||
"Failed to acquire inhibitor; retrying after poll interval\n");
|
"Failed to acquire inhibitor; retrying after poll interval\n");
|
||||||
exit_code = EXIT_RUNTIME;
|
exit_code = EXIT_RUNTIME;
|
||||||
} else {
|
} else if (!quiet) {
|
||||||
fprintf(stderr, "Inhibitor acquired (fd=%d)\n", inh.fd);
|
fprintf(stderr, "Inhibitor acquired (fd=%d)\n", inh.fd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -573,7 +588,8 @@ int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inh.fd >= 0 && running) {
|
if (inh.fd >= 0 && running) {
|
||||||
fprintf(stderr, "Target exited - releasing inhibitor\n");
|
if (!quiet)
|
||||||
|
fprintf(stderr, "Target exited - releasing inhibitor\n");
|
||||||
inhibitor_release(&inh);
|
inhibitor_release(&inh);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -590,7 +606,7 @@ int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
if (cleanup_requested) {
|
if (cleanup_requested && !quiet) {
|
||||||
fprintf(stderr, "Shutting down gracefully...\n");
|
fprintf(stderr, "Shutting down gracefully...\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue