2019-04-07 05:22:11 +00:00
|
|
|
// fish_test_helper is a little program with no fish dependencies that acts like certain other
|
|
|
|
// programs, allowing fish to test its behavior.
|
|
|
|
|
2019-12-14 00:16:19 +00:00
|
|
|
#include <fcntl.h>
|
2019-04-07 05:22:11 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-02-20 18:51:02 +00:00
|
|
|
#include <algorithm>
|
2019-11-19 01:11:16 +00:00
|
|
|
#include <csignal>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2020-03-26 19:45:40 +00:00
|
|
|
#include <iterator> // for std::begin/end
|
2019-11-19 01:11:16 +00:00
|
|
|
|
2019-04-07 05:22:11 +00:00
|
|
|
static void become_foreground_then_print_stderr() {
|
|
|
|
if (tcsetpgrp(STDOUT_FILENO, getpgrp()) < 0) {
|
|
|
|
perror("tcsetgrp");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
usleep(1000000 / 4); //.25 secs
|
|
|
|
fprintf(stderr, "become_foreground_then_print_stderr done\n");
|
|
|
|
}
|
|
|
|
|
2019-07-16 04:30:58 +00:00
|
|
|
static void report_foreground() {
|
|
|
|
int was_fg = -1;
|
|
|
|
const auto grp = getpgrp();
|
|
|
|
for (;;) {
|
|
|
|
int is_fg = (tcgetpgrp(STDIN_FILENO) == grp);
|
|
|
|
if (is_fg != was_fg) {
|
|
|
|
was_fg = is_fg;
|
|
|
|
if (fputs(is_fg ? "foreground\n" : "background\n", stderr) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usleep(1000000 / 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sigint_parent() {
|
2020-02-20 18:51:02 +00:00
|
|
|
// SIGINT the parent after a time, then exit
|
2019-07-16 04:30:58 +00:00
|
|
|
int parent = getppid();
|
|
|
|
usleep(1000000 / 4); //.25 secs
|
|
|
|
kill(parent, SIGINT);
|
|
|
|
fprintf(stderr, "Sent SIGINT to %d\n", parent);
|
|
|
|
}
|
|
|
|
|
2019-10-13 23:06:16 +00:00
|
|
|
static void print_stdout_stderr() {
|
|
|
|
fprintf(stdout, "stdout\n");
|
|
|
|
fprintf(stderr, "stderr\n");
|
|
|
|
fflush(nullptr);
|
|
|
|
}
|
|
|
|
|
2019-12-08 19:44:21 +00:00
|
|
|
static void print_pid_then_sleep() {
|
2020-02-12 18:20:40 +00:00
|
|
|
// On some systems getpid is a long, on others it's an int, let's just cast it.
|
|
|
|
fprintf(stdout, "%ld\n", (long)getpid());
|
2019-12-08 19:44:21 +00:00
|
|
|
fflush(nullptr);
|
2020-01-08 01:02:04 +00:00
|
|
|
usleep(1000000 / 2); //.5 secs
|
2019-12-08 19:44:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 18:20:40 +00:00
|
|
|
static void print_pgrp() { fprintf(stdout, "%ld\n", (long)getpgrp()); }
|
2019-12-08 21:28:30 +00:00
|
|
|
|
2019-12-14 00:16:19 +00:00
|
|
|
static void print_fds() {
|
|
|
|
bool needs_space = false;
|
|
|
|
for (int fd = 0; fd <= 100; fd++) {
|
|
|
|
if (fcntl(fd, F_GETFD) >= 0) {
|
|
|
|
fprintf(stdout, "%s%d", needs_space ? " " : "", fd);
|
|
|
|
needs_space = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fputc('\n', stdout);
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:07:42 +00:00
|
|
|
static void print_blocked_signals() {
|
|
|
|
sigset_t sigs;
|
|
|
|
sigemptyset(&sigs);
|
|
|
|
if (sigprocmask(SIG_SETMASK, nullptr, &sigs)) {
|
|
|
|
perror("sigprocmask");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2020-04-07 19:22:14 +00:00
|
|
|
// There is no obviously portable way to get the maximum number of signals.
|
|
|
|
// Here we limit it to 64 because strsignal on Linux returns "Unknown signal" for anything above.
|
|
|
|
for (int sig=1; sig < 65; sig++) {
|
2020-04-05 22:07:42 +00:00
|
|
|
if (sigismember(&sigs, sig)) {
|
|
|
|
if (const char *s = strsignal(sig)) {
|
2020-04-07 19:22:14 +00:00
|
|
|
fprintf(stderr, "%s", s);
|
|
|
|
if (strchr(s, ':') == nullptr) {
|
|
|
|
fprintf(stderr, ": %d", sig);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
2020-04-05 22:07:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 18:51:02 +00:00
|
|
|
static void show_help();
|
|
|
|
|
|
|
|
/// A thing that fish_test_helper can do.
|
|
|
|
struct fth_command_t {
|
|
|
|
/// The argument to match against.
|
|
|
|
const char *arg;
|
|
|
|
|
|
|
|
/// Function to invoke.
|
|
|
|
void (*func)();
|
|
|
|
|
|
|
|
/// Description of what this does.
|
|
|
|
const char *desc;
|
|
|
|
};
|
|
|
|
|
|
|
|
static fth_command_t s_commands[] = {
|
|
|
|
{"become_foreground_then_print_stderr", become_foreground_then_print_stderr,
|
|
|
|
"Claim the terminal (tcsetpgrp) and then print to stderr"},
|
|
|
|
{"report_foreground", report_foreground,
|
|
|
|
"Continually report to stderr whether we own the terminal"},
|
|
|
|
{"sigint_parent", sigint_parent, "Wait .25 seconds, then SIGINT the parent process"},
|
|
|
|
{"print_stdout_stderr", print_stdout_stderr, "Print 'stdout' to stdout and 'stderr' to stderr"},
|
|
|
|
{"print_pid_then_sleep", print_pid_then_sleep, "Print our pid, then sleep for .5 seconds"},
|
|
|
|
{"print_pgrp", print_pgrp, "Print our pgroup to stdout"},
|
|
|
|
{"print_fds", print_fds, "Print the list of active FDs to stdout"},
|
2020-04-05 22:07:42 +00:00
|
|
|
{"print_blocked_signals", print_blocked_signals, "Print to stdout the name(s) of blocked signals"},
|
2020-02-20 18:51:02 +00:00
|
|
|
{"help", show_help, "Print list of fish_test_helper commands"},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void show_help() {
|
|
|
|
printf("fish_test_helper: helper utility for fish\n\n");
|
|
|
|
printf("Commands\n");
|
|
|
|
printf("--------\n");
|
|
|
|
for (const auto &cmd : s_commands) {
|
|
|
|
printf(" %s:\n %s\n\n", cmd.arg, cmd.desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 05:22:11 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2020-02-20 18:51:02 +00:00
|
|
|
std::sort(std::begin(s_commands), std::end(s_commands),
|
|
|
|
[](const fth_command_t &lhs, const fth_command_t &rhs) {
|
|
|
|
return strcmp(lhs.arg, rhs.arg) < 0;
|
|
|
|
});
|
|
|
|
|
2019-04-07 05:22:11 +00:00
|
|
|
if (argc <= 1) {
|
2019-07-16 04:30:58 +00:00
|
|
|
fprintf(stderr, "No commands given.\n");
|
2019-04-07 05:22:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (int i = 1; i < argc; i++) {
|
2020-02-20 18:51:02 +00:00
|
|
|
if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "help") || !strcmp(argv[i], "-h")) {
|
|
|
|
show_help();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fth_command_t *found = nullptr;
|
|
|
|
for (const auto &cmd : s_commands) {
|
|
|
|
if (!strcmp(argv[i], cmd.arg)) {
|
|
|
|
found = &cmd;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
found->func();
|
2019-04-07 05:22:11 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s: Unknown command: %s\n", argv[0], argv[i]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|