xref: /linux/tools/perf/util/data.c (revision 4246b92cf9fb32da8d8b060c92d8302797c6fbea)
1 #include <linux/compiler.h>
2 #include <linux/kernel.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <errno.h>
6 #include <unistd.h>
7 #include <string.h>
8 
9 #include "data.h"
10 #include "util.h"
11 #include "debug.h"
12 
13 #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 
23 static 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 
44 static 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 
60 static int open_file_read(struct perf_data_file *file)
61 {
62 	struct stat st;
63 	int fd;
64 	char sbuf[STRERR_BUFSIZE];
65 
66 	fd = open(file->path, O_RDONLY);
67 	if (fd < 0) {
68 		int err = errno;
69 
70 		pr_err("failed to open %s: %s", file->path,
71 			str_error_r(err, sbuf, sizeof(sbuf)));
72 		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())) {
82 		pr_err("File %s not owned by current user or root (use -f to override)\n",
83 		       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 
101 static int open_file_write(struct perf_data_file *file)
102 {
103 	int fd;
104 	char sbuf[STRERR_BUFSIZE];
105 
106 	if (check_backup(file))
107 		return -1;
108 
109 	fd = open(file->path, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC,
110 		  S_IRUSR|S_IWUSR);
111 
112 	if (fd < 0)
113 		pr_err("failed to open %s : %s\n", file->path,
114 			str_error_r(errno, sbuf, sizeof(sbuf)));
115 
116 	return fd;
117 }
118 
119 static 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 
130 int 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 
141 void perf_data_file__close(struct perf_data_file *file)
142 {
143 	close(file->fd);
144 }
145 
146 ssize_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 }
151 
152 int 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;
188 out:
189 	free(new_filepath);
190 	return ret;
191 }
192