aboutsummaryrefslogtreecommitdiffstats
path: root/wsutil/ws_pipe.c
diff options
context:
space:
mode:
authorGerald Combs <gerald@wireshark.org>2018-03-12 08:49:12 -0700
committerAnders Broman <a.broman58@gmail.com>2018-03-13 17:18:30 +0000
commita2f926761525ac67feeda742a796917a1f043b33 (patch)
treecf79825b1d983633dc7d7f6d1a28ec2fe70dfa1e /wsutil/ws_pipe.c
parent0874b8bac6ca89f1d91d30d66d54f425e4e7c81e (diff)
Windows: Always assign newly-created processes to our job.
Move ws_pipe_kill_child_on_exit to win32-utils. Add win32_create_process, which calls CreateProcess + AssignProcessToJobObject. Use win32_create_process instead of CreateProcess everywhere. Bug: 1419 Change-Id: I7a1f17dddf6a73f6973d54621f271b69311400d1 Reviewed-on: https://code.wireshark.org/review/26448 Reviewed-by: Gerald Combs <gerald@wireshark.org> Petri-Dish: Gerald Combs <gerald@wireshark.org> Tested-by: Petri Dish Buildbot Reviewed-by: Anders Broman <a.broman58@gmail.com>
Diffstat (limited to 'wsutil/ws_pipe.c')
-rw-r--r--wsutil/ws_pipe.c62
1 files changed, 2 insertions, 60 deletions
diff --git a/wsutil/ws_pipe.c b/wsutil/ws_pipe.c
index d683b925f6..a18897adfb 100644
--- a/wsutil/ws_pipe.c
+++ b/wsutil/ws_pipe.c
@@ -33,54 +33,6 @@
#include "wsutil/filesystem.h"
#include "wsutil/ws_pipe.h"
-#ifdef _WIN32
-#include <tchar.h>
-#include "wsutil/win32-utils.h"
-
-// This appears to be the closest equivalent to SIGPIPE on Windows.
-// https://blogs.msdn.microsoft.com/oldnewthing/20131209-00/?p=2433
-// https://stackoverflow.com/a/53214/82195
-// We might want to make it public and use it for our other external
-// processes.
-
-// CreateProcess flags.
-// CREATE_NEW_CONSOLE: Don't interfere with the main application console.
-// CREATE_SUSPENDED: Suspend the child so that we can cleanly call
-// AssignProcessToJobObject.
-// CREATE_BREAKAWAY_FROM_JOB: The main application might be associated with
-// a job, e.g. if we're running from ConEmu or Visual Studio. Break away
-// from it so that we can cleanly call AssignProcessToJobObject on *our* job.
-#define WS_CP_JOB_FLAGS (CREATE_NEW_CONSOLE|CREATE_SUSPENDED|CREATE_BREAKAWAY_FROM_JOB)
-
-static void ws_pipe_kill_child_on_exit(ws_pipe_handle child_handle) {
- static HANDLE cjo_handle = NULL;
- if (!cjo_handle) {
- cjo_handle = CreateJobObject(NULL, _T("Local\\Wireshark child process cleanup"));
-
- if (!cjo_handle) {
- g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not create child cleanup job object: %s",
- win32strerror(GetLastError()));
- return;
- }
-
- JOBOBJECT_EXTENDED_LIMIT_INFORMATION cjo_jel_info = { 0 };
- cjo_jel_info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
- BOOL sijo_ret = SetInformationJobObject(cjo_handle, JobObjectExtendedLimitInformation,
- &cjo_jel_info, sizeof(cjo_jel_info));
- if (!sijo_ret) {
- g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not set child cleanup limits: %s",
- win32strerror(GetLastError()));
- }
- }
-
- BOOL aptjo_ret = AssignProcessToJobObject(cjo_handle, child_handle);
- if (!aptjo_ret) {
- g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not assign child cleanup process: %s",
- win32strerror(GetLastError()));
- }
-}
-#endif // _WIN32
-
gboolean ws_pipe_spawn_sync(gchar *dirname, gchar *command, gint argc, gchar **args, gchar **command_output)
{
gboolean status = FALSE;
@@ -94,7 +46,6 @@ gboolean ws_pipe_spawn_sync(gchar *dirname, gchar *command, gint argc, gchar **a
GString *winargs = g_string_sized_new(200);
gchar *quoted_arg;
- gunichar2 *wcommandline;
STARTUPINFO info;
PROCESS_INFORMATION processInfo;
@@ -158,8 +109,6 @@ gboolean ws_pipe_spawn_sync(gchar *dirname, gchar *command, gint argc, gchar **a
g_free(quoted_arg);
}
- wcommandline = g_utf8_to_utf16(winargs->str, (glong)winargs->len, NULL, NULL, NULL);
-
memset(&processInfo, 0, sizeof(PROCESS_INFORMATION));
memset(&info, 0, sizeof(STARTUPINFO));
@@ -169,12 +118,10 @@ gboolean ws_pipe_spawn_sync(gchar *dirname, gchar *command, gint argc, gchar **a
info.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
info.wShowWindow = SW_HIDE;
- if (CreateProcess(NULL, wcommandline, NULL, NULL, TRUE, WS_CP_JOB_FLAGS, NULL, NULL, &info, &processInfo))
+ if (win32_create_process(NULL, winargs->str, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &info, &processInfo))
{
gchar* buffer;
- ws_pipe_kill_child_on_exit(processInfo.hProcess);
- ResumeThread(processInfo.hThread);
WaitForSingleObject(processInfo.hProcess, INFINITE);
buffer = (gchar*)g_malloc(BUFFER_SIZE);
status = ws_read_string_from_pipe(child_stdout_rd, buffer, BUFFER_SIZE);
@@ -237,7 +184,6 @@ GPid ws_pipe_spawn_async(ws_pipe_t *ws_pipe, GPtrArray *args)
GString *winargs = g_string_sized_new(200);
gchar *quoted_arg;
- gunichar2 *wcommandline;
STARTUPINFO info;
PROCESS_INFORMATION processInfo;
@@ -290,8 +236,6 @@ GPid ws_pipe_spawn_async(ws_pipe_t *ws_pipe, GPtrArray *args)
g_free(quoted_arg);
}
- wcommandline = g_utf8_to_utf16(winargs->str, (glong)winargs->len, NULL, NULL, NULL);
-
memset(&processInfo, 0, sizeof(PROCESS_INFORMATION));
memset(&info, 0, sizeof(STARTUPINFO));
@@ -302,10 +246,8 @@ GPid ws_pipe_spawn_async(ws_pipe_t *ws_pipe, GPtrArray *args)
info.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
info.wShowWindow = SW_HIDE;
- if (CreateProcess(NULL, wcommandline, NULL, NULL, TRUE, WS_CP_JOB_FLAGS, NULL, NULL, &info, &processInfo))
+ if (win32_create_process(NULL, winargs->str, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &info, &processInfo))
{
- ws_pipe_kill_child_on_exit(processInfo.hProcess);
- ResumeThread(processInfo.hThread);
ws_pipe->stdin_fd = _open_osfhandle((intptr_t)(child_stdin_wr), _O_BINARY);
ws_pipe->stdout_fd = _open_osfhandle((intptr_t)(child_stdout_rd), _O_BINARY);
ws_pipe->stderr_fd = _open_osfhandle((intptr_t)(child_stderr_rd), _O_BINARY);