File: procfs.c

package info (click to toggle)
scratchbox2 2.2.4-1debian1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 5,392 kB
  • ctags: 5,239
  • sloc: ansic: 21,734; sh: 4,360; perl: 2,170; cpp: 1,913; makefile: 610; python: 184
file content (391 lines) | stat: -rw-r--r-- 10,438 bytes parent folder | download
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
/*
 * Copyright (c) 2008 Nokia Corporation.
 * Author: Lauri T. Aarnio
 *
 * Licensed under LGPL version 2.1, see top level LICENSE file for details.
 *
 * ----------------
 *
 * /proc simulation for SB2.
 *
 * /proc/self/exe (as well as /proc/<MY_PID>/exe and /proc/<PID>L/exe)
 * needs special care if the binary was started by anything else
 * than direct exec:
 *    a) if CPU transparency is used, "exe" may point to e.g. Qemu
 *    b) if "ld.so-start" was used, "exe" points to ld.so and not
 *       to the binary itself.
 *
 * (all other files under /proc are used directly)
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#ifdef _GNU_SOURCE
#undef _GNU_SOURCE
#include <string.h>
#include <libgen.h>
#define _GNU_SOURCE
#else
#include <string.h>
#include <libgen.h>
#endif

#include <mapping.h>
#include <sb2.h>
#include "libsb2.h"
#include "exported.h"

#define BLOCK_SIZE (4*1024)

/*
 * We use dynamic buffer because size of the files in procfs
 * cannot been checked by stat() or lseek()
 * Note that caller must free a buffer if the function returns
 * other than zero.
*/
static size_t read_procfs_file_to_buffer(const char *path, char **buffer)
{
	int fd;
	char *buf;
	ssize_t rc = 0, total = 0;
	size_t count = BLOCK_SIZE;

	fd = open_nomap(path, O_RDONLY);
	if (fd < 0) {
		return(0);
	}

	buf = malloc(count);
	if (!buf) {
		close(fd);
		return(0);
	}

	for (;;) {
		rc = read(fd, buf + total, BLOCK_SIZE);
		if (rc == 0) {
			/* eof */
			break;
		}
		if (rc < 0) {
			if (errno == EINTR) {
				continue;
			} else {
				free(buf);
				close(fd);
				return(0);
			}
		}

		if (rc == BLOCK_SIZE) {
			count += BLOCK_SIZE;
			buf = realloc(buf, count);
			if (!buf) {
				close(fd);
				return(0);
			}
		}
		total += rc;
	}

	close(fd);
	*buffer = buf;

	return (total);
}

/* different environment varibles are `\0`separated */
static const char *read_env_variable(
	const char *name, const char *buf, size_t len)
{
        size_t name_len = strlen(name);
        size_t l, i = 0;

        while (i < len) {
                l = strlen(buf + i);
                if (l > 0) {
                        if (strncmp(buf + i, name, name_len) == 0) {
				return(strdup(buf + i));
                        }
                }
                i += l + 1;
        }
        return(NULL);
}

/* name=value'\0' */
static const char *read_env_value(const char *env)
{
	const char *cp;

	if (env == NULL) return(NULL);

	cp = strchr(env, '=');
	if (cp) return(cp + 1);
	return(NULL);
}

