Commit | Line | Data |
---|---|---|
0fdd1e2c DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
d14d33bf AM |
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. | |
0fdd1e2c | 8 | * |
d14d33bf AM |
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. | |
0fdd1e2c | 13 | * |
d14d33bf AM |
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. | |
0fdd1e2c DG |
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 | ||
db758600 | 29 | #include <common/error.h> |
0fdd1e2c DG |
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; | |
0fdd1e2c DG |
44 | mode_t mode; |
45 | ||
0525e9ae DG |
46 | assert(shm_path); |
47 | ||
0fdd1e2c DG |
48 | /* Default permissions */ |
49 | mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; | |
50 | ||
51 | /* Change owner of the shm path */ | |
52 | if (global) { | |
53 | ret = chown(shm_path, 0, 0); | |
54 | if (ret < 0) { | |
55 | if (errno != ENOENT) { | |
df0f840b | 56 | PERROR("chown wait shm"); |
0fdd1e2c DG |
57 | goto error; |
58 | } | |
59 | } | |
60 | ||
61 | /* | |
62 | * If global session daemon, any application can register so the shm | |
63 | * needs to be set in read-only mode for others. | |
64 | */ | |
65 | mode |= S_IROTH; | |
66 | } else { | |
67 | ret = chown(shm_path, getuid(), getgid()); | |
68 | if (ret < 0) { | |
69 | if (errno != ENOENT) { | |
df0f840b | 70 | PERROR("chown wait shm"); |
0fdd1e2c DG |
71 | goto error; |
72 | } | |
73 | } | |
74 | } | |
75 | ||
76 | /* | |
77 | * Set permissions to the shm even if we did not create the shm. | |
78 | */ | |
79 | ret = chmod(shm_path, mode); | |
80 | if (ret < 0) { | |
81 | if (errno != ENOENT) { | |
df0f840b | 82 | PERROR("chmod wait shm"); |
0fdd1e2c DG |
83 | goto error; |
84 | } | |
85 | } | |
86 | ||
87 | /* | |
7d051034 DG |
88 | * We're alone in a child process, so we can modify the process-wide |
89 | * umask. | |
0fdd1e2c | 90 | */ |
7d051034 | 91 | umask(~mode); |
0fdd1e2c | 92 | |
7d051034 DG |
93 | /* |
94 | * Try creating shm (or get rw access). We don't do an exclusive open, | |
95 | * because we allow other processes to create+ftruncate it concurrently. | |
96 | */ | |
97 | wait_shm_fd = shm_open(shm_path, O_RDWR | O_CREAT, mode); | |
98 | if (wait_shm_fd < 0) { | |
df0f840b | 99 | PERROR("shm_open wait shm"); |
7d051034 DG |
100 | goto error; |
101 | } | |
0fdd1e2c | 102 | |
7d051034 DG |
103 | ret = ftruncate(wait_shm_fd, mmap_size); |
104 | if (ret < 0) { | |
df0f840b | 105 | PERROR("ftruncate wait shm"); |
7d051034 DG |
106 | exit(EXIT_FAILURE); |
107 | } | |
0fdd1e2c | 108 | |
409a0c56 | 109 | #ifndef __FreeBSD__ |
7d051034 DG |
110 | ret = fchmod(wait_shm_fd, mode); |
111 | if (ret < 0) { | |
df0f840b | 112 | PERROR("fchmod"); |
0fdd1e2c | 113 | exit(EXIT_FAILURE); |
0fdd1e2c | 114 | } |
409a0c56 MD |
115 | #else |
116 | #warning "FreeBSD does not support setting file mode on shm FD. Remember that for secure use, lttng-sessiond should be started before applications linked on lttng-ust." | |
117 | #endif | |
0fdd1e2c | 118 | |
0fdd1e2c DG |
119 | DBG("Got the wait shm fd %d", wait_shm_fd); |
120 | ||
121 | return wait_shm_fd; | |
122 | ||
123 | error: | |
124 | DBG("Failing to get the wait shm fd"); | |
125 | ||
126 | return -1; | |
127 | } | |
128 | ||
129 | /* | |
130 | * Return the wait shm mmap for UST application notification. The global | |
131 | * variable is used to indicate if the the session daemon is global | |
132 | * (root:tracing) or running with an unprivileged user. | |
133 | * | |
134 | * This returned value is used by futex_wait_update() in futex.c to WAKE all | |
135 | * waiters which are UST application waiting for a session daemon. | |
136 | */ | |
137 | char *shm_ust_get_mmap(char *shm_path, int global) | |
138 | { | |
139 | size_t mmap_size = sysconf(_SC_PAGE_SIZE); | |
140 | int wait_shm_fd, ret; | |
141 | char *wait_shm_mmap; | |
142 | ||
0525e9ae DG |
143 | assert(shm_path); |
144 | ||
0fdd1e2c DG |
145 | wait_shm_fd = get_wait_shm(shm_path, mmap_size, global); |
146 | if (wait_shm_fd < 0) { | |
147 | goto error; | |
148 | } | |
149 | ||
150 | wait_shm_mmap = mmap(NULL, mmap_size, PROT_WRITE | PROT_READ, | |
151 | MAP_SHARED, wait_shm_fd, 0); | |
7d051034 | 152 | |
0fdd1e2c DG |
153 | /* close shm fd immediately after taking the mmap reference */ |
154 | ret = close(wait_shm_fd); | |
155 | if (ret) { | |
df0f840b | 156 | PERROR("Error closing fd"); |
0fdd1e2c DG |
157 | } |
158 | ||
159 | if (wait_shm_mmap == MAP_FAILED) { | |
160 | DBG("mmap error (can be caused by race with ust)."); | |
161 | goto error; | |
162 | } | |
163 | ||
164 | return wait_shm_mmap; | |
165 | ||
166 | error: | |
167 | return NULL; | |
168 | } |