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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
|
/*
* This file is part of the bladeRF project:
* http://www.github.com/nuand/bladeRF
*
* Copyright (C) 2013-2014 Nuand LLC
* Copyright (C) 2013 Daniel Gröber <dxld ÄT darkboxed DOT org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <libbladeRF.h>
#include "host_config.h"
#include "minmax.h"
#include "log.h"
#include "rel_assert.h"
#include "helpers/file.h"
/* Paths to search for bladeRF files */
struct search_path_entries {
bool prepend_home;
const char *path;
};
int file_write(FILE *f, uint8_t *buf, size_t len)
{
size_t rv;
rv = fwrite(buf, 1, len, f);
if(rv < len) {
log_debug("File write failed: %s\n", strerror(errno));
return BLADERF_ERR_IO;
}
return 0;
}
int file_read(FILE *f, char *buf, size_t len)
{
size_t rv;
rv = fread(buf, 1, len, f);
if(rv < len) {
if(feof(f))
log_debug("Unexpected end of file: %s\n", strerror(errno));
else
log_debug("Error reading file: %s\n", strerror(errno));
return BLADERF_ERR_IO;
}
return 0;
}
ssize_t file_size(FILE *f)
{
ssize_t rv = BLADERF_ERR_IO;
long int fpos = ftell(f);
long len;
if(fpos < 0) {
log_verbose("ftell failed: %s\n", strerror(errno));
goto out;
}
if(fseek(f, 0, SEEK_END)) {
log_verbose("fseek failed: %s\n", strerror(errno));
goto out;
}
len = ftell(f);
if(len < 0) {
log_verbose("ftell failed: %s\n", strerror(errno));
goto out;
} else if (len == LONG_MAX) {
log_debug("ftell called with a directory?\n");
goto out;
}
if(fseek(f, fpos, SEEK_SET)) {
log_debug("fseek failed: %s\n", strerror(errno));
goto out;
}
rv = (ssize_t) len;
assert(rv == len);
out:
return rv;
}
int file_read_buffer(const char *filename, uint8_t **buf_ret, size_t *size_ret)
{
int status = BLADERF_ERR_UNEXPECTED;
FILE *f;
uint8_t *buf = NULL;
ssize_t len;
f = fopen(filename, "rb");
if (!f) {
log_error("%s: could not open %s: %s\n", __FUNCTION__, filename,
strerror(errno));
switch (errno) {
case ENOENT:
return BLADERF_ERR_NO_FILE;
case EACCES:
return BLADERF_ERR_PERMISSION;
default:
return BLADERF_ERR_IO;
}
}
len = file_size(f);
if (len < 0) {
status = BLADERF_ERR_IO;
goto out;
}
buf = (uint8_t *)malloc(len);
if (!buf) {
status = BLADERF_ERR_MEM;
goto out;
}
status = file_read(f, (char *)buf, len);
if (status < 0) {
goto out;
}
*buf_ret = buf;
*size_ret = len;
fclose(f);
return 0;
out:
free(buf);
if (f) {
fclose(f);
}
return status;
}
/* Remove the last entry in a path. This is used to strip the executable name
* from a path to get the directory that the executable resides in. */
static size_t strip_last_path_entry(char *buf, char dir_delim)
{
size_t len = strlen(buf);
while (len > 0 && buf[len - 1] != dir_delim) {
buf[len - 1] = '\0';
len--;
}
return len;
}
#if BLADERF_OS_LINUX || BLADERF_OS_OSX || BLADERF_OS_FREEBSD
#define ACCESS_FILE_EXISTS F_OK
#define DIR_DELIMETER '/'
static const struct search_path_entries search_paths[] = {
{ false, "" },
{ true, "/.config/Nuand/bladeRF/" },
{ true, "/.Nuand/bladeRF/" },
/* LIBBLADERF_SEARCH_PATH_PREFIX is defined in the libbladeRF
* CMakeLists.txt file. It defaults to ${CMAKE_INSTALL_PREFIX}, but
* can be overridden via -DLIBBLADERF_SEARCH_PATH_OVERRIDE
*/
//{ false, LIBBLADERF_SEARCH_PREFIX "/etc/Nuand/bladeRF/" },
//{ false, LIBBLADERF_SEARCH_PREFIX "/share/Nuand/bladeRF/" },
/* These two entries are here for reverse compatibility.
*
* We failed to prefix ${CMAKE_INSTALL_PREFIX} on these from the beginning,
* forcing package maintainers to hard-code one of these locations,
* despite having a different ${CMAKE_INSTALL_PREFIX}.
*
* We'll keep these around for some time as fall-backs, as not to break
* existing packaging scripts.
*/
{ false, "/etc/Nuand/bladeRF/" },
{ false, "/usr/share/Nuand/bladeRF/" },
};
static inline size_t get_home_dir(char *buf, size_t max_len)
{
const char *home;
home = getenv("HOME");
if (home != NULL && strlen(home) > 0 && strlen(home) < max_len) {
strncat(buf, home, max_len);
} else {
const struct passwd *passwd;
const uid_t uid = getuid();
passwd = getpwuid(uid);
strncat(buf, passwd->pw_dir, max_len);
}
return strlen(buf);
}
static inline size_t get_install_dir(char *buf, size_t max_len)
{
return 0;
}
#if BLADERF_OS_LINUX
static inline size_t get_binary_dir(char *buf, size_t max_len)
{
ssize_t result = readlink("/proc/self/exe", buf, max_len);
if (result > 0) {
return strip_last_path_entry(buf, DIR_DELIMETER);
} else {
return 0;
}
}
#elif BLADERF_OS_FREEBSD
static inline size_t get_binary_dir(char *buf, size_t max_len)
{
int mib[4];
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
mib[3] = -1;
ssize_t result = sysctl(mib, 4, buf, &max_len, NULL, 0);
if (result > 0) {
return strip_last_path_entry(buf, DIR_DELIMETER);
} else {
return 0;
}
}
#elif BLADERF_OS_OSX
#include <mach-o/dyld.h>
static inline size_t get_binary_dir(char *buf, size_t max_len)
{
uint32_t buf_size = max_len;
int status = _NSGetExecutablePath(buf, &buf_size);
if (status == 0) {
return strip_last_path_entry(buf, DIR_DELIMETER);
} else {
return 0;
}
}
#endif
#elif BLADERF_OS_WINDOWS
#define ACCESS_FILE_EXISTS 0
#define DIR_DELIMETER '\\'
#include <shlobj.h>
static const struct search_path_entries search_paths[] = {
{ false, "" },
{ true, "/Nuand/bladeRF/" },
};
static inline size_t get_home_dir(char *buf, size_t max_len)
{
/* Explicitly link to a runtime DLL to get SHGetKnownFolderPath.
* This deals with the case where we might not be able to staticly
* link it at build time, e.g. mingw32.
*
* http://msdn.microsoft.com/en-us/library/784bt7z7.aspx
*/
typedef HRESULT (CALLBACK* LPFNSHGKFP_T)(REFKNOWNFOLDERID, DWORD, HANDLE, PWSTR*);
HINSTANCE hDLL; // Handle to DLL
LPFNSHGKFP_T lpfnSHGetKnownFolderPath; // Function pointer
const KNOWNFOLDERID folder_id = FOLDERID_RoamingAppData;
PWSTR path;
HRESULT status;
assert(max_len < INT_MAX);
hDLL = LoadLibrary("Shell32");
if (hDLL == NULL) {
// DLL couldn't be loaded, bail out.
return 0;
}
lpfnSHGetKnownFolderPath = (LPFNSHGKFP_T)GetProcAddress(hDLL, "SHGetKnownFolderPath");
if (!lpfnSHGetKnownFolderPath) {
// Can't find the procedure we want. Free and bail.
FreeLibrary(hDLL);
return 0;
}
status = lpfnSHGetKnownFolderPath(&folder_id, 0, NULL, &path);
if (status == S_OK) {
WideCharToMultiByte(CP_ACP, 0, path, -1, buf, (int)max_len, NULL, NULL);
CoTaskMemFree(path);
}
FreeLibrary(hDLL);
return strlen(buf);
}
static inline size_t get_binary_dir(char *buf, size_t max_len)
{
DWORD status;
assert(max_len <= MAXDWORD);
status = GetModuleFileName(NULL, buf, (DWORD) max_len);
if (status > 0) {
return strip_last_path_entry(buf, DIR_DELIMETER);
} else {
return 0;
}
}
static inline size_t get_install_dir(char *buf, size_t max_len)
{
typedef LONG (CALLBACK* LPFNREGOPEN_T)(HKEY, LPCTSTR, DWORD, REGSAM, PHKEY);
typedef LONG (CALLBACK* LPFNREGQUERY_T)(HKEY, LPCTSTR, LPDWORD, LPDWORD, LPBYTE, LPDWORD);
typedef LONG (CALLBACK* LPFNREGCLOSE_T)(HKEY);
HINSTANCE hDLL; // Handle to DLL
LPFNREGOPEN_T lpfnRegOpenKeyEx; // Function pointer
LPFNREGQUERY_T lpfnRegQueryValueEx; // Function pointer
LPFNREGCLOSE_T lpfnRegCloseKey; // Function pointer
HKEY hk;
DWORD len;
assert(max_len < INT_MAX);
memset(buf, 0, max_len);
hDLL = LoadLibrary("Advapi32");
if (hDLL == NULL) {
// DLL couldn't be loaded, bail out.
return 0;
}
lpfnRegOpenKeyEx = (LPFNREGOPEN_T)GetProcAddress(hDLL, "RegOpenKeyExA");
if (!lpfnRegOpenKeyEx) {
// Can't find the procedure we want. Free and bail.
FreeLibrary(hDLL);
return 0;
}
lpfnRegQueryValueEx = (LPFNREGQUERY_T)GetProcAddress(hDLL, "RegQueryValueExA");
if (!lpfnRegQueryValueEx) {
// Can't find the procedure we want. Free and bail.
FreeLibrary(hDLL);
return 0;
}
lpfnRegCloseKey = (LPFNREGCLOSE_T)GetProcAddress(hDLL, "RegCloseKey");
if (!lpfnRegCloseKey) {
// Can't find the procedure we want. Free and bail.
FreeLibrary(hDLL);
return 0;
}
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Nuand LLC", 0, KEY_READ, &hk)) {
FreeLibrary(hDLL);
return 0;
}
len = (DWORD)max_len;
if (RegQueryValueEx(hk, "Path", 0, NULL, (LPBYTE) buf, &len) == ERROR_SUCCESS) {
if (len > 0 && len < max_len && buf[len - 1] != '\\')
strcat(buf, "\\");
} else
len = 0;
if (len) {
lpfnRegCloseKey(hk);
}
FreeLibrary(hDLL);
return len;
}
#else
#error "Unknown OS or missing BLADERF_OS_* definition"
#endif
/* We're not using functions that use the *nix PATH_MAX (which is known to be
* problematic), or the WIN32 MAX_PATH. Therefore, we'll just use this
* arbitrary, but "sufficiently" large max buffer size for paths */
#define PATH_MAX_LEN 4096
char *file_find(const char *filename)
{
size_t i, max_len;
char *full_path;
const char *env_var;
full_path = calloc(PATH_MAX_LEN+1, 1);
if (full_path == NULL) {
return NULL;
}
/* Check directory specified by environment variable */
env_var = getenv("BLADERF_SEARCH_DIR");
if (env_var != NULL) {
strncat(full_path, env_var, PATH_MAX_LEN - 1);
full_path[strlen(full_path)] = DIR_DELIMETER;
max_len = PATH_MAX_LEN - strlen(full_path);
if (max_len >= strlen(filename)) {
strncat(full_path, filename, max_len);
if (access(full_path, ACCESS_FILE_EXISTS) != -1) {
return full_path;
}
}
}
/* Check the directory containing the currently running binary */
memset(full_path, 0, PATH_MAX_LEN);
max_len = PATH_MAX_LEN - 1;
if (get_binary_dir(full_path, max_len) != 0) {
max_len -= strlen(full_path);
if (max_len >= strlen(filename)) {
strncat(full_path, filename, max_len);
if (access(full_path, ACCESS_FILE_EXISTS) != -1) {
return full_path;
}
} else {
log_debug("Skipping search for %s in %s. "
"Path would be truncated.\n",
filename, full_path);
}
}
/* Search our list of pre-determined paths */
for (i = 0; i < ARRAY_SIZE(search_paths); i++) {
memset(full_path, 0, PATH_MAX_LEN);
max_len = PATH_MAX_LEN;
if (search_paths[i].prepend_home) {
const size_t len = get_home_dir(full_path, max_len);
if (len != 0) {
max_len -= len;
} else {
continue;
}
}
strncat(full_path, search_paths[i].path, max_len);
max_len = PATH_MAX_LEN - strlen(full_path);
if (max_len >= strlen(filename)) {
strncat(full_path, filename, max_len);
if (access(full_path, ACCESS_FILE_EXISTS) != -1) {
return full_path;
}
} else {
log_debug("Skipping search for %s in %s. "
"Path would be truncated.\n",
filename, full_path);
}
}
/* Search the installation directory, if applicable */
if (get_install_dir(full_path, PATH_MAX_LEN)) {
max_len = PATH_MAX_LEN - strlen(full_path);
if (max_len >= strlen(filename)) {
strncat(full_path, filename, max_len);
if (access(full_path, ACCESS_FILE_EXISTS) != -1) {
return full_path;
}
} else {
log_debug("Skipping search for %s in %s. "
"Path would be truncated.\n",
filename, full_path);
}
}
free(full_path);
return NULL;
}
|