data.c 3.6 KB
Newer Older
1 2 3 4
#include <linux/compiler.h>
#include <linux/kernel.h>
#include <sys/types.h>
#include <sys/stat.h>
5
#include <errno.h>
6
#include <fcntl.h>
7 8 9 10 11
#include <unistd.h>
#include <string.h>

#include "data.h"
#include "util.h"
12
#include "debug.h"
13

14 15 16 17 18 19 20 21 22 23
#ifndef O_CLOEXEC
#ifdef __sparc__
#define O_CLOEXEC	0x400000
#elif defined(__alpha__) || defined(__hppa__)
#define O_CLOEXEC	010000000
#else
#define O_CLOEXEC	02000000
#endif
#endif

24
static bool check_pipe(struct perf_data *data)
25 26 27
{
	struct stat st;
	bool is_pipe = false;
28
	int fd = perf_data__is_read(data) ?
29 30
		 STDIN_FILENO : STDOUT_FILENO;

J
Jiri Olsa 已提交
31
	if (!data->file.path) {
32 33 34
		if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
			is_pipe = true;
	} else {
J
Jiri Olsa 已提交
35
		if (!strcmp(data->file.path, "-"))
36 37 38 39
			is_pipe = true;
	}

	if (is_pipe)
J
Jiri Olsa 已提交
40
		data->file.fd = fd;
41

42
	return data->is_pipe = is_pipe;
43 44
}

45
static int check_backup(struct perf_data *data)
46 47 48
{
	struct stat st;

J
Jiri Olsa 已提交
49
	if (!stat(data->file.path, &st) && st.st_size) {
50 51 52
		/* TODO check errors properly */
		char oldname[PATH_MAX];
		snprintf(oldname, sizeof(oldname), "%s.old",
J
Jiri Olsa 已提交
53
			 data->file.path);
54
		unlink(oldname);
J
Jiri Olsa 已提交
55
		rename(data->file.path, oldname);
56 57 58 59 60
	}

	return 0;
}

61
static int open_file_read(struct perf_data *data)
62 63 64
{
	struct stat st;
	int fd;
65
	char sbuf[STRERR_BUFSIZE];
66

J
Jiri Olsa 已提交
67
	fd = open(data->file.path, O_RDONLY);
68 69 70
	if (fd < 0) {
		int err = errno;

J
Jiri Olsa 已提交
71
		pr_err("failed to open %s: %s", data->file.path,
72
			str_error_r(err, sbuf, sizeof(sbuf)));
J
Jiri Olsa 已提交
73
		if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
74 75 76 77 78 79 80 81
			pr_err("  (try 'perf record' first)");
		pr_err("\n");
		return -err;
	}

	if (fstat(fd, &st) < 0)
		goto out_close;

82
	if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
83
		pr_err("File %s not owned by current user or root (use -f to override)\n",
J
Jiri Olsa 已提交
84
		       data->file.path);
85 86 87 88
		goto out_close;
	}

	if (!st.st_size) {
89
		pr_info("zero-sized data (%s), nothing to do!\n",
J
Jiri Olsa 已提交
90
			data->file.path);
91 92 93
		goto out_close;
	}

94
	data->size = st.st_size;
95 96 97 98 99 100 101
	return fd;

 out_close:
	close(fd);
	return -1;
}

102
static int open_file_write(struct perf_data *data)
103
{
A
Adrien BAK 已提交
104
	int fd;
105
	char sbuf[STRERR_BUFSIZE];
A
Adrien BAK 已提交
106

107
	if (check_backup(data))
108 109
		return -1;

J
Jiri Olsa 已提交
110
	fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
111
		  S_IRUSR|S_IWUSR);
A
Adrien BAK 已提交
112 113

	if (fd < 0)
J
Jiri Olsa 已提交
114
		pr_err("failed to open %s : %s\n", data->file.path,
115
			str_error_r(errno, sbuf, sizeof(sbuf)));
A
Adrien BAK 已提交
116 117

	return fd;
118 119
}

120
static int open_file(struct perf_data *data)
121 122 123
{
	int fd;

124 125
	fd = perf_data__is_read(data) ?
	     open_file_read(data) : open_file_write(data);
126

J
Jiri Olsa 已提交
127
	data->file.fd = fd;
128 129 130
	return fd < 0 ? -1 : 0;
}

131
int perf_data__open(struct perf_data *data)
132
{
133
	if (check_pipe(data))
134 135
		return 0;

J
Jiri Olsa 已提交
136 137
	if (!data->file.path)
		data->file.path = "perf.data";
138

139
	return open_file(data);
140 141
}

142
void perf_data__close(struct perf_data *data)
143
{
J
Jiri Olsa 已提交
144
	close(data->file.fd);
145
}
146

147
ssize_t perf_data__write(struct perf_data *data,
148 149
			      void *buf, size_t size)
{
J
Jiri Olsa 已提交
150
	return writen(data->file.fd, buf, size);
151
}
152

153
int perf_data__switch(struct perf_data *data,
154 155 156 157 158 159
			   const char *postfix,
			   size_t pos, bool at_exit)
{
	char *new_filepath;
	int ret;

160
	if (check_pipe(data))
161
		return -EINVAL;
162
	if (perf_data__is_read(data))
163 164
		return -EINVAL;

J
Jiri Olsa 已提交
165
	if (asprintf(&new_filepath, "%s.%s", data->file.path, postfix) < 0)
166 167 168 169 170 171
		return -ENOMEM;

	/*
	 * Only fire a warning, don't return error, continue fill
	 * original file.
	 */
J
Jiri Olsa 已提交
172 173
	if (rename(data->file.path, new_filepath))
		pr_warning("Failed to rename %s to %s\n", data->file.path, new_filepath);
174 175

	if (!at_exit) {
J
Jiri Olsa 已提交
176
		close(data->file.fd);
177
		ret = perf_data__open(data);
178 179 180
		if (ret < 0)
			goto out;

J
Jiri Olsa 已提交
181
		if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
182 183 184 185 186 187
			ret = -errno;
			pr_debug("Failed to lseek to %zu: %s",
				 pos, strerror(errno));
			goto out;
		}
	}
J
Jiri Olsa 已提交
188
	ret = data->file.fd;
189 190 191 192
out:
	free(new_filepath);
	return ret;
}