2024-08-03 08:24:46 +00:00
|
|
|
// Copyright 2024 Justine Alexandra Roberts Tunney
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for
|
|
|
|
// any purpose with or without fee is hereby granted, provided that the
|
|
|
|
// above copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
|
|
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
|
|
|
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
2024-08-04 00:48:00 +00:00
|
|
|
#include <signal.h>
|
2024-08-03 08:24:46 +00:00
|
|
|
#include <stdatomic.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2024-08-04 00:48:00 +00:00
|
|
|
// test that lseek() is shared across fork()
|
2024-08-03 08:24:46 +00:00
|
|
|
|
2024-08-04 00:48:00 +00:00
|
|
|
void on_unexpected_death(int sig) {
|
|
|
|
int ws;
|
|
|
|
if (wait(&ws) == -1)
|
|
|
|
_Exit(33);
|
|
|
|
if (!WIFEXITED(ws))
|
|
|
|
_Exit(34);
|
|
|
|
if (!(WEXITSTATUS(ws) & 255))
|
|
|
|
_Exit(35);
|
|
|
|
_Exit(WEXITSTATUS(ws));
|
|
|
|
}
|
2024-08-03 08:24:46 +00:00
|
|
|
|
|
|
|
int main() {
|
2024-08-04 00:48:00 +00:00
|
|
|
signal(SIGCHLD, on_unexpected_death);
|
2024-08-03 08:24:46 +00:00
|
|
|
|
2024-08-04 00:48:00 +00:00
|
|
|
atomic_int *phase;
|
2024-08-03 08:24:46 +00:00
|
|
|
if ((phase = mmap(0, sizeof(atomic_int), PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED | MAP_ANONYMOUS, -1, 0)) == MAP_FAILED)
|
2024-08-04 00:48:00 +00:00
|
|
|
return 2;
|
2024-08-03 08:24:46 +00:00
|
|
|
|
|
|
|
int fd;
|
2024-08-04 00:48:00 +00:00
|
|
|
char path[] = "/tmp/file_offset_fork_test.XXXXXX";
|
2024-08-03 08:24:46 +00:00
|
|
|
if ((fd = mkstemp(path)) == -1)
|
2024-08-04 00:48:00 +00:00
|
|
|
return 3;
|
2024-08-03 08:24:46 +00:00
|
|
|
if (lseek(fd, 0, SEEK_CUR) != 0)
|
2024-08-04 00:48:00 +00:00
|
|
|
return 4;
|
2024-08-03 08:24:46 +00:00
|
|
|
|
|
|
|
if (write(fd, "0", 1) != 1)
|
2024-08-04 00:48:00 +00:00
|
|
|
return 5;
|
2024-08-03 08:24:46 +00:00
|
|
|
if (lseek(fd, 0, SEEK_CUR) != 1)
|
2024-08-04 00:48:00 +00:00
|
|
|
return 6;
|
2024-08-03 08:24:46 +00:00
|
|
|
|
|
|
|
int pid;
|
|
|
|
if ((pid = fork()) == -1)
|
2024-08-04 00:48:00 +00:00
|
|
|
return 7;
|
2024-08-03 08:24:46 +00:00
|
|
|
|
|
|
|
if (!pid) {
|
|
|
|
if (write(fd, "1", 1) != 1)
|
2024-08-04 00:48:00 +00:00
|
|
|
_Exit(8);
|
2024-08-03 08:24:46 +00:00
|
|
|
if (lseek(fd, 0, SEEK_CUR) != 2)
|
2024-08-04 00:48:00 +00:00
|
|
|
_Exit(9);
|
2024-08-03 08:24:46 +00:00
|
|
|
|
|
|
|
*phase = 1;
|
|
|
|
for (;;)
|
|
|
|
if (*phase == 2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (write(fd, "3", 1) != 1)
|
2024-08-04 00:48:00 +00:00
|
|
|
_Exit(10);
|
2024-08-03 08:24:46 +00:00
|
|
|
if (lseek(fd, 0, SEEK_CUR) != 4)
|
2024-08-04 00:48:00 +00:00
|
|
|
_Exit(11);
|
|
|
|
|
2024-08-03 08:24:46 +00:00
|
|
|
*phase = 3;
|
2024-08-04 00:48:00 +00:00
|
|
|
for (;;)
|
|
|
|
if (*phase == 4)
|
|
|
|
break;
|
|
|
|
|
2024-08-03 08:24:46 +00:00
|
|
|
_Exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
if (*phase == 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (write(fd, "2", 1) != 1)
|
2024-08-04 00:48:00 +00:00
|
|
|
return 12;
|
2024-08-03 08:24:46 +00:00
|
|
|
if (lseek(fd, 0, SEEK_CUR) != 3)
|
2024-08-04 00:48:00 +00:00
|
|
|
return 13;
|
2024-08-03 08:24:46 +00:00
|
|
|
|
|
|
|
*phase = 2;
|
|
|
|
for (;;)
|
|
|
|
if (*phase == 3)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (write(fd, "4", 1) != 1)
|
2024-08-04 00:48:00 +00:00
|
|
|
return 14;
|
2024-08-03 08:24:46 +00:00
|
|
|
if (lseek(fd, 0, SEEK_CUR) != 5)
|
2024-08-04 00:48:00 +00:00
|
|
|
return 15;
|
|
|
|
|
|
|
|
signal(SIGCHLD, SIG_DFL);
|
|
|
|
*phase = 4;
|
2024-08-03 08:24:46 +00:00
|
|
|
|
|
|
|
int ws;
|
|
|
|
if (wait(&ws) == -1)
|
2024-08-04 00:48:00 +00:00
|
|
|
return 16;
|
2024-08-03 08:24:46 +00:00
|
|
|
if (!WIFEXITED(ws))
|
2024-08-04 00:48:00 +00:00
|
|
|
return 17;
|
2024-08-03 08:24:46 +00:00
|
|
|
if (WEXITSTATUS(ws))
|
|
|
|
return WEXITSTATUS(ws);
|
|
|
|
|
|
|
|
char buf[16] = {0};
|
|
|
|
if (pread(fd, buf, 15, 0) != 5)
|
2024-08-04 00:48:00 +00:00
|
|
|
return 18;
|
2024-08-03 08:24:46 +00:00
|
|
|
if (lseek(fd, 0, SEEK_CUR) != 5)
|
2024-08-04 00:48:00 +00:00
|
|
|
return 19;
|
2024-08-03 08:24:46 +00:00
|
|
|
|
|
|
|
if (close(fd))
|
2024-08-04 00:48:00 +00:00
|
|
|
return 20;
|
|
|
|
|
|
|
|
if (munmap(phase, sizeof(atomic_int)))
|
|
|
|
return 21;
|
2024-08-03 08:24:46 +00:00
|
|
|
|
|
|
|
if (unlink(path))
|
2024-08-04 00:48:00 +00:00
|
|
|
return 22;
|
2024-08-03 08:24:46 +00:00
|
|
|
|
|
|
|
if (strcmp(buf, "01234"))
|
2024-08-04 00:48:00 +00:00
|
|
|
return 23;
|
2024-08-03 08:24:46 +00:00
|
|
|
}
|