summary refs log tree commit diff
path: root/rt/tst-shm.c
blob: a1ec6df5851d210bd67e87a6b30b3edfb5b40a39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* Test program for POSIX shm_* functions.
   Copyright (C) 2000 Free Software Foundation, Inc.
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>


/* We want to see output immediately.  */
#define STDOUT_UNBUFFERED


static int
do_open (void)
{
  int fd;

  /* Create the shared memory object.  */
  fd = shm_open ("/shm-test", O_RDWR, 0600);
  if (fd == -1)
    {
      /* We don't regard this as a bug.  Simply don't run the test.  It could
	 means there is no such implementation or the object is already in
	 use in which case we don't want to disturb.  */
      perror ("failed to open shared memory object: shm_open");
      return -1;
    }

  return fd;
}


static void
worker (int write_now)
{
  struct timespec ts;
  struct stat st;
  int i;
  int fd = do_open ();
  char *mem;

  if (fd == -1)
    exit (fd);

  if (fstat (fd, &st) == -1 || st.st_size != 4000)
    error (EXIT_FAILURE, 0, "stat failed");

  mem = mmap (NULL, 4000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (mem == NULL)
    error (EXIT_FAILURE, 0, "mmap failed");

  ts.tv_sec = 0;
  ts.tv_nsec = 500000000;

  if (write_now)
    for (i = 0; i <= 4; ++i)
      mem[i] = i;
  else
    /* Wait until the first bytes of the memory region are 0, 1, 2, 3, 4.  */
    while (1)
      {
	for (i = 0; i <= 4; ++i)
	  if (mem[i] != i)
	    break;

	if (i > 4)
	  /* OK, that's done.  */
	  break;

	nanosleep (&ts, NULL);
      }

  if (!write_now)
    for (i = 0; i <= 4; ++i)
      mem[i] = 4 + i;
  else
    /* Wait until the first bytes of the memory region are 4, 5, 6, 7, 8.  */
    while (1)
      {
	for (i = 0; i <= 4; ++i)
	  if (mem[i] != 4 + i)
	    break;

	if (i > 4)
	  /* OK, that's done.  */
	  break;

	nanosleep (&ts, NULL);
      }

  if (munmap (mem, 4000) == -1)
    error (EXIT_FAILURE, errno, "munmap");

  close (fd);

  exit (0);
}


int
do_test (void)
{
  int fd;
  pid_t pid1;
  pid_t pid2;
  int status1;
  int status2;

  /* Create the shared memory object.  */
  fd = shm_open ("/shm-test", O_RDWR | O_CREAT | O_TRUNC | O_EXCL, 0600);
  if (fd == -1)
    {
      /* We don't regard this as a bug.  Simply don't run the test.  It could
	 means there is no such implementation or the object is already in
	 use in which case we don't want to disturb.  */
      perror ("failed to create a shared memory object: shm_open");
      return 0;
    }

  /* Size the object.  We make it 4000 bytes long.  */
  if (ftruncate (fd, 4000) == -1)
    {
      /* This failed.  Must be a bug in the implementation of the
         shared memory itself.  */
      perror ("failed to size of shared memory object: ftruncate");
      close (fd);
      shm_unlink ("/shm-test");
      return 0;
    }

  /* Spawn to processes which will do the work.  */
  pid1 = fork ();
  if (pid1 == 0)
    worker (0);
  else if (pid1 == -1)
    {
      /* Couldn't create a second process.  */
      perror ("fork");
      close (fd);
      shm_unlink ("/shm-test");
      return 0;
    }

  pid2 = fork ();
  if (pid2 == 0)
    worker (1);
  else if (pid2 == -1)
    {
      /* Couldn't create a second process.  */
      int ignore;
      perror ("fork");
      kill (pid1, SIGTERM);
      waitpid (pid1, &ignore, 0);
      close (fd);
      shm_unlink ("/shm-test");
      return 0;
    }

  /* Wait until the two processes are finished.  */
  waitpid (pid1, &status1, 0);
  waitpid (pid2, &status2, 0);

  /* Now we can unlink the shared object.  */
  shm_unlink ("/shm-test");

  return (!WIFEXITED (status1) || WEXITSTATUS (status1) != 0
	  || !WIFEXITED (status2) || WEXITSTATUS (status2) != 0);
}
#define TEST_FUNCTION do_test ()

#define CLEANUP_HANDLER shm_unlink ("/shm-test");


#include "../test-skeleton.c"