forked from bashbaug/OpenCL-ICD-Loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicd_linux.c
More file actions
244 lines (214 loc) · 7.12 KB
/
icd_linux.c
File metadata and controls
244 lines (214 loc) · 7.12 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright (c) 2016-2026 The Khronos Group Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* OpenCL is a trademark of Apple Inc. used under license by Khronos.
*/
#include "icd.h"
#include "icd_envvars.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pthread.h>
#include <limits.h>
static pthread_once_t initialized = PTHREAD_ONCE_INIT;
/*
*
* Vendor enumeration functions
*
*/
typedef void khrIcdFileAdd(const char *);
static inline void khrIcdOsDirEntryValidateAndAdd(const char *d_name, const char *path,
const char *extension, khrIcdFileAdd addFunc)
{
struct stat statBuff;
char* fileName = NULL;
// make sure the file name ends in `extension` (eg. .icd, or .lay)
if (strlen(extension) > strlen(d_name))
{
return;
}
if (strcmp(d_name + strlen(d_name) - strlen(extension), extension))
{
return;
}
// allocate space for the full path of the vendor library name
fileName = malloc(strlen(d_name) + strlen(path) + 2);
if (!fileName)
{
KHR_ICD_TRACE("Failed allocate space for ICD file path\n");
return;
}
sprintf(fileName, "%s/%s", path, d_name);
if (stat(fileName, &statBuff))
{
KHR_ICD_TRACE("Failed stat for: %s, continuing\n", fileName);
free(fileName);
return;
}
if (S_ISREG(statBuff.st_mode) || S_ISLNK(statBuff.st_mode))
{
FILE *fin = NULL;
char* buffer = NULL;
long bufferSize = 0;
// open the file and read its contents
fin = fopen(fileName, "r");
if (!fin)
{
free(fileName);
return;
}
fseek(fin, 0, SEEK_END);
bufferSize = ftell(fin);
buffer = malloc(bufferSize+1);
if (!buffer)
{
free(fileName);
fclose(fin);
return;
}
memset(buffer, 0, bufferSize+1);
fseek(fin, 0, SEEK_SET);
if (bufferSize != (long)fread(buffer, 1, bufferSize, fin))
{
free(fileName);
free(buffer);
fclose(fin);
return;
}
// ignore a newline at the end of the file
if (buffer[bufferSize-1] == '\n') buffer[bufferSize-1] = '\0';
// load the string read from the file
addFunc(buffer);
free(fileName);
free(buffer);
fclose(fin);
}
else
{
KHR_ICD_TRACE("File %s is not a regular file nor symbolic link, continuing\n", fileName);
free(fileName);
}
}
struct dirElem
{
char *d_name;
unsigned char d_type;
};
static int compareDirElem(const void *a, const void *b)
{
// sort files the same way libc alpahnumerically sorts directory entries.
return strcoll(((const struct dirElem *)a)->d_name, ((const struct dirElem *)b)->d_name);
}
static inline void khrIcdOsDirEnumerate(const char *path, const char *env,
const char *extension,
khrIcdFileAdd addFunc, int bSort)
{
DIR *dir = NULL;
char* envPath = NULL;
envPath = khrIcd_secure_getenv(env);
if (NULL != envPath)
{
path = envPath;
}
dir = opendir(path);
if (NULL == dir)
{
KHR_ICD_TRACE("Failed to open path %s, continuing\n", path);
}
else
{
struct dirent *dirEntry = NULL;
// attempt to load all files in the directory
if (bSort) {
// store the entries name and type in a buffer for sorting
size_t sz = 0;
size_t elemCount = 0;
size_t elemAlloc = 0;
struct dirElem *dirElems = NULL;
struct dirElem *newDirElems = NULL;
const size_t startupAlloc = 8;
// start with a small buffer
dirElems = (struct dirElem *)malloc(startupAlloc*sizeof(struct dirElem));
if (NULL != dirElems) {
elemAlloc = startupAlloc;
for (dirEntry = readdir(dir); dirEntry; dirEntry = readdir(dir) ) {
char *nameCopy = NULL;
if (elemCount + 1 > elemAlloc) {
// double buffer size if necessary and possible
if (elemAlloc >= UINT_MAX/2)
break;
newDirElems = (struct dirElem *)realloc(dirElems, elemAlloc*2*sizeof(struct dirElem));
if (NULL == newDirElems)
break;
dirElems = newDirElems;
elemAlloc *= 2;
}
sz = strlen(dirEntry->d_name) + 1;
nameCopy = (char *)malloc(sz);
if (NULL == nameCopy)
break;
memcpy(nameCopy, dirEntry->d_name, sz);
dirElems[elemCount].d_name = nameCopy;
#if defined(__QNXNTO__)
dirElems[elemCount].d_type = _DEXTRA_FIRST(dirEntry)->d_type;
#else
dirElems[elemCount].d_type = dirEntry->d_type;
#endif
elemCount++;
}
qsort(dirElems, elemCount, sizeof(struct dirElem), compareDirElem);
for (struct dirElem *elem = dirElems; elem < dirElems + elemCount; ++elem) {
khrIcdOsDirEntryValidateAndAdd(elem->d_name, path, extension, addFunc);
free(elem->d_name);
}
free(dirElems);
}
} else
// use system provided ordering
for (dirEntry = readdir(dir); dirEntry; dirEntry = readdir(dir) )
khrIcdOsDirEntryValidateAndAdd(dirEntry->d_name, path, extension, addFunc);
closedir(dir);
}
if (NULL != envPath)
{
khrIcd_free_getenv(envPath);
}
}
// go through the list of vendors in the two configuration files
void khrIcdOsVendorsEnumerate(void)
{
khrIcdInitializeEnvOptions();
khrIcdVendorsEnumerateEnv();
khrIcdOsDirEnumerate(ICD_VENDOR_PATH, "OCL_ICD_VENDORS", ".icd", khrIcdVendorAdd, 0);
#if defined(CL_ENABLE_LAYERS)
// system layers should be closer to the driver
khrIcdOsDirEnumerate(LAYER_PATH, "OPENCL_LAYER_PATH", ".lay", khrIcdLayerAdd, 1);
khrIcdLayersEnumerateEnv();
#endif // defined(CL_ENABLE_LAYERS)
}
// go through the list of vendors only once
void khrIcdOsVendorsEnumerateOnce(void)
{
pthread_once(&initialized, khrIcdOsVendorsEnumerate);
}
#ifndef CL_LAYER_INFO
static
void __attribute__((destructor)) khrIcdDestructor(void) {
khrIcdDeinitialize();
}
#endif