CS318 - Pintos
Pintos source browser for JHU CS318 course
child-rox.c
Go to the documentation of this file.
1 /** Child process run by rox-child and rox-multichild tests.
2  Opens and tries to write to its own executable, verifying that
3  that is disallowed.
4  Then recursively executes itself to the depth indicated by the
5  first command-line argument. */
6 
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <syscall.h>
11 #include "tests/lib.h"
12 
13 const char *test_name = "child-rox";
14 
15 static void
16 try_write (void)
17 {
18  int handle;
19  char buffer[19];
20 
21  quiet = true;
22  CHECK ((handle = open ("child-rox")) > 1, "open \"child-rox\"");
23  quiet = false;
24 
25  CHECK (write (handle, buffer, sizeof buffer) == 0,
26  "try to write \"child-rox\"");
27 
28  close (handle);
29 }
30 
31 int
32 main (int argc UNUSED, char *argv[])
33 {
34  msg ("begin");
35  try_write ();
36 
37  if (!isdigit (*argv[1]))
38  fail ("bad command-line arguments");
39  if (atoi (argv[1]) > 1)
40  {
41  char cmd[128];
42  int child;
43 
44  snprintf (cmd, sizeof cmd, "child-rox %d", atoi (argv[1]) - 1);
45  CHECK ((child = exec (cmd)) != -1, "exec \"%s\"", cmd);
46  quiet = true;
47  CHECK (wait (child) == 12, "wait for \"child-rox\"");
48  quiet = false;
49  }
50 
51  try_write ();
52  msg ("end");
53 
54  return 12;
55 }
lib.h
snprintf
int snprintf(char *buffer, size_t buf_size, const char *format,...)
Like printf(), except that output is stored into BUFFER, which must have space for BUF_SIZE character...
Definition: stdio.c:62
test_name
const char * test_name
Child process run by rox-child and rox-multichild tests.
Definition: child-rox.c:13
write
int write(int fd, const void *buffer, unsigned size)
Definition: syscall.c:121
CHECK
#define CHECK(SUCCESS,...)
Takes an expression to test for SUCCESS and a message, which may include printf-style arguments.
Definition: lib.h:29
atoi
int atoi(const char *s)
Converts a string representation of a signed decimal integer in S into an ‘int’, which is returned.
Definition: stdlib.c:10
UNUSED
#define UNUSED
GCC lets us add "attributes" to functions, function parameters, etc.
Definition: debug.h:7
exec
pid_t exec(const char *file)
Definition: syscall.c:79
ctype.h
buffer
static struct intq buffer
Stores keys from the keyboard and serial port.
Definition: input.c:7
open
int open(const char *file)
Definition: syscall.c:103
fail
void fail(const char *format,...)
Definition: lib.c:40
try_write
static void try_write(void)
Definition: child-rox.c:16
wait
static void wait(struct intq *q, struct thread **waiter)
close
void close(int fd)
Definition: syscall.c:139
msg
void msg(const char *format,...)
Definition: lib.c:28
stdlib.h
isdigit
static int isdigit(int c)
Definition: ctype.h:7
main
int main(int argc UNUSED, char *argv[])
Definition: child-rox.c:32
quiet
bool quiet
Definition: lib.c:9