~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/util/data.c

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
 12 static 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 
 33 static 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 
 49 static 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                         strerror_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 
 90 static 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                         strerror_r(errno, sbuf, sizeof(sbuf)));
103 
104         return fd;
105 }
106 
107 static 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 
118 int 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 
129 void perf_data_file__close(struct perf_data_file *file)
130 {
131         close(file->fd);
132 }
133 
134 ssize_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 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp