blob: 263f5a906ba5a2bde3d0898a1e19d783920fca47 [file] [log] [blame]
Jiri Olsa6a4d98d2013-10-15 16:27:33 +02001#include <linux/compiler.h>
2#include <linux/kernel.h>
3#include <sys/types.h>
4#include <sys/stat.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03005#include <errno.h>
Jiri Olsa6a4d98d2013-10-15 16:27:33 +02006#include <unistd.h>
7#include <string.h>
8
9#include "data.h"
10#include "util.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +020011#include "debug.h"
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020012
Jiri Olsacd6379e2017-09-08 10:46:20 +020013#ifndef O_CLOEXEC
14#ifdef __sparc__
15#define O_CLOEXEC 0x400000
16#elif defined(__alpha__) || defined(__hppa__)
17#define O_CLOEXEC 010000000
18#else
19#define O_CLOEXEC 02000000
20#endif
21#endif
22
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020023static bool check_pipe(struct perf_data_file *file)
24{
25 struct stat st;
26 bool is_pipe = false;
27 int fd = perf_data_file__is_read(file) ?
28 STDIN_FILENO : STDOUT_FILENO;
29
30 if (!file->path) {
31 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
32 is_pipe = true;
33 } else {
34 if (!strcmp(file->path, "-"))
35 is_pipe = true;
36 }
37
38 if (is_pipe)
39 file->fd = fd;
40
41 return file->is_pipe = is_pipe;
42}
43
44static int check_backup(struct perf_data_file *file)
45{
46 struct stat st;
47
48 if (!stat(file->path, &st) && st.st_size) {
49 /* TODO check errors properly */
50 char oldname[PATH_MAX];
51 snprintf(oldname, sizeof(oldname), "%s.old",
52 file->path);
53 unlink(oldname);
54 rename(file->path, oldname);
55 }
56
57 return 0;
58}
59
60static int open_file_read(struct perf_data_file *file)
61{
62 struct stat st;
63 int fd;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +000064 char sbuf[STRERR_BUFSIZE];
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020065
66 fd = open(file->path, O_RDONLY);
67 if (fd < 0) {
68 int err = errno;
69
Masami Hiramatsu6e81c742014-08-14 02:22:36 +000070 pr_err("failed to open %s: %s", file->path,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -030071 str_error_r(err, sbuf, sizeof(sbuf)));
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020072 if (err == ENOENT && !strcmp(file->path, "perf.data"))
73 pr_err(" (try 'perf record' first)");
74 pr_err("\n");
75 return -err;
76 }
77
78 if (fstat(fd, &st) < 0)
79 goto out_close;
80
81 if (!file->force && st.st_uid && (st.st_uid != geteuid())) {
Arnaldo Carvalho de Melo071266b2014-07-08 12:40:11 -030082 pr_err("File %s not owned by current user or root (use -f to override)\n",
Jiri Olsa6a4d98d2013-10-15 16:27:33 +020083 file->path);
84 goto out_close;
85 }
86
87 if (!st.st_size) {
88 pr_info("zero-sized file (%s), nothing to do!\n",
89 file->path);
90 goto out_close;
91 }
92
93 file->size = st.st_size;
94 return fd;
95
96 out_close:
97 close(fd);
98 return -1;
99}
100
101static int open_file_write(struct perf_data_file *file)
102{
Adrien BAKffa91882014-04-18 11:00:43 +0900103 int fd;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000104 char sbuf[STRERR_BUFSIZE];
Adrien BAKffa91882014-04-18 11:00:43 +0900105
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200106 if (check_backup(file))
107 return -1;
108
Jiri Olsacd6379e2017-09-08 10:46:20 +0200109 fd = open(file->path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
110 S_IRUSR|S_IWUSR);
Adrien BAKffa91882014-04-18 11:00:43 +0900111
112 if (fd < 0)
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000113 pr_err("failed to open %s : %s\n", file->path,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300114 str_error_r(errno, sbuf, sizeof(sbuf)));
Adrien BAKffa91882014-04-18 11:00:43 +0900115
116 return fd;
Jiri Olsa6a4d98d2013-10-15 16:27:33 +0200117}
118
119static int open_file(struct perf_data_file *file)
120{
121 int fd;
122
123 fd = perf_data_file__is_read(file) ?
124 open_file_read(file) : open_file_write(file);
125
126 file->fd = fd;
127 return fd < 0 ? -1 : 0;
128}
129
130int perf_data_file__open(struct perf_data_file *file)
131{
132 if (check_pipe(file))
133 return 0;
134
135 if (!file->path)
136 file->path = "perf.data";
137
138 return open_file(file);
139}
140
141void perf_data_file__close(struct perf_data_file *file)
142{
143 close(file->fd);
144}
Jiri Olsa6f9a3172013-11-28 11:30:17 +0100145
146ssize_t perf_data_file__write(struct perf_data_file *file,
147 void *buf, size_t size)
148{
149 return writen(file->fd, buf, size);
150}
Wang Nan040f9912016-04-13 08:21:05 +0000151
152int perf_data_file__switch(struct perf_data_file *file,
153 const char *postfix,
154 size_t pos, bool at_exit)
155{
156 char *new_filepath;
157 int ret;
158
159 if (check_pipe(file))
160 return -EINVAL;
161 if (perf_data_file__is_read(file))
162 return -EINVAL;
163
164 if (asprintf(&new_filepath, "%s.%s", file->path, postfix) < 0)
165 return -ENOMEM;
166
167 /*
168 * Only fire a warning, don't return error, continue fill
169 * original file.
170 */
171 if (rename(file->path, new_filepath))
172 pr_warning("Failed to rename %s to %s\n", file->path, new_filepath);
173
174 if (!at_exit) {
175 close(file->fd);
176 ret = perf_data_file__open(file);
177 if (ret < 0)
178 goto out;
179
180 if (lseek(file->fd, pos, SEEK_SET) == (off_t)-1) {
181 ret = -errno;
182 pr_debug("Failed to lseek to %zu: %s",
183 pos, strerror(errno));
184 goto out;
185 }
186 }
187 ret = file->fd;
188out:
189 free(new_filepath);
190 return ret;
191}