- test for external iterator
[oweals/gnunet.git] / src / util / winproc.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file util/winproc.c
23  * @brief Functions for MS Windows
24  * @author Nils Durner
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29
30
31 #ifdef MINGW
32
33 static HINSTANCE hNTDLL, hIphlpapi, hAdvapi, hNetapi;
34 #ifdef W32_VEH
35 static void *GNWinVEH_handle = NULL;
36 #endif
37
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;
58 TLsaClose GNLsaClose;
59 TLookupAccountName GNLookupAccountName;
60 TGetFileSecurity GNGetFileSecurity;
61 TInitializeSecurityDescriptor GNInitializeSecurityDescriptor;
62 TGetSecurityDescriptorDacl GNGetSecurityDescriptorDacl;
63 TGetAclInformation GNGetAclInformation;
64 TInitializeAcl GNInitializeAcl;
65 TGetAce GNGetAce;
66 TEqualSid GNEqualSid;
67 TAddAce GNAddAce;
68 TAddAccessAllowedAce GNAddAccessAllowedAce;
69 TSetNamedSecurityInfo GNSetNamedSecurityInfo;
70
71 #define LOG(kind,...) GNUNET_log_from (kind, "winproc", __VA_ARGS__)
72 /**
73  * Log (panic) messages from PlibC
74  */
75 void
76 plibc_panic (int err, char *msg)
77 {
78   LOG (((err == INT_MAX) ? GNUNET_ERROR_TYPE_DEBUG : GNUNET_ERROR_TYPE_ERROR),
79        "%s", msg);
80 }
81
82 #ifdef W32_VEH
83 /**
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).
91  */
92 LONG __stdcall
93 GNWinVEH (PEXCEPTION_POINTERS ExceptionInfo)
94 {
95   char debugger[MAX_PATH + 1];
96   char *debugger_env = NULL;
97   if (IsDebuggerPresent ())
98   {
99     DebugBreak ();
100     return EXCEPTION_CONTINUE_EXECUTION;
101   }
102   debugger_env = getenv ("GNUNET_DEBUGGER");
103   if (debugger_env != NULL)
104   {
105     STARTUPINFO si;
106     PROCESS_INFORMATION pi;
107     HANDLE event;
108     SECURITY_ATTRIBUTES sa;
109     memset (&si, 0, sizeof (si));
110     si.cb = 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))
119     {
120       CloseHandle (pi.hProcess);
121       CloseHandle (pi.hThread);
122       WaitForSingleObject (event, 60000);
123       CloseHandle (event);
124       if (IsDebuggerPresent ())
125       {
126         return EXCEPTION_CONTINUE_EXECUTION;
127       }
128     }
129     else
130       CloseHandle (event);
131   }
132   return EXCEPTION_CONTINUE_SEARCH;
133 }
134 #endif
135
136 /**
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
140 */
141 int
142 GNInitWinEnv ()
143 {
144   int ret;
145
146   plibc_initialized ();
147   plibc_set_panic_proc (plibc_panic);
148   ret = plibc_init_utf8 ("GNU", PACKAGE, 1);
149
150   /* don't load other DLLs twice */
151   if (hNTDLL)
152     return ret;
153
154 #ifdef W32_VEH
155   if (GNWinVEH_handle == NULL)
156   {
157     GNWinVEH_handle = AddVectoredExceptionHandler (1, &GNWinVEH);
158     if (GNWinVEH_handle == NULL)
159     {
160       /* This is bad, but what can we do? */
161       printf ("Failed to set up an exception handler!\n");
162     }
163   }
164 #endif
165
166   hNTDLL = LoadLibrary ("ntdll.dll");
167
168   /* Function to get CPU usage under Win NT */
169   if (hNTDLL)
170   {
171     GNNtQuerySystemInformation =
172         (TNtQuerySystemInformation) GetProcAddress (hNTDLL,
173                                                     "NtQuerySystemInformation");
174   }
175   else
176   {
177     GNNtQuerySystemInformation = NULL;
178   }
179
180   /* Functions to get information about a network adapter */
181   hIphlpapi = LoadLibrary ("iphlpapi.dll");
182   if (hIphlpapi)
183   {
184     GNGetIfEntry = (TGetIfEntry) GetProcAddress (hIphlpapi, "GetIfEntry");
185     GNGetIpAddrTable =
186         (TGetIpAddrTable) GetProcAddress (hIphlpapi, "GetIpAddrTable");
187     GNGetIfTable = (TGetIfTable) GetProcAddress (hIphlpapi, "GetIfTable");
188     GNGetBestInterfaceEx =
189         (TGetBestInterfaceEx) GetProcAddress (hIphlpapi, "GetBestInterfaceEx");
190     GNGetAdaptersInfo =
191         (TGetAdaptersInfo) GetProcAddress (hIphlpapi, "GetAdaptersInfo");
192   }
193   else
194   {
195     GNGetIfEntry = NULL;
196     GNGetIpAddrTable = NULL;
197     GNGetIfTable = NULL;
198     GNGetBestInterfaceEx = NULL;
199     GNGetAdaptersInfo = NULL;
200   }
201
202   /* Service & Account functions */
203   hAdvapi = LoadLibrary ("advapi32.dll");
204   if (hAdvapi)
205   {
206     GNOpenSCManager =
207         (TOpenSCManager) GetProcAddress (hAdvapi, "OpenSCManagerA");
208     GNCreateService =
209         (TCreateService) GetProcAddress (hAdvapi, "CreateServiceA");
210     GNCloseServiceHandle =
211         (TCloseServiceHandle) GetProcAddress (hAdvapi, "CloseServiceHandle");
212     GNDeleteService =
213         (TDeleteService) GetProcAddress (hAdvapi, "DeleteService");
214     GNRegisterServiceCtrlHandler =
215         (TRegisterServiceCtrlHandler) GetProcAddress (hAdvapi,
216                                                       "RegisterServiceCtrlHandlerA");
217     GNSetServiceStatus =
218         (TSetServiceStatus) GetProcAddress (hAdvapi, "SetServiceStatus");
219     GNStartServiceCtrlDispatcher =
220         (TStartServiceCtrlDispatcher) GetProcAddress (hAdvapi,
221                                                       "StartServiceCtrlDispatcherA");
222     GNControlService =
223         (TControlService) GetProcAddress (hAdvapi, "ControlService");
224     GNOpenService = (TOpenService) GetProcAddress (hAdvapi, "OpenServiceA");
225
226     GNLsaOpenPolicy =
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");
236
237     GNGetFileSecurity =
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");
247     GNInitializeAcl =
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");
257   }
258   else
259   {
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;
269
270     GNLsaOpenPolicy = NULL;
271     GNLsaAddAccountRights = NULL;
272     GNLsaRemoveAccountRights = NULL;
273     GNLsaClose = NULL;
274     GNLookupAccountName = NULL;
275
276     GNGetFileSecurity = NULL;
277     GNInitializeSecurityDescriptor = NULL;
278     GNGetSecurityDescriptorDacl = NULL;
279     GNGetAclInformation = NULL;
280     GNInitializeAcl = NULL;
281     GNGetAce = NULL;
282     GNEqualSid = NULL;
283     GNAddAce = NULL;
284     GNAddAccessAllowedAce = NULL;
285     GNSetNamedSecurityInfo = NULL;
286   }
287
288   /* Account function */
289   hNetapi = LoadLibrary ("netapi32.dll");
290   if (hNetapi)
291   {
292     GNNetUserAdd = (TNetUserAdd) GetProcAddress (hNetapi, "NetUserAdd");
293     GNNetUserSetInfo =
294         (TNetUserSetInfo) GetProcAddress (hNetapi, "NetUserSetInfo");
295   }
296   else
297   {
298     GNNetUserAdd = NULL;
299     GNNetUserSetInfo = NULL;
300   }
301
302   return ret;
303 }
304
305 /**
306  * Clean up Windows environment
307  */
308 void
309 GNShutdownWinEnv ()
310 {
311   plibc_shutdown ();
312
313 #ifdef W32_VEH
314   if (GNWinVEH_handle != NULL)
315   {
316     RemoveVectoredExceptionHandler (GNWinVEH_handle);
317     GNWinVEH_handle = NULL;
318   }
319 #endif
320
321   FreeLibrary (hNTDLL);
322   FreeLibrary (hIphlpapi);
323   FreeLibrary (hAdvapi);
324   FreeLibrary (hNetapi);
325
326   CoUninitialize ();
327 }
328
329 #endif /* MINGW */
330
331 #if !HAVE_ATOLL
332 long long
333 atoll (const char *nptr)
334 {
335   return atol (nptr);
336 }
337 #endif