diff options
author | pgen <p.gen.progs@gmail.com> | 2022-02-23 00:01:47 +0100 |
---|---|---|
committer | pgen <p.gen.progs@gmail.com> | 2022-02-23 00:01:47 +0100 |
commit | 9e4e5e9bf00329643cc3c227f4b6fcee171d095e (patch) | |
tree | fc8ebb601d22b618bb9a142e29581bf12917635e | |
parent | dfcb0b777e4575e37e46f420382003ccfddeec74 (diff) |
Prevent smenu from running in the background
-rw-r--r-- | smenu.c | 30 | ||||
-rw-r--r-- | smenu.h | 2 |
2 files changed, 32 insertions, 0 deletions
@@ -36,6 +36,8 @@ #include <sys/ioctl.h> #include <sys/time.h> #include <sys/param.h> +#include <sys/stat.h> +#include <fcntl.h> #include <wchar.h> #include "xmalloc.h" @@ -6217,6 +6219,26 @@ reset_search_buffer(win_t * win, search_data_t * search_data, ticker_t * timers, } } +/* =============================================================== */ +/* Detect if the current terminal belongs to the foreground group. */ +/* returns 1 if yes else returns 0. */ +/* =============================================================== */ +int +is_in_foreground_process_group(void) +{ + int fd, fg; + + fd = open("/dev/tty", O_RDONLY); + if (fd < 0) + return 0; + + fg = (tcgetpgrp(fd) == getpgid(0)); + + close(fd); + + return fg; +} + /* ================= */ /* Main entry point. */ /* ================= */ @@ -9426,6 +9448,14 @@ main(int argc, char * argv[]) setvbuf(stdout, NULL, _IONBF, 0); + /* Make sure smenu runs in foreground. */ + /* """"""""""""""""""""""""""""""""""" */ + if (!is_in_foreground_process_group()) + { + fprintf(stderr, "smenu cannot be launched in background.\n"); + exit(EXIT_FAILURE); + } + /* Set the characteristics of the terminal. */ /* """""""""""""""""""""""""""""""""""""""" */ setup_term(fileno(stdin)); @@ -626,4 +626,6 @@ reset_search_buffer(win_t * win, search_data_t * search_data, ticker_t * timers, toggle_t * toggles, term_t * term, daccess_t * daccess, langinfo_t * langinfo, long last_line, char * tmp_word, long word_real_max_size); +int +is_in_foreground_process_group(void); #endif |