Blame sysdeps/posix/shm-directory.h

Packit 6c4009
/* Header for directory for shm/sem files.
Packit 6c4009
   Copyright (C) 2014-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef _SHM_DIRECTORY_H
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <limits.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
extern const char *__shm_directory (size_t *len);
Packit 6c4009
Packit 6c4009
/* This defines local variables SHM_DIR and SHM_DIRLEN, giving the
Packit 6c4009
   directory prefix (with trailing slash) and length (not including '\0'
Packit 6c4009
   terminator) of the directory used for shm files.  If that cannot be
Packit 6c4009
   determined, it sets errno to ENOSYS and returns RETVAL_FOR_INVALID.
Packit 6c4009
Packit 6c4009
   This uses the local variable NAME as an lvalue, and increments it past
Packit 6c4009
   any leading slashes.  It then defines the local variable NAMELEN, giving
Packit 6c4009
   strlen (NAME) + 1.  If NAME is invalid, it sets errno to
Packit 6c4009
   ERRNO_FOR_INVALID and returns RETVAL_FOR_INVALID.  Finally, it defines
Packit 6c4009
   the local variable SHM_NAME, giving the absolute file name of the shm
Packit 6c4009
   file corresponding to NAME.  PREFIX is a string constant used as a
Packit 6c4009
   prefix on NAME.  */
Packit 6c4009
Packit 6c4009
#define SHM_GET_NAME(errno_for_invalid, retval_for_invalid, prefix)           \
Packit 6c4009
  size_t shm_dirlen;							      \
Packit 6c4009
  const char *shm_dir = __shm_directory (&shm_dirlen);			      \
Packit 6c4009
  /* If we don't know what directory to use, there is nothing we can do.  */  \
Packit 6c4009
  if (__glibc_unlikely (shm_dir == NULL))				      \
Packit 6c4009
    {									      \
Packit 6c4009
      __set_errno (ENOSYS);						      \
Packit 6c4009
      return retval_for_invalid;					      \
Packit 6c4009
    }									      \
Packit 6c4009
  /* Construct the filename.  */					      \
Packit 6c4009
  while (name[0] == '/')						      \
Packit 6c4009
    ++name;								      \
Packit 6c4009
  size_t namelen = strlen (name) + 1;					      \
Packit 6c4009
  /* Validate the filename.  */						      \
Packit 6c4009
  if (namelen == 1 || namelen >= NAME_MAX || strchr (name, '/') != NULL)      \
Packit 6c4009
    {									      \
Packit 6c4009
      __set_errno (errno_for_invalid);					      \
Packit 6c4009
      return retval_for_invalid;					      \
Packit 6c4009
    }									      \
Packit 6c4009
  char *shm_name = __alloca (shm_dirlen + sizeof prefix - 1 + namelen);	      \
Packit 6c4009
  __mempcpy (__mempcpy (__mempcpy (shm_name, shm_dir, shm_dirlen),	      \
Packit 6c4009
                        prefix, sizeof prefix - 1),			      \
Packit 6c4009
             name, namelen)
Packit 6c4009
Packit 6c4009
#endif	/* shm-directory.h */