From: b Date: Thu, 21 Sep 2023 20:57:45 +0000 (+0000) Subject: Add IO redirect option X-Git-Url: http://bicyclesonthemoon.info/git-projects/?a=commitdiff_plain;h=92cf35c0340afcadb39f06248de26e114ad5603c;p=botm%2Fexec Add IO redirect option --- diff --git a/exec.c b/exec.c index d8873a5..e7d04ff 100644 --- a/exec.c +++ b/exec.c @@ -16,16 +16,51 @@ along with this program. If not, see . */ #include #include +#include #define TARGET "/path/to/exec" /* replace with actual target ###EXEC_TARGET:*/ +/* #define REDIRECT_STDIN [path] ###EXEC_REDIRECT_STDIN: */ +/* #define REDIRECT_STDOUT [path] ###EXEC_REDIRECT_STDOUT: */ +/* #define REDIRECT_STDERR [path] ###EXEC_REDIRECT_STDERR: */ +/* #define BINMODE ###EXEC_BINMODE: */ + +#ifdef BINMODE + #define IN_MODE "rb" + #define OUT_MODE "ab" +#else + #define IN_MODE "rt" + #define OUT_MODE "at" +#endif int main(int argc, char *argv[], char *envp[]) { uid_t euid; gid_t egid; int r; +#if defined REDIRECT_STDIN || defined REDIRECT_STDOUT || defined REDIRECT_STDERR + FILE *f; +#endif euid = geteuid(); egid = getegid(); + +#ifdef REDIRECT_STDIN + f = freopen(REDIRECT_STDIN, IN_MODE, stdin); + if (f == NULL) + return (r = errno); +#endif + +#ifdef REDIRECT_STDOUT + f = freopen(REDIRECT_STDOUT, OUT_MODE, stdout); + if (f == NULL) + return (r = errno); +#endif + +#ifdef REDIRECT_STDERR + f = freopen(REDIRECT_STDERR, OUT_MODE, stderr); + if (f == NULL) + return (r = errno); +#endif + if ((r = setreuid(euid, euid))) return (r = errno); if ((r = setregid(egid, egid))) diff --git a/settings.txt b/settings.txt index 81baa77..299f2f1 100644 --- a/settings.txt +++ b/settings.txt @@ -17,3 +17,30 @@ _C_DEFINE_STR: #define $0 "@_ESCAPE($1)" _TARGET = @_PATH($path, $target) EXEC_TARGET = @_C_DEFINE_STR(TARGET, $_TARGET) + +if $stdin +_REDIRECT_STDIN = @_PATH($io_path, $stdin) +EXEC_REDIRECT_STDIN = @_C_DEFINE_STR(REDIRECT_STDIN, $_REDIRECT_STDIN ) +else +EXEC_REDIRECT_STDIN: /* EXEC_REDIRECT_STDIN */ +endif + +if $stdout +_REDIRECT_STDOUT = @_PATH($io_path, $stdout) +EXEC_REDIRECT_STDOUT = @_C_DEFINE_STR(REDIRECT_STDOUT, $_REDIRECT_STDOUT) +else +EXEC_REDIRECT_STDOUT: /* EXEC_REDIRECT_STDOUT */ +endif + +if $stderr +_REDIRECT_STDERR = @_PATH($io_path, $stderr) +EXEC_REDIRECT_STDERR = @_C_DEFINE_STR(REDIRECT_STDERR, $_REDIRECT_STDERR) +else +EXEC_REDIRECT_STDERR: /* EXEC_REDIRECT_STDERR */ +endif + +if $binmode +EXEC_BINMODE: @_C_DEFINE_STR(BINMODE, $binmode) +else +EXEC_BINMODE: /* EXEC_BINMODE */ +endif