PKUOS - Pintos
Pintos source browser for PKU Operating System 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
13const char *test_name = "child-rox";
14
15static void
16try_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
31int
32main (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}
int main(int argc UNUSED, char *argv[])
Definition: child-rox.c:32
static void try_write(void)
Definition: child-rox.c:16
const char * test_name
Child process run by rox-child and rox-multichild tests.
Definition: child-rox.c:13
static int isdigit(int c)
Definition: ctype.h:7
#define UNUSED
GCC lets us add "attributes" to functions, function parameters, etc.
Definition: debug.h:7
static struct intq buffer
Stores keys from the keyboard and serial port.
Definition: input.c:7
static void wait(struct intq *q, struct thread **waiter)
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
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
void close(int fd)
Definition: syscall.c:139
int open(const char *file)
Definition: syscall.c:103
int write(int fd, const void *buffer, unsigned size)
Definition: syscall.c:121
pid_t exec(const char *file)
Definition: syscall.c:79
void fail(const char *format,...)
Definition: lib.c:40
void msg(const char *format,...)
Definition: lib.c:28
bool quiet
Definition: lib.c:9
#define CHECK(SUCCESS,...)
Takes an expression to test for SUCCESS and a message, which may include printf-style arguments.
Definition: lib.h:29