1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | // Contains wrappers for functions whose required headers conflict with the PAL |
6 | |
7 | #include <sys/file.h> |
8 | #include <sys/stat.h> |
9 | #include <sys/types.h> |
10 | |
11 | #include <fcntl.h> |
12 | #include <signal.h> |
13 | #include <stdio.h> |
14 | #include <string.h> |
15 | #include <unistd.h> |
16 | |
17 | #define _countof(a) (sizeof(a) / sizeof(a[0])) |
18 | |
19 | #undef PAGE_SIZE |
20 | #define PAGE_SIZE (4096) |
21 | |
22 | auto test_strcpy = strcpy; |
23 | auto test_strcmp = strcmp; |
24 | auto test_strlen = strlen; |
25 | auto test_sprintf = sprintf; |
26 | auto test_sscanf = sscanf; |
27 | auto test_close = close; |
28 | auto test_unlink = unlink; |
29 | |
30 | unsigned int test_getpid() |
31 | { |
32 | return getpid(); |
33 | } |
34 | |
35 | int test_kill(unsigned int pid) |
36 | { |
37 | return kill(pid, SIGKILL); |
38 | } |
39 | |
40 | bool TestFileExists(const char *path) |
41 | { |
42 | int fd = open(path, O_RDWR); |
43 | if (fd == -1) |
44 | return false; |
45 | close(fd); |
46 | return true; |
47 | } |
48 | |
49 | bool (const char *path, char sharedMemoryType, char version, int *fdRef) |
50 | { |
51 | int fd = open(path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
52 | if (fd == -1) |
53 | return false; |
54 | *fdRef = fd; |
55 | if (ftruncate(fd, PAGE_SIZE) != 0) |
56 | return false; |
57 | if (lseek(fd, 0, SEEK_SET) != 0) |
58 | return false; |
59 | |
60 | // See SharedMemorySharedDataHeader for format |
61 | char buffer[] = {sharedMemoryType, version}; |
62 | if (write(fd, buffer, _countof(buffer)) != _countof(buffer)) |
63 | return false; |
64 | |
65 | return flock(fd, LOCK_SH | LOCK_NB) == 0; |
66 | } |
67 | |