| 1 | /* Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com> |
| 2 | * |
| 3 | * This library is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of the GNU Lesser General Public |
| 5 | * License as published by the Free Software Foundation; only |
| 6 | * version 2.1 of the License. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public |
| 14 | * License along with this library; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #include <assert.h> |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/mman.h> |
| 24 | #include <fcntl.h> |
| 25 | |
| 26 | #include <lttng/align.h> |
| 27 | #include "libringbuffer/shm.h" |
| 28 | |
| 29 | #include "tap.h" |
| 30 | |
| 31 | #define SHM_PATH "/ust-shm-test" |
| 32 | |
| 33 | int main() |
| 34 | { |
| 35 | int shmfd; |
| 36 | size_t shmsize = LTTNG_UST_PAGE_SIZE * 10; |
| 37 | struct shm_object_table *table; |
| 38 | struct shm_object *shmobj; |
| 39 | struct shm_ref shm_ref; |
| 40 | |
| 41 | plan_tests(5); |
| 42 | |
| 43 | /* Open a zero byte shm fd */ |
| 44 | shmfd = shm_open(SHM_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); |
| 45 | ok(shmfd > 0, "Open a POSIX shm fd"); |
| 46 | |
| 47 | /* Create a dummy shm object table to test the allocation function */ |
| 48 | table = shm_object_table_create(1); |
| 49 | ok(table, "Create a shm object table"); |
| 50 | assert(table); |
| 51 | |
| 52 | /* This function sets the initial size of the shm with ftruncate and zeros it */ |
| 53 | shmobj = shm_object_table_alloc(table, shmsize, SHM_OBJECT_SHM, shmfd, -1); |
| 54 | ok(shmobj, "Allocate the shm object table"); |
| 55 | assert(shmobj); |
| 56 | |
| 57 | shm_ref = zalloc_shm(shmobj, LTTNG_UST_PAGE_SIZE * 5); |
| 58 | ok(shm_ref.index != -1, "Allocate an object in the shm with sufficient space"); |
| 59 | |
| 60 | shm_ref = zalloc_shm(shmobj, LTTNG_UST_PAGE_SIZE * 6); |
| 61 | ok(shm_ref.index == -1, "Allocate an object in the shm with insufficient space"); |
| 62 | |
| 63 | /* Cleanup */ |
| 64 | shm_object_table_destroy(table, 1); |
| 65 | |
| 66 | return exit_status(); |
| 67 | } |