13f72cbe09d11918b40f1e89dc7ca7014eda13a1
[platform/upstream/krb5.git] / src / ccapi / common / win / OldCC / ccutils.c
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
33 BOOL isNT() {
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
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
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 0
105     cci_debug_printf("%s event_name:%s", __FUNCTION__, event_name);
106 #endif
107     if (!status) {
108         hEvent = CreateEvent(psa, FALSE, FALSE, event_name);
109         if (!hEvent)     status = cci_check_error(GetLastError());
110         }
111
112     if (!status) ResetEvent(hEvent);
113
114
115     if (event_name) free(event_name);
116     if (isNT())     free(sa.lpSecurityDescriptor);
117
118     return hEvent;
119     }
120
121 HANDLE openThreadEvent(char* uuid, char* suffix) {
122     LPSTR   event_name  = NULL;
123     HANDLE  hEvent      = NULL;
124     DWORD   status      = 0;
125
126     event_name = allocEventName(uuid, suffix);
127     if (!event_name) status = cci_check_error(ccErrNoMem);
128 #if 0
129     cci_debug_printf("%s event_name:%s", __FUNCTION__, event_name);
130 #endif
131     if (!status) {
132         hEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, event_name);
133         if (!hEvent) status = cci_check_error(GetLastError());
134         }
135
136     if (event_name) free(event_name);
137
138     return hEvent;
139     }