Merge branch 'master' of git://git.lttng.org/lttng-tools
[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 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 <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 /* Default permissions */
47 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
48
49 /* Change owner of the shm path */
50 if (global) {
51 ret = chown(shm_path, 0, 0);
52 if (ret < 0) {
53 if (errno != ENOENT) {
54 PERROR("chown wait shm");
55 goto error;
56 }
57 }
58
59 /*
60 * If global session daemon, any application can register so the shm
61 * needs to be set in read-only mode for others.
62 */
63 mode |= S_IROTH;
64 } else {
65 ret = chown(shm_path, getuid(), getgid());
66 if (ret < 0) {
67 if (errno != ENOENT) {
68 PERROR("chown wait shm");
69 goto error;
70 }
71 }
72 }
73
74 /*
75 * Set permissions to the shm even if we did not create the shm.
76 */
77 ret = chmod(shm_path, mode);
78 if (ret < 0) {
79 if (errno != ENOENT) {
80 PERROR("chmod wait shm");
81 goto error;
82 }
83 }
84
85 /*
86 * We're alone in a child process, so we can modify the process-wide
87 * umask.
88 */
89 umask(~mode);
90
91 /*
92 * Try creating shm (or get rw access). We don't do an exclusive open,
93 * because we allow other processes to create+ftruncate it concurrently.
94 */
95 wait_shm_fd = shm_open(shm_path, O_RDWR | O_CREAT, mode);
96 if (wait_shm_fd < 0) {
97 PERROR("shm_open wait shm");
98 goto error;
99 }
100
101 ret = ftruncate(wait_shm_fd, mmap_size);
102 if (ret < 0) {
103 PERROR("ftruncate wait shm");
104 exit(EXIT_FAILURE);
105 }
106
107 #ifndef __FreeBSD__
108 ret = fchmod(wait_shm_fd, mode);
109 if (ret < 0) {
110 PERROR("fchmod");
111 exit(EXIT_FAILURE);
112 }
113 #else
114 #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."
115 #endif
116
117 DBG("Got the wait shm fd %d", wait_shm_fd);
118
119 return wait_shm_fd;
120
121 error:
122 DBG("Failing to get the wait shm fd");
123
124 return -1;
125 }
126
127 /*
128 * Return the wait shm mmap for UST application notification. The global
129 * variable is used to indicate if the the session daemon is global
130 * (root:tracing) or running with an unprivileged user.
131 *
132 * This returned value is used by futex_wait_update() in futex.c to WAKE all
133 * waiters which are UST application waiting for a session daemon.
134 */
135 char *shm_ust_get_mmap(char *shm_path, int global)
136 {
137 size_t mmap_size = sysconf(_SC_PAGE_SIZE);
138 int wait_shm_fd, ret;
139 char *wait_shm_mmap;
140
141 wait_shm_fd = get_wait_shm(shm_path, mmap_size, global);
142 if (wait_shm_fd < 0) {
143 goto error;
144 }
145
146 wait_shm_mmap = mmap(NULL, mmap_size, PROT_WRITE | PROT_READ,
147 MAP_SHARED, wait_shm_fd, 0);
148
149 /* close shm fd immediately after taking the mmap reference */
150 ret = close(wait_shm_fd);
151 if (ret) {
152 PERROR("Error closing fd");
153 }
154
155 if (wait_shm_mmap == MAP_FAILED) {
156 DBG("mmap error (can be caused by race with ust).");
157 goto error;
158 }
159
160 return wait_shm_mmap;
161
162 error:
163 return NULL;
164 }
This page took 0.042056 seconds and 5 git commands to generate.