Blame nptl/tst-umask1.c

Packit 6c4009
/* Copyright (C) 2003-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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
#include <fcntl.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
static struct
Packit 6c4009
{
Packit 6c4009
  int (*fp) (const char *, mode_t);
Packit 6c4009
  const char *name;
Packit 6c4009
  bool is_fd;
Packit 6c4009
} fcts[] =
Packit 6c4009
{
Packit 6c4009
  { creat, "creat", true },
Packit 6c4009
  { mkdir, "mkdir", false },
Packit 6c4009
  { mkfifo, "mkfifo", false },
Packit 6c4009
};
Packit 6c4009
#define nfcts (sizeof (fcts) / sizeof (fcts[0]))
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
work (const char *fname, int mask)
Packit 6c4009
{
Packit 6c4009
  int result = 0;
Packit 6c4009
  size_t i;
Packit 6c4009
  for (i = 0; i < nfcts; ++i)
Packit 6c4009
    {
Packit 6c4009
      remove (fname);
Packit 6c4009
      int fd = fcts[i].fp (fname, 0777);
Packit 6c4009
      if (fd == -1)
Packit 6c4009
	{
Packit 6c4009
	  printf ("cannot %s %s: %m\n", fcts[i].name, fname);
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
      if (fcts[i].is_fd)
Packit 6c4009
	close (fd);
Packit 6c4009
      struct stat64 st;
Packit 6c4009
      if (stat64 (fname, &st) == -1)
Packit 6c4009
	{
Packit 6c4009
	  printf ("cannot stat %s after %s: %m\n", fname, fcts[i].name);
Packit 6c4009
	  exit (1);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if ((st.st_mode & mask) != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("mask not successful after %s: %x still set\n",
Packit 6c4009
		  fcts[i].name, (unsigned int) (st.st_mode & mask));
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static pthread_barrier_t bar;
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
tf (void *arg)
Packit 6c4009
{
Packit 6c4009
  pthread_barrier_wait (&bar);
Packit 6c4009
Packit 6c4009
  int result = work (arg, 022);
Packit 6c4009
Packit 6c4009
  pthread_barrier_wait (&bar);
Packit 6c4009
Packit 6c4009
  pthread_barrier_wait (&bar);
Packit 6c4009
Packit 6c4009
  return (work (arg, 0) | result) ? (void *) -1l : NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (const char *fname)
Packit 6c4009
{
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  umask (0);
Packit 6c4009
  result |= work (fname, 0);
Packit 6c4009
Packit 6c4009
  pthread_barrier_init (&bar, NULL, 2);
Packit 6c4009
Packit 6c4009
  pthread_t th;
Packit 6c4009
  if (pthread_create (&th, NULL, tf, (void *) fname) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("cannot create thread");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  umask (022);
Packit 6c4009
  result |= work (fname, 022);
Packit 6c4009
Packit 6c4009
  pthread_barrier_wait (&bar);
Packit 6c4009
Packit 6c4009
  pthread_barrier_wait (&bar);
Packit 6c4009
Packit 6c4009
  umask (0);
Packit 6c4009
Packit 6c4009
  pthread_barrier_wait (&bar);
Packit 6c4009
Packit 6c4009
  void *res;
Packit 6c4009
  if (pthread_join (th, &res) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("join failed");
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  remove (fname);
Packit 6c4009
Packit 6c4009
  return result || res != NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test (argc < 2 ? "/tmp/tst-umask.tmp" : argv[1])
Packit 6c4009
#include "../test-skeleton.c"