2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/winproc.c
23 * @brief Functions for MS Windows
28 #include "gnunet_common.h"
33 static HINSTANCE hNTDLL, hIphlpapi, hAdvapi, hNetapi;
35 static void *GNWinVEH_handle = NULL;
38 TNtQuerySystemInformation GNNtQuerySystemInformation;
39 TGetIfEntry GNGetIfEntry;
40 TGetIpAddrTable GNGetIpAddrTable;
41 TGetIfTable GNGetIfTable;
42 TOpenSCManager GNOpenSCManager;
43 TCreateService GNCreateService;
44 TCloseServiceHandle GNCloseServiceHandle;
45 TDeleteService GNDeleteService;
46 TRegisterServiceCtrlHandler GNRegisterServiceCtrlHandler;
47 TSetServiceStatus GNSetServiceStatus;
48 TStartServiceCtrlDispatcher GNStartServiceCtrlDispatcher;
49 TControlService GNControlService;
50 TOpenService GNOpenService;
51 TGetBestInterfaceEx GNGetBestInterfaceEx;
52 TGetAdaptersInfo GNGetAdaptersInfo;
53 TNetUserAdd GNNetUserAdd;
54 TNetUserSetInfo GNNetUserSetInfo;
55 TLsaOpenPolicy GNLsaOpenPolicy;
56 TLsaAddAccountRights GNLsaAddAccountRights;
57 TLsaRemoveAccountRights GNLsaRemoveAccountRights;
59 TLookupAccountName GNLookupAccountName;
60 TGetFileSecurity GNGetFileSecurity;
61 TInitializeSecurityDescriptor GNInitializeSecurityDescriptor;
62 TGetSecurityDescriptorDacl GNGetSecurityDescriptorDacl;
63 TGetAclInformation GNGetAclInformation;
64 TInitializeAcl GNInitializeAcl;
68 TAddAccessAllowedAce GNAddAccessAllowedAce;
69 TSetNamedSecurityInfo GNSetNamedSecurityInfo;
71 #define LOG(kind,...) GNUNET_log_from (kind, "winproc", __VA_ARGS__)
73 * Log (panic) messages from PlibC
76 plibc_panic (int err, char *msg)
78 LOG (((err == INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR),
84 * Handles exceptions (useful for debugging).
85 * Issues a DebugBreak() call if the process is being debugged (not really
86 * useful - if the process is being debugged, this handler won't be invoked
87 * anyway). If it is not, runs a debugger from GNUNET_DEBUGGER env var,
88 * substituting first %u in it for PID, and the second one for the event,
89 * that should be set once the debugger attaches itself (otherwise the
90 * only way out of WaitForSingleObject() is to time out after 1 minute).
93 GNWinVEH (PEXCEPTION_POINTERS ExceptionInfo)
95 char debugger[MAX_PATH + 1];
96 char *debugger_env = NULL;
97 if (IsDebuggerPresent ())
100 return EXCEPTION_CONTINUE_EXECUTION;
102 debugger_env = getenv ("GNUNET_DEBUGGER");
103 if (debugger_env != NULL)
106 PROCESS_INFORMATION pi;
108 SECURITY_ATTRIBUTES sa;
109 memset (&si, 0, sizeof (si));
111 memset (&pi, 0, sizeof (pi));
112 memset (&sa, 0, sizeof (sa));
113 sa.nLength = sizeof (sa);
114 sa.bInheritHandle = TRUE;
115 event = CreateEvent (&sa, FALSE, FALSE, NULL);
116 snprintf (debugger, MAX_PATH + 1, debugger_env, GetCurrentProcessId (), (uintptr_t) event);
117 debugger[MAX_PATH] = '\0';
118 if (0 != CreateProcessA (NULL, debugger, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi))
120 CloseHandle (pi.hProcess);
121 CloseHandle (pi.hThread);
122 WaitForSingleObject (event, 60000);
124 if (IsDebuggerPresent ())
126 return EXCEPTION_CONTINUE_EXECUTION;
132 return EXCEPTION_CONTINUE_SEARCH;
137 * @brief Initialize PlibC and set up Windows environment
138 * @param logging context, NULL means stderr
139 * @return Error code from winerror.h, ERROR_SUCCESS on success
146 plibc_initialized ();
147 plibc_set_panic_proc (plibc_panic);
148 ret = plibc_init_utf8 ("GNU", PACKAGE, 1);
150 /* don't load other DLLs twice */
155 if (GNWinVEH_handle == NULL)
157 GNWinVEH_handle = AddVectoredExceptionHandler (1, &GNWinVEH);
158 if (GNWinVEH_handle == NULL)
160 /* This is bad, but what can we do? */
161 printf ("Failed to set up an exception handler!\n");
166 hNTDLL = LoadLibrary ("ntdll.dll");
168 /* Function to get CPU usage under Win NT */
171 GNNtQuerySystemInformation =
172 (TNtQuerySystemInformation) GetProcAddress (hNTDLL,
173 "NtQuerySystemInformation");
177 GNNtQuerySystemInformation = NULL;
180 /* Functions to get information about a network adapter */
181 hIphlpapi = LoadLibrary ("iphlpapi.dll");
184 GNGetIfEntry = (TGetIfEntry) GetProcAddress (hIphlpapi, "GetIfEntry");
186 (TGetIpAddrTable) GetProcAddress (hIphlpapi, "GetIpAddrTable");
187 GNGetIfTable = (TGetIfTable) GetProcAddress (hIphlpapi, "GetIfTable");
188 GNGetBestInterfaceEx =
189 (TGetBestInterfaceEx) GetProcAddress (hIphlpapi, "GetBestInterfaceEx");
191 (TGetAdaptersInfo) GetProcAddress (hIphlpapi, "GetAdaptersInfo");
196 GNGetIpAddrTable = NULL;
198 GNGetBestInterfaceEx = NULL;
199 GNGetAdaptersInfo = NULL;
202 /* Service & Account functions */
203 hAdvapi = LoadLibrary ("advapi32.dll");
207 (TOpenSCManager) GetProcAddress (hAdvapi, "OpenSCManagerA");
209 (TCreateService) GetProcAddress (hAdvapi, "CreateServiceA");
210 GNCloseServiceHandle =
211 (TCloseServiceHandle) GetProcAddress (hAdvapi, "CloseServiceHandle");
213 (TDeleteService) GetProcAddress (hAdvapi, "DeleteService");
214 GNRegisterServiceCtrlHandler =
215 (TRegisterServiceCtrlHandler) GetProcAddress (hAdvapi,
216 "RegisterServiceCtrlHandlerA");
218 (TSetServiceStatus) GetProcAddress (hAdvapi, "SetServiceStatus");
219 GNStartServiceCtrlDispatcher =
220 (TStartServiceCtrlDispatcher) GetProcAddress (hAdvapi,
221 "StartServiceCtrlDispatcherA");
223 (TControlService) GetProcAddress (hAdvapi, "ControlService");
224 GNOpenService = (TOpenService) GetProcAddress (hAdvapi, "OpenServiceA");
227 (TLsaOpenPolicy) GetProcAddress (hAdvapi, "LsaOpenPolicy");
228 GNLsaAddAccountRights =
229 (TLsaAddAccountRights) GetProcAddress (hAdvapi, "LsaAddAccountRights");
230 GNLsaRemoveAccountRights =
231 (TLsaRemoveAccountRights) GetProcAddress (hAdvapi,
232 "LsaRemoveAccountRights");
233 GNLsaClose = (TLsaClose) GetProcAddress (hAdvapi, "LsaClose");
234 GNLookupAccountName =
235 (TLookupAccountName) GetProcAddress (hAdvapi, "LookupAccountNameA");
238 (TGetFileSecurity) GetProcAddress (hAdvapi, "GetFileSecurityA");
239 GNInitializeSecurityDescriptor =
240 (TInitializeSecurityDescriptor) GetProcAddress (hAdvapi,
241 "InitializeSecurityDescriptor");
242 GNGetSecurityDescriptorDacl =
243 (TGetSecurityDescriptorDacl) GetProcAddress (hAdvapi,
244 "GetSecurityDescriptorDacl");
245 GNGetAclInformation =
246 (TGetAclInformation) GetProcAddress (hAdvapi, "GetAclInformation");
248 (TInitializeAcl) GetProcAddress (hAdvapi, "InitializeAcl");
249 GNGetAce = (TGetAce) GetProcAddress (hAdvapi, "GetAce");
250 GNEqualSid = (TEqualSid) GetProcAddress (hAdvapi, "EqualSid");
251 GNAddAce = (TAddAce) GetProcAddress (hAdvapi, "AddAce");
252 GNAddAccessAllowedAce =
253 (TAddAccessAllowedAce) GetProcAddress (hAdvapi, "AddAccessAllowedAce");
254 GNSetNamedSecurityInfo =
255 (TSetNamedSecurityInfo) GetProcAddress (hAdvapi,
256 "SetNamedSecurityInfoA");
260 GNOpenSCManager = NULL;
261 GNCreateService = NULL;
262 GNCloseServiceHandle = NULL;
263 GNDeleteService = NULL;
264 GNRegisterServiceCtrlHandler = NULL;
265 GNSetServiceStatus = NULL;
266 GNStartServiceCtrlDispatcher = NULL;
267 GNControlService = NULL;
268 GNOpenService = NULL;
270 GNLsaOpenPolicy = NULL;
271 GNLsaAddAccountRights = NULL;
272 GNLsaRemoveAccountRights = NULL;
274 GNLookupAccountName = NULL;
276 GNGetFileSecurity = NULL;
277 GNInitializeSecurityDescriptor = NULL;
278 GNGetSecurityDescriptorDacl = NULL;
279 GNGetAclInformation = NULL;
280 GNInitializeAcl = NULL;
284 GNAddAccessAllowedAce = NULL;
285 GNSetNamedSecurityInfo = NULL;
288 /* Account function */
289 hNetapi = LoadLibrary ("netapi32.dll");
292 GNNetUserAdd = (TNetUserAdd) GetProcAddress (hNetapi, "NetUserAdd");
294 (TNetUserSetInfo) GetProcAddress (hNetapi, "NetUserSetInfo");
299 GNNetUserSetInfo = NULL;
306 * Clean up Windows environment
314 if (GNWinVEH_handle != NULL)
316 RemoveVectoredExceptionHandler (GNWinVEH_handle);
317 GNWinVEH_handle = NULL;
321 FreeLibrary (hNTDLL);
322 FreeLibrary (hIphlpapi);
323 FreeLibrary (hAdvapi);
324 FreeLibrary (hNetapi);
333 atoll (const char *nptr)