aboutsummaryrefslogtreecommitdiff
path: root/posix/tst-_Fork.c
blob: ffebe9ddc5c5dbc620f4b57e6f0f7e915ddfc89c (plain)
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
/* Basic tests for _Fork.
   Copyright (C) 2021-2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <array_length.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <stdbool.h>
#include <support/check.h>
#include <support/xsignal.h>
#include <support/temp_file.h>
#include <support/xunistd.h>
#include <support/xthread.h>

/* For single-thread, _Fork behaves like fork.  */
static int
singlethread_test (void)
{
  const char testdata1[] = "abcdefghijklmnopqrtuvwxz";
  enum { testdatalen1 = array_length (testdata1) };
  const char testdata2[] = "01234567890";
  enum { testdatalen2 = array_length (testdata2) };

  pid_t ppid = getpid ();

  int tempfd = create_temp_file ("tst-_Fork", NULL);

  /* Check if the opened file is shared between process by read and write
     some data on parent and child processes.  */
  xwrite (tempfd, testdata1, testdatalen1);
  off_t off = xlseek (tempfd, 0, SEEK_CUR);
  TEST_COMPARE (off, testdatalen1);

  pid_t pid = _Fork ();
  TEST_VERIFY_EXIT (pid != -1);
  if (pid == 0)
    {
      TEST_VERIFY_EXIT (getpid () != ppid);
      TEST_COMPARE (getppid(), ppid);

      TEST_COMPARE (xlseek (tempfd, 0, SEEK_CUR), testdatalen1);

      xlseek (tempfd, 0, SEEK_SET);
      char buf[testdatalen1];
      TEST_COMPARE (read (tempfd, buf, sizeof (buf)), testdatalen1);
      TEST_COMPARE_BLOB (buf, testdatalen1, testdata1, testdatalen1);

      xlseek (tempfd, 0, SEEK_SET);
      xwrite (tempfd, testdata2, testdatalen2);

      xclose (tempfd);

      _exit (EXIT_SUCCESS);
    }

  int status;
  xwaitpid (pid, &status, 0);
  TEST_VERIFY (WIFEXITED (status));
  TEST_COMPARE (WEXITSTATUS (status), EXIT_SUCCESS);

  TEST_COMPARE (xlseek (tempfd, 0, SEEK_CUR), testdatalen2);

  xlseek (tempfd, 0, SEEK_SET);
  char buf[testdatalen2];
  TEST_COMPARE (read (tempfd, buf, sizeof (buf)), testdatalen2);

  TEST_COMPARE_BLOB (buf, testdatalen2, testdata2, testdatalen2);

  return 0;
}


#define SIG_PID_EXIT_CODE 20

static bool atfork_prepare_var;
static bool atfork_parent_var;
static bool atfork_child_var;

static void
atfork_prepare (void)
{
  atfork_prepare_var = true;
}

static void
atfork_parent (void)
{
  atfork_parent_var = true;
}

static void
atfork_child (void)
{
  atfork_child_var = true;
}

/* Different than fork, _Fork does not execute any pthread_atfork
   handlers.  */
static int
singlethread_atfork_test (void)
{
  pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
  singlethread_test ();
  TEST_VERIFY (!atfork_prepare_var);
  TEST_VERIFY (!atfork_parent_var);
  TEST_VERIFY (!atfork_child_var);

  return 0;
}

static void *
mt_atfork_test (void *args)
{
  singlethread_atfork_test ();

  return NULL;
}

static int
multithread_atfork_test (void)
{
  pthread_t thr = xpthread_create (NULL, mt_atfork_test, NULL);
  xpthread_join (thr);

  return 0;
}


static int
do_test (void)
{
  singlethread_atfork_test ();
  multithread_atfork_test ();

  return 0;
}

#include <support/test-driver.c>