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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
17 * @file util/winproc.c
18 * @brief Functions for MS Windows
23 #include "gnunet_crypto_lib.h"
24 #include "gnunet_common.h"
29 static HINSTANCE hNTDLL, hIphlpapi, hAdvapi, hNetapi;
31 static void *GNWinVEH_handle = NULL;
34 TNtQuerySystemInformation GNNtQuerySystemInformation;
35 TGetIfEntry GNGetIfEntry;
36 TGetIpAddrTable GNGetIpAddrTable;
37 TGetIfTable GNGetIfTable;
38 TOpenSCManager GNOpenSCManager;
39 TCreateService GNCreateService;
40 TCloseServiceHandle GNCloseServiceHandle;
41 TDeleteService GNDeleteService;
42 TRegisterServiceCtrlHandler GNRegisterServiceCtrlHandler;
43 TSetServiceStatus GNSetServiceStatus;
44 TStartServiceCtrlDispatcher GNStartServiceCtrlDispatcher;
45 TControlService GNControlService;
46 TOpenService GNOpenService;
47 TGetBestInterfaceEx GNGetBestInterfaceEx;
48 TGetAdaptersInfo GNGetAdaptersInfo;
49 TNetUserAdd GNNetUserAdd;
50 TNetUserSetInfo GNNetUserSetInfo;
51 TLsaOpenPolicy GNLsaOpenPolicy;
52 TLsaAddAccountRights GNLsaAddAccountRights;
53 TLsaRemoveAccountRights GNLsaRemoveAccountRights;
55 TLookupAccountName GNLookupAccountName;
56 TGetFileSecurity GNGetFileSecurity;
57 TInitializeSecurityDescriptor GNInitializeSecurityDescriptor;
58 TGetSecurityDescriptorDacl GNGetSecurityDescriptorDacl;
59 TGetAclInformation GNGetAclInformation;
60 TInitializeAcl GNInitializeAcl;
64 TAddAccessAllowedAce GNAddAccessAllowedAce;
65 TSetNamedSecurityInfo GNSetNamedSecurityInfo;
67 #define LOG(kind,...) GNUNET_log_from (kind, "util-winproc", __VA_ARGS__)
69 * Log (panic) messages from PlibC
72 plibc_panic (int err, char *msg)
74 LOG (((err == INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR),
80 * Handles exceptions (useful for debugging).
81 * Issues a DebugBreak() call if the process is being debugged (not really
82 * useful - if the process is being debugged, this handler won't be invoked
83 * anyway). If it is not, runs a debugger from GNUNET_DEBUGGER env var,
84 * substituting first %u in it for PID, and the second one for the event,
85 * that should be set once the debugger attaches itself (otherwise the
86 * only way out of WaitForSingleObject() is to time out after 1 minute).
89 GNWinVEH (PEXCEPTION_POINTERS ExceptionInfo)
91 char debugger[MAX_PATH + 1];
92 char *debugger_env = NULL;
93 if (IsDebuggerPresent ())
96 return EXCEPTION_CONTINUE_EXECUTION;
98 debugger_env = getenv ("GNUNET_DEBUGGER");
99 if (debugger_env != NULL)
102 PROCESS_INFORMATION pi;
104 SECURITY_ATTRIBUTES sa;
105 memset (&si, 0, sizeof (si));
107 memset (&pi, 0, sizeof (pi));
108 memset (&sa, 0, sizeof (sa));
109 sa.nLength = sizeof (sa);
110 sa.bInheritHandle = TRUE;
111 event = CreateEvent (&sa, FALSE, FALSE, NULL);
112 snprintf (debugger, MAX_PATH + 1, debugger_env, GetCurrentProcessId (), (uintptr_t) event);
113 debugger[MAX_PATH] = '\0';
114 if (0 != CreateProcessA (NULL, debugger, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi))
116 CloseHandle (pi.hProcess);
117 CloseHandle (pi.hThread);
118 WaitForSingleObject (event, 60000);
120 if (IsDebuggerPresent ())
122 return EXCEPTION_CONTINUE_EXECUTION;
128 return EXCEPTION_CONTINUE_SEARCH;
133 * @brief Initialize PlibC and set up Windows environment
134 * @param logging context, NULL means stderr
135 * @return Error code from winerror.h, ERROR_SUCCESS on success
142 plibc_initialized ();
143 plibc_set_panic_proc (plibc_panic);
144 ret = plibc_init_utf8 ("GNU", PACKAGE, 1);
145 plibc_set_stat_size_size (sizeof (((struct stat *) 0)->st_size));
146 plibc_set_stat_time_size (sizeof (((struct stat *) 0)->st_mtime));
147 /* don't load other DLLs twice */
152 if (GNWinVEH_handle == NULL)
154 GNWinVEH_handle = AddVectoredExceptionHandler (1, &GNWinVEH);
155 if (GNWinVEH_handle == NULL)
157 /* This is bad, but what can we do? */
158 printf ("Failed to set up an exception handler!\n");
163 hNTDLL = LoadLibrary ("ntdll.dll");
165 /* Function to get CPU usage under Win NT */
168 GNNtQuerySystemInformation =
169 (TNtQuerySystemInformation) GetProcAddress (hNTDLL,
170 "NtQuerySystemInformation");
174 GNNtQuerySystemInformation = NULL;
177 /* Functions to get information about a network adapter */
178 hIphlpapi = LoadLibrary ("iphlpapi.dll");
181 GNGetIfEntry = (TGetIfEntry) GetProcAddress (hIphlpapi, "GetIfEntry");
183 (TGetIpAddrTable) GetProcAddress (hIphlpapi, "GetIpAddrTable");
184 GNGetIfTable = (TGetIfTable) GetProcAddress (hIphlpapi, "GetIfTable");
185 GNGetBestInterfaceEx =
186 (TGetBestInterfaceEx) GetProcAddress (hIphlpapi, "GetBestInterfaceEx");
188 (TGetAdaptersInfo) GetProcAddress (hIphlpapi, "GetAdaptersInfo");
193 GNGetIpAddrTable = NULL;
195 GNGetBestInterfaceEx = NULL;
196 GNGetAdaptersInfo = NULL;
199 /* Service & Account functions */
200 hAdvapi = LoadLibrary ("advapi32.dll");
204 (TOpenSCManager) GetProcAddress (hAdvapi, "OpenSCManagerA");
206 (TCreateService) GetProcAddress (hAdvapi, "CreateServiceA");
207 GNCloseServiceHandle =
208 (TCloseServiceHandle) GetProcAddress (hAdvapi, "CloseServiceHandle");
210 (TDeleteService) GetProcAddress (hAdvapi, "DeleteService");
211 GNRegisterServiceCtrlHandler =
212 (TRegisterServiceCtrlHandler) GetProcAddress (hAdvapi,
213 "RegisterServiceCtrlHandlerA");
215 (TSetServiceStatus) GetProcAddress (hAdvapi, "SetServiceStatus");
216 GNStartServiceCtrlDispatcher =
217 (TStartServiceCtrlDispatcher) GetProcAddress (hAdvapi,
218 "StartServiceCtrlDispatcherA");
220 (TControlService) GetProcAddress (hAdvapi, "ControlService");
221 GNOpenService = (TOpenService) GetProcAddress (hAdvapi, "OpenServiceA");
224 (TLsaOpenPolicy) GetProcAddress (hAdvapi, "LsaOpenPolicy");
225 GNLsaAddAccountRights =
226 (TLsaAddAccountRights) GetProcAddress (hAdvapi, "LsaAddAccountRights");
227 GNLsaRemoveAccountRights =
228 (TLsaRemoveAccountRights) GetProcAddress (hAdvapi,
229 "LsaRemoveAccountRights");
230 GNLsaClose = (TLsaClose) GetProcAddress (hAdvapi, "LsaClose");
231 GNLookupAccountName =
232 (TLookupAccountName) GetProcAddress (hAdvapi, "LookupAccountNameA");
235 (TGetFileSecurity) GetProcAddress (hAdvapi, "GetFileSecurityA");
236 GNInitializeSecurityDescriptor =
237 (TInitializeSecurityDescriptor) GetProcAddress (hAdvapi,
238 "InitializeSecurityDescriptor");
239 GNGetSecurityDescriptorDacl =
240 (TGetSecurityDescriptorDacl) GetProcAddress (hAdvapi,
241 "GetSecurityDescriptorDacl");
242 GNGetAclInformation =
243 (TGetAclInformation) GetProcAddress (hAdvapi, "GetAclInformation");
245 (TInitializeAcl) GetProcAddress (hAdvapi, "InitializeAcl");
246 GNGetAce = (TGetAce) GetProcAddress (hAdvapi, "GetAce");
247 GNEqualSid = (TEqualSid) GetProcAddress (hAdvapi, "EqualSid");
248 GNAddAce = (TAddAce) GetProcAddress (hAdvapi, "AddAce");
249 GNAddAccessAllowedAce =
250 (TAddAccessAllowedAce) GetProcAddress (hAdvapi, "AddAccessAllowedAce");
251 GNSetNamedSecurityInfo =
252 (TSetNamedSecurityInfo) GetProcAddress (hAdvapi,
253 "SetNamedSecurityInfoA");
257 GNOpenSCManager = NULL;
258 GNCreateService = NULL;
259 GNCloseServiceHandle = NULL;
260 GNDeleteService = NULL;
261 GNRegisterServiceCtrlHandler = NULL;
262 GNSetServiceStatus = NULL;
263 GNStartServiceCtrlDispatcher = NULL;
264 GNControlService = NULL;
265 GNOpenService = NULL;
267 GNLsaOpenPolicy = NULL;
268 GNLsaAddAccountRights = NULL;
269 GNLsaRemoveAccountRights = NULL;
271 GNLookupAccountName = NULL;
273 GNGetFileSecurity = NULL;
274 GNInitializeSecurityDescriptor = NULL;
275 GNGetSecurityDescriptorDacl = NULL;
276 GNGetAclInformation = NULL;
277 GNInitializeAcl = NULL;
281 GNAddAccessAllowedAce = NULL;
282 GNSetNamedSecurityInfo = NULL;
285 /* Account function */
286 hNetapi = LoadLibrary ("netapi32.dll");
289 GNNetUserAdd = (TNetUserAdd) GetProcAddress (hNetapi, "NetUserAdd");
291 (TNetUserSetInfo) GetProcAddress (hNetapi, "NetUserSetInfo");
296 GNNetUserSetInfo = NULL;
303 * Clean up Windows environment
311 if (GNWinVEH_handle != NULL)
313 RemoveVectoredExceptionHandler (GNWinVEH_handle);
314 GNWinVEH_handle = NULL;
318 FreeLibrary (hNTDLL);
319 FreeLibrary (hIphlpapi);
320 FreeLibrary (hAdvapi);
321 FreeLibrary (hNetapi);
330 atoll (const char *nptr)