From 01c4b9b537952c762866350e6e38fcf0cd38962c Mon Sep 17 00:00:00 2001 From: "Yury V. Zaytsev" Date: Fri, 30 Aug 2024 10:28:02 +0200 Subject: [PATCH] mock my_execvp Signed-off-by: Yury V. Zaytsev --- lib/tty/tty-ncurses.c | 3 +- lib/tty/tty.c | 7 ++- lib/util.h | 13 ++++ lib/utilunix.c | 87 +++++++++++++++++++++----- lib/vfs/netutil.c | 3 +- lib/vfs/vfs.c | 12 ++-- src/background.c | 3 +- src/cons.handler.c | 2 +- src/editor/etags.c | 2 +- src/execute.c | 4 +- src/main.c | 4 +- src/subshell/common.c | 2 +- src/vfs/shell/shell.c | 4 +- tests/lib/mc_realpath.c | 2 +- tests/lib/utilunix__my_system-common.c | 13 ++-- tests/lib/vfs/current_dir.c | 3 +- tests/lib/vfs/relative_cd.c | 3 +- tests/lib/vfs/vfs_setup_cwd.c | 3 +- 18 files changed, 120 insertions(+), 50 deletions(-) diff --git a/lib/tty/tty-ncurses.c b/lib/tty/tty-ncurses.c index fa503da785..7766297799 100644 --- a/lib/tty/tty-ncurses.c +++ b/lib/tty/tty-ncurses.c @@ -41,6 +41,7 @@ #include "lib/global.h" #include "lib/strutil.h" /* str_term_form */ +#include "lib/util.h" #ifndef WANT_TERM_H #define WANT_TERM_H @@ -102,7 +103,7 @@ tty_setup_sigwinch (void (*handler) (int)) #ifdef SA_RESTART act.sa_flags = SA_RESTART; #endif /* SA_RESTART */ - sigaction (SIGWINCH, &act, &oact); + my_sigaction (SIGWINCH, &act, &oact); #endif /* SIGWINCH */ tty_create_winch_pipe (); diff --git a/lib/tty/tty.c b/lib/tty/tty.c index 55ba0e99f8..c2ec4c506a 100644 --- a/lib/tty/tty.c +++ b/lib/tty/tty.c @@ -53,6 +53,7 @@ #include "lib/global.h" #include "lib/strutil.h" +#include "lib/util.h" #include "tty.h" #include "tty-internal.h" @@ -146,7 +147,7 @@ tty_start_interrupt_key (void) #ifdef SA_RESTART act.sa_flags = SA_RESTART; #endif /* SA_RESTART */ - sigaction (SIGINT, &act, NULL); + my_sigaction (SIGINT, &act, NULL); } /* --------------------------------------------------------------------------------------------- */ @@ -159,7 +160,7 @@ tty_enable_interrupt_key (void) memset (&act, 0, sizeof (act)); act.sa_handler = sigintr_handler; sigemptyset (&act.sa_mask); - sigaction (SIGINT, &act, NULL); + my_sigaction (SIGINT, &act, NULL); got_interrupt = 0; } @@ -173,7 +174,7 @@ tty_disable_interrupt_key (void) memset (&act, 0, sizeof (act)); act.sa_handler = SIG_IGN; sigemptyset (&act.sa_mask); - sigaction (SIGINT, &act, NULL); + my_sigaction (SIGINT, &act, NULL); } /* --------------------------------------------------------------------------------------------- */ diff --git a/lib/util.h b/lib/util.h index 952bae9a73..7b920a02cb 100644 --- a/lib/util.h +++ b/lib/util.h @@ -126,6 +126,11 @@ typedef struct mc_pipe_stream_t err; } mc_pipe_t; +/* sighandler_t is GNU extension */ +#ifndef HAVE_SIGHANDLER_T +typedef void (*sighandler_t) (int); +#endif + /*** structures declarations (and typedefs of structures)*****************************************/ /*** global variables defined in .c file *********************************************************/ @@ -200,6 +205,14 @@ const char *get_owner (uid_t uid); /* Returns a copy of *s until a \n is found and is below top */ const char *extract_line (const char *s, const char *top); +/* System call wrappers */ +sighandler_t my_signal (int signum, sighandler_t handler) __attribute__((weak)); +int my_sigaction (int signum, const struct sigaction *act, struct sigaction *oldact) + __attribute__((weak)); +pid_t my_fork (void) __attribute__((weak)); +int my_execvp (const char *file, char *const argv[]) __attribute__((weak)); +char *my_get_current_dir (void) __attribute__((weak)); + /* Process spawning */ int my_system (int flags, const char *shell, const char *command); int my_systeml (int flags, const char *shell, ...); diff --git a/lib/utilunix.c b/lib/utilunix.c index 63ea8d735e..6c0d69ba23 100644 --- a/lib/utilunix.c +++ b/lib/utilunix.c @@ -138,11 +138,11 @@ i_cache_add (int id, int_cache *cache, int size, char *text, int *last) /* --------------------------------------------------------------------------------------------- */ static my_fork_state_t -my_fork (void) +my_fork_state (void) { pid_t pid; - pid = fork (); + pid = my_fork (); if (pid < 0) { @@ -176,12 +176,12 @@ my_system__save_sigaction_handlers (my_system_sigactions_t *sigactions) ignore.sa_handler = SIG_IGN; sigemptyset (&ignore.sa_mask); - sigaction (SIGINT, &ignore, &sigactions->intr); - sigaction (SIGQUIT, &ignore, &sigactions->quit); + my_sigaction (SIGINT, &ignore, &sigactions->intr); + my_sigaction (SIGQUIT, &ignore, &sigactions->quit); /* Restore the original SIGTSTP handler, we don't want ncurses' */ /* handler messing the screen after the SIGCONT */ - sigaction (SIGTSTP, &startup_handler, &sigactions->stop); + my_sigaction (SIGTSTP, &startup_handler, &sigactions->stop); } /* --------------------------------------------------------------------------------------------- */ @@ -189,9 +189,9 @@ my_system__save_sigaction_handlers (my_system_sigactions_t *sigactions) static void my_system__restore_sigaction_handlers (my_system_sigactions_t *sigactions) { - sigaction (SIGINT, &sigactions->intr, NULL); - sigaction (SIGQUIT, &sigactions->quit, NULL); - sigaction (SIGTSTP, &sigactions->stop, NULL); + my_sigaction (SIGINT, &sigactions->intr, NULL); + my_sigaction (SIGQUIT, &sigactions->quit, NULL); + my_sigaction (SIGTSTP, &sigactions->stop, NULL); } /* --------------------------------------------------------------------------------------------- */ @@ -330,7 +330,7 @@ get_group (gid_t gid) void save_stop_handler (void) { - sigaction (SIGTSTP, NULL, &startup_handler); + my_sigaction (SIGTSTP, NULL, &startup_handler); } /* --------------------------------------------------------------------------------------------- */ @@ -349,6 +349,61 @@ my_exit (int status) _exit (status); } +/* --------------------------------------------------------------------------------------------- */ +/** + * Wrapper for signal() system call. + */ + +sighandler_t +my_signal (int signum, sighandler_t handler) +{ + return signal (signum, handler); +} + +/* --------------------------------------------------------------------------------------------- */ +/** + * Wrapper for sigaction() system call. + */ + +int +my_sigaction (int signum, const struct sigaction *act, struct sigaction *oldact) +{ + return sigaction (signum, act, oldact); +} + +/* --------------------------------------------------------------------------------------------- */ +/** + * Wrapper for fork() system call. + */ + +pid_t +my_fork (void) +{ + return fork (); +} + +/* --------------------------------------------------------------------------------------------- */ +/** + * Wrapper for execvp() system call. + */ + +int +my_execvp (const char *file, char *const argv[]) +{ + return execvp (file, argv); +} + +/* --------------------------------------------------------------------------------------------- */ +/** + * Wrapper for g_get_current_dir() library function. + */ + +char * +my_get_current_dir (void) +{ + return g_get_current_dir (); +} + /* --------------------------------------------------------------------------------------------- */ /** * Call external programs. @@ -423,7 +478,7 @@ my_systemv (const char *command, char *const argv[]) my_system__save_sigaction_handlers (&sigactions); - fork_state = my_fork (); + fork_state = my_fork_state (); switch (fork_state) { case FORK_ERROR: @@ -431,12 +486,12 @@ my_systemv (const char *command, char *const argv[]) break; case FORK_CHILD: { - signal (SIGINT, SIG_DFL); - signal (SIGQUIT, SIG_DFL); - signal (SIGTSTP, SIG_DFL); - signal (SIGCHLD, SIG_DFL); + my_signal (SIGINT, SIG_DFL); + my_signal (SIGQUIT, SIG_DFL); + my_signal (SIGTSTP, SIG_DFL); + my_signal (SIGCHLD, SIG_DFL); - execvp (command, argv); + my_execvp (command, argv); my_exit (127); /* Exec error */ } MC_FALLTHROUGH; @@ -1037,7 +1092,7 @@ mc_realpath (const char *path, char *resolved_path) /* If it's a relative pathname use getwd for starters. */ if (!IS_PATH_SEP (*path)) { - new_path = g_get_current_dir (); + new_path = my_get_current_dir (); if (new_path == NULL) strcpy (got_path, ""); else diff --git a/lib/vfs/netutil.c b/lib/vfs/netutil.c index 3f31d46c3c..a2654706c8 100644 --- a/lib/vfs/netutil.c +++ b/lib/vfs/netutil.c @@ -32,6 +32,7 @@ #include /* memset() */ #include "lib/global.h" +#include "lib/util.h" #include "netutil.h" @@ -75,7 +76,7 @@ tcp_init (void) memset (&sa, 0, sizeof (sa)); sa.sa_handler = sig_pipe; sigemptyset (&sa.sa_mask); - sigaction (SIGPIPE, &sa, NULL); + my_sigaction (SIGPIPE, &sa, NULL); initialized = TRUE; } diff --git a/lib/vfs/vfs.c b/lib/vfs/vfs.c index f4dad4649a..93a9286c1a 100644 --- a/lib/vfs/vfs.c +++ b/lib/vfs/vfs.c @@ -77,10 +77,10 @@ extern vfs_class *current_vfs; /*** global variables ****************************************************************************/ -struct vfs_dirent *mc_readdir_result = NULL; -GPtrArray *vfs__classes_list = NULL; -GString *vfs_str_buffer = NULL; -vfs_class *current_vfs = NULL; +struct vfs_dirent __attribute__((weak)) * mc_readdir_result = NULL; +GPtrArray __attribute__((weak)) * vfs__classes_list = NULL; +GString __attribute__((weak)) * vfs_str_buffer = NULL; +vfs_class __attribute__((weak)) * current_vfs = NULL; /*** file scope macro definitions ****************************************************************/ @@ -642,7 +642,7 @@ vfs_setup_cwd (void) if (vfs_get_raw_current_dir () == NULL) { - current_dir = g_get_current_dir (); + current_dir = my_get_current_dir (); vfs_set_raw_current_dir (vfs_path_from_str (current_dir)); g_free (current_dir); @@ -661,7 +661,7 @@ vfs_setup_cwd (void) me = vfs_path_get_last_path_vfs (vfs_get_raw_current_dir ()); if ((me->flags & VFSF_LOCAL) != 0) { - current_dir = g_get_current_dir (); + current_dir = my_get_current_dir (); tmp_vpath = vfs_path_from_str (current_dir); g_free (current_dir); diff --git a/src/background.c b/src/background.c index ce727c4bd0..72a0eda73e 100644 --- a/src/background.c +++ b/src/background.c @@ -49,6 +49,7 @@ #include "lib/tty/key.h" /* add_select_channel(), delete_select_channel() */ #include "lib/widget.h" /* message() */ #include "lib/event-types.h" +#include "lib/util.h" /* my_fork() */ #include "filemanager/fileopctx.h" /* file_op_context_t */ @@ -535,7 +536,7 @@ do_background (file_op_context_t *ctx, char *info) return (-1); } - pid = fork (); + pid = my_fork (); if (pid == -1) { int saved_errno = errno; diff --git a/src/cons.handler.c b/src/cons.handler.c index 5f58a4271e..2a026960b8 100644 --- a/src/cons.handler.c +++ b/src/cons.handler.c @@ -154,7 +154,7 @@ handle_console_linux (console_action_t action) break; } /* Get the console saver running */ - cons_saver_pid = fork (); + cons_saver_pid = my_fork (); if (cons_saver_pid < 0) { /* Cannot fork */ diff --git a/src/editor/etags.c b/src/editor/etags.c index f5163bdae8..31b2f2cd41 100644 --- a/src/editor/etags.c +++ b/src/editor/etags.c @@ -426,7 +426,7 @@ edit_get_match_keyword_cmd (WEdit *edit) for (i = 0; i < word_len; i++) g_string_append_c (match_expr, edit_buffer_get_byte (&edit->buffer, word_start + i)); - ptr = g_get_current_dir (); + ptr = my_get_current_dir (); path = g_strconcat (ptr, PATH_SEP_STR, (char *) NULL); g_free (ptr); diff --git a/src/execute.c b/src/execute.c index 1d51ffa19b..b2b791cdd6 100644 --- a/src/execute.c +++ b/src/execute.c @@ -160,12 +160,12 @@ do_suspend_cmd (void) /* Make sure that the SIGTSTP below will suspend us directly, without calling ncurses' SIGTSTP handler; we *don't* want ncurses to redraw the screen immediately after the SIGCONT */ - sigaction (SIGTSTP, &startup_handler, &sigtstp_action); + my_sigaction (SIGTSTP, &startup_handler, &sigtstp_action); kill (getpid (), SIGTSTP); /* Restore previous SIGTSTP action */ - sigaction (SIGTSTP, &sigtstp_action, NULL); + my_sigaction (SIGTSTP, &sigtstp_action, NULL); } #endif /* SIGTSTP */ diff --git a/src/main.c b/src/main.c index 02a49ba20e..0cf7a744bd 100644 --- a/src/main.c +++ b/src/main.c @@ -208,7 +208,7 @@ init_sigchld (void) sigchld_action.sa_flags = SA_RESTART; #endif /* !SA_RESTART */ - if (sigaction (SIGCHLD, &sigchld_action, NULL) == -1) + if (my_sigaction (SIGCHLD, &sigchld_action, NULL) == -1) { #ifdef ENABLE_SUBSHELL /* @@ -499,7 +499,7 @@ main (int argc, char *argv[]) if (mc_global.tty.alternate_plus_minus) numeric_keypad_mode (); - (void) signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */ + (void) my_signal (SIGCHLD, SIG_DFL); /* Disable the SIGCHLD handler */ if (mc_global.tty.console_flag != '\0') handle_console (CONSOLE_DONE); diff --git a/src/subshell/common.c b/src/subshell/common.c index 72f0015690..c196865c00 100644 --- a/src/subshell/common.c +++ b/src/subshell/common.c @@ -1403,7 +1403,7 @@ init_subshell (void) subshell_alive = TRUE; subshell_stopped = FALSE; - subshell_pid = fork (); + subshell_pid = my_fork (); if (subshell_pid == -1) { diff --git a/src/vfs/shell/shell.c b/src/vfs/shell/shell.c index f4133771a0..b13fb9911f 100644 --- a/src/vfs/shell/shell.c +++ b/src/vfs/shell/shell.c @@ -426,7 +426,7 @@ shell_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[]) if ((pipe (fileset1) < 0) || (pipe (fileset2) < 0)) vfs_die ("Cannot pipe(): %m."); - res = fork (); + res = my_fork (); if (res != 0) { @@ -449,7 +449,7 @@ shell_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[]) res = open ("/dev/null", O_WRONLY); close (fileset2[0]); close (fileset2[1]); - execvp (path, (char **) argv); + my_execvp (path, (char **) argv); my_exit (3); } } diff --git a/tests/lib/mc_realpath.c b/tests/lib/mc_realpath.c index cd3ff106cb..2af5406a6e 100644 --- a/tests/lib/mc_realpath.c +++ b/tests/lib/mc_realpath.c @@ -125,7 +125,7 @@ main (void) tc_core = tcase_create ("Core"); /* writable directory where check creates temporary files */ - cwd = g_get_current_dir (); + cwd = my_get_current_dir (); g_setenv ("TEMP", cwd, TRUE); g_free (cwd); diff --git a/tests/lib/utilunix__my_system-common.c b/tests/lib/utilunix__my_system-common.c index f41cec1f51..c7a8ad0de6 100644 --- a/tests/lib/utilunix__my_system-common.c +++ b/tests/lib/utilunix__my_system-common.c @@ -28,11 +28,6 @@ #include "lib/vfs/vfs.h" -/* sighandler_t is GNU extension */ -#ifndef HAVE_SIGHANDLER_T -typedef void (*sighandler_t) (int); -#endif - /* --------------------------------------------------------------------------------------------- */ /* @CapturedValue */ @@ -65,7 +60,7 @@ static int sigaction__return_value = 0; /* @Mock */ int -sigaction (int signum, const struct sigaction *act, struct sigaction *oldact) +my_sigaction (int signum, const struct sigaction *act, struct sigaction *oldact) { int *tmp_signum; struct sigaction *tmp_act; @@ -133,7 +128,7 @@ static sighandler_t signal__return_value = NULL; /* @Mock */ sighandler_t -signal (int signum, sighandler_t handler) +my_signal (int signum, sighandler_t handler) { int *tmp_signum; sighandler_t *tmp_handler; @@ -180,7 +175,7 @@ static pid_t fork__return_value; /* @Mock */ pid_t -fork (void) +my_fork (void) { return fork__return_value; } @@ -207,7 +202,7 @@ static int execvp__return_value = 0; /* @Mock */ int -execvp (const char *file, char *const argv[]) +my_execvp (const char *file, char *const argv[]) { char **one_arg; execvp__file__captured = g_strdup (file); diff --git a/tests/lib/vfs/current_dir.c b/tests/lib/vfs/current_dir.c index 1cdd83593b..d22a8d6b38 100644 --- a/tests/lib/vfs/current_dir.c +++ b/tests/lib/vfs/current_dir.c @@ -32,6 +32,7 @@ #include "lib/global.h" #include "lib/strutil.h" #include "lib/vfs/xdirentry.h" +#include "lib/util.h" #include "src/vfs/local/local.c" @@ -191,7 +192,7 @@ main (void) tc_core = tcase_create ("Core"); /* writable directory where check creates temporary files */ - cwd = g_get_current_dir (); + cwd = my_get_current_dir (); g_setenv ("TEMP", cwd, TRUE); g_free (cwd); diff --git a/tests/lib/vfs/relative_cd.c b/tests/lib/vfs/relative_cd.c index 93ec93322a..219d75bffb 100644 --- a/tests/lib/vfs/relative_cd.c +++ b/tests/lib/vfs/relative_cd.c @@ -31,6 +31,7 @@ #include "lib/strutil.h" #include "lib/vfs/xdirentry.h" #include "lib/vfs/path.h" +#include "lib/util.h" #include "src/vfs/local/local.c" @@ -201,7 +202,7 @@ main (void) tc_core = tcase_create ("Core"); /* writable directory where check creates temporary files */ - cwd = g_get_current_dir (); + cwd = my_get_current_dir (); g_setenv ("TEMP", cwd, TRUE); g_free (cwd); diff --git a/tests/lib/vfs/vfs_setup_cwd.c b/tests/lib/vfs/vfs_setup_cwd.c index e623150081..95ff79222f 100644 --- a/tests/lib/vfs/vfs_setup_cwd.c +++ b/tests/lib/vfs/vfs_setup_cwd.c @@ -30,6 +30,7 @@ #include #include "lib/strutil.h" +#include "lib/util.h" #include "lib/vfs/xdirentry.h" #include "src/vfs/local/local.c" @@ -37,7 +38,7 @@ /* @Mock */ char * -g_get_current_dir (void) +my_get_current_dir (void) { return g_strdup ("/some/path"); }