The Pedigree Project  0.1
syscall-test.c
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 #define PASS 1
28 #define FAIL 0
29 
30 int test_open(void)
31 {
32  printf("open():\n");
33  printf("\tOpen existing file - ");
34  int fd = open("/applications/shell", O_RDONLY);
35  if (fd != -1)
36  {
37  close(fd);
38  printf("PASS\n");
39  }
40  else
41  {
42  printf("FAIL - errno %d (%s)\n", errno, strerror(errno));
43  return FAIL;
44  }
45 
46  printf("\tOpen nonexistant file - ");
47  int fd2 = open("/applications/penis", O_RDONLY);
48  if (fd2 == -1 && errno == ENOENT)
49  {
50  close(fd2);
51  printf("PASS\n");
52  }
53  else
54  {
55  printf("FAIL - errno %d (%s)\n", errno, strerror(errno));
56  return FAIL;
57  }
58 
59  printf("\tCreate file - ");
60  int fd3 = open("/file-doesnt-exist", O_RDWR | O_CREAT);
61  if (fd3 != -1)
62  {
63  close(fd3);
64  printf("PASS\n");
65  }
66  else
67  {
68  printf("FAIL - errno %d (%s)\n", errno, strerror(errno));
69  return FAIL;
70  }
71 
72  printf("\tRecycle descriptors - ");
73  int fd4 = open("/applications/bash", O_RDWR);
74  close(fd4);
75  int fd5 = open("/applications/bash", O_RDWR);
76  close(fd5);
77  if (fd4 == fd5)
78  {
79  printf("PASS\n");
80  }
81  else
82  {
83  printf("FAIL - %d, %d\n", fd4, fd5);
84  return FAIL;
85  }
86 
87  int hahaha = open("/applications/bash", O_RDWR);
88  pid_t pid = fork();
89 
90  if (pid == -1)
91  {
92  printf("FAIL - fork failed\n");
93  return FAIL;
94  }
95 
96  if (pid == 0)
97  {
98  close(hahaha);
99  int rofl = open("/applications/bash", O_RDWR);
100 
101  printf("%d/%d\n", hahaha, rofl);
102 
103  close(rofl);
104  exit(0);
105  }
106  close(hahaha);
107 
108  printf("Complete\n");
109 
110  while (1)
111  ;
112 
113  return PASS;
114 }
115 
116 int main(int argc, char **argv)
117 {
118  printf("argc: %d, argv[0]: %s, &optind: %x\n", argc, argv[0], &optind);
119  while (getopt(
120  argc, argv,
121  "h?ABC:DEFHIKLNOQ:RST:UVWY:abcdefgijklmo:pr:s:tvwxz") != -1)
122  {
123  printf("bleh\n");
124  }
125  printf("optind: %d\n", optind);
126  printf("Syscall test starting...\n");
127 
128  if (test_open() == PASS)
129  return EXIT_SUCCESS;
130  else
131  return EXIT_FAILURE;
132 }