perf machine: Fix the lifetime of the VDSO temporary file
[deliverable/linux.git] / tools / perf / util / vdso.c
CommitLineData
7dbf4dcf
JO
1
2#include <unistd.h>
3#include <stdio.h>
4#include <string.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <stdlib.h>
9#include <linux/kernel.h>
10
11#include "vdso.h"
12#include "util.h"
13#include "symbol.h"
2a03068c 14#include "machine.h"
7dbf4dcf 15#include "linux/string.h"
84f5d36f 16#include "debug.h"
7dbf4dcf 17
30f4f815
AH
18#define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
19
20struct vdso_file {
21 bool found;
22 bool error;
23 char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)];
24 const char *dso_name;
25};
26
27struct vdso_info {
28 struct vdso_file vdso;
29};
30
d027b640
AH
31static struct vdso_info *vdso_info__new(void)
32{
33 static const struct vdso_info vdso_info_init = {
34 .vdso = {
35 .temp_file_name = VDSO__TEMP_FILE_NAME,
36 .dso_name = VDSO__MAP_NAME,
37 },
38 };
39
40 return memdup(&vdso_info_init, sizeof(vdso_info_init));
41}
7dbf4dcf
JO
42
43static int find_vdso_map(void **start, void **end)
44{
45 FILE *maps;
46 char line[128];
47 int found = 0;
48
49 maps = fopen("/proc/self/maps", "r");
50 if (!maps) {
51 pr_err("vdso: cannot open maps\n");
52 return -1;
53 }
54
55 while (!found && fgets(line, sizeof(line), maps)) {
56 int m = -1;
57
58 /* We care only about private r-x mappings. */
59 if (2 != sscanf(line, "%p-%p r-xp %*x %*x:%*x %*u %n",
60 start, end, &m))
61 continue;
62 if (m < 0)
63 continue;
64
65 if (!strncmp(&line[m], VDSO__MAP_NAME,
66 sizeof(VDSO__MAP_NAME) - 1))
67 found = 1;
68 }
69
70 fclose(maps);
71 return !found;
72}
73
30f4f815 74static char *get_file(struct vdso_file *vdso_file)
7dbf4dcf
JO
75{
76 char *vdso = NULL;
77 char *buf = NULL;
78 void *start, *end;
79 size_t size;
80 int fd;
81
30f4f815
AH
82 if (vdso_file->found)
83 return vdso_file->temp_file_name;
7dbf4dcf 84
30f4f815 85 if (vdso_file->error || find_vdso_map(&start, &end))
7dbf4dcf
JO
86 return NULL;
87
88 size = end - start;
89
90 buf = memdup(start, size);
91 if (!buf)
92 return NULL;
93
30f4f815 94 fd = mkstemp(vdso_file->temp_file_name);
7dbf4dcf
JO
95 if (fd < 0)
96 goto out;
97
98 if (size == (size_t) write(fd, buf, size))
30f4f815 99 vdso = vdso_file->temp_file_name;
7dbf4dcf
JO
100
101 close(fd);
102
103 out:
104 free(buf);
105
30f4f815
AH
106 vdso_file->found = (vdso != NULL);
107 vdso_file->error = !vdso_file->found;
7dbf4dcf
JO
108 return vdso;
109}
110
d027b640 111void vdso__exit(struct machine *machine)
7dbf4dcf 112{
d027b640
AH
113 struct vdso_info *vdso_info = machine->vdso_info;
114
115 if (!vdso_info)
116 return;
117
30f4f815
AH
118 if (vdso_info->vdso.found)
119 unlink(vdso_info->vdso.temp_file_name);
d027b640
AH
120
121 zfree(&machine->vdso_info);
7dbf4dcf
JO
122}
123
2a03068c 124struct dso *vdso__dso_findnew(struct machine *machine)
7dbf4dcf 125{
d027b640
AH
126 struct vdso_info *vdso_info;
127 struct dso *dso;
128
129 if (!machine->vdso_info)
130 machine->vdso_info = vdso_info__new();
131
132 vdso_info = machine->vdso_info;
133 if (!vdso_info)
134 return NULL;
7dbf4dcf 135
d027b640 136 dso = dsos__find(&machine->user_dsos, VDSO__MAP_NAME, true);
7dbf4dcf
JO
137 if (!dso) {
138 char *file;
139
30f4f815 140 file = get_file(&vdso_info->vdso);
7dbf4dcf
JO
141 if (!file)
142 return NULL;
143
144 dso = dso__new(VDSO__MAP_NAME);
145 if (dso != NULL) {
2a03068c 146 dsos__add(&machine->user_dsos, dso);
7e155d4d 147 dso__set_long_name(dso, file, false);
7dbf4dcf
JO
148 }
149 }
150
151 return dso;
152}
This page took 0.118547 seconds and 5 git commands to generate.