e9fd8e2859497a778fa78f4cde185d286944238c
[external/glibc.git] / nptl / tst-mutex9.c
1 /* Copyright (C) 2003-2019 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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    <http://www.gnu.org/licenses/>.  */
18
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/time.h>
28 #include <sys/wait.h>
29 #include <support/check.h>
30 #include <support/timespec.h>
31 #include <support/xunistd.h>
32
33
34 static int
35 do_test (void)
36 {
37   size_t ps = sysconf (_SC_PAGESIZE);
38   char tmpfname[] = "/tmp/tst-mutex9.XXXXXX";
39   char data[ps];
40   void *mem;
41   int fd;
42   pthread_mutex_t *m;
43   pthread_mutexattr_t a;
44   pid_t pid;
45
46   fd = mkstemp (tmpfname);
47   if (fd == -1)
48       FAIL_EXIT1 ("cannot open temporary file: %m\n");
49
50   /* Make sure it is always removed.  */
51   unlink (tmpfname);
52
53   /* Create one page of data.  */
54   memset (data, '\0', ps);
55
56   /* Write the data to the file.  */
57   xwrite (fd, data, ps);
58
59   mem = xmmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd);
60
61   m = (pthread_mutex_t *) (((uintptr_t) mem + __alignof (pthread_mutex_t))
62                            & ~(__alignof (pthread_mutex_t) - 1));
63
64   TEST_COMPARE (pthread_mutexattr_init (&a), 0);
65
66   TEST_COMPARE (pthread_mutexattr_setpshared (&a, PTHREAD_PROCESS_SHARED), 0);
67
68   TEST_COMPARE (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_RECURSIVE), 0);
69
70 #ifdef ENABLE_PI
71   TEST_COMPARE (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT), 0);
72 #endif
73
74   int e;
75   if ((e = pthread_mutex_init (m, &a)) != 0)
76     {
77 #ifdef ENABLE_PI
78       if (e == ENOTSUP)
79         FAIL_UNSUPPORTED ("PI mutexes unsupported");
80 #endif
81       FAIL_EXIT1 ("mutex_init failed");
82     }
83
84   TEST_COMPARE (pthread_mutex_lock (m), 0);
85
86   TEST_COMPARE (pthread_mutexattr_destroy (&a), 0);
87
88   puts ("going to fork now");
89   pid = xfork ();
90   if (pid == 0)
91     {
92       if (pthread_mutex_trylock (m) == 0)
93         FAIL_EXIT1 ("child: mutex_trylock succeeded");
94
95       if (pthread_mutex_unlock (m) == 0)
96         FAIL_EXIT1 ("child: mutex_unlock succeeded");
97
98       const struct timespec ts = timespec_add (xclock_now (CLOCK_REALTIME),
99                                                make_timespec (0, 500000000));
100
101       TEST_COMPARE (pthread_mutex_timedlock (m, &ts), ETIMEDOUT);
102
103       alarm (1);
104
105       pthread_mutex_lock (m);
106
107       puts ("child: mutex_lock returned");
108
109       exit (0);
110     }
111
112   sleep (2);
113
114   int status;
115   if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
116     FAIL_EXIT1 ("waitpid failed");
117   if (! WIFSIGNALED (status))
118     FAIL_EXIT1 ("child not killed by signal");
119   TEST_COMPARE (WTERMSIG (status), SIGALRM);
120
121   return 0;
122 }
123
124 #include <support/test-driver.c>