about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/tools/misc/saleae-logic/preload.c
blob: b4451e4d99f7356f63fe9211b6a8efbc58f85e90 (plain) (blame)
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
/*
 * LD_PRELOAD trick to make Saleae Logic work from a read-only installation
 * directory.
 *
 * Saleae Logic tries to write to a few directories inside its installation
 * directory. Because the Nix store is read-only, we have to redirect access to
 * this file somewhere else. Here's the map:
 *
 *   $out/Settings/    => $HOME/.saleae-logic/Settings/
 *   $out/Databases/   => $HOME/.saleae-logic/Databases/
 *   $out/Errors/      => $HOME/.saleae-logic/Errors/
 *   $out/Calibration/ => $HOME/.saleae-logic/Calibration/
 *
 * This also makes the software multi-user aware :-)
 *
 * NOTE: AFAIK (Bjørn Forsman), Logic version 1.2+ was supposed to have a
 * command line parameter for redirecting these write operations, but
 * apparently that feature got postponed.
 *
 * Usage:
 *   gcc -shared -fPIC -DOUT="$out" preload.c -o preload.so -ldl
 *   LD_PRELOAD=$PWD/preload.so ./result/Logic
 *
 * To see the paths that are modified at runtime, set the environment variable
 * PRELOAD_DEBUG to 1 (or anything really; debugging is on as long as the
 * variable exists).
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>

#ifndef OUT
#error Missing OUT define - path to the installation directory.
#endif

/*
 * TODO: How to properly wrap "open", which is declared as a variadic function
 * in glibc? The man page lists these signatures:
 *
 *   int open(const char *pathname, int flags);
 *   int open(const char *pathname, int flags, mode_t mode);
 *
 * But using either signature in this code cause a compile error, because
 * glibc has declared the function as "int open(const char *, int, ...)".
 * Same thing with "openat".
 *
 * For now we discard the variadic args. It seems to work.
 *
 * Relevant:
 * http://stackoverflow.com/questions/28462523/how-to-wrap-ioctlint-d-unsigned-long-request-using-ld-preload
 */
typedef FILE *(*fopen_func_t)(const char *path, const char *mode);
typedef FILE *(*fopen64_func_t)(const char *path, const char *mode);
typedef int (*open_func_t)(const char *pathname, int flags, ...);
typedef int (*open64_func_t)(const char *pathname, int flags, ...);
typedef int (*openat_func_t)(int dirfd, const char *pathname, int flags, ...);
typedef int (*openat64_func_t)(int dirfd, const char *pathname, int flags, ...);
typedef int (*xstat_func_t)(int vers, const char *pathname, struct stat *buf);
typedef int (*xstat64_func_t)(int vers, const char *pathname, struct stat64 *buf);
typedef int (*access_func_t)(const char *pathname, int mode);
typedef int (*faccessat_func_t)(int dirfd, const char *pathname, int mode, int flags);
typedef int (*unlink_func_t)(const char *pathname);

/*
 * Redirect $out/{Settings,Databases,Errors,Calibration}/ => $HOME/.saleae-logic/.
 * Path is truncated if bigger than PATH_MAX.
 *
 * @param pathname Original file path.
 * @param buffer Pointer to a buffer of size PATH_MAX bytes that this function
 * will write the new redirected path to (if needed).
 *
 * @return Pointer to the resulting path. It will either be equal to the
 * pathname (no redirect) or buffer argument (was redirected).
 */
