Hide lttng_event_copy symbol
[lttng-tools.git] / src / bin / lttng-sessiond / shm.c
CommitLineData
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
6c1c0768 19#define _LGPL_SOURCE
0fdd1e2c
DG
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 */
41static 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
7972d619
DG
51 /*
52 * Change owner of the shm path.
53 */
0fdd1e2c 54 if (global) {
0fdd1e2c 55 /*
7972d619
DG
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.
0fdd1e2c 60 */
7972d619 61 mode |= S_IROTH | S_IWOTH;
0fdd1e2c
DG
62 }
63
64 /*
7d051034
DG
65 * We're alone in a child process, so we can modify the process-wide
66 * umask.
0fdd1e2c 67 */
7d051034 68 umask(~mode);
0fdd1e2c 69
7d051034
DG
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 wait_shm_fd = shm_open(shm_path, O_RDWR | O_CREAT, mode);
75 if (wait_shm_fd < 0) {
df0f840b 76 PERROR("shm_open wait shm");
7d051034
DG
77 goto error;
78 }
0fdd1e2c 79
7d051034
DG
80 ret = ftruncate(wait_shm_fd, mmap_size);
81 if (ret < 0) {
df0f840b 82 PERROR("ftruncate wait shm");
7d051034
DG
83 exit(EXIT_FAILURE);
84 }
0fdd1e2c 85
409a0c56 86#ifndef __FreeBSD__
7972d619
DG
87 if (global) {
88 ret = fchown(wait_shm_fd, 0, 0);
89 if (ret < 0) {
90 PERROR("fchown");
91 exit(EXIT_FAILURE);
92 }
93 /*
94 * If global session daemon, any application can
95 * register so the shm needs to be set in read-only mode
96 * for others.
97 */
98 mode &= ~S_IWOTH;
99 ret = fchmod(wait_shm_fd, mode);
100 if (ret < 0) {
101 PERROR("fchmod");
102 exit(EXIT_FAILURE);
103 }
104 } else {
105 ret = fchown(wait_shm_fd, getuid(), getgid());
106 if (ret < 0) {
107 PERROR("fchown");
108 exit(EXIT_FAILURE);
109 }
0fdd1e2c 110 }
409a0c56 111#else
7972d619 112#warning "FreeBSD does not support setting file mode on shm FD."
409a0c56 113#endif
0fdd1e2c 114
0fdd1e2c
DG
115 DBG("Got the wait shm fd %d", wait_shm_fd);
116
117 return wait_shm_fd;
118
119error:
120 DBG("Failing to get the wait shm fd");
121
122 return -1;
123}
124
125/*
126 * Return the wait shm mmap for UST application notification. The global
127 * variable is used to indicate if the the session daemon is global
128 * (root:tracing) or running with an unprivileged user.
129 *
130 * This returned value is used by futex_wait_update() in futex.c to WAKE all
131 * waiters which are UST application waiting for a session daemon.
132 */
133char *shm_ust_get_mmap(char *shm_path, int global)
134{
6c699394 135 size_t mmap_size;
0fdd1e2c
DG
136 int wait_shm_fd, ret;
137 char *wait_shm_mmap;
6c699394 138 long sys_page_size;
0fdd1e2c 139
0525e9ae
DG
140 assert(shm_path);
141
6c699394
DG
142 sys_page_size = sysconf(_SC_PAGE_SIZE);
143 if (sys_page_size < 0) {
144 PERROR("sysconf PAGE_SIZE");
145 goto error;
146 }
147 mmap_size = sys_page_size;
148
0fdd1e2c
DG
149 wait_shm_fd = get_wait_shm(shm_path, mmap_size, global);
150 if (wait_shm_fd < 0) {
151 goto error;
152 }
153
154 wait_shm_mmap = mmap(NULL, mmap_size, PROT_WRITE | PROT_READ,
155 MAP_SHARED, wait_shm_fd, 0);
7d051034 156
0fdd1e2c
DG
157 /* close shm fd immediately after taking the mmap reference */
158 ret = close(wait_shm_fd);
159 if (ret) {
df0f840b 160 PERROR("Error closing fd");
0fdd1e2c
DG
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
170error:
171 return NULL;
172}
This page took 0.055597 seconds and 4 git commands to generate.