-
Notifications
You must be signed in to change notification settings - Fork 71
/
UUIDPlugin.c
167 lines (129 loc) · 3.01 KB
/
UUIDPlugin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "config.h"
#include "sq.h"
typedef char sqUUID[16];
int MakeUUID(sqUUID location);
int sqUUIDInit(void);
int sqUUIDShutdown(void);
struct VirtualMachine* interpreterProxy;
#ifdef _WIN32
#include <windows.h>
#include <ole2.h>
int
MakeUUID(char *location) {
if (CoCreateGuid((GUID*)location) == S_OK)
return 1;
interpreterProxy->primitiveFail();
return 0;
}
#else
#if defined(HAVE_SYS_UUID_H)
# include <sys/types.h>
# include <sys/uuid.h>
#elif defined(HAVE_UUID_UUID_H)
# include <uuid/uuid.h>
#elif defined(HAVE_UUID_H)
# include <uuid.h>
#else
# error cannot find a uuid.h to include
#endif
int MakeUUID(char *location)
{
uuid_t uuid;
#if defined(HAVE_UUIDGEN) || __FreeBSD__
uuidgen(&uuid, 1);
#else
uuid_generate(uuid);
#endif
memcpy((void *)location, (void *)&uuid, sizeof(uuid));
return 1;
}
#endif
#if defined(__linux__)
# include <setjmp.h>
# include <signal.h>
static sigjmp_buf env;
static void sigsegvHandler(int signal)
{
siglongjmp(env, 1);
}
int sqUUIDInit(void)
{
/* check if we get a segmentation fault when using libuuid */
int pluginAvailable= 0;
struct sigaction originalAction;
uuid_t uuid;
if (!sigsetjmp(env, 1))
{
struct sigaction newAction;
newAction.sa_handler= sigsegvHandler;
newAction.sa_flags= 0;
sigemptyset(&newAction.sa_mask);
if (sigaction(SIGSEGV, &newAction, &originalAction))
/* couldn't change the signal handler: give up now */
return 0;
else
pluginAvailable= MakeUUID((char *)&uuid);
}
sigaction(SIGSEGV, &originalAction, NULL);
return pluginAvailable;
}
#else /* !__linux__ */
int sqUUIDInit(void)
{
return 1;
}
#endif /* !__linux__ */
int sqUUIDShutdown(void)
{
return 1;
}
EXPORT(sqInt) setInterpreter(struct VirtualMachine *anInterpreter)
{
sqInt ok;
interpreterProxy = anInterpreter;
return 0;
}
EXPORT(const char*)
getModuleName(void)
{
return "UUIDPlugin";
}
EXPORT(sqInt)
initialiseModule(void)
{
return sqUUIDInit();
}
EXPORT(sqInt)
shutdownModule(void)
{
return sqUUIDShutdown();
}
/* UUIDPlugin>>#primitiveMakeUUID */
EXPORT(sqInt)
primitiveMakeUUID(void)
{
char *location;
sqInt oop;
oop = interpreterProxy->stackValue(0);
if (!(((interpreterProxy->methodArgumentCount()) == 0)
&& ((interpreterProxy->isBytes(oop))
&& ((interpreterProxy->byteSizeOf(oop)) == 16)))) {
return interpreterProxy->primitiveFail();
}
location = interpreterProxy->firstIndexableField(oop);
MakeUUID(location);
return oop;
}
#ifdef SQUEAK_BUILTIN_PLUGIN
static char _m[] = "UUIDPlugin";
void* UUIDPlugin_exports[][3] = {
{(void*)_m, "getModuleName", (void*)getModuleName},
{(void*)_m, "initialiseModule", (void*)initialiseModule},
{(void*)_m, "primitiveMakeUUID\000\001", (void*)primitiveMakeUUID},
{(void*)_m, "setInterpreter", (void*)setInterpreter},
{(void*)_m, "shutdownModule\000\377", (void*)shutdownModule},
{NULL, NULL, NULL}
};
#else /* ifdef SQ_BUILTIN_PLUGIN */
EXPORT(signed char) primitiveMakeUUIDAccessorDepth = 1;
#endif /* ifdef SQ_BUILTIN_PLUGIN */