| 1 | /* Header for directory for shm/sem files. |
| 2 | Copyright (C) 2014-2021 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #ifndef _SHM_DIRECTORY_H |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <limits.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | extern const char *__shm_directory (size_t *len); |
| 28 | |
| 29 | /* This defines local variables SHM_DIR and SHM_DIRLEN, giving the |
| 30 | directory prefix (with trailing slash) and length (not including '\0' |
| 31 | terminator) of the directory used for shm files. If that cannot be |
| 32 | determined, it sets errno to ENOSYS and returns RETVAL_FOR_INVALID. |
| 33 | |
| 34 | This uses the local variable NAME as an lvalue, and increments it past |
| 35 | any leading slashes. It then defines the local variable NAMELEN, giving |
| 36 | strlen (NAME) + 1. If NAME is invalid, it sets errno to |
| 37 | ERRNO_FOR_INVALID and returns RETVAL_FOR_INVALID. Finally, it defines |
| 38 | the local variable SHM_NAME, giving the absolute file name of the shm |
| 39 | file corresponding to NAME. PREFIX is a string constant used as a |
| 40 | prefix on NAME. */ |
| 41 | |
| 42 | #define SHM_GET_NAME(errno_for_invalid, retval_for_invalid, prefix) \ |
| 43 | size_t shm_dirlen; \ |
| 44 | const char *shm_dir = __shm_directory (&shm_dirlen); \ |
| 45 | /* If we don't know what directory to use, there is nothing we can do. */ \ |
| 46 | if (__glibc_unlikely (shm_dir == NULL)) \ |
| 47 | { \ |
| 48 | __set_errno (ENOSYS); \ |
| 49 | return retval_for_invalid; \ |
| 50 | } \ |
| 51 | /* Construct the filename. */ \ |
| 52 | while (name[0] == '/') \ |
| 53 | ++name; \ |
| 54 | size_t namelen = strlen (name) + 1; \ |
| 55 | /* Validate the filename. */ \ |
| 56 | if (namelen == 1 || namelen >= NAME_MAX || strchr (name, '/') != NULL) \ |
| 57 | { \ |
| 58 | __set_errno (errno_for_invalid); \ |
| 59 | return retval_for_invalid; \ |
| 60 | } \ |
| 61 | char *shm_name = __alloca (shm_dirlen + sizeof prefix - 1 + namelen); \ |
| 62 | __mempcpy (__mempcpy (__mempcpy (shm_name, shm_dir, shm_dirlen), \ |
| 63 | prefix, sizeof prefix - 1), \ |
| 64 | name, namelen) |
| 65 | |
| 66 | #endif /* shm-directory.h */ |
| 67 | |