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