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>
|
2023-01-14 22:56:24 +00:00
|
|
|
#include <signal.h>
|
2022-09-08 23:47:43 +00:00
|
|
|
#include <sys/wait.h>
|
2019-04-07 05:22:11 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2024-06-11 14:53:01 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-11-19 01:11:16 +00:00
|
|
|
|
2022-09-08 23:47:43 +00:00
|
|
|
static void abandon_tty() {
|
2022-11-28 23:01:12 +00:00
|
|
|
// The parent may get SIGSTOPed when it tries to call tcsetpgrp if the child has already done
|
|
|
|
// it. Prevent this by ignoring signals.
|
|
|
|
signal(SIGTTIN, SIG_IGN);
|
|
|
|
signal(SIGTTOU, SIG_IGN);
|
2022-09-08 23:47:43 +00:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
perror("fork");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
// Both parent and child do the same thing.
|
|
|
|
pid_t child = pid ? pid : getpid();
|
|
|
|
if (setpgid(child, child)) {
|
|
|
|
perror("setpgid");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
// tcsetpgrp may fail in the parent if the child has already exited.
|
|
|
|
// This is the benign race.
|
|
|
|
(void)tcsetpgrp(STDIN_FILENO, child);
|
|
|
|
// Parent waits for child to exit.
|
|
|
|
if (pid > 0) {
|
2024-06-11 14:53:01 +00:00
|
|
|
waitpid(child, NULL, 0);
|
2022-09-08 23:47:43 +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");
|
|
|
|
}
|
|
|
|
|
2021-04-05 20:04:05 +00:00
|
|
|
static void nohup_wait() {
|
|
|
|
pid_t init_parent = getppid();
|
|
|
|
if (signal(SIGHUP, SIG_IGN)) {
|
|
|
|
perror("tcsetgrp");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
// Note: these silly close() calls are necessary to prevent our parent process (presumably fish)
|
|
|
|
// from getting stuck in the "E" state ("Trying to exit"). This appears to be a (kernel?) bug on
|
|
|
|
// macOS: the process is no longer running but is not a zombie either, and so cannot be reaped.
|
|
|
|
// It is unclear why closing these fds successfully works around this issue.
|
|
|
|
close(STDIN_FILENO);
|
|
|
|
close(STDOUT_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
|
|
|
// To avoid leaving fish_test_helpers around, we exit once our parent changes, meaning the fish
|
|
|
|
// instance exited.
|
|
|
|
while (getppid() == init_parent) {
|
|
|
|
usleep(1000000 / 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 00:55:00 +00:00
|
|
|
static void report_foreground_loop() {
|
2019-07-16 04:30:58 +00:00
|
|
|
int was_fg = -1;
|
2024-06-11 14:53:01 +00:00
|
|
|
const pid_t grp = getpgrp();
|
2019-07-16 04:30:58 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 00:55:00 +00:00
|
|
|
static void report_foreground() {
|
|
|
|
bool is_fg = (tcgetpgrp(STDIN_FILENO) == getpgrp());
|
|
|
|
fputs(is_fg ? "foreground\n" : "background\n", stderr);
|
|
|
|
}
|
|
|
|
|
2019-07-16 04:30:58 +00:00
|
|
|
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");
|
2024-06-11 14:53:01 +00:00
|
|
|
fflush(NULL);
|
2019-10-13 23:06:16 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2024-06-11 14:53:01 +00:00
|
|
|
fprintf(stdout, "%ld\n", (long)getpid());
|
|
|
|
fflush(NULL);
|
2020-01-08 01:02:04 +00:00
|
|
|
usleep(1000000 / 2); //.5 secs
|
2019-12-08 19:44:21 +00:00
|
|
|
}
|
|
|
|
|
2024-06-11 14:53:01 +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-08-10 19:26:30 +00:00
|
|
|
static void print_signal(int sig) {
|
|
|
|
// Print a signal description to stderr.
|
2024-06-11 14:53:01 +00:00
|
|
|
const char *s = strsignal(sig);
|
|
|
|
if (s) {
|
2020-08-10 19:26:30 +00:00
|
|
|
fprintf(stderr, "%s", s);
|
2024-06-11 14:53:01 +00:00
|
|
|
if (strchr(s, ':') == NULL) {
|
2020-08-10 19:26:30 +00:00
|
|
|
fprintf(stderr, ": %d", sig);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 22:07:42 +00:00
|
|
|
static void print_blocked_signals() {
|
|
|
|
sigset_t sigs;
|
|
|
|
sigemptyset(&sigs);
|
2024-06-11 14:53:01 +00:00
|
|
|
if (sigprocmask(SIG_SETMASK, NULL, &sigs)) {
|
2020-04-05 22:07:42 +00:00
|
|
|
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.
|
2022-09-16 23:53:05 +00:00
|
|
|
// POSIX says sigqueue(2) can be used with signo 0 to validate the pid and signo parameters,
|
|
|
|
// but it is missing from OpenBSD and returns ENOSYS (not implemented) under WSL.
|
2020-05-21 07:34:04 +00:00
|
|
|
// Here we limit it to 32 because strsignal on OpenBSD returns "Unknown signal" for anything
|
2022-09-16 23:53:05 +00:00
|
|
|
// above, while NetBSD taps out at 63, and Linux at 64.
|
2020-05-21 07:34:04 +00:00
|
|
|
for (int sig = 1; sig < 33; sig++) {
|
2020-04-05 22:07:42 +00:00
|
|
|
if (sigismember(&sigs, sig)) {
|
2020-08-10 19:26:30 +00:00
|
|
|
print_signal(sig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_ignored_signals() {
|
|
|
|
for (int sig = 1; sig < 33; sig++) {
|
|
|
|
struct sigaction act = {};
|
2024-06-11 14:53:01 +00:00
|
|
|
sigaction(sig, NULL, &act);
|
2020-08-10 19:26:30 +00:00
|
|
|
if (act.sa_handler == SIG_IGN) {
|
|
|
|
print_signal(sig);
|
2020-04-05 22:07:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-11 14:53:01 +00:00
|
|
|
static void sigtstp_handler(int x) {
|
2024-08-31 18:16:51 +00:00
|
|
|
(void)!write(STDOUT_FILENO, "SIGTSTP\n", strlen("SIGTSTP\n"));
|
2024-06-11 14:53:01 +00:00
|
|
|
kill(getpid(), SIGSTOP);
|
|
|
|
}
|
|
|
|
static void sigcont_handler(int x) {
|
2024-08-31 18:16:51 +00:00
|
|
|
(void)!write(STDOUT_FILENO, "SIGCONT\n", strlen("SIGCONT\n"));
|
2024-06-11 14:53:01 +00:00
|
|
|
}
|
2020-11-20 03:15:41 +00:00
|
|
|
static void print_stop_cont() {
|
2024-06-11 14:53:01 +00:00
|
|
|
signal(SIGTSTP, &sigtstp_handler);
|
|
|
|
signal(SIGCONT, &sigcont_handler);
|
2020-11-20 03:15:41 +00:00
|
|
|
char buff[1];
|
|
|
|
for (;;) {
|
|
|
|
if (read(STDIN_FILENO, buff, sizeof buff) >= 0) {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 21:34:52 +00:00
|
|
|
static void sigkill_self() {
|
|
|
|
kill(getpid(), SIGKILL);
|
2024-03-19 15:41:25 +00:00
|
|
|
usleep(20000000); // 20 secs
|
2021-05-17 21:34:52 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2022-05-28 23:08:17 +00:00
|
|
|
static void sigint_self() {
|
|
|
|
kill(getpid(), SIGINT);
|
2024-03-19 15:41:25 +00:00
|
|
|
usleep(20000000); // 20 secs
|
2022-05-28 23:08:17 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2024-06-11 14:53:01 +00:00
|
|
|
static void do_nothing(int x) {
|
|
|
|
}
|
2022-02-12 19:41:03 +00:00
|
|
|
static void stdin_make_nonblocking() {
|
|
|
|
const int fd = STDIN_FILENO;
|
|
|
|
// Catch SIGCONT so pause() wakes us up.
|
2024-06-11 14:53:01 +00:00
|
|
|
signal(SIGCONT, &do_nothing);
|
2022-02-12 19:41:03 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
fprintf(stdout, "stdin was %sblocking\n", (flags & O_NONBLOCK) ? "non" : "");
|
|
|
|
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
|
|
|
|
perror("fcntl");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 18:51:02 +00:00
|
|
|
static void show_help();
|
|
|
|
|
|
|
|
/// A thing that fish_test_helper can do.
|
2024-06-11 14:53:01 +00:00
|
|
|
typedef struct {
|
2020-02-20 18:51:02 +00:00
|
|
|
/// The argument to match against.
|
|
|
|
const char *arg;
|
|
|
|
|
|
|
|
/// Function to invoke.
|
|
|
|
void (*func)();
|
|
|
|
|
|
|
|
/// Description of what this does.
|
|
|
|
const char *desc;
|
2024-06-11 14:53:01 +00:00
|
|
|
} fth_command_t;
|
2020-02-20 18:51:02 +00:00
|
|
|
|
|
|
|
static fth_command_t s_commands[] = {
|
2022-09-08 23:47:43 +00:00
|
|
|
{"abandon_tty", abandon_tty, "Create a new pgroup and transfer tty ownership to it"},
|
2020-02-20 18:51:02 +00:00
|
|
|
{"become_foreground_then_print_stderr", become_foreground_then_print_stderr,
|
|
|
|
"Claim the terminal (tcsetpgrp) and then print to stderr"},
|
2021-04-05 20:04:05 +00:00
|
|
|
{"nohup_wait", nohup_wait, "Ignore SIGHUP and just wait"},
|
2020-07-27 00:55:00 +00:00
|
|
|
{"report_foreground", report_foreground, "Report to stderr whether we own the terminal"},
|
|
|
|
{"report_foreground_loop", report_foreground_loop,
|
2020-02-20 18:51:02 +00:00
|
|
|
"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-21 17:24:33 +00:00
|
|
|
{"print_blocked_signals", print_blocked_signals,
|
|
|
|
"Print to stdout the name(s) of blocked signals"},
|
2020-08-10 19:26:30 +00:00
|
|
|
{"print_ignored_signals", print_ignored_signals,
|
|
|
|
"Print to stdout the name(s) of ignored signals"},
|
2020-11-20 03:15:41 +00:00
|
|
|
{"print_stop_cont", print_stop_cont, "Print when we get SIGTSTP and SIGCONT, exiting on input"},
|
2022-05-28 23:08:17 +00:00
|
|
|
{"sigint_self", sigint_self, "Send SIGINT to self"},
|
2021-05-17 21:34:52 +00:00
|
|
|
{"sigkill_self", sigkill_self, "Send SIGKILL to self"},
|
2022-02-12 19:41:03 +00:00
|
|
|
{"stdin_make_nonblocking", stdin_make_nonblocking,
|
|
|
|
"Print if stdin is blocking and then make it nonblocking"},
|
2020-02-20 18:51:02 +00:00
|
|
|
{"help", show_help, "Print list of fish_test_helper commands"},
|
2024-06-11 14:53:01 +00:00
|
|
|
{NULL, NULL, NULL},
|
2020-02-20 18:51:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void show_help() {
|
|
|
|
printf("fish_test_helper: helper utility for fish\n\n");
|
|
|
|
printf("Commands\n");
|
|
|
|
printf("--------\n");
|
2024-06-11 14:53:01 +00:00
|
|
|
for (int i = 0; s_commands[i].arg; i++) {
|
|
|
|
printf(" %s:\n %s\n\n", s_commands[i].arg, s_commands[i].desc);
|
2020-02-20 18:51:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 05:22:11 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-06-11 14:53:01 +00:00
|
|
|
const fth_command_t *found = NULL;
|
|
|
|
for (int j = 0; s_commands[j].arg; j++) {
|
|
|
|
if (!strcmp(argv[i], s_commands[j].arg)) {
|
|
|
|
found = &s_commands[j];
|
2020-02-20 18:51:02 +00:00
|
|
|
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;
|
|
|
|
}
|