aboutsummaryrefslogtreecommitdiffstats
path: root/ui
diff options
context:
space:
mode:
authorJoão Valverde <j@v6e.pt>2022-10-06 06:43:30 +0100
committerJoão Valverde <j@v6e.pt>2022-10-11 14:25:04 +0100
commita19834b98cda6d31bc31534b8cd497d055645439 (patch)
tree7de3e05fa14042c9d014bd353c5ed33460c8440d /ui
parent44d1cc6d4a8ac93c235356554d505759ea1d6aba (diff)
Windows: Store "gui.console_open" in the Windows registry
This removes the last dependency of the logging subsystem on the preferences module. The latter is started much later than the former and this is an issue. The Windows-only preference "gui.console_open" is stored in the registry as HKEY_LOCAL_USER\Software\Wireshark\ConsoleOpen. The semantics are exactly the same. The preference is read by the logging subsystem for initialization and then again by the preferences (read/write) so the user can configure it as before. The code to store the preference also in the preferences file was kept, for backward compatibility and because it is not incompatible with using the Registry concurrently. The elimination of the prefs dependency also allows moving the Windows console logic to wsutil and add the functionality to wslog directly, thereby eliminating the superfluous Wireshark/Logray custom log handler. To be able to read the ws_log_console_open global variable from libwireshark it becomes necessary to add a new export macro symbol called WSUTIL_EXPORT.
Diffstat (limited to 'ui')
-rw-r--r--ui/CMakeLists.txt1
-rw-r--r--ui/commandline.c4
-rw-r--r--ui/console.c51
-rw-r--r--ui/console.h40
-rw-r--r--ui/logray/logray_main.cpp5
-rw-r--r--ui/qt/main.cpp5
-rw-r--r--ui/qt/main_application.cpp8
-rw-r--r--ui/win32/console_win32.cpp295
-rw-r--r--ui/win32/console_win32.h64
9 files changed, 7 insertions, 466 deletions
diff --git a/ui/CMakeLists.txt b/ui/CMakeLists.txt
index 302ba53e6d..90fe884729 100644
--- a/ui/CMakeLists.txt
+++ b/ui/CMakeLists.txt
@@ -14,7 +14,6 @@ set(NONGENERATED_UI_SRC
clopts_common.c
cmdarg_err.c
commandline.c
- console.c
decode_as_utils.c
dissect_opts.c
export_pdu_ui_utils.c
diff --git a/ui/commandline.c b/ui/commandline.c
index bb9f79686c..7a417228a8 100644
--- a/ui/commandline.c
+++ b/ui/commandline.c
@@ -25,6 +25,9 @@
#include <ui/exit_codes.h>
#include <wsutil/filesystem.h>
#include <wsutil/ws_assert.h>
+#ifdef _WIN32
+#include <wsutil/console_win32.h>
+#endif
#include <epan/ex-opt.h>
#include <epan/packet.h>
@@ -36,7 +39,6 @@
#include "capture_opts.h"
#include "persfilepath_opt.h"
#include "preference_utils.h"
-#include "console.h"
#include "recent.h"
#include "decode_as_utils.h"
diff --git a/ui/console.c b/ui/console.c
deleted file mode 100644
index ab0ffea1d5..0000000000
--- a/ui/console.c
+++ /dev/null
@@ -1,51 +0,0 @@
-/* console.c
- * Console log handler routines
- *
- * Wireshark - Network traffic analyzer
- * By Gerald Combs <gerald@wireshark.org>
- * Copyright 1998 Gerald Combs
- *
- * SPDX-License-Identifier: GPL-2.0-or-later
- */
-
-#include "config.h"
-
-#include <stdio.h>
-
-
-#include "epan/prefs.h"
-#include "wsutil/time_util.h"
-#include <wsutil/ws_assert.h>
-
-#include "console.h"
-
-void
-console_log_writer(const char *domain, enum ws_log_level level,
- struct timespec timestamp,
- const char *file, long line, const char *func,
- const char *user_format, va_list user_ap,
- void *user_data _U_)
-{
- gboolean fatal = level == LOG_LEVEL_ERROR;
-
-#ifdef _WIN32
- if (prefs.gui_console_open != console_open_never || fatal) {
- /* the user wants a console or the application will terminate immediately */
- create_console();
- }
-#else
- (void)fatal;
-#endif /* _WIN32 */
-
- ws_log_console_writer(domain, level, timestamp, file, line, func,
- user_format, user_ap);
-
-#ifdef _WIN32
- if (fatal) {
- /* wait for a key press before the following error handler will terminate the program
- this way the user at least can read the error message */
- printf("\n\nPress any key to exit\n");
- _getch();
- }
-#endif /* _WIN32 */
-}
diff --git a/ui/console.h b/ui/console.h
deleted file mode 100644
index 214053bf12..0000000000
--- a/ui/console.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/** @file
- *
- * Console log handler routines
- *
- * Wireshark - Network traffic analyzer
- * By Gerald Combs <gerald@wireshark.org>
- * Copyright 1998 Gerald Combs
- *
- * SPDX-License-Identifier: GPL-2.0-or-later
- */
-
-#ifndef __CONSOLE_H__
-#define __CONSOLE_H__
-
-#ifdef _WIN32 /* Needed for console I/O */
-#include <fcntl.h>
-#include <conio.h>
-#include <ui/win32/console_win32.h>
-#endif
-
-#ifdef __cplusplus
-extern "C" {
-#endif /* __cplusplus */
-
-#include <wsutil/wslog.h>
-
-/** The GUI log writer.
- */
-void
-console_log_writer(const char *domain, enum ws_log_level level,
- struct timespec timestamp,
- const char *file, long line, const char *func,
- const char *user_format, va_list user_ap,
- void *user_data);
-
-#ifdef __cplusplus
-}
-#endif /* __cplusplus */
-
-#endif /* __CONSOLE_H__ */
diff --git a/ui/logray/logray_main.cpp b/ui/logray/logray_main.cpp
index d6baddadf4..db2a044614 100644
--- a/ui/logray/logray_main.cpp
+++ b/ui/logray/logray_main.cpp
@@ -19,7 +19,7 @@
#include <tchar.h>
#include <wchar.h>
#include <shellapi.h>
-#include "ui/win32/console_win32.h"
+#include <wsutil/console_win32.h>
#endif
#include <ui/clopts_common.h>
@@ -64,7 +64,6 @@
#include "epan/srt_table.h"
#include "ui/alert_box.h"
-#include "ui/console.h"
#include "ui/iface_lists.h"
#include "ui/language.h"
#include "ui/persfilepath_opt.h"
@@ -484,7 +483,7 @@ int main(int argc, char *qt_argv[])
cmdarg_err_init(logray_cmdarg_err, logray_cmdarg_err_cont);
/* Initialize log handler early so we can have proper logging during startup. */
- ws_log_init_with_writer("logray", console_log_writer, vcmdarg_err);
+ ws_log_init("logray", vcmdarg_err);
/* For backward compatibility with GLib logging and Wireshark 3.4. */
ws_log_console_writer_set_use_stdout(TRUE);
diff --git a/ui/qt/main.cpp b/ui/qt/main.cpp
index 575e924a13..c090a7d350 100644
--- a/ui/qt/main.cpp
+++ b/ui/qt/main.cpp
@@ -19,7 +19,7 @@
#include <tchar.h>
#include <wchar.h>
#include <shellapi.h>
-#include "ui/win32/console_win32.h"
+#include <wsutil/console_win32.h>
#endif
#include <ui/clopts_common.h>
@@ -64,7 +64,6 @@
#include "epan/srt_table.h"
#include "ui/alert_box.h"
-#include "ui/console.h"
#include "ui/iface_lists.h"
#include "ui/language.h"
#include "ui/persfilepath_opt.h"
@@ -486,7 +485,7 @@ int main(int argc, char *qt_argv[])
cmdarg_err_init(wireshark_cmdarg_err, wireshark_cmdarg_err_cont);
/* Initialize log handler early so we can have proper logging during startup. */
- ws_log_init_with_writer("wireshark", console_log_writer, vcmdarg_err);
+ ws_log_init("wireshark", vcmdarg_err);
/* For backward compatibility with GLib logging and Wireshark 3.4. */
ws_log_console_writer_set_use_stdout(TRUE);
diff --git a/ui/qt/main_application.cpp b/ui/qt/main_application.cpp
index 1d9f35930d..25941be6d8 100644
--- a/ui/qt/main_application.cpp
+++ b/ui/qt/main_application.cpp
@@ -63,7 +63,6 @@
#include "wsutil/utf8_entities.h"
#ifdef _WIN32
-# include "ui/win32/console_win32.h"
# include "wsutil/file_util.h"
# include <QMessageBox>
# include <QSettings>
@@ -1073,13 +1072,6 @@ _e_prefs *MainApplication::readConfigurationFiles(bool reset)
/* Load libwireshark settings from the current profile. */
prefs_p = epan_load_settings();
-#ifdef _WIN32
- /* if the user wants a console to be always there, well, we should open one for him */
- if (prefs_p->gui_console_open == console_open_always) {
- create_console();
- }
-#endif
-
/* Read the capture filter file. */
read_filter_list(CFILTER_LIST);
diff --git a/ui/win32/console_win32.cpp b/ui/win32/console_win32.cpp
deleted file mode 100644
index 744791fe7d..0000000000
--- a/ui/win32/console_win32.cpp
+++ /dev/null
@@ -1,295 +0,0 @@
-/* console_win32.c
- * Console support for MSWindows
- *
- * Wireshark - Network traffic analyzer
- * By Gerald Combs <gerald@wireshark.org>
- * Copyright 2002, Jeffrey C. Foster <jfoste@woodward.com>
- *
- * SPDX-License-Identifier: GPL-2.0-or-later
- */
-
-#ifdef _WIN32
-
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#include <glib.h>
-#include <wsutil/file_util.h>
-
-#include "console_win32.h"
-
-#include <fcntl.h>
-#include <conio.h>
-#include <windows.h>
-#include <tchar.h>
-
-static gboolean has_console; /* TRUE if app has console */
-static gboolean console_wait; /* "Press any key..." */
-static gboolean stdin_capture = FALSE; /* Don't grab stdin & stdout if TRUE */
-
-/*
- * Check whether a given standard handle needs to be redirected.
- *
- * If you run a Windows-subsystem program from cmd.exe on Windows XP,
- * and you haven't redirected the handle in question, GetStdHandle()
- * succeeds (so it doesn't return INVALID_HANDLE_VALUE or NULL), but
- * GetFile_type fails on the results with ERROR_INVALID_HANDLE.
- * In that case, redirection to a console is necessary.
- *
- * If you run it from the shell prompt in "mintty" in at least some
- * versions of Cygwin on Windows XP, and you haven't redirected the
- * handle in question, GetStdHandle() succeeds and returns a handle
- * that's a pipe or socket; it appears mintty reads from it and outputs
- * what it reads to the console.
- */
-static gboolean
-needs_redirection(int std_handle)
-{
- HANDLE fd;
- DWORD handle_type;
- DWORD error;
-
- fd = GetStdHandle(std_handle);
- if (fd == NULL) {
- /*
- * No standard handle. According to Microsoft's
- * documentation for GetStdHandle(), one reason for
- * this would be that the process is "a service on
- * an interactive desktop"; I'm not sure whether
- * such a process should be popping up a console.
- *
- * However, it also appears to be the case for
- * the standard input and standard error, but
- * *not* the standard output, for something run
- * with a double-click in Windows Explorer,
- * sow we'll say it needs redirection.
- */
- return TRUE;
- }
- if (fd == INVALID_HANDLE_VALUE) {
- /*
- * OK, I'm not when this would happen; return
- * "no redirection" for now.
- */
- return FALSE;
- }
- handle_type = GetFileType(fd);
- if (handle_type == FILE_TYPE_UNKNOWN) {
- error = GetLastError();
- if (error == ERROR_INVALID_HANDLE) {
- /*
- * OK, this appears to be the case where we're
- * running something in a mode that needs a
- * console.
- */
- return TRUE;
- }
- }
-
- /*
- * Assume no redirection is needed for all other cases.
- */
- return FALSE;
-}
-
-/*
- * If this application has no console window to which its standard output
- * would go, create one.
- */
-void
-create_console(void)
-{
- gboolean must_redirect_stdin;
- gboolean must_redirect_stdout;
- gboolean must_redirect_stderr;
-
- if (stdin_capture) {
- /* We've been handed "-i -". Don't mess with stdio. */
- return;
- }
-
- if (has_console) {
- return;
- }
-
- /* Are the standard input, output, and error invalid handles? */
- must_redirect_stdin = needs_redirection(STD_INPUT_HANDLE);
- must_redirect_stdout = needs_redirection(STD_OUTPUT_HANDLE);
- must_redirect_stderr = needs_redirection(STD_ERROR_HANDLE);
-
- /* If none of them are invalid, we don't need to do anything. */
- if (!must_redirect_stdin && !must_redirect_stdout && !must_redirect_stderr)
- return;
-
- /* OK, at least one of them needs to be redirected to a console;
- try to attach to the parent process's console and, if that fails,
- try to create one. */
- /*
- * See if we have an existing console (i.e. we were run from a
- * command prompt).
- */
- if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
- /* Probably not, as we couldn't attach to the parent process's
- console.
- Try to create a console.
-
- According to a comment on
-
-http://msdn.microsoft.com/en-us/library/windows/desktop/ms681952(v=vs.85).aspx
-
- (which now redirects to a docs.microsoft.com page that is
- devoid of comments, and which is not available on the
- Wayback Machine)
-
- and according to
-
-http://connect.microsoft.com/VisualStudio/feedback/details/689696/installing-security-update-kb2507938-prevents-console-allocation
-
- (which has disappeared, and isn't available on the Wayback
- Machine)
-
- and
-
-https://answers.microsoft.com/en-us/windows/forum/windows_xp-windows_update/kb2567680-andor-kb2507938-breaks-attachconsole-api/e8191280-2d49-4be4-9918-18486fba0afa
-
-even a failed attempt to attach to another process's console
-will cause subsequent AllocConsole() calls to fail, possibly due
-to bugs introduced by a security patch. To work around this, we
-do a FreeConsole() first. */
- FreeConsole();
- if (AllocConsole()) {
- /* That succeeded. */
- console_wait = TRUE;
- SetConsoleTitle(_T("Wireshark Debug Console"));
- } else {
- /* On Windows XP, this still fails; FreeConsole() apparently
- doesn't clear the state, as it does on Windows 7. */
- return; /* couldn't create console */
- }
- }
-
- if (must_redirect_stdin)
- ws_freopen("CONIN$", "r", stdin);
- if (must_redirect_stdout) {
- ws_freopen("CONOUT$", "w", stdout);
- fprintf(stdout, "\n");
- }
- if (must_redirect_stderr) {
- ws_freopen("CONOUT$", "w", stderr);
- fprintf(stderr, "\n");
- }
-
- /* Now register "destroy_console()" as a routine to be called just
- before the application exits, so that we can destroy the console
- after the user has typed a key (so that the console doesn't just
- disappear out from under them, giving the user no chance to see
- the message(s) we put in there). */
- atexit(destroy_console);
-
- /* Well, we have a console now. */
- has_console = TRUE;
-}
-
-void
-restore_pipes(void)
-{
- gboolean must_redirect_stdin;
- gboolean must_redirect_stdout;
- gboolean must_redirect_stderr;
-
- HANDLE fd;
-
- if (stdin_capture) {
- /* We've been handed "-i -". Don't mess with stdio. */
- return;
- }
-
- if (has_console) {
- return;
- }
-
- /* Are the standard input, output, and error invalid handles? */
- must_redirect_stdin = needs_redirection(STD_INPUT_HANDLE);
- must_redirect_stdout = needs_redirection(STD_OUTPUT_HANDLE);
- must_redirect_stderr = needs_redirection(STD_ERROR_HANDLE);
-
- /* If none of them are invalid, we don't need to do anything. */
- if (!must_redirect_stdin && !must_redirect_stdout && !must_redirect_stderr)
- return;
-
- if (!must_redirect_stdin)
- fd = GetStdHandle(STD_INPUT_HANDLE);
-
- /* OK, at least one of them needs to be redirected to a console;
- try to attach to the parent process's console and, if that fails,
- cleanup and return. */
- /*
- * See if we have an existing console (i.e. we were run from a
- * command prompt).
- */
- if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
- FreeConsole();
- return; /* No parent - cleanup and exit */
- }
-
- if (must_redirect_stdin) {
- ws_freopen("CONIN$", "r", stdin);
- } else {
- SetStdHandle(STD_INPUT_HANDLE, fd);
- }
- if (must_redirect_stdout) {
- ws_freopen("CONOUT$", "w", stdout);
- fprintf(stdout, "\n");
- }
- if (must_redirect_stderr) {
- ws_freopen("CONOUT$", "w", stderr);
- fprintf(stderr, "\n");
- }
-
- /* Now register "destroy_console()" as a routine to be called just
- before the application exits, so that we can destroy the console
- after the user has typed a key (so that the console doesn't just
- disappear out from under them, giving the user no chance to see
- the message(s) we put in there). */
- atexit(destroy_console);
-
- /* Well, we have a console now. */
- has_console = TRUE;
-}
-
-void
-destroy_console(void)
-{
- if (console_wait) {
- printf("\n\nPress any key to exit\n");
- _getch();
- }
- FreeConsole();
-}
-
-void
-set_console_wait(gboolean set_console_wait)
-{
- console_wait = set_console_wait;
-}
-
-gboolean
-get_console_wait(void)
-{
- return console_wait;
-}
-
-void
-set_stdin_capture(gboolean set_stdin_capture)
-{
- stdin_capture = set_stdin_capture;
-}
-
-gboolean
-get_stdin_capture(void)
-{
- return stdin_capture;
-}
-
-#endif /* _WIN32 */
diff --git a/ui/win32/console_win32.h b/ui/win32/console_win32.h
deleted file mode 100644
index 044356fe40..0000000000
--- a/ui/win32/console_win32.h
+++ /dev/null
@@ -1,64 +0,0 @@
-/** @file
- *
- * Console support for MSWindows
- *
- * Wireshark - Network traffic analyzer
- * By Gerald Combs <gerald@wireshark.org>
- * Copyright 2002, Jeffrey C. Foster <jfoste@woodward.com>
- *
- * SPDX-License-Identifier: GPL-2.0-or-later
- */
-
-#ifndef __CONSOLE_WIN32_H__
-#define __CONSOLE_WIN32_H__
-
-#ifdef __cplusplus
-extern "C" {
-#endif /* __cplusplus */
-
-#ifdef _WIN32
-
-/** @file
- * Win32 specific console.
- */
-
-/** Create Windows console.
- *
- */
-void create_console(void);
-
-/** Connect to stdio if available.
- *
- */
-void restore_pipes(void);
-
-/** Destroy Windows console.
- *
- */
-void destroy_console(void);
-
-/** Set console wait. GTK+ only.
- * @param console_wait set/no set console wait
- */
-void set_console_wait(gboolean console_wait);
-/** get console wait
- * @return set/no set console wait
- */
-gboolean get_console_wait(void);
-
-/** Set stdin capture.
- * @param console_wait set/no stdin_capture
- */
-void set_stdin_capture(gboolean set_stdin_capture);
-
-/** get stdin caputre
- * @return set/no set stdin_capture
- */
-gboolean get_stdin_capture(void);
-#endif/* _WIN32 */
-
-#ifdef __cplusplus
-}
-#endif /* __cplusplus */
-
-#endif /* __CONSOLE_WIN32_H__ */