2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file util/winproc.c
23 * @brief Functions for MS Windows
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_common.h"
34 static HINSTANCE hNTDLL, hIphlpapi, hAdvapi, hNetapi;
36 static void *GNWinVEH_handle = NULL;
39 TNtQuerySystemInformation GNNtQuerySystemInformation;
40 TGetIfEntry GNGetIfEntry;
41 TGetIpAddrTable GNGetIpAddrTable;
42 TGetIfTable GNGetIfTable;
43 TOpenSCManager GNOpenSCManager;
44 TCreateService GNCreateService;
45 TCloseServiceHandle GNCloseServiceHandle;
46 TDeleteService GNDeleteService;
47 TRegisterServiceCtrlHandler GNRegisterServiceCtrlHandler;
48 TSetServiceStatus GNSetServiceStatus;
49 TStartServiceCtrlDispatcher GNStartServiceCtrlDispatcher;
50 TControlService GNControlService;
51 TOpenService GNOpenService;
52 TGetBestInterfaceEx GNGetBestInterfaceEx;
53 TGetAdaptersInfo GNGetAdaptersInfo;
54 TNetUserAdd GNNetUserAdd;
55 TNetUserSetInfo GNNetUserSetInfo;
56 TLsaOpenPolicy GNLsaOpenPolicy;
57 TLsaAddAccountRights GNLsaAddAccountRights;
58 TLsaRemoveAccountRights GNLsaRemoveAccountRights;
60 TLookupAccountName GNLookupAccountName;
61 TGetFileSecurity GNGetFileSecurity;
62 TInitializeSecurityDescriptor GNInitializeSecurityDescriptor;
63 TGetSecurityDescriptorDacl GNGetSecurityDescriptorDacl;
64 TGetAclInformation GNGetAclInformation;
65 TInitializeAcl GNInitializeAcl;
69 TAddAccessAllowedAce GNAddAccessAllowedAce;
70 TSetNamedSecurityInfo GNSetNamedSecurityInfo;
72 #define LOG(kind,...) GNUNET_log_from (kind, "util-winproc", __VA_ARGS__)
74 * Log (panic) messages from PlibC
77 plibc_panic (int err, char *msg)
79 LOG (((err == INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR),
85 * Handles exceptions (useful for debugging).
86 * Issues a DebugBreak() call if the process is being debugged (not really
87 * useful - if the process is being debugged, this handler won't be invoked
88 * anyway). If it is not, runs a debugger from GNUNET_DEBUGGER env var,
89 * substituting first %u in it for PID, and the second one for the event,
90 * that should be set once the debugger attaches itself (otherwise the
91 * only way out of WaitForSingleObject() is to time out after 1 minute).
94 GNWinVEH (PEXCEPTION_POINTERS ExceptionInfo)
96 char debugger[MAX_PATH + 1];
97 char *debugger_env = NULL;
98 if (IsDebuggerPresent ())
101 return EXCEPTION_CONTINUE_EXECUTION;
103 debugger_env = getenv ("GNUNET_DEBUGGER");
104 if (debugger_env != NULL)
107 PROCESS_INFORMATION pi;
109 SECURITY_ATTRIBUTES sa;
110 memset (&si, 0, sizeof (si));
112 memset (&pi, 0, sizeof (pi));
113 memset (&sa, 0, sizeof (sa));
114 sa.nLength = sizeof (sa);
115 sa.bInheritHandle = TRUE;
116 event = CreateEvent (&sa, FALSE, FALSE, NULL);
117 snprintf (debugger, MAX_PATH + 1, debugger_env, GetCurrentProcessId (), (uintptr_t) event);
118 debugger[MAX_PATH] = '\0';
119 if (0 != CreateProcessA (NULL, debugger, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi))
121 CloseHandle (pi.hProcess);
122 CloseHandle (pi.hThread);
123 WaitForSingleObject (event, 60000);
125 if (IsDebuggerPresent ())
127 return EXCEPTION_CONTINUE_EXECUTION;
133 return EXCEPTION_CONTINUE_SEARCH;
138 * @brief Initialize PlibC and set up Windows environment
139 * @param logging context, NULL means stderr
140 * @return Error code from winerror.h, ERROR_SUCCESS on success
147 plibc_initialized ();
148 plibc_set_panic_proc (plibc_panic);
149 ret = plibc_init_utf8 ("GNU", PACKAGE, 1);
150 plibc_set_stat_size_size (sizeof (((struct stat *) 0)->st_size));
151 plibc_set_stat_time_size (sizeof (((struct stat *) 0)->st_mtime));
152 /* don't load other DLLs twice */
157 if (GNWinVEH_handle == NULL)
159 GNWinVEH_handle = AddVectoredExceptionHandler (1, &GNWinVEH);
160 if (GNWinVEH_handle == NULL)
162 /* This is bad, but what can we do? */
163 printf ("Failed to set up an exception handler!\n");
168 hNTDLL = LoadLibrary ("ntdll.dll");
170 /* Function to get CPU usage under Win NT */
173 GNNtQuerySystemInformation =
174 (TNtQuerySystemInformation) GetProcAddress (hNTDLL,
175 "NtQuerySystemInformation");
179 GNNtQuerySystemInformation = NULL;
182 /* Functions to get information about a network adapter */
183 hIphlpapi = LoadLibrary ("iphlpapi.dll");
186 GNGetIfEntry = (TGetIfEntry) GetProcAddress (hIphlpapi, "GetIfEntry");
188 (TGetIpAddrTable) GetProcAddress (hIphlpapi, "GetIpAddrTable");
189 GNGetIfTable = (TGetIfTable) GetProcAddress (hIphlpapi, "GetIfTable");
190 GNGetBestInterfaceEx =
191 (TGetBestInterfaceEx) GetProcAddress (hIphlpapi, "GetBestInterfaceEx");
193 (TGetAdaptersInfo) GetProcAddress (hIphlpapi, "GetAdaptersInfo");
198 GNGetIpAddrTable = NULL;
200 GNGetBestInterfaceEx = NULL;
201 GNGetAdaptersInfo = NULL;
204 /* Service & Account functions */
205 hAdvapi = LoadLibrary ("advapi32.dll");
209 (TOpenSCManager) GetProcAddress (hAdvapi, "OpenSCManagerA");
211 (TCreateService) GetProcAddress (hAdvapi, "CreateServiceA");
212 GNCloseServiceHandle =
213 (TCloseServiceHandle) GetProcAddress (hAdvapi, "CloseServiceHandle");
215 (TDeleteService) GetProcAddress (hAdvapi, "DeleteService");
216 GNRegisterServiceCtrlHandler =
217 (TRegisterServiceCtrlHandler) GetProcAddress (hAdvapi,
218 "RegisterServiceCtrlHandlerA");
220 (TSetServiceStatus) GetProcAddress (hAdvapi, "SetServiceStatus");
221 GNStartServiceCtrlDispatcher =
222 (TStartServiceCtrlDispatcher) GetProcAddress (hAdvapi,
223 "StartServiceCtrlDispatcherA");
225 (TControlService) GetProcAddress (hAdvapi, "ControlService");
226 GNOpenService = (TOpenService) GetProcAddress (hAdvapi, "OpenServiceA");
229 (TLsaOpenPolicy) GetProcAddress (hAdvapi, "LsaOpenPolicy");
230 GNLsaAddAccountRights =
231 (TLsaAddAccountRights) GetProcAddress (hAdvapi, "LsaAddAccountRights");
232 GNLsaRemoveAccountRights =
233 (TLsaRemoveAccountRights) GetProcAddress (hAdvapi,
234 "LsaRemoveAccountRights");
235 GNLsaClose = (TLsaClose) GetProcAddress (hAdvapi, "LsaClose");
236 GNLookupAccountName =
237 (TLookupAccountName) GetProcAddress (hAdvapi, "LookupAccountNameA");
240 (TGetFileSecurity) GetProcAddress (hAdvapi, "GetFileSecurityA");
241 GNInitializeSecurityDescriptor =
242 (TInitializeSecurityDescriptor) GetProcAddress (hAdvapi,
243 "InitializeSecurityDescriptor");
244 GNGetSecurityDescriptorDacl =
245 (TGetSecurityDescriptorDacl) GetProcAddress (hAdvapi,
246 "GetSecurityDescriptorDacl");
247 GNGetAclInformation =
248 (TGetAclInformation) GetProcAddress (hAdvapi, "GetAclInformation");
250 (TInitializeAcl) GetProcAddress (hAdvapi, "InitializeAcl");
251 GNGetAce = (TGetAce) GetProcAddress (hAdvapi, "GetAce");
252 GNEqualSid = (TEqualSid) GetProcAddress (hAdvapi, "EqualSid");
253 GNAddAce = (TAddAce) GetProcAddress (hAdvapi, "AddAce");
254 GNAddAccessAllowedAce =
255 (TAddAccessAllowedAce) GetProcAddress (hAdvapi, "AddAccessAllowedAce");
256 GNSetNamedSecurityInfo =
257 (TSetNamedSecurityInfo) GetProcAddress (hAdvapi,
258 "SetNamedSecurityInfoA");
262 GNOpenSCManager = NULL;
263 GNCreateService = NULL;
264 GNCloseServiceHandle = NULL;
265 GNDeleteService = NULL;
266 GNRegisterServiceCtrlHandler = NULL;
267 GNSetServiceStatus = NULL;
268 GNStartServiceCtrlDispatcher = NULL;
269 GNControlService = NULL;
270 GNOpenService = NULL;
272 GNLsaOpenPolicy = NULL;
273 GNLsaAddAccountRights = NULL;
274 GNLsaRemoveAccountRights = NULL;
276 GNLookupAccountName = NULL;
278 GNGetFileSecurity = NULL;
279 GNInitializeSecurityDescriptor = NULL;
280 GNGetSecurityDescriptorDacl = NULL;
281 GNGetAclInformation = NULL;
282 GNInitializeAcl = NULL;
286 GNAddAccessAllowedAce = NULL;
287 GNSetNamedSecurityInfo = NULL;
290 /* Account function */
291 hNetapi = LoadLibrary ("netapi32.dll");
294 GNNetUserAdd = (TNetUserAdd) GetProcAddress (hNetapi, "NetUserAdd");
296 (TNetUserSetInfo) GetProcAddress (hNetapi, "NetUserSetInfo");
301 GNNetUserSetInfo = NULL;
308 * Clean up Windows environment
316 if (GNWinVEH_handle != NULL)
318 RemoveVectoredExceptionHandler (GNWinVEH_handle);
319 GNWinVEH_handle = NULL;
323 FreeLibrary (hNTDLL);
324 FreeLibrary (hIphlpapi);
325 FreeLibrary (hAdvapi);
326 FreeLibrary (hNetapi);
335 atoll (const char *nptr)