1 /* ccapi/common/win/OldCC/ccutils.c */
2 /*
3 * Copyright 2008 Massachusetts Institute of Technology.
4 * All Rights Reserved.
5 *
6 * Export of this software from the United States of America may
7 * require a specific license from the United States Government.
8 * It is the responsibility of any person or organization contemplating
9 * export to obtain such a license before exporting.
10 *
11 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12 * distribute this software and its documentation for any purpose and
13 * without fee is hereby granted, provided that the above copyright
14 * notice appear in all copies and that both that copyright notice and
15 * this permission notice appear in supporting documentation, and that
16 * the name of M.I.T. not be used in advertising or publicity pertaining
17 * to distribution of the software without specific, written prior
18 * permission. Furthermore if you modify this software you must label
19 * your software as modified software and not distribute it in such a
20 * fashion that it might be confused with the original M.I.T. software.
21 * M.I.T. makes no representations about the suitability of
22 * this software for any purpose. It is provided "as is" without express
23 * or implied warranty.
24 */
25
26 #include <windows.h>
27 #include <stdlib.h>
28 #include <malloc.h>
29
30 #include "cci_debugging.h"
31 #include "util.h"
32
isNT(void)33 BOOL isNT(void) {
34 OSVERSIONINFO osvi;
35 DWORD status = 0;
36 BOOL bSupportedVersion = FALSE;
37 BOOL bIsNT = FALSE;
38
39 memset(&osvi, 0, sizeof(osvi));
40 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
41
42 status = !GetVersionEx(&osvi); // Returns a boolean. Invert to 0 is OK.
43
44 if (!status) {
45 switch(osvi.dwPlatformId) {
46 case VER_PLATFORM_WIN32_WINDOWS:
47 bIsNT = FALSE;
48 bSupportedVersion = TRUE;
49 break;
50 case VER_PLATFORM_WIN32_NT:
51 bIsNT = TRUE;
52 bSupportedVersion = TRUE;
53 break;
54 case VER_PLATFORM_WIN32s:
55 default:
56 bIsNT = FALSE;
57 break;
58 }
59
60 if (!bSupportedVersion) {
61 cci_debug_printf("%s Running on an unsupported version of Windows", __FUNCTION__);
62 status = 1;
63 }
64 }
65
66 return (!status && bIsNT && bSupportedVersion);
67 }
68
allocEventName(char * uuid_string,char * suffix)69 char* allocEventName(char* uuid_string, char* suffix) {
70 LPSTR event_name = NULL;
71 cc_int32 err = ccNoError;
72
73 event_name = malloc(strlen(uuid_string) + strlen(suffix) + 3);
74 if (!event_name) err = cci_check_error(ccErrNoMem);
75
76 if (!err) {
77 strcpy(event_name, uuid_string);
78 strcat(event_name, "_");
79 strcat(event_name, suffix);
80 }
81
82 return event_name;
83 }
84
createThreadEvent(char * uuid,char * suffix)85 HANDLE createThreadEvent(char* uuid, char* suffix) {
86 LPSTR event_name = NULL;
87 HANDLE hEvent = NULL;
88 PSECURITY_ATTRIBUTES psa = 0; // Everything having to do with
89 SECURITY_ATTRIBUTES sa = { 0 }; // sa, psa, security is copied
90 DWORD status = 0; // from the previous implementation.
91
92 psa = isNT() ? &sa : 0;
93
94 if (isNT()) {
95 sa.nLength = sizeof(sa);
96 status = alloc_own_security_descriptor_NT(&sa.lpSecurityDescriptor);
97 cci_check_error(status);
98 }
99
100 if (!status) {
101 event_name = allocEventName(uuid, suffix);
102 if (!event_name) status = cci_check_error(ccErrNoMem);
103 }
104 if (!status) {
105 hEvent = CreateEvent(psa, FALSE, FALSE, event_name);
106 if (!hEvent) status = cci_check_error(GetLastError());
107 }
108
109 if (!status) ResetEvent(hEvent);
110
111
112 if (event_name) free(event_name);
113 if (isNT()) free(sa.lpSecurityDescriptor);
114
115 return hEvent;
116 }
117
openThreadEvent(char * uuid,char * suffix)118 HANDLE openThreadEvent(char* uuid, char* suffix) {
119 LPSTR event_name = NULL;
120 HANDLE hEvent = NULL;
121 DWORD status = 0;
122
123 event_name = allocEventName(uuid, suffix);
124 if (!event_name) status = cci_check_error(ccErrNoMem);
125 if (!status) {
126 hEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, event_name);
127 if (!hEvent) status = cci_check_error(GetLastError());
128 }
129
130 if (event_name) free(event_name);
131
132 return hEvent;
133 }
134