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 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.
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
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.
24 #include <sys/types.h>
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
41 static int get_wait_shm(char *shm_path
, size_t mmap_size
, int global
)
47 /* Default permissions */
48 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
50 /* Change owner of the shm path */
52 ret
= chown(shm_path
, 0, 0);
54 if (errno
!= ENOENT
) {
55 perror("chown wait shm");
61 * If global session daemon, any application can register so the shm
62 * needs to be set in read-only mode for others.
66 ret
= chown(shm_path
, getuid(), getgid());
68 if (errno
!= ENOENT
) {
69 perror("chown wait shm");
76 * Set permissions to the shm even if we did not create the shm.
78 ret
= chmod(shm_path
, mode
);
80 if (errno
!= ENOENT
) {
81 perror("chmod wait shm");
87 * If the open failed because the file did not exist, try creating it
94 * Parent: wait for child to return, in which case the shared memory
95 * map will have been created.
98 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
103 * Try to open read-only again after creation.
105 wait_shm_fd
= shm_open(shm_path
, O_RDWR
, 0);
106 if (wait_shm_fd
< 0) {
108 * Real-only open did not work. It's a failure that prohibits using
111 ERR("Error opening shm %s", shm_path
);
115 } else if (pid
== 0) {
117 * We're alone in a child process, so we can modify the process-wide
123 * Try creating shm (or get rw access). We don't do an exclusive open,
124 * because we allow other processes to create+ftruncate it
127 wait_shm_fd
= shm_open(shm_path
, O_RDWR
| O_CREAT
, mode
);
128 if (wait_shm_fd
>= 0) {
129 ret
= ftruncate(wait_shm_fd
, mmap_size
);
131 perror("ftruncate wait shm");
135 ret
= fchmod(wait_shm_fd
, mode
);
142 ERR("Error opening shm %s", shm_path
);
149 DBG("Got the wait shm fd %d", wait_shm_fd
);
154 DBG("Failing to get the wait shm fd");
160 * Return the wait shm mmap for UST application notification. The global
161 * variable is used to indicate if the the session daemon is global
162 * (root:tracing) or running with an unprivileged user.
164 * This returned value is used by futex_wait_update() in futex.c to WAKE all
165 * waiters which are UST application waiting for a session daemon.
167 char *shm_ust_get_mmap(char *shm_path
, int global
)
169 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
170 int wait_shm_fd
, ret
;
173 wait_shm_fd
= get_wait_shm(shm_path
, mmap_size
, global
);
174 if (wait_shm_fd
< 0) {
178 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_WRITE
| PROT_READ
,
179 MAP_SHARED
, wait_shm_fd
, 0);
180 /* close shm fd immediately after taking the mmap reference */
181 ret
= close(wait_shm_fd
);
183 perror("Error closing fd");
186 if (wait_shm_mmap
== MAP_FAILED
) {
187 DBG("mmap error (can be caused by race with ust).");
191 return wait_shm_mmap
;
This page took 0.048849 seconds and 5 git commands to generate.