X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fshm.c;h=8422230708253fd3a246fc8ec1eb5fdaeada5fe7;hb=a2f8cd58605942314ebf734b3e95c65721e6394a;hp=60a92cd15d3461850e898cdb95c4b00448ef12ba;hpb=cf86ff2c4ababd01fea7ab2c9c289cb7c0a1bcd5;p=lttng-tools.git diff --git a/src/bin/lttng-sessiond/shm.c b/src/bin/lttng-sessiond/shm.c index 60a92cd15..842223070 100644 --- a/src/bin/lttng-sessiond/shm.c +++ b/src/bin/lttng-sessiond/shm.c @@ -1,19 +1,9 @@ /* - * Copyright (C) 2011 - David Goulet - * Mathieu Desnoyers + * Copyright (C) 2011 EfficiOS Inc. + * Copyright (C) 2011 Mathieu Desnoyers * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License, version 2 only, - * as published by the Free Software Foundation. + * SPDX-License-Identifier: GPL-2.0-only * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #define _LGPL_SOURCE @@ -31,8 +21,7 @@ #include "shm.h" /* - * Using fork to set umask in the child process (not multi-thread safe). We - * deal with the shm_open vs ftruncate race (happening when the sessiond owns + * We deal with the shm_open vs ftruncate race (happening when the sessiond owns * the shm and does not let everybody modify it, to ensure safety against * shm_unlink) by simply letting the mmap fail and retrying after a few * seconds. For global shm, everybody has rw access to it until the sessiond @@ -41,7 +30,7 @@ static int get_wait_shm(char *shm_path, size_t mmap_size, int global) { int wait_shm_fd, ret; - mode_t mode; + mode_t mode, old_mode; assert(shm_path); @@ -61,11 +50,7 @@ static int get_wait_shm(char *shm_path, size_t mmap_size, int global) mode |= S_IROTH | S_IWOTH; } - /* - * We're alone in a child process, so we can modify the process-wide - * umask. - */ - umask(~mode); + old_mode = umask(~mode); /* * Try creating shm (or get rw access). We don't do an exclusive open, @@ -113,7 +98,7 @@ static int get_wait_shm(char *shm_path, size_t mmap_size, int global) ret = ftruncate(wait_shm_fd, mmap_size); if (ret < 0) { PERROR("ftruncate wait shm"); - exit(EXIT_FAILURE); + goto error; } #ifndef __FreeBSD__ @@ -121,7 +106,7 @@ static int get_wait_shm(char *shm_path, size_t mmap_size, int global) ret = fchown(wait_shm_fd, 0, 0); if (ret < 0) { PERROR("fchown"); - exit(EXIT_FAILURE); + goto error; } /* * If global session daemon, any application can @@ -132,13 +117,13 @@ static int get_wait_shm(char *shm_path, size_t mmap_size, int global) ret = fchmod(wait_shm_fd, mode); if (ret < 0) { PERROR("fchmod"); - exit(EXIT_FAILURE); + goto error; } } else { ret = fchown(wait_shm_fd, getuid(), getgid()); if (ret < 0) { PERROR("fchown"); - exit(EXIT_FAILURE); + goto error; } } #else @@ -146,13 +131,20 @@ static int get_wait_shm(char *shm_path, size_t mmap_size, int global) #endif DBG("Got the wait shm fd %d", wait_shm_fd); - +end: + (void) umask(old_mode); return wait_shm_fd; error: DBG("Failing to get the wait shm fd"); + if (wait_shm_fd >= 0) { + if (close(wait_shm_fd)) { + PERROR("Failed to close wait shm file descriptor during error handling"); + } + } - return -1; + wait_shm_fd = -1; + goto end; } /*