X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=libringbuffer%2Fshm.c;h=3dbb9f4928a6d81b43c01fc1dbd4dbd3620ebf0c;hb=c0c0989ab70574e09b2f7e8b48c2da6af664a849;hp=9160c2184f9874841e30802bab2288be30f36894;hpb=b72687b85f4f07a9b63be329ad1fe95b36d24900;p=lttng-ust.git diff --git a/libringbuffer/shm.c b/libringbuffer/shm.c index 9160c218..3dbb9f49 100644 --- a/libringbuffer/shm.c +++ b/libringbuffer/shm.c @@ -1,21 +1,7 @@ /* - * libringbuffer/shm.c + * SPDX-License-Identifier: LGPL-2.1-only * * Copyright (C) 2005-2012 Mathieu Desnoyers - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; only - * version 2.1 of the License. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #define _LGPL_SOURCE @@ -128,25 +114,35 @@ struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table, } memcpy(obj->wait_fd, waitfd, sizeof(waitfd)); - /* create shm */ + /* + * Set POSIX shared memory object size + * + * First, use ftruncate() to set its size, some implementations won't + * allow writes past the size set by ftruncate. + * Then, use write() to fill it with zeros, this allows us to fully + * allocate it and detect a shortage of shm space without dealing with + * a SIGBUS. + */ shmfd = stream_fd; - ret = zero_file(shmfd, memory_map_size); - if (ret) { - PERROR("zero_file"); - goto error_zero_file; - } ret = ftruncate(shmfd, memory_map_size); if (ret) { PERROR("ftruncate"); goto error_ftruncate; } + ret = zero_file(shmfd, memory_map_size); + if (ret) { + PERROR("zero_file"); + goto error_zero_file; + } + /* * Also ensure the file metadata is synced with the storage by using - * fsync(2). + * fsync(2). Some platforms don't allow fsync on POSIX shm fds, ignore + * EINVAL accordingly. */ ret = fsync(shmfd); - if (ret) { + if (ret && errno != EINVAL) { PERROR("fsync"); goto error_fsync; }