Commit | Line | Data |
---|---|---|
0fdd1e2c DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; only version 2 of the License. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along with | |
15 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
16 | * Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | */ | |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #include <fcntl.h> | |
21 | #include <limits.h> | |
22 | #include <sys/mman.h> | |
23 | #include <sys/stat.h> | |
24 | #include <sys/types.h> | |
25 | #include <sys/wait.h> | |
26 | #include <unistd.h> | |
27 | #include <urcu.h> | |
28 | ||
29 | #include <lttngerr.h> | |
30 | ||
62d53818 DG |
31 | #include "benchmark.h" |
32 | #include "measures.h" | |
0fdd1e2c DG |
33 | #include "shm.h" |
34 | ||
35 | /* | |
36 | * Using fork to set umask in the child process (not multi-thread safe). We | |
37 | * deal with the shm_open vs ftruncate race (happening when the sessiond owns | |
38 | * the shm and does not let everybody modify it, to ensure safety against | |
39 | * shm_unlink) by simply letting the mmap fail and retrying after a few | |
40 | * seconds. For global shm, everybody has rw access to it until the sessiond | |
41 | * starts. | |
42 | */ | |
43 | static int get_wait_shm(char *shm_path, size_t mmap_size, int global) | |
44 | { | |
45 | int wait_shm_fd, ret; | |
0fdd1e2c DG |
46 | mode_t mode; |
47 | ||
62d53818 DG |
48 | tracepoint(ust_notify_perms_start); |
49 | ||
0fdd1e2c DG |
50 | /* Default permissions */ |
51 | mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; | |
52 | ||
53 | /* Change owner of the shm path */ | |
54 | if (global) { | |
55 | ret = chown(shm_path, 0, 0); | |
56 | if (ret < 0) { | |
57 | if (errno != ENOENT) { | |
58 | perror("chown wait shm"); | |
59 | goto error; | |
60 | } | |
61 | } | |
62 | ||
63 | /* | |
64 | * If global session daemon, any application can register so the shm | |
65 | * needs to be set in read-only mode for others. | |
66 | */ | |
67 | mode |= S_IROTH; | |
68 | } else { | |
69 | ret = chown(shm_path, getuid(), getgid()); | |
70 | if (ret < 0) { | |
71 | if (errno != ENOENT) { | |
72 | perror("chown wait shm"); | |
73 | goto error; | |
74 | } | |
75 | } | |
76 | } | |
77 | ||
78 | /* | |
79 | * Set permissions to the shm even if we did not create the shm. | |
80 | */ | |
81 | ret = chmod(shm_path, mode); | |
82 | if (ret < 0) { | |
83 | if (errno != ENOENT) { | |
84 | perror("chmod wait shm"); | |
85 | goto error; | |
86 | } | |
87 | } | |
88 | ||
89 | /* | |
7d051034 DG |
90 | * We're alone in a child process, so we can modify the process-wide |
91 | * umask. | |
0fdd1e2c | 92 | */ |
7d051034 | 93 | umask(~mode); |
0fdd1e2c | 94 | |
62d53818 DG |
95 | tracepoint(ust_notify_perms_stop); |
96 | ||
97 | tracepoint(ust_notify_shm_start); | |
7d051034 DG |
98 | /* |
99 | * Try creating shm (or get rw access). We don't do an exclusive open, | |
100 | * because we allow other processes to create+ftruncate it concurrently. | |
101 | */ | |
102 | wait_shm_fd = shm_open(shm_path, O_RDWR | O_CREAT, mode); | |
103 | if (wait_shm_fd < 0) { | |
104 | perror("shm_open wait shm"); | |
105 | goto error; | |
106 | } | |
0fdd1e2c | 107 | |
7d051034 DG |
108 | ret = ftruncate(wait_shm_fd, mmap_size); |
109 | if (ret < 0) { | |
110 | perror("ftruncate wait shm"); | |
111 | exit(EXIT_FAILURE); | |
112 | } | |
0fdd1e2c | 113 | |
7d051034 DG |
114 | ret = fchmod(wait_shm_fd, mode); |
115 | if (ret < 0) { | |
116 | perror("fchmod"); | |
0fdd1e2c | 117 | exit(EXIT_FAILURE); |
0fdd1e2c DG |
118 | } |
119 | ||
62d53818 DG |
120 | tracepoint(ust_notify_shm_stop); |
121 | ||
0fdd1e2c DG |
122 | DBG("Got the wait shm fd %d", wait_shm_fd); |
123 | ||
124 | return wait_shm_fd; | |
125 | ||
126 | error: | |
127 | DBG("Failing to get the wait shm fd"); | |
128 | ||
129 | return -1; | |
130 | } | |
131 | ||
132 | /* | |
133 | * Return the wait shm mmap for UST application notification. The global | |
134 | * variable is used to indicate if the the session daemon is global | |
135 | * (root:tracing) or running with an unprivileged user. | |
136 | * | |
137 | * This returned value is used by futex_wait_update() in futex.c to WAKE all | |
138 | * waiters which are UST application waiting for a session daemon. | |
139 | */ | |
140 | char *shm_ust_get_mmap(char *shm_path, int global) | |
141 | { | |
142 | size_t mmap_size = sysconf(_SC_PAGE_SIZE); | |
143 | int wait_shm_fd, ret; | |
144 | char *wait_shm_mmap; | |
145 | ||
146 | wait_shm_fd = get_wait_shm(shm_path, mmap_size, global); | |
147 | if (wait_shm_fd < 0) { | |
148 | goto error; | |
149 | } | |
150 | ||
62d53818 DG |
151 | tracepoint(ust_notify_mmap_start); |
152 | ||
0fdd1e2c DG |
153 | wait_shm_mmap = mmap(NULL, mmap_size, PROT_WRITE | PROT_READ, |
154 | MAP_SHARED, wait_shm_fd, 0); | |
7d051034 | 155 | |
62d53818 | 156 | tracepoint(ust_notify_mmap_stop); |
0fdd1e2c DG |
157 | /* close shm fd immediately after taking the mmap reference */ |
158 | ret = close(wait_shm_fd); | |
159 | if (ret) { | |
160 | perror("Error closing fd"); | |
161 | } | |
162 | ||
163 | if (wait_shm_mmap == MAP_FAILED) { | |
164 | DBG("mmap error (can be caused by race with ust)."); | |
165 | goto error; | |
166 | } | |
167 | ||
168 | return wait_shm_mmap; | |
169 | ||
170 | error: | |
171 | return NULL; | |
172 | } |