| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2020 Michael Jeanson <mjeanson@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <stdio.h> |
| 9 | #include <string.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <sys/types.h> |
| 12 | #include <sys/mman.h> |
| 13 | #include <fcntl.h> |
| 14 | |
| 15 | #include <lttng/align.h> |
| 16 | #include "libringbuffer/shm.h" |
| 17 | |
| 18 | #include "tap.h" |
| 19 | |
| 20 | #define SHM_PATH "/ust-shm-test" |
| 21 | |
| 22 | int main() |
| 23 | { |
| 24 | int shmfd; |
| 25 | size_t shmsize = LTTNG_UST_PAGE_SIZE * 10; |
| 26 | struct shm_object_table *table; |
| 27 | struct shm_object *shmobj; |
| 28 | struct shm_ref shm_ref; |
| 29 | |
| 30 | plan_tests(5); |
| 31 | |
| 32 | /* Open a zero byte shm fd */ |
| 33 | shmfd = shm_open(SHM_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); |
| 34 | ok(shmfd > 0, "Open a POSIX shm fd"); |
| 35 | |
| 36 | /* Create a dummy shm object table to test the allocation function */ |
| 37 | table = shm_object_table_create(1); |
| 38 | ok(table, "Create a shm object table"); |
| 39 | assert(table); |
| 40 | |
| 41 | /* This function sets the initial size of the shm with ftruncate and zeros it */ |
| 42 | shmobj = shm_object_table_alloc(table, shmsize, SHM_OBJECT_SHM, shmfd, -1); |
| 43 | ok(shmobj, "Allocate the shm object table"); |
| 44 | assert(shmobj); |
| 45 | |
| 46 | shm_ref = zalloc_shm(shmobj, LTTNG_UST_PAGE_SIZE * 5); |
| 47 | ok(shm_ref.index != -1, "Allocate an object in the shm with sufficient space"); |
| 48 | |
| 49 | shm_ref = zalloc_shm(shmobj, LTTNG_UST_PAGE_SIZE * 6); |
| 50 | ok(shm_ref.index == -1, "Allocate an object in the shm with insufficient space"); |
| 51 | |
| 52 | /* Cleanup */ |
| 53 | shm_object_table_destroy(table, 1); |
| 54 | |
| 55 | return exit_status(); |
| 56 | } |