]> bicyclesonthemoon.info Git - botm/exec/commitdiff
Add IO redirect option
authorb <rowerynaksiezycu@gmail.com>
Thu, 21 Sep 2023 20:57:45 +0000 (20:57 +0000)
committerb <rowerynaksiezycu@gmail.com>
Thu, 21 Sep 2023 20:57:45 +0000 (20:57 +0000)
exec.c
settings.txt

diff --git a/exec.c b/exec.c
index d8873a5a430231802c8bf9dee6f0e1d449c072d0..e7d04ff47d5621e6ebce6b60c9f69fd0a85ee4df 100644 (file)
--- a/exec.c
+++ b/exec.c
@@ -16,16 +16,51 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 #include <unistd.h>
 #include <errno.h>
+#include <stdio.h>
 
 #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)))
index 81baa77a165bfb8a0a6a21b0d75892c5d3d61df8..299f2f1ababd54215373f6ad090c9a5915af10d1 100644 (file)
@@ -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