wireshark/wsutil/ws_pipe.h
Tomasz Moń 4240381026 wsutil: Remove flawed ws_pipe_close() function
The semantics behind ws_pipe_close() were broken since its introduction.
Forcing process termination on Windows, while simply setting variable on
other systems results in more OS specific code sprinkled all over the
place instead of less. Moreover ws_pipe_close() never handled standard
file handles. It is really hard to come up with sensible ws_pipe_close()
replacement, as process exit is actually asynchronous action. It is
recommended to register child watch using g_child_watch_add() instead.

Do not call ws_pipe_close() when deleting capture interface. Things will
break if extcap is still running when interface opts are being freed and
terminating process won't help.

Rework maxmind shutdown to rely on GIOChannel state. For unknown reason
TerminateProcess() is still needed on Windows. The actual root cause
should be identified and fixed instead of giving up hope that it will
ever work correctly on Windows. In other words, TerminateProcess()
should not be used as a pattern, but rather as a last resort.
2022-08-14 16:05:22 +00:00

104 lines
3.1 KiB
C

/** @file
*
* Routines for handling pipes.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __WS_PIPE_H__
#define __WS_PIPE_H__
// ws_symbol_export and WS_INVALID_PID
#include "wsutil/processes.h"
#include <glib.h>
#ifdef _WIN32
#include <windows.h>
#include <io.h>
#define ws_pipe_handle HANDLE
#define ws_get_pipe_handle(pipe_fd) ((HANDLE)_get_osfhandle(pipe_fd))
#else
#define ws_pipe_handle int
#define ws_get_pipe_handle(pipe_fd) (pipe_fd)
#endif
typedef struct _ws_pipe_t {
GPid pid;
GIOChannel *stdin_io;
GIOChannel *stdout_io;
GIOChannel *stderr_io;
} ws_pipe_t;
/**
* @brief Run a process using g_spawn_sync on UNIX and Linux, and
* CreateProcess on Windows. Wait for it to finish.
* @param [IN] working_directory Initial working directory.
* @param [IN] command Command to run.
* @param [IN] argc Number of arguments for the command, not including the command itself.
* @param [IN] args Arguments for the command, not including the command itself.
* The last element must be NULL.
* @param [OUT] command_output If not NULL, receives a copy of the command output. Must be g_freed.
* @return TRUE on success or FALSE on failure.
*/
WS_DLL_PUBLIC gboolean ws_pipe_spawn_sync(const gchar * working_directory, const gchar * command, gint argc, gchar ** args, gchar ** command_output);
/**
* @brief Initialize a ws_pipe_t struct. Sets .pid to WS_INVALID_PID and all other members to 0 or NULL.
* @param ws_pipe [IN] The pipe to initialize.
*/
WS_DLL_PUBLIC void ws_pipe_init(ws_pipe_t *ws_pipe);
/**
* @brief Checks whether a pipe is valid (for reading or writing).
*/
static inline gboolean ws_pipe_valid(ws_pipe_t *ws_pipe)
{
return ws_pipe && ws_pipe->pid && ws_pipe->pid != WS_INVALID_PID;
}
/**
* @brief Start a process using g_spawn_sync on UNIX and Linux, and CreateProcess on Windows.
* @param ws_pipe The process PID, stdio descriptors, etc.
* @param args The command to run along with its arguments.
* @return A valid PID on success, otherwise WS_INVALID_PID.
*/
WS_DLL_PUBLIC GPid ws_pipe_spawn_async (ws_pipe_t * ws_pipe, GPtrArray * args );
#ifdef _WIN32
/**
* @brief Wait for a set of handles using WaitForMultipleObjects. Windows only.
* @param pipe_handles An array of handles
* @param num_pipe_handles The size of the array.
* @param pid Child process PID.
* @return TRUE on success or FALSE on failure.
*/
WS_DLL_PUBLIC gboolean ws_pipe_wait_for_pipe(HANDLE * pipe_handles, int num_pipe_handles, HANDLE pid);
#endif
/**
* @brief Check to see if a file descriptor has data available.
* @param pipe_fd File descriptor.
* @return TRUE if data is available or FALSE otherwise.
*/
WS_DLL_PUBLIC gboolean ws_pipe_data_available(int pipe_fd);
#endif /* __WS_PIPE_H__ */
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/