[go: up one dir, main page]

File: shmem.c

package info (click to toggle)
uftrace 0.18.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,356 kB
  • sloc: ansic: 49,770; python: 11,181; asm: 837; makefile: 769; sh: 637; cpp: 627; javascript: 191
file content (96 lines) | stat: -rw-r--r-- 1,605 bytes parent folder | download | duplicates (2)
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
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#include "utils/shmem.h"

#ifdef __ANDROID__

const char *uftrace_shmem_root(void)
{
	static char uftrace_dir[PATH_MAX] = "";

	if (uftrace_dir[0] == 0) {
		const char *tmpdir;
		tmpdir = getenv("TMPDIR");
		if (!tmpdir)
			tmpdir = "/tmp";

		snprintf(uftrace_dir, sizeof(uftrace_dir), "%s/uftrace", tmpdir);
	}

	return uftrace_dir;
}

int uftrace_shmem_open(const char *name, int oflag, mode_t mode)
{
	const char *uftrace_dir;
	char *fname;
	int fd;
	int status;

	uftrace_dir = uftrace_shmem_root();

	status = mkdir(uftrace_dir, mode);
	if (status < 0 && errno != EEXIST)
		return -1;

	if (asprintf(&fname, "%s/%s", uftrace_dir, name) < 0)
		return -1;

	fd = open(fname, oflag, mode);
	if (fd >= 0) {
		int flags = fcntl(fd, F_GETFD, 0);
		flags |= FD_CLOEXEC;
		if (fcntl(fd, F_SETFD, flags) < 0) {
			int saved_errno = errno;
			close(fd);
			fd = -1;
			errno = saved_errno;
		}
	}

	free(fname);

	return fd;
}

int uftrace_shmem_unlink(const char *name)
{
	const char *uftrace_dir;
	char *fname;
	int status;

	uftrace_dir = uftrace_shmem_root();

	if (asprintf(&fname, "%s/%s", uftrace_dir, name))
		return -1;
	status = unlink(fname);
	free(fname);

	return status;
}

#else /* ! __ANDROID__ */

#include <sys/mman.h>

const char *uftrace_shmem_root(void)
{
	return "/dev/shm";
}

int uftrace_shmem_open(const char *name, int oflag, mode_t mode)
{
	return shm_open(name, oflag, mode);
}

int uftrace_shmem_unlink(const char *name)
{
	return shm_unlink(name);
}

#endif