Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/compiler.h>
3#include <linux/kernel.h>
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <errno.h>
7#include <fcntl.h>
8#include <unistd.h>
9#include <string.h>
10
11#include "data.h"
12#include "util.h"
13#include "debug.h"
14
15static bool check_pipe(struct perf_data *data)
16{
17 struct stat st;
18 bool is_pipe = false;
19 int fd = perf_data__is_read(data) ?
20 STDIN_FILENO : STDOUT_FILENO;
21
22 if (!data->file.path) {
23 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
24 is_pipe = true;
25 } else {
26 if (!strcmp(data->file.path, "-"))
27 is_pipe = true;
28 }
29
30 if (is_pipe)
31 data->file.fd = fd;
32
33 return data->is_pipe = is_pipe;
34}
35
36static int check_backup(struct perf_data *data)
37{
38 struct stat st;
39
40 if (!stat(data->file.path, &st) && st.st_size) {
41 /* TODO check errors properly */
42 char oldname[PATH_MAX];
43 snprintf(oldname, sizeof(oldname), "%s.old",
44 data->file.path);
45 unlink(oldname);
46 rename(data->file.path, oldname);
47 }
48
49 return 0;
50}
51
52static int open_file_read(struct perf_data *data)
53{
54 struct stat st;
55 int fd;
56 char sbuf[STRERR_BUFSIZE];
57
58 fd = open(data->file.path, O_RDONLY);
59 if (fd < 0) {
60 int err = errno;
61
62 pr_err("failed to open %s: %s", data->file.path,
63 str_error_r(err, sbuf, sizeof(sbuf)));
64 if (err == ENOENT && !strcmp(data->file.path, "perf.data"))
65 pr_err(" (try 'perf record' first)");
66 pr_err("\n");
67 return -err;
68 }
69
70 if (fstat(fd, &st) < 0)
71 goto out_close;
72
73 if (!data->force && st.st_uid && (st.st_uid != geteuid())) {
74 pr_err("File %s not owned by current user or root (use -f to override)\n",
75 data->file.path);
76 goto out_close;
77 }
78
79 if (!st.st_size) {
80 pr_info("zero-sized data (%s), nothing to do!\n",
81 data->file.path);
82 goto out_close;
83 }
84
85 data->size = st.st_size;
86 return fd;
87
88 out_close:
89 close(fd);
90 return -1;
91}
92
93static int open_file_write(struct perf_data *data)
94{
95 int fd;
96 char sbuf[STRERR_BUFSIZE];
97
98 if (check_backup(data))
99 return -1;
100
101 fd = open(data->file.path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
102 S_IRUSR|S_IWUSR);
103
104 if (fd < 0)
105 pr_err("failed to open %s : %s\n", data->file.path,
106 str_error_r(errno, sbuf, sizeof(sbuf)));
107
108 return fd;
109}
110
111static int open_file(struct perf_data *data)
112{
113 int fd;
114
115 fd = perf_data__is_read(data) ?
116 open_file_read(data) : open_file_write(data);
117
118 data->file.fd = fd;
119 return fd < 0 ? -1 : 0;
120}
121
122int perf_data__open(struct perf_data *data)
123{
124 if (check_pipe(data))
125 return 0;
126
127 if (!data->file.path)
128 data->file.path = "perf.data";
129
130 return open_file(data);
131}
132
133void perf_data__close(struct perf_data *data)
134{
135 close(data->file.fd);
136}
137
138ssize_t perf_data_file__write(struct perf_data_file *file,
139 void *buf, size_t size)
140{
141 return writen(file->fd, buf, size);
142}
143
144ssize_t perf_data__write(struct perf_data *data,
145 void *buf, size_t size)
146{
147 return perf_data_file__write(&data->file, buf, size);
148}
149
150int perf_data__switch(struct perf_data *data,
151 const char *postfix,
152 size_t pos, bool at_exit)
153{
154 char *new_filepath;
155 int ret;
156
157 if (check_pipe(data))
158 return -EINVAL;
159 if (perf_data__is_read(data))
160 return -EINVAL;
161
162 if (asprintf(&new_filepath, "%s.%s", data->file.path, postfix) < 0)
163 return -ENOMEM;
164
165 /*
166 * Only fire a warning, don't return error, continue fill
167 * original file.
168 */
169 if (rename(data->file.path, new_filepath))
170 pr_warning("Failed to rename %s to %s\n", data->file.path, new_filepath);
171
172 if (!at_exit) {
173 close(data->file.fd);
174 ret = perf_data__open(data);
175 if (ret < 0)
176 goto out;
177
178 if (lseek(data->file.fd, pos, SEEK_SET) == (off_t)-1) {
179 ret = -errno;
180 pr_debug("Failed to lseek to %zu: %s",
181 pos, strerror(errno));
182 goto out;
183 }
184 }
185 ret = data->file.fd;
186out:
187 free(new_filepath);
188 return ret;
189}
1#include <linux/compiler.h>
2#include <linux/kernel.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <unistd.h>
6#include <string.h>
7
8#include "data.h"
9#include "util.h"
10#include "debug.h"
11
12static bool check_pipe(struct perf_data_file *file)
13{
14 struct stat st;
15 bool is_pipe = false;
16 int fd = perf_data_file__is_read(file) ?
17 STDIN_FILENO : STDOUT_FILENO;
18
19 if (!file->path) {
20 if (!fstat(fd, &st) && S_ISFIFO(st.st_mode))
21 is_pipe = true;
22 } else {
23 if (!strcmp(file->path, "-"))
24 is_pipe = true;
25 }
26
27 if (is_pipe)
28 file->fd = fd;
29
30 return file->is_pipe = is_pipe;
31}
32
33static int check_backup(struct perf_data_file *file)
34{
35 struct stat st;
36
37 if (!stat(file->path, &st) && st.st_size) {
38 /* TODO check errors properly */
39 char oldname[PATH_MAX];
40 snprintf(oldname, sizeof(oldname), "%s.old",
41 file->path);
42 unlink(oldname);
43 rename(file->path, oldname);
44 }
45
46 return 0;
47}
48
49static int open_file_read(struct perf_data_file *file)
50{
51 struct stat st;
52 int fd;
53 char sbuf[STRERR_BUFSIZE];
54
55 fd = open(file->path, O_RDONLY);
56 if (fd < 0) {
57 int err = errno;
58
59 pr_err("failed to open %s: %s", file->path,
60 str_error_r(err, sbuf, sizeof(sbuf)));
61 if (err == ENOENT && !strcmp(file->path, "perf.data"))
62 pr_err(" (try 'perf record' first)");
63 pr_err("\n");
64 return -err;
65 }
66
67 if (fstat(fd, &st) < 0)
68 goto out_close;
69
70 if (!file->force && st.st_uid && (st.st_uid != geteuid())) {
71 pr_err("File %s not owned by current user or root (use -f to override)\n",
72 file->path);
73 goto out_close;
74 }
75
76 if (!st.st_size) {
77 pr_info("zero-sized file (%s), nothing to do!\n",
78 file->path);
79 goto out_close;
80 }
81
82 file->size = st.st_size;
83 return fd;
84
85 out_close:
86 close(fd);
87 return -1;
88}
89
90static int open_file_write(struct perf_data_file *file)
91{
92 int fd;
93 char sbuf[STRERR_BUFSIZE];
94
95 if (check_backup(file))
96 return -1;
97
98 fd = open(file->path, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
99
100 if (fd < 0)
101 pr_err("failed to open %s : %s\n", file->path,
102 str_error_r(errno, sbuf, sizeof(sbuf)));
103
104 return fd;
105}
106
107static int open_file(struct perf_data_file *file)
108{
109 int fd;
110
111 fd = perf_data_file__is_read(file) ?
112 open_file_read(file) : open_file_write(file);
113
114 file->fd = fd;
115 return fd < 0 ? -1 : 0;
116}
117
118int perf_data_file__open(struct perf_data_file *file)
119{
120 if (check_pipe(file))
121 return 0;
122
123 if (!file->path)
124 file->path = "perf.data";
125
126 return open_file(file);
127}
128
129void perf_data_file__close(struct perf_data_file *file)
130{
131 close(file->fd);
132}
133
134ssize_t perf_data_file__write(struct perf_data_file *file,
135 void *buf, size_t size)
136{
137 return writen(file->fd, buf, size);
138}
139
140int perf_data_file__switch(struct perf_data_file *file,
141 const char *postfix,
142 size_t pos, bool at_exit)
143{
144 char *new_filepath;
145 int ret;
146
147 if (check_pipe(file))
148 return -EINVAL;
149 if (perf_data_file__is_read(file))
150 return -EINVAL;
151
152 if (asprintf(&new_filepath, "%s.%s", file->path, postfix) < 0)
153 return -ENOMEM;
154
155 /*
156 * Only fire a warning, don't return error, continue fill
157 * original file.
158 */
159 if (rename(file->path, new_filepath))
160 pr_warning("Failed to rename %s to %s\n", file->path, new_filepath);
161
162 if (!at_exit) {
163 close(file->fd);
164 ret = perf_data_file__open(file);
165 if (ret < 0)
166 goto out;
167
168 if (lseek(file->fd, pos, SEEK_SET) == (off_t)-1) {
169 ret = -errno;
170 pr_debug("Failed to lseek to %zu: %s",
171 pos, strerror(errno));
172 goto out;
173 }
174 }
175 ret = file->fd;
176out:
177 free(new_filepath);
178 return ret;
179}