wmem: allow wmem_destroy_list to ignore a NULL list.
[metze/wireshark/wip.git] / wsutil / win32-utils.c
1 /* win32-utils.c
2  * Win32 utility routines
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 2006 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include <config.h>
12
13 #include "win32-utils.h"
14
15 #include <log.h>
16
17 #include <tchar.h>
18 #include <VersionHelpers.h>
19
20 /* Quote the argument element if necessary, so that it will get
21  * reconstructed correctly in the C runtime startup code.  Note that
22  * the unquoting algorithm in the C runtime is really weird, and
23  * rather different than what Unix shells do. See stdargv.c in the C
24  * runtime sources (in the Platform SDK, in src/crt).
25  *
26  * Stolen from GLib's protect_argv(), an internal routine that quotes
27  * string in an argument list so that they arguments will be handled
28  * correctly in the command-line string passed to CreateProcess()
29  * if that string is constructed by gluing those strings together.
30  */
31 gchar *
32 protect_arg (const gchar *argv)
33 {
34     gchar *new_arg;
35     const gchar *p = argv;
36     gchar *q;
37     gint len = 0;
38     gboolean need_dblquotes = FALSE;
39
40     while (*p) {
41         if (*p == ' ' || *p == '\t')
42             need_dblquotes = TRUE;
43         else if (*p == '"')
44             len++;
45         else if (*p == '\\') {
46             const gchar *pp = p;
47
48             while (*pp && *pp == '\\')
49                 pp++;
50             if (*pp == '"')
51                 len++;
52         }
53         len++;
54         p++;
55     }
56
57     q = new_arg = g_malloc (len + need_dblquotes*2 + 1);
58     p = argv;
59
60     if (need_dblquotes)
61         *q++ = '"';
62
63     while (*p) {
64         if (*p == '"')
65             *q++ = '\\';
66         else if (*p == '\\') {
67             const gchar *pp = p;
68
69             while (*pp && *pp == '\\')
70                 pp++;
71             if (*pp == '"')
72                 *q++ = '\\';
73         }
74         *q++ = *p;
75         p++;
76     }
77
78     if (need_dblquotes)
79         *q++ = '"';
80     *q++ = '\0';
81
82     return new_arg;
83 }
84
85 /*
86  * Generate a string for a Win32 error.
87  */
88 #define ERRBUF_SIZE    1024
89 const char *
90 win32strerror(DWORD error)
91 {
92     static char errbuf[ERRBUF_SIZE+1];
93     size_t errlen;
94     char *p;
95
96     FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
97                    NULL, error, 0, errbuf, ERRBUF_SIZE, NULL);
98
99     /*
100      * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
101      * message.  Get rid of it.
102      */
103     errlen = strlen(errbuf);
104     if (errlen >= 2) {
105         errbuf[errlen - 1] = '\0';
106         errbuf[errlen - 2] = '\0';
107     }
108     p = strchr(errbuf, '\0');
109     g_snprintf(p, (gulong)(sizeof errbuf - (p-errbuf)), " (%lu)", error);
110     return errbuf;
111 }
112
113 /*
114  * Generate a string for a Win32 exception code.
115  */
116 const char *
117 win32strexception(DWORD exception)
118 {
119     static char errbuf[ERRBUF_SIZE+1];
120     static const struct exception_msg {
121         int code;
122         char *msg;
123     } exceptions[] = {
124         { EXCEPTION_ACCESS_VIOLATION, "Access violation" },
125         { EXCEPTION_ARRAY_BOUNDS_EXCEEDED, "Array bounds exceeded" },
126         { EXCEPTION_BREAKPOINT, "Breakpoint" },
127         { EXCEPTION_DATATYPE_MISALIGNMENT, "Data type misalignment" },
128         { EXCEPTION_FLT_DENORMAL_OPERAND, "Denormal floating-point operand" },
129         { EXCEPTION_FLT_DIVIDE_BY_ZERO, "Floating-point divide by zero" },
130         { EXCEPTION_FLT_INEXACT_RESULT, "Floating-point inexact result" },
131         { EXCEPTION_FLT_INVALID_OPERATION, "Invalid floating-point operation" },
132         { EXCEPTION_FLT_OVERFLOW, "Floating-point overflow" },
133         { EXCEPTION_FLT_STACK_CHECK, "Floating-point stack check" },
134         { EXCEPTION_FLT_UNDERFLOW, "Floating-point underflow" },
135         { EXCEPTION_GUARD_PAGE, "Guard page violation" },
136         { EXCEPTION_ILLEGAL_INSTRUCTION, "Illegal instruction" },
137         { EXCEPTION_IN_PAGE_ERROR, "Page-in error" },
138         { EXCEPTION_INT_DIVIDE_BY_ZERO, "Integer divide by zero" },
139         { EXCEPTION_INT_OVERFLOW, "Integer overflow" },
140         { EXCEPTION_INVALID_DISPOSITION, "Invalid disposition" },
141         { EXCEPTION_INVALID_HANDLE, "Invalid handle" },
142         { EXCEPTION_NONCONTINUABLE_EXCEPTION, "Non-continuable exception" },
143         { EXCEPTION_PRIV_INSTRUCTION, "Privileged instruction" },
144         { EXCEPTION_SINGLE_STEP, "Single-step complete" },
145         { EXCEPTION_STACK_OVERFLOW, "Stack overflow" },
146         { 0, NULL }
147     };
148 #define N_EXCEPTIONS    (sizeof exceptions / sizeof exceptions[0])
149     int i;
150
151     for (i = 0; i < N_EXCEPTIONS; i++) {
152         if (exceptions[i].code == exception)
153             return exceptions[i].msg;
154     }
155     g_snprintf(errbuf, (gulong)sizeof errbuf, "Exception 0x%08x", exception);
156     return errbuf;
157 }
158
159 // This appears to be the closest equivalent to SIGPIPE on Windows.
160 // https://blogs.msdn.microsoft.com/oldnewthing/20131209-00/?p=2433
161 // https://stackoverflow.com/a/53214/82195
162
163 static void win32_kill_child_on_exit(HANDLE child_handle) {
164     static HANDLE cjo_handle = NULL;
165     if (!cjo_handle) {
166         cjo_handle = CreateJobObject(NULL, _T("Local\\Wireshark child process cleanup"));
167
168         if (!cjo_handle) {
169             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not create child cleanup job object: %s",
170                 win32strerror(GetLastError()));
171             return;
172         }
173
174         JOBOBJECT_EXTENDED_LIMIT_INFORMATION cjo_jel_info = { 0 };
175         cjo_jel_info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
176         BOOL sijo_ret = SetInformationJobObject(cjo_handle, JobObjectExtendedLimitInformation,
177             &cjo_jel_info, sizeof(cjo_jel_info));
178         if (!sijo_ret) {
179             g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not set child cleanup limits: %s",
180                 win32strerror(GetLastError()));
181         }
182     }
183
184     BOOL aptjo_ret = AssignProcessToJobObject(cjo_handle, child_handle);
185     if (!aptjo_ret) {
186         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "Could not assign child cleanup process: %s",
187             win32strerror(GetLastError()));
188     }
189 }
190
191 BOOL win32_create_process(const char *application_name, const char *command_line, LPSECURITY_ATTRIBUTES process_attributes, LPSECURITY_ATTRIBUTES thread_attributes, BOOL inherit_handles, DWORD creation_flags, LPVOID environment, const char *current_directory, LPSTARTUPINFO startup_info, LPPROCESS_INFORMATION process_information)
192 {
193     gunichar2 *wappname = NULL, *wcurrentdirectory = NULL;
194     gunichar2 *wcommandline = g_utf8_to_utf16(command_line, -1, NULL, NULL, NULL);
195     // CREATE_SUSPENDED: Suspend the child so that we can cleanly call
196     //     AssignProcessToJobObject.
197     DWORD wcreationflags = creation_flags|CREATE_SUSPENDED;
198     // CREATE_BREAKAWAY_FROM_JOB: The main application might be associated with a job,
199     //     e.g. if we're running under "Run As", ConEmu, or Visual Studio. On Windows
200     //     <= 7 our child process needs to break away from it so that we can cleanly
201     //     call AssignProcessToJobObject on *our* job.
202     //     Windows >= 8 supports nested jobs so this isn't neccessary there.
203     //     https://blogs.msdn.microsoft.com/winsdk/2014/09/22/job-object-insanity/
204     //
205     if (! IsWindowsVersionOrGreater(6, 2, 0)) { // Windows 8
206         wcreationflags |= CREATE_BREAKAWAY_FROM_JOB;
207     }
208
209     if (application_name) {
210         wappname = g_utf8_to_utf16(application_name, -1, NULL, NULL, NULL);
211     }
212     if (current_directory) {
213         wcurrentdirectory = g_utf8_to_utf16(current_directory, -1, NULL, NULL, NULL);
214     }
215     BOOL cp_res = CreateProcess(wappname, wcommandline, process_attributes, thread_attributes,
216         inherit_handles, wcreationflags, environment, wcurrentdirectory, startup_info,
217         process_information);
218     if (cp_res) {
219         win32_kill_child_on_exit(process_information->hProcess);
220         ResumeThread(process_information->hThread);
221     }
222     // XXX Else try again if CREATE_BREAKAWAY_FROM_JOB and GetLastError() == ERROR_ACCESS_DENIED?
223
224     g_free(wappname);
225     g_free(wcommandline);
226     g_free(wcurrentdirectory);
227     return cp_res;
228 }
229
230 /*
231  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
232  *
233  * Local Variables:
234  * c-basic-offset: 4
235  * tab-width: 8
236  * indent-tabs-mode: nil
237  * End:
238  *
239  * ex: set shiftwidth=4 tabstop=8 expandtab:
240  * :indentSize=4:tabSize=8:noTabs=true:
241  */