/* check (and create if it doesn't exits) the symlink which is used to
 * replace /proc/self/exe or /proc/<pid>/exe
 *
 * [Note that this code may be called from several threads or even several
 * processes in parallel, but because the resulting link is always the same,
 * this doesn't matter!]
*/
static char *symlink_for_exe_path(
	char *pathbuf, size_t bufsize, const char *exe_path, pid_t pid)
{
	int		depth;
	const char	*cp;
	int		prefixlen;

	/* first count number of slashes in exe_path */
	for (cp=exe_path, depth=0; *cp; cp++) {
		if (*cp == '/') {
			depth++;
			while (cp[1] == '/') cp++;
		}
	}
	/* ensure that the directory for links with "depth" levels exists: */
	prefixlen = snprintf(pathbuf, bufsize, "%s/proc/X.%d",
		sbox_session_dir, depth);
	if ((prefixlen + 2 + strlen(exe_path)) >= bufsize) {
		SB_LOG(SB_LOGLEVEL_ERROR,
			"Can't create replacement for /proc/%u/exe; "
		        "resulting path is too long", pid);
		return(NULL);
	}
	mkdir_nomap_nolog(pathbuf, 0755);

	for (cp=exe_path; cp && *cp; ) {
		char	*next_slash = strchr(cp+1, '/');

		/* This algorithm is not too efficient (first copies
		 * everything, then cuts it), but usually "exe_path"
		 * is really short, so usually it doesn't matter..
		*/
		strcat(pathbuf, cp); /* Always fits; We already checked that */
		if (next_slash) {
			pathbuf[prefixlen + (next_slash - exe_path)] = '\0';
			SB_LOG(SB_LOGLEVEL_NOISE, "Create DIR '%s'", pathbuf);
			mkdir_nomap_nolog(pathbuf, 0755);
			cp = next_slash;
		} else {
			SB_LOG(SB_LOGLEVEL_NOISE, "Create SYMLINK '%s' => '%s'",
				pathbuf, exe_path);
			symlink_nomap_nolog(exe_path, pathbuf);
			cp = NULL;
		}
	}
	return(pathbuf);
}

/* Determine best possible path specification for executable.
 *
 * Returns string in newly allocated buffer, caller should deallocate it.
 */
static char *select_exe_path_for_sb2(
	const char *orig_binary_name, const char *real_binary_name)
{
	if (real_binary_name) {
		/* real_binary_name contains host real path */
		/* try to determine virtal real path */
		char *exe = scratchbox_reverse_path("select_exe_path_for_sb2", real_binary_name);
		if (exe)
			return(exe);
		return(strdup(real_binary_name));
	}

	if (orig_binary_name) {
		/* only unmapped, unclean path is available */
		char *rp;
		char *reverse;
		struct lua_instance *luaif = get_lua();

		/* lua mapping is disabled at this point, need to enable it */
		enable_mapping(luaif);
		/* calculate host real path */
		rp = canonicalize_file_name(real_binary_name);
		disable_mapping(luaif);
		release_lua(luaif);

		if (!rp) {
			SB_LOG(SB_LOGLEVEL_ERROR,
				"select_exe_path_for_sb2(%s, %s):"
				" error while determining real path: %s",
				orig_binary_name,
				real_binary_name,
				strerror(errno));
			return(NULL);
		}

		reverse = scratchbox_reverse_path("select_exe_path_for_sb2", rp);
		if (reverse) {
			free(rp);
			rp = reverse;
		}
		return(rp);
	}

	SB_LOG(SB_LOGLEVEL_DEBUG,
	       "procfs_mapping failed to get absolute exe path");
	return(NULL);
}

static char *procfs_mapping_request_for_my_files(
	char *full_path, char *base_path)
{
	SB_LOG(SB_LOGLEVEL_DEBUG, "procfs_mapping_request_for_my_files(%s)",
		full_path);

	if (!strcmp(base_path,"exe")) {
		char	*exe_path_inside_sb2;
		char	pathbuf[PATH_MAX];
		char    link_dest[PATH_MAX+1];
		int	link_len;

                exe_path_inside_sb2 = select_exe_path_for_sb2(
			sbox_orig_binary_name, sbox_real_binary_name);

                if (!exe_path_inside_sb2) return(NULL);

		/* check if the real link is OK: */
		link_len = readlink_nomap(full_path, link_dest, PATH_MAX);
		if ((link_len > 0) &&
		    !strcmp(exe_path_inside_sb2, link_dest)) {
			SB_LOG(SB_LOGLEVEL_DEBUG,
				"procfs_mapping_request_for_my_files:"
				" real link is ok (%s,%s)",
				full_path, link_dest);
			free(exe_path_inside_sb2);
			return(NULL);
		}
		/* must create a replacement: */
		if (symlink_for_exe_path(
		    pathbuf, sizeof(pathbuf), exe_path_inside_sb2, getpid())) {
			free(exe_path_inside_sb2);
			return(strdup(pathbuf));
		}
		/* oops, failed to create the replacement.
		 * must use the real link, it points to wrong place.. */
		free(exe_path_inside_sb2);
		return(NULL);
	}
	return(NULL);
}

