Add UST namespace contexts
[lttng-tools.git] / src / bin / lttng-sessiond / shm.c
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
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _LGPL_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 <common/error.h>
30
31 #include "shm.h"
32
33 /*
34 * Using fork to set umask in the child process (not multi-thread safe). We
35 * deal with the shm_open vs ftruncate race (happening when the sessiond owns
36 * the shm and does not let everybody modify it, to ensure safety against
37 * shm_unlink) by simply letting the mmap fail and retrying after a few
38 * seconds. For global shm, everybody has rw access to it until the sessiond
39 * starts.
40 */
41 static int get_wait_shm(char *shm_path, size_t mmap_size, int global)
42 {
43 int wait_shm_fd, ret;
44 mode_t mode;
45
46 assert(shm_path);
47
48 /* Default permissions */
49 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
50
51 /*
52 * Change owner of the shm path.
53 */
54 if (global) {
55 /*
56 * If global session daemon, any application can
57 * register. Make it initially writeable so applications
58 * registering concurrently can do ftruncate() by
59 * themselves.
60 */
61 mode |= S_IROTH | S_IWOTH;
62 }
63
64 /*
65 * We're alone in a child process, so we can modify the process-wide
66 * umask.
67 */
68 umask(~mode);
69
70 /*
71 * Try creating shm (or get rw access). We don't do an exclusive open,
72 * because we allow other processes to create+ftruncate it concurrently.
73 *
74 * A sysctl, fs.protected_regular may prevent the session daemon from
75 * opening a previously created shm when the O_CREAT flag is provided.
76 * Systemd enables this ABI-breaking change by default since v241.
77 *
78 * First, attempt to use the create-or-open semantic that is
79 * desired here. If this fails with EACCES, work around this broken
80 * behaviour and attempt to open the shm without the O_CREAT flag.
81 *
82 * The two attempts are made in this order since applications are
83 * expected to race with the session daemon to create this shm.
84 * Attempting an shm_open() without the O_CREAT flag first could fail
85 * because the file doesn't exist. It could then be created by an
86 * application, which would cause a second try with the O_CREAT flag to
87 * fail with EACCES.
88 *
89 * Note that this introduces a new failure mode where a user could
90 * launch an application (creating the shm) and unlink the shm while
91 * the session daemon is launching, causing the second attempt
92 * to fail. This is not recovered-from as unlinking the shm will
93 * prevent userspace tracing from succeeding anyhow: the sessiond would
94 * use a now-unlinked shm, while the next application would create
95 * a new named shm.
96 */
97 wait_shm_fd = shm_open(shm_path, O_RDWR | O_CREAT, mode);
98 if (wait_shm_fd < 0) {
99 if (errno == EACCES) {
100 /* Work around sysctl fs.protected_regular. */
101 DBG("shm_open of %s returned EACCES, this may be caused "
102 "by the fs.protected_regular sysctl. "
103 "Attempting to open the shm without "
104 "creating it.", shm_path);
105 wait_shm_fd = shm_open(shm_path, O_RDWR, mode);
106 }
107 if (wait_shm_fd < 0) {
108 PERROR("Failed to open wait shm at %s", shm_path);
109 goto error;
110 }
111 }
112
113 ret = ftruncate(wait_shm_fd, mmap_size);
114 if (ret < 0) {
115 PERROR("ftruncate wait shm");
116 exit(EXIT_FAILURE);
117 }
118
119 #ifndef __FreeBSD__
120 if (global) {
121 ret = fchown(wait_shm_fd, 0, 0);
122 if (ret < 0) {
123 PERROR("fchown");
124 exit(EXIT_FAILURE);
125 }
126 /*
127 * If global session daemon, any application can
128 * register so the shm needs to be set in read-only mode
129 * for others.
130 */
131 mode &= ~S_IWOTH;
132 ret = fchmod(wait_shm_fd, mode);
133 if (ret < 0) {
134 PERROR("fchmod");
135 exit(EXIT_FAILURE);
136 }
137 } else {
138 ret = fchown(wait_shm_fd, getuid(), getgid());
139 if (ret < 0) {
140 PERROR("fchown");
141 exit(EXIT_FAILURE);
142 }
143 }
144 #else
145 #warning "FreeBSD does not support setting file mode on shm FD."
146 #endif
147
148 DBG("Got the wait shm fd %d", wait_shm_fd);
149
150 return wait_shm_fd;
151
152 error:
153 DBG("Failing to get the wait shm fd");
154
155 return -1;
156 }
157
158 /*
159 * Return the wait shm mmap for UST application notification. The global
160 * variable is used to indicate if the the session daemon is global
161 * (root:tracing) or running with an unprivileged user.
162 *
163 * This returned value is used by futex_wait_update() in futex.c to WAKE all
164 * waiters which are UST application waiting for a session daemon.
165 */
166 char *shm_ust_get_mmap(char *shm_path, int global)
167 {
168 size_t mmap_size;
169 int wait_shm_fd, ret;
170 char *wait_shm_mmap;
171 long sys_page_size;
172
173 assert(shm_path);
174
175 sys_page_size = sysconf(_SC_PAGE_SIZE);
176 if (sys_page_size < 0) {
177 PERROR("sysconf PAGE_SIZE");
178 goto error;
179 }
180 mmap_size = sys_page_size;
181
182 wait_shm_fd = get_wait_shm(shm_path, mmap_size, global);
183 if (wait_shm_fd < 0) {
184 goto error;
185 }
186
187 wait_shm_mmap = mmap(NULL, mmap_size, PROT_WRITE | PROT_READ,
188 MAP_SHARED, wait_shm_fd, 0);
189
190 /* close shm fd immediately after taking the mmap reference */
191 ret = close(wait_shm_fd);
192 if (ret) {
193 PERROR("Error closing fd");
194 }
195
196 if (wait_shm_mmap == MAP_FAILED) {
197 DBG("mmap error (can be caused by race with ust).");
198 goto error;
199 }
200
201 return wait_shm_mmap;
202
203 error:
204 return NULL;
205 }
This page took 0.032534 seconds and 4 git commands to generate.