2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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.
25 #include <sys/types.h>
30 #include <common/error.h>
35 * Using fork to set umask in the child process (not multi-thread safe). We
36 * deal with the shm_open vs ftruncate race (happening when the sessiond owns
37 * the shm and does not let everybody modify it, to ensure safety against
38 * shm_unlink) by simply letting the mmap fail and retrying after a few
39 * seconds. For global shm, everybody has rw access to it until the sessiond
42 static int get_wait_shm(char *shm_path
, size_t mmap_size
, int global
)
49 /* Default permissions */
50 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
53 * Change owner of the shm path.
57 * If global session daemon, any application can
58 * register. Make it initially writeable so applications
59 * registering concurrently can do ftruncate() by
62 mode
|= S_IROTH
| S_IWOTH
;
66 * We're alone in a child process, so we can modify the process-wide
72 * Try creating shm (or get rw access). We don't do an exclusive open,
73 * because we allow other processes to create+ftruncate it concurrently.
75 wait_shm_fd
= shm_open(shm_path
, O_RDWR
| O_CREAT
, mode
);
76 if (wait_shm_fd
< 0) {
77 PERROR("shm_open wait shm");
81 ret
= ftruncate(wait_shm_fd
, mmap_size
);
83 PERROR("ftruncate wait shm");
89 ret
= fchown(wait_shm_fd
, 0, 0);
95 * If global session daemon, any application can
96 * register so the shm needs to be set in read-only mode
100 ret
= fchmod(wait_shm_fd
, mode
);
106 ret
= fchown(wait_shm_fd
, getuid(), getgid());
113 #warning "FreeBSD does not support setting file mode on shm FD."
116 DBG("Got the wait shm fd %d", wait_shm_fd
);
121 DBG("Failing to get the wait shm fd");
127 * Return the wait shm mmap for UST application notification. The global
128 * variable is used to indicate if the the session daemon is global
129 * (root:tracing) or running with an unprivileged user.
131 * This returned value is used by futex_wait_update() in futex.c to WAKE all
132 * waiters which are UST application waiting for a session daemon.
134 char *shm_ust_get_mmap(char *shm_path
, int global
)
137 int wait_shm_fd
, ret
;
143 sys_page_size
= sysconf(_SC_PAGE_SIZE
);
144 if (sys_page_size
< 0) {
145 PERROR("sysconf PAGE_SIZE");
148 mmap_size
= sys_page_size
;
150 wait_shm_fd
= get_wait_shm(shm_path
, mmap_size
, global
);
151 if (wait_shm_fd
< 0) {
155 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_WRITE
| PROT_READ
,
156 MAP_SHARED
, wait_shm_fd
, 0);
158 /* close shm fd immediately after taking the mmap reference */
159 ret
= close(wait_shm_fd
);
161 PERROR("Error closing fd");
164 if (wait_shm_mmap
== MAP_FAILED
) {
165 DBG("mmap error (can be caused by race with ust).");
169 return wait_shm_mmap
;
This page took 0.035162 seconds and 5 git commands to generate.