static const char *redirect(const char *pathname, char *buffer)
{
	const char *homepath;
	const char *new_path;
	static char have_warned;
	const char *remainder;
	static char have_initialized;
	static size_t out_strlen;
	static size_t settings_strlen;
	static size_t databases_strlen;
	static size_t errors_strlen;
	static size_t calibration_strlen;
	int ret;
	int i;

	homepath = getenv("HOME");
	if (!homepath) {
		homepath = "/";
		if (!have_warned && getenv("PRELOAD_DEBUG")) {
			fprintf(stderr, "preload_debug: WARNING: HOME is unset, using \"/\" (root) instead.\n");
			have_warned = 1;
		}
	}

	if (!have_initialized) {
		/*
		 * The directories that Saleae Logic expects to find.
		 * The first element is intentionally empty (create base dir).
		 */
		char *dirs[] = {"", "/Settings", "/Databases", "/Errors", "/Calibration"};
		char old_settings_path[PATH_MAX];
		access_func_t orig_access;

		out_strlen = strlen(OUT);
		settings_strlen = out_strlen + strlen("/Settings");
		databases_strlen = out_strlen + strlen("/Databases");
		errors_strlen = out_strlen + strlen("/Errors");
		calibration_strlen = out_strlen + strlen("/Calibration");
		for (i = 0; i < sizeof dirs / sizeof dirs[0]; i++) {
			snprintf(buffer, PATH_MAX, "%s/.saleae-logic%s", homepath, dirs[i]);
			buffer[PATH_MAX-1] = '\0';
			ret = mkdir(buffer, 0755);
			if (0 != ret && errno != EEXIST) {
				fprintf(stderr, "ERROR: Failed to create directory \"%s\": %s\n",
						buffer, strerror(errno));
				return NULL;
			}
		}

		/*
		 * Automatic migration of the settings file:
		 * ~/.saleae-logic-settings.xml => ~/.saleae-logic/Settings/settings.xml
		 */
		snprintf(old_settings_path, PATH_MAX, "%s/.saleae-logic-settings.xml", homepath);
		old_settings_path[PATH_MAX-1] = '\0';
		orig_access = (access_func_t)dlsym(RTLD_NEXT, "access");
		if (orig_access(old_settings_path, F_OK) == 0) {
			snprintf(buffer, PATH_MAX, "%s/.saleae-logic/Settings/settings.xml", homepath);
			buffer[PATH_MAX-1] = '\0';
			ret = rename(old_settings_path, buffer);
			if (ret != 0) {
				fprintf(stderr, "WARN: Failed to move %s to %s",
						old_settings_path, buffer);
			}
		}

		have_initialized = 1;
	}

	new_path = pathname;
	remainder = pathname + out_strlen;

	if ((strncmp(OUT "/Settings", pathname, settings_strlen) == 0) ||
	    (strncmp(OUT "/Databases", pathname, databases_strlen) == 0) ||
	    (strncmp(OUT "/Errors", pathname, errors_strlen) == 0) ||
	    (strncmp(OUT "/Calibration", pathname, calibration_strlen) == 0)) {
		snprintf(buffer, PATH_MAX, "%s/.saleae-logic%s", homepath, remainder);
		buffer[PATH_MAX-1] = '\0';
		new_path = buffer;
	}

	return new_path;
}

FILE *fopen(const char *pathname, const char *mode)
{
	const char *path;
	char buffer[PATH_MAX];
	fopen_func_t orig_fopen;

	orig_fopen = (fopen_func_t)dlsym(RTLD_NEXT, "fopen");
	path = redirect(pathname, buffer);
	if (path != pathname && getenv("PRELOAD_DEBUG")) {
		fprintf(stderr, "preload_debug: fopen(\"%s\", \"%s\") => \"%s\"\n", pathname, mode, path);
	}

	return orig_fopen(path, mode);
}

FILE *fopen64(const char *pathname, const char *mode)
{
	const char *path;
	char buffer[PATH_MAX];
	fopen64_func_t orig_fopen64;

	orig_fopen64 = (fopen64_func_t)dlsym(RTLD_NEXT, "fopen64");
	path = redirect(pathname, buffer);
	if (path != pathname && getenv("PRELOAD_DEBUG")) {
		fprintf(stderr, "preload_debug: fopen64(\"%s\", \"%s\") => \"%s\"\n", pathname, mode, path);
	}

	return orig_fopen64(path, mode);
}

int open(const char *pathname, int flags, ...)
{
	const char *path;
	char buffer[PATH_MAX];
	open_func_t orig_open;

	orig_open = (open_func_t)dlsym(RTLD_NEXT, "open");
	path = redirect(pathname, buffer);
	if (path != pathname && getenv("PRELOAD_DEBUG")) {
		fprintf(stderr, "preload_debug: open(\"%s\", ...) => \"%s\"\n", pathname, path);
	}

	return orig_open(path, flags);
}

int open64(const char *pathname, int flags, ...)
{
	const char *path;
	char buffer[PATH_MAX];
	open64_func_t orig_open64;

	orig_open64 = (open64_func_t)dlsym(RTLD_NEXT, "open64");
	path = redirect(pathname, buffer);
	if (path != pathname && getenv("PRELOAD_DEBUG")) {
		fprintf(stderr, "preload_debug: open64(\"%s\", ...) => \"%s\"\n", pathname, path);
	}

	return orig_open64(path, flags);
}

