1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/kernel.h>
4 #include <linux/string.h>
5 #include <linux/zalloc.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <asm/bug.h>
13 #include <dirent.h>
14
15 #include "data.h"
16 #include "util.h" // rm_rf_perf_data()
17 #include "debug.h"
18 #include "header.h"
19 #include <internal/lib.h>
20
close_dir(struct perf_data_file * files,int nr)21 static void close_dir(struct perf_data_file *files, int nr)
22 {
23 while (--nr >= 0) {
24 close(files[nr].fd);
25 zfree(&files[nr].path);
26 }
27 free(files);
28 }
29
perf_data__close_dir(struct perf_data * data)30 void perf_data__close_dir(struct perf_data *data)
31 {
32 close_dir(data->dir.files, data->dir.nr);
33 }
34
perf_data__create_dir(struct perf_data * data,int nr)35 int perf_data__create_dir(struct perf_data *data, int nr)
36 {
37 struct perf_data_file *files = NULL;
38 int i, ret;
39
40 if (WARN_ON(!data->is_dir))
41 return -EINVAL;
42
43 files = zalloc(nr * sizeof(*files));
44 if (!files)
45 return -ENOMEM;
46
47 for (i = 0; i < nr; i++) {
48 struct perf_data_file *file = &files[i];
49
50 ret = asprintf(&file->path, "%s/data.%d", data->path, i);
51 if (ret < 0)
52 goto out_err;
53
54 ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
55 if (ret < 0)
56 goto out_err;
57
58 file->fd = ret;
59 }
60
61 data->dir.version = PERF_DIR_VERSION;
62 data->dir.files = files;
63 data->dir.nr = nr;
64 return 0;
65
66 out_err:
67 close_dir(files, i);
68 return ret;
69 }
70
perf_data__open_dir(struct perf_data * data)71 int perf_data__open_dir(struct perf_data *data)
72 {
73 struct perf_data_file *files = NULL;
74 struct dirent *dent;
75 int ret = -1;
76 DIR *dir;
77 int nr = 0;
78
79 if (WARN_ON(!data->is_dir))
80 return -EINVAL;
81
82 /* The version is provided by DIR_FORMAT feature. */
83 if (WARN_ON(data->dir.version != PERF_DIR_VERSION))
84 return -1;
85
86 dir = opendir(data->path);
87 if (!dir)
88 return -EINVAL;
89
90 while ((dent = readdir(dir)) != NULL) {
91 struct perf_data_file *file;
92 char path[PATH_MAX];
93 struct stat st;
94
95 snprintf(path, sizeof(path), "%s/%s", data->path, dent->d_name);
96 if (stat(path, &st))
97 continue;
98
99 if (!S_ISREG(st.st_mode) || strncmp(dent->d_name, "data", 4))
100 continue;
101
102 ret = -ENOMEM;
103
104 file = realloc(files, (nr + 1) * sizeof(*files));
105 if (!file)
106 goto out_err;
107
108 files = file;
109 file = &files[nr++];
110
111 file->path = strdup(path);
112 if (!file->path)
113 goto out_err;
114
115 ret = open(file->path, O_RDONLY);
116 if (ret < 0)
117 goto out_err;
118
119 file->fd = ret;
120 file->size = st.st_size;
121 }
122
123 closedir(dir);
124 if (!files)
125 return -EINVAL;
126
127 data->dir.files = files;
128 data->dir.nr = nr;
129 return 0;
130
131 out_err:
132 closedir(dir);
133 close_dir(files, nr);
134 return ret;
135 }
136
perf_data__update_dir(struct perf_data * data)137 int perf_data__update_dir(struct perf_data *data)
138 {
139 int i;
140
141 if (WARN_ON(!data->is_dir))
142 return -EINVAL;
143
144 for (i = 0; i < data->dir.nr; i++) {
145 struct perf_data_file *file = &data->dir.files[i];
146 struct stat st;
147
148 if (fstat(file->fd, &st))
149 return -1;
150
151 file->size = st.st_size;
152 }
153
154 return 0;
155 }
156
check_pipe(struct perf_data * data)157 static bool check_pipe(struct perf_data *data)
158 {
159 struct stat st;
160 bool is_pipe = false;
161 int fd = perf_data__is_read(data) ?
162 STDIN_FILENO : STDOUT_FILENO;
163
164 if (!data->path) {
165 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
166 is_pipe = true;
167 } else {
168 if (!strcmp(data->path, "-"))
169 is_pipe = true;
170 }
171
172 if (is_pipe)
173 data->file.fd = fd;
174
175 return data->is_pipe = is_pipe;
176 }
177
check_backup(struct perf_data * data)178 static int check_backup(struct perf_data *data)
179 {
180 struct stat st;
181
182 if (perf_data__is_read(data))
183 return 0;
184
185 if (!stat(data->path, &st) && st.st_size) {
186 char oldname[PATH_MAX];
187 int ret;
188
189 snprintf(oldname, sizeof(oldname), "%s.old",
190 data->path);
191
192 ret = rm_rf_perf_data(oldname);
193 if (ret) {
194 pr_err("Can't remove old data: %s (%s)\n",
195 ret == -2 ?
196 "Unknown file found" : strerror(errno),
197 oldname);
198 return -1;
199 }
200
201 if (rename(data->path, oldname)) {
202 pr_err("Can't move data: %s (%s to %s)\n",
203 strerror(errno),
204 data->path, oldname);
205 return -1;
206 }
207 }
208
209 return 0;
210 }
211
is_dir(struct perf_data * data)212 static bool is_dir(struct perf_data *data)
213 {
214 struct stat st;
215
216 if (stat(data->path, &st))
217 return false;
218
219 return (st.st_mode & S_IFMT) == S_IFDIR;
220 }
221
open_file_read(struct perf_data * data)222 static int open_file_read(struct perf_data *data)
223 {
224 struct stat st;
225 int fd;
226 char sbuf[STRERR_BUFSIZE];
227
228 fd = open(data->file.path, O_RDONLY);
229 if (fd < 0) {
230 int err = errno;
231
232 pr_err("failed to open %s: %s", data->file.path,
233 str_error_r(err, sbuf, sizeof(sbuf)));
234 if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
235 pr_err(" (try 'perf record' first)");
236 pr_err("\n");
237 return -err;
238 }
239
240 if (fstat(fd, &st) < 0)
241 goto out_close;
242
243 if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
244 pr_err("File %s not owned by current user or root (use -f to override)\n",
245 data->file.path);
246 goto out_close;
247 }
248
249 if (!st.st_size) {
250 pr_info("zero-sized data (%s), nothing to do!\n",
251 data->file.path);
252 goto out_close;
253 }
254
255 data->file.size = st.st_size;
256 return fd;
257
258 out_close:
259 close(fd);
260 return -1;
261 }
262
open_file_write(struct perf_data * data)263 static int open_file_write(struct perf_data *data)
264 {
265 int fd;
266 char sbuf[STRERR_BUFSIZE];
267
268 fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
269 S_IRUSR|S_IWUSR);
270
271 if (fd < 0)
272 pr_err("failed to open %s : %s\n", data->file.path,
273 str_error_r(errno, sbuf, sizeof(sbuf)));
274
275 return fd;
276 }
277
open_file(struct perf_data * data)278 static int open_file(struct perf_data *data)
279 {
280 int fd;
281
282 fd = perf_data__is_read(data) ?
283 open_file_read(data) : open_file_write(data);
284
285 if (fd < 0) {
286 zfree(&data->file.path);
287 return -1;
288 }
289
290 data->file.fd = fd;
291 return 0;
292 }
293
open_file_dup(struct perf_data * data)294 static int open_file_dup(struct perf_data *data)
295 {
296 data->file.path = strdup(data->path);
297 if (!data->file.path)
298 return -ENOMEM;
299
300 return open_file(data);
301 }
302
open_dir(struct perf_data * data)303 static int open_dir(struct perf_data *data)
304 {
305 int ret;
306
307 /*
308 * So far we open only the header, so we can read the data version and
309 * layout.
310 */
311 if (asprintf(&data->file.path, "%s/header", data->path) < 0)
312 return -1;
313
314 if (perf_data__is_write(data) &&
315 mkdir(data->path, S_IRWXU) < 0)
316 return -1;
317
318 ret = open_file(data);
319
320 /* Cleanup whatever we managed to create so far. */
321 if (ret && perf_data__is_write(data))
322 rm_rf_perf_data(data->path);
323
324 return ret;
325 }
326
perf_data__open(struct perf_data * data)327 int perf_data__open(struct perf_data *data)
328 {
329 if (check_pipe(data))
330 return 0;
331
332 if (!data->path)
333 data->path = "perf.data";
334
335 if (check_backup(data))
336 return -1;
337
338 if (perf_data__is_read(data))
339 data->is_dir = is_dir(data);
340
341 return perf_data__is_dir(data) ?
342 open_dir(data) : open_file_dup(data);
343 }
344
perf_data__close(struct perf_data * data)345 void perf_data__close(struct perf_data *data)
346 {
347 if (perf_data__is_dir(data))
348 perf_data__close_dir(data);
349
350 zfree(&data->file.path);
351 close(data->file.fd);
352 }
353
perf_data_file__write(struct perf_data_file * file,void * buf,size_t size)354 ssize_t perf_data_file__write(struct perf_data_file *file,
355 void *buf, size_t size)
356 {
357 return writen(file->fd, buf, size);
358 }
359
perf_data__write(struct perf_data * data,void * buf,size_t size)360 ssize_t perf_data__write(struct perf_data *data,
361 void *buf, size_t size)
362 {
363 return perf_data_file__write(&data->file, buf, size);
364 }
365
perf_data__switch(struct perf_data * data,const char * postfix,size_t pos,bool at_exit,char ** new_filepath)366 int perf_data__switch(struct perf_data *data,
367 const char *postfix,
368 size_t pos, bool at_exit,
369 char **new_filepath)
370 {
371 int ret;
372
373 if (check_pipe(data))
374 return -EINVAL;
375 if (perf_data__is_read(data))
376 return -EINVAL;
377
378 if (asprintf(new_filepath, "%s.%s", data->path, postfix) < 0)
379 return -ENOMEM;
380
381 /*
382 * Only fire a warning, don't return error, continue fill
383 * original file.
384 */
385 if (rename(data->path, *new_filepath))
386 pr_warning("Failed to rename %s to %s\n", data->path, *new_filepath);
387
388 if (!at_exit) {
389 close(data->file.fd);
390 ret = perf_data__open(data);
391 if (ret < 0)
392 goto out;
393
394 if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
395 ret = -errno;
396 pr_debug("Failed to lseek to %zu: %s",
397 pos, strerror(errno));
398 goto out;
399 }
400 }
401 ret = data->file.fd;
402 out:
403 return ret;
404 }
405
perf_data__size(struct perf_data * data)406 unsigned long perf_data__size(struct perf_data *data)
407 {
408 u64 size = data->file.size;
409 int i;
410
411 if (!data->is_dir)
412 return size;
413
414 for (i = 0; i < data->dir.nr; i++) {
415 struct perf_data_file *file = &data->dir.files[i];
416
417 size += file->size;
418 }
419
420 return size;
421 }
422