static char *procfs_mapping_request_for_other_files(
        char *full_path, char *base_path, char *pid_path, pid_t pid)
{
        SB_LOG(SB_LOGLEVEL_DEBUG, "procfs_mapping_request_for_other_files(%s)",
	       full_path);

        if (!strcmp(base_path,"exe")) {
                char    *exe_path_inside_sb2;
                char    *buffer;
                char    pathbuf[PATH_MAX];
                char    link_dest[PATH_MAX+1];
                int     link_len;
                size_t  len;
                const char *orig_binary_name;
                const char *real_binary_name;

                /* Check the process environment to find out is this 
		 * runned under sb2 
		 */
                (void)snprintf(pathbuf, sizeof(pathbuf), "%s/environ",
			       pid_path);
                len = read_procfs_file_to_buffer(pathbuf, &buffer);
                if (len == 0) {
                        return(NULL);
                }

                orig_binary_name = read_env_variable("__SB2_ORIG_BINARYNAME",
                                                  buffer, len);
                real_binary_name = read_env_variable("__SB2_REAL_BINARYNAME",
                                                  buffer, len);

		/* we don't need buffer anymore */
		free(buffer);

		orig_binary_name = read_env_value(orig_binary_name);
		real_binary_name = read_env_value(real_binary_name);

                exe_path_inside_sb2 = select_exe_path_for_sb2(
			orig_binary_name, real_binary_name);
		
                /* this is not under runned binary */
                if (!exe_path_inside_sb2) {
                        return(NULL);
                }

                /* check if the real link is OK: */
                link_len = readlink_nomap(full_path, link_dest, PATH_MAX);
                if ((link_len > 0) &&
                    !strcmp(exe_path_inside_sb2, link_dest)) {
                        SB_LOG(SB_LOGLEVEL_DEBUG,
			       "procfs_mapping_request_for_other_files:"
			       " real link is ok (%s,%s)",
			       full_path, link_dest);
			free(exe_path_inside_sb2);
			return(NULL);
		}
		/* must create a replacement: */
		if (symlink_for_exe_path(
		    pathbuf, sizeof(pathbuf), exe_path_inside_sb2, pid)) {
			free(exe_path_inside_sb2);
			return(strdup(pathbuf));
		}
		/* oops, failed to create the replacement.
                 * must use the real link, it points to wrong place.. 
		 */
		free(exe_path_inside_sb2);
		return(NULL);
	}
	return(NULL);
}

static const char proc_self_path[] = "/proc/self/";

/* procfs_mapping_request(): Called from luaif.c
 * returns mapped path if the path needs mapping,
 * or NULL if the original path can be used directly.
*/
char *procfs_mapping_request(char *path)
{
	char	my_process_path[PATH_MAX];
	int	len, count;
	pid_t   pid;

	SB_LOG(SB_LOGLEVEL_DEBUG, "procfs_mapping_request(%s)", path);

	if (!strncmp(path, proc_self_path, sizeof(proc_self_path)-1))
		return(procfs_mapping_request_for_my_files(path,
			path+sizeof(proc_self_path)-1));

	len = snprintf(my_process_path, sizeof(my_process_path), "/proc/%u/",
		(unsigned)getpid());
	if (!strncmp(path, my_process_path, len))
		return(procfs_mapping_request_for_my_files(path, path+len));

	/* some other /proc/<pid>/exe */
	count = sscanf(path, "/proc/%u/", &pid);
	if (count != 1)
		return(NULL);

	len = snprintf(my_process_path, sizeof(my_process_path), "/proc/%u/",
		       (unsigned)pid);

	if (!strncmp(path, my_process_path, len))
		return(procfs_mapping_request_for_other_files(
			       path, path+len, my_process_path, pid));

	SB_LOG(SB_LOGLEVEL_DEBUG, "procfs_mapping_request: not mapped");
	return(NULL);
}