int openat(int dirfd, const char *pathname, int flags, ...)
{
	const char *path;
	char buffer[PATH_MAX];
	openat_func_t orig_openat;

	orig_openat = (openat_func_t)dlsym(RTLD_NEXT, "openat");
	path = redirect(pathname, buffer);
	if (path != pathname && getenv("PRELOAD_DEBUG")) {
		fprintf(stderr, "preload_debug: openat(%d, \"%s\", %#x) => \"%s\"\n", dirfd, pathname, flags, path);
	}

	return orig_openat(dirfd, path, flags);
}

int openat64(int dirfd, const char *pathname, int flags, ...)
{
	const char *path;
	char buffer[PATH_MAX];
	openat64_func_t orig_openat64;

	orig_openat64 = (openat64_func_t)dlsym(RTLD_NEXT, "openat64");
	path = redirect(pathname, buffer);
	if (path != pathname && getenv("PRELOAD_DEBUG")) {
		fprintf(stderr, "preload_debug: openat64(%d, \"%s\", %#x) => \"%s\"\n", dirfd, pathname, flags, path);
	}

	return orig_openat64(dirfd, path, flags);
}

/*
 * Notes about "stat".
 *
 * The stat function is special, at least in glibc, in that it cannot be
 * directly overridden by LD_PRELOAD, due to it being inline wrapper around
 * __xstat. The __xstat functions take one extra parameter, a version number,
 * to indicate what "struct stat" should look like. This trick allows changing
 * the contents of mode_t without changing the shared library major number. See
 * sys/stat.h header for more info.
 */
int __xstat(int vers, const char *pathname, struct stat *buf)
{
	const char *path;
	char buffer[PATH_MAX];
	xstat_func_t orig_xstat;

	orig_xstat = (xstat_func_t)dlsym(RTLD_NEXT, "__xstat");
	path = redirect(pathname, buffer);
	if (path != pathname && getenv("PRELOAD_DEBUG")) {
		fprintf(stderr, "preload_debug: (__x)stat(\"%s\", ...) => \"%s\"\n", pathname, path);
	}

	return orig_xstat(vers, path, buf);
}

int __xstat64(int vers, const char *pathname, struct stat64 *buf)
{
	const char *path;
	char buffer[PATH_MAX];
	xstat64_func_t orig_xstat64;

	orig_xstat64 = (xstat64_func_t)dlsym(RTLD_NEXT, "__xstat64");
	path = redirect(pathname, buffer);
	if (path != pathname && getenv("PRELOAD_DEBUG")) {
		fprintf(stderr, "preload_debug: (__x)stat64(\"%s\", ...) => \"%s\"\n", pathname, path);
	}

	return orig_xstat64(vers, path, buf);
}

int access(const char *pathname, int mode)
{
	const char *path;
	char buffer[PATH_MAX];
	access_func_t orig_access;

	orig_access = (access_func_t)dlsym(RTLD_NEXT, "access");
	path = redirect(pathname, buffer);
	if (path != pathname && getenv("PRELOAD_DEBUG")) {
		fprintf(stderr, "preload_debug: access(\"%s\", ...) => \"%s\"\n", pathname, path);
	}

	return orig_access(path, mode);
}

int faccessat(int dirfd, const char *pathname, int mode, int flags)
{
	const char *path;
	char buffer[PATH_MAX];
	faccessat_func_t orig_faccessat;

	orig_faccessat = (faccessat_func_t)dlsym(RTLD_NEXT, "faccessat");
	path = redirect(pathname, buffer);
	if (path != pathname && getenv("PRELOAD_DEBUG")) {
		fprintf(stderr, "preload_debug: faccessat(\"%s\", ...) => \"%s\"\n", pathname, path);
	}

	return orig_faccessat(dirfd, path, mode, flags);
}

int unlink(const char *pathname)
{
	const char *path;
	char buffer[PATH_MAX];
	unlink_func_t orig_unlink;

	orig_unlink = (unlink_func_t)dlsym(RTLD_NEXT, "unlink");
	path = redirect(pathname, buffer);
	if (path != pathname && getenv("PRELOAD_DEBUG")) {
		fprintf(stderr, "preload_debug: unlink(\"%s\") => \"%s\"\n", pathname, path);
	}

	return orig_unlink(path);
}