From d624e6e2d0f29e07849e90eb1e7531bd0e1b20c8 Mon Sep 17 00:00:00 2001 From: Jorgen Lundman Date: Mon, 28 Oct 2024 13:34:55 +0900 Subject: [PATCH] Attempt to execute a script Signed-off-by: Jorgen Lundman --- cmd/zed/os/windows/zed.d/all-debug.ps1 | 2 ++ cmd/zed/os/windows/zed_exec.c | 39 ++++++++++++++++++++++++++ cmd/zed/zed_conf.c | 2 ++ lib/libspl/include/os/windows/unistd.h | 2 +- 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 cmd/zed/os/windows/zed.d/all-debug.ps1 diff --git a/cmd/zed/os/windows/zed.d/all-debug.ps1 b/cmd/zed/os/windows/zed.d/all-debug.ps1 new file mode 100644 index 000000000000..3ad358a18701 --- /dev/null +++ b/cmd/zed/os/windows/zed.d/all-debug.ps1 @@ -0,0 +1,2 @@ +Get-Process + diff --git a/cmd/zed/os/windows/zed_exec.c b/cmd/zed/os/windows/zed_exec.c index 0c3d8701041c..6a744166c043 100644 --- a/cmd/zed/os/windows/zed_exec.c +++ b/cmd/zed/os/windows/zed_exec.c @@ -108,6 +108,38 @@ _zed_exec_create_env(zed_strings_t *zsp) return ((char **)buf); } +void +launch_process(const char *path, const char *prog, char *env[]) +{ + STARTUPINFO si; + PROCESS_INFORMATION pi; + ZeroMemory(&si, sizeof (si)); + si.cb = sizeof (si); + ZeroMemory(&pi, sizeof (pi)); + char cmd[MAX_PATH]; + + fprintf(stderr, "Launching process '%s' in '%s'\r\n", prog, path); + fflush(stderr); + + snprintf(cmd, sizeof (cmd), + "powershell.exe -ExecutionPolicy Bypass -File %s", path); + + if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, + &si, &pi)) { + zed_log_msg(LOG_WARNING, + "Failed to create process \"%s\": %s", + prog, strerror(GetLastError())); + return; + } + + // While we debug + WaitForSingleObject(pi.hProcess, INFINITE); + + // Close process and thread handles + CloseHandle(pi.hProcess); + CloseHandle(pi.hThread); +} + /* * Fork a child process to handle event [eid]. The program [prog] * in directory [dir] is executed with the environment [env]. @@ -168,6 +200,13 @@ _zed_exec_fork_child(uint64_t eid, const char *dir, const char *prog, _exit(127); } +#ifdef _WIN32 + /* + * fork() always returns 1 (parent) above, so here we can spawn process + */ + launch_process(path, prog, env); +#endif + /* parent process */ node = calloc(1, sizeof (*node)); diff --git a/cmd/zed/zed_conf.c b/cmd/zed/zed_conf.c index 29de27c77c34..42d8c7f2d645 100644 --- a/cmd/zed/zed_conf.c +++ b/cmd/zed/zed_conf.c @@ -386,12 +386,14 @@ zed_conf_scan_dir(struct zed_conf *zcp) direntp->d_name); continue; } +#ifndef _WIN32 if (!(st.st_mode & S_IXUSR)) { zed_log_msg(LOG_INFO, "Ignoring \"%s\": not executable by user", direntp->d_name); continue; } +#endif if ((st.st_mode & S_IWGRP) && !zcp->do_force) { zed_log_msg(LOG_NOTICE, "Ignoring \"%s\": writable by group", diff --git a/lib/libspl/include/os/windows/unistd.h b/lib/libspl/include/os/windows/unistd.h index 6f52c67fca33..414a09ba30f9 100644 --- a/lib/libspl/include/os/windows/unistd.h +++ b/lib/libspl/include/os/windows/unistd.h @@ -104,7 +104,7 @@ extern pid_t setsid(void); static inline pid_t fork(void) { - return (0); + return (1); } extern int mkostemps(char *templ, int suffixlen, DWORD flags);