1 // SPDX-License-Identifier: GPL-2.0
8 #include <sys/utsname.h>
17 #include <linux/kernel.h>
18 #include <linux/log2.h>
19 #include <linux/time64.h>
24 * XXX We need to find a better place for these things...
26 unsigned int page_size;
29 int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
30 int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
32 bool test_attr__enabled;
34 bool perf_host = true;
35 bool perf_guest = false;
37 void event_attr_init(struct perf_event_attr *attr)
40 attr->exclude_host = 1;
42 attr->exclude_guest = 1;
43 /* to capture ABI version */
44 attr->size = sizeof(*attr);
47 int mkdir_p(char *path, mode_t mode)
56 if (stat(path, &st) == 0)
61 while ((d = strchr(d, '/'))) {
63 err = stat(path, &st) && mkdir(path, mode);
70 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
73 int rm_rf(const char *path)
78 char namebuf[PATH_MAX];
84 while ((d = readdir(dir)) != NULL && !ret) {
87 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
90 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
93 /* We have to check symbolic link itself */
94 ret = lstat(namebuf, &statbuf);
96 pr_debug("stat failed: %s\n", namebuf);
100 if (S_ISDIR(statbuf.st_mode))
101 ret = rm_rf(namebuf);
103 ret = unlink(namebuf);
113 /* A filter which removes dot files */
114 bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
116 return d->d_name[0] != '.';
119 /* lsdir reads a directory and store it in strlist */
120 struct strlist *lsdir(const char *name,
121 bool (*filter)(const char *, struct dirent *))
123 struct strlist *list = NULL;
131 list = strlist__new(NULL, NULL);
137 while ((d = readdir(dir)) != NULL) {
138 if (!filter || filter(name, d))
139 strlist__add(list, d->d_name);
147 static int slow_copyfile(const char *from, const char *to, struct nsinfo *nsi)
152 FILE *from_fp, *to_fp;
155 nsinfo__mountns_enter(nsi, &nsc);
156 from_fp = fopen(from, "r");
157 nsinfo__mountns_exit(&nsc);
161 to_fp = fopen(to, "w");
163 goto out_fclose_from;
165 while (getline(&line, &n, from_fp) > 0)
166 if (fputs(line, to_fp) == EOF)
178 int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
183 pgoff = off_in & ~(page_size - 1);
186 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
187 if (ptr == MAP_FAILED)
191 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
192 if (ret < 0 && errno == EINTR)
201 munmap(ptr, off_in + size);
203 return size ? -1 : 0;
206 static int copyfile_mode_ns(const char *from, const char *to, mode_t mode,
212 char *tmp = NULL, *ptr = NULL;
215 nsinfo__mountns_enter(nsi, &nsc);
216 err = stat(from, &st);
217 nsinfo__mountns_exit(&nsc);
222 /* extra 'x' at the end is to reserve space for '.' */
223 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
227 ptr = strrchr(tmp, '/');
230 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
237 if (fchmod(tofd, mode))
240 if (st.st_size == 0) { /* /proc? do it slowly... */
241 err = slow_copyfile(from, tmp, nsi);
245 nsinfo__mountns_enter(nsi, &nsc);
246 fromfd = open(from, O_RDONLY);
247 nsinfo__mountns_exit(&nsc);
251 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
264 int copyfile_ns(const char *from, const char *to, struct nsinfo *nsi)
266 return copyfile_mode_ns(from, to, 0755, nsi);
269 int copyfile_mode(const char *from, const char *to, mode_t mode)
271 return copyfile_mode_ns(from, to, mode, NULL);
274 int copyfile(const char *from, const char *to)
276 return copyfile_mode(from, to, 0755);
279 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
281 void *buf_start = buf;
285 /* buf must be treated as const if !is_read. */
286 ssize_t ret = is_read ? read(fd, buf, left) :
287 write(fd, buf, left);
289 if (ret < 0 && errno == EINTR)
298 BUG_ON((size_t)(buf - buf_start) != n);
303 * Read exactly 'n' bytes or return an error.
305 ssize_t readn(int fd, void *buf, size_t n)
307 return ion(true, fd, buf, n);
311 * Write exactly 'n' bytes or return an error.
313 ssize_t writen(int fd, const void *buf, size_t n)
315 /* ion does not modify buf. */
316 return ion(false, fd, (void *)buf, n);
319 size_t hex_width(u64 v)
329 static int hex(char ch)
331 if ((ch >= '0') && (ch <= '9'))
333 if ((ch >= 'a') && (ch <= 'f'))
334 return ch - 'a' + 10;
335 if ((ch >= 'A') && (ch <= 'F'))
336 return ch - 'A' + 10;
341 * While we find nice hex chars, build a long_val.
342 * Return number of chars processed.
344 int hex2u64(const char *ptr, u64 *long_val)
350 const int hex_val = hex(*p);
355 *long_val = (*long_val << 4) | hex_val;
362 int perf_event_paranoid(void)
366 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
372 fetch_ubuntu_kernel_version(unsigned int *puint)
376 char *ptr, *line = NULL;
377 int version, patchlevel, sublevel, err;
383 vsig = fopen("/proc/version_signature", "r");
385 pr_debug("Open /proc/version_signature failed: %s\n",
390 len = getline(&line, &line_len, vsig);
394 pr_debug("Reading from /proc/version_signature failed: %s\n",
399 ptr = strrchr(line, ' ');
401 pr_debug("Parsing /proc/version_signature failed: %s\n", line);
405 err = sscanf(ptr + 1, "%d.%d.%d",
406 &version, &patchlevel, &sublevel);
408 pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
413 *puint = (version << 16) + (patchlevel << 8) + sublevel;
421 fetch_kernel_version(unsigned int *puint, char *str,
424 struct utsname utsname;
425 int version, patchlevel, sublevel, err;
426 bool int_ver_ready = false;
428 if (access("/proc/version_signature", R_OK) == 0)
429 if (!fetch_ubuntu_kernel_version(puint))
430 int_ver_ready = true;
435 if (str && str_size) {
436 strncpy(str, utsname.release, str_size);
437 str[str_size - 1] = '\0';
440 if (!puint || int_ver_ready)
443 err = sscanf(utsname.release, "%d.%d.%d",
444 &version, &patchlevel, &sublevel);
447 pr_debug("Unable to get kernel version from uname '%s'\n",
452 *puint = (version << 16) + (patchlevel << 8) + sublevel;
456 const char *perf_tip(const char *dirpath)
458 struct strlist *tips;
459 struct str_node *node;
461 struct strlist_config conf = {
466 tips = strlist__new("tips.txt", &conf);
468 return errno == ENOENT ? NULL :
469 "Tip: check path of tips.txt or get more memory! ;-p";
471 if (strlist__nr_entries(tips) == 0)
474 node = strlist__entry(tips, random() % strlist__nr_entries(tips));
475 if (asprintf(&tip, "Tip: %s", node->s) < 0)
476 tip = (char *)"Tip: get more memory! ;-)";
479 strlist__delete(tips);