mirror of
https://github.com/drasko/codezero.git
synced 2026-02-12 18:03:15 +01:00
Modified the kernel and all tasks with well-formatted printout messages.
This commit is contained in:
@@ -7,15 +7,14 @@
|
||||
#include <sys/mman.h>
|
||||
#include <sched.h>
|
||||
#include <errno.h>
|
||||
#include <tests.h>
|
||||
|
||||
int clone_global = 0;
|
||||
|
||||
int my_thread_func(void *arg)
|
||||
{
|
||||
printf("Cloned child %d running...\n", getpid());
|
||||
for (int i = 0; i < 25; i++)
|
||||
clone_global++;
|
||||
printf("Cloned child exiting with global increased to: %d. (Should be just about 100 at final)\n", clone_global);
|
||||
_exit(0);
|
||||
}
|
||||
|
||||
@@ -27,27 +26,30 @@ int clonetest(void)
|
||||
/* Parent loops and calls clone() to clone new threads. Children don't come back from the clone() call */
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if ((child_stack = mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_GROWSDOWN, 0, 0)) == MAP_FAILED) {
|
||||
printf("MMAP failed.\n");
|
||||
_exit(1);
|
||||
test_printf("MMAP failed.\n");
|
||||
goto out_err;
|
||||
} else {
|
||||
printf("Mapped area starting at %p\n", child_stack);
|
||||
test_printf("Mapped area starting at %p\n", child_stack);
|
||||
}
|
||||
((int *)child_stack)[-1] = 5; /* Test mapped area */
|
||||
|
||||
printf("Cloning...\n");
|
||||
test_printf("Cloning...\n");
|
||||
|
||||
if ((childid = clone(my_thread_func, child_stack,
|
||||
CLONE_PARENT | CLONE_FS | CLONE_VM | CLONE_THREAD | CLONE_SIGHAND, 0)) < 0) {
|
||||
perror("CLONE failed.\n");
|
||||
test_printf("CLONE failed.\n");
|
||||
goto out_err;
|
||||
} else {
|
||||
printf("Cloned a new thread with child pid %d\n", childid);
|
||||
test_printf("Cloned a new thread with child pid %d\n", childid);
|
||||
}
|
||||
}
|
||||
_exit(0);
|
||||
|
||||
/* TODO: Add wait() or something similar and check that global is 100 */
|
||||
|
||||
printf("CLONE TEST -- PASSED --\n");
|
||||
return 0;
|
||||
out_err:
|
||||
printf("CLONE TEST -- FAILED --\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -95,7 +95,7 @@ void print_dirents(char *path, void *buf, int cnt)
|
||||
//if (stat(pathbuf, &statbuf) < 0)
|
||||
// perror("STAT");
|
||||
// print_fstat(&statbuf);
|
||||
printf("%s\n", dp->d_name);
|
||||
test_printf("%s\n", dp->d_name);
|
||||
cnt -= dp->d_reclen;
|
||||
dp = (struct dirent *)((void *)dp + dp->d_reclen);
|
||||
i++;
|
||||
@@ -111,13 +111,13 @@ int lsdir(char *path)
|
||||
memset(dents, 0, sizeof(struct dirent) * DENTS_TOTAL);
|
||||
|
||||
if ((fd = open(path, O_RDONLY)) < 0) {
|
||||
printf("OPEN failed.\n");
|
||||
test_printf("OPEN failed.\n");
|
||||
return -1;
|
||||
} else
|
||||
printf("Got fd: %d for opening %s\n", fd, path);
|
||||
test_printf("Got fd: %d for opening %s\n", fd, path);
|
||||
|
||||
if ((bytes = os_readdir(fd, dents, sizeof(struct dirent) * DENTS_TOTAL)) < 0) {
|
||||
printf("GETDENTS error: %d\n", bytes);
|
||||
test_printf("GETDENTS error: %d\n", bytes);
|
||||
return -1;
|
||||
} else {
|
||||
print_dirents(path, dents, bytes);
|
||||
@@ -126,61 +126,88 @@ int lsdir(char *path)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int dirtest(void)
|
||||
{
|
||||
printf("\nlsdir current directory:\n");
|
||||
if (lsdir(".") < 0) {
|
||||
printf("lsdir failed.\n");
|
||||
test_printf("lsdir failed.\n");
|
||||
goto out_err;
|
||||
}
|
||||
printf("\nlsdir root directory:\n");
|
||||
if (lsdir("/") < 0) {
|
||||
printf("lsdir failed.\n");
|
||||
test_printf("lsdir failed.\n");
|
||||
goto out_err;
|
||||
}
|
||||
|
||||
printf("\nCreating directories: usr, etc, tmp, var, home, opt, bin, boot, lib, dev\n");
|
||||
if (mkdir("/usr", 0) < 0)
|
||||
perror("MKDIR");
|
||||
if (mkdir("/etc", 0) < 0)
|
||||
perror("MKDIR");
|
||||
if (mkdir("/tmp", 0) < 0)
|
||||
perror("MKDIR");
|
||||
if (mkdir("/var", 0) < 0)
|
||||
perror("MKDIR");
|
||||
if (mkdir("/bin", 0) < 0)
|
||||
perror("MKDIR");
|
||||
if (mkdir("/boot", 0) < 0)
|
||||
perror("MKDIR");
|
||||
if (mkdir("/lib", 0) < 0)
|
||||
perror("MKDIR");
|
||||
if (mkdir("/dev", 0) < 0)
|
||||
perror("MKDIR");
|
||||
test_printf("\nCreating directories: usr, etc, tmp, var, home, opt, bin, boot, lib, dev\n");
|
||||
if (mkdir("/usr", 0) < 0) {
|
||||
test_printf("MKDIR: %d\n", errno);
|
||||
goto out_err;
|
||||
}
|
||||
if (mkdir("/etc", 0) < 0) {
|
||||
test_printf("MKDIR: %d\n", errno);
|
||||
goto out_err;
|
||||
}
|
||||
if (mkdir("/tmp", 0) < 0) {
|
||||
test_printf("MKDIR: %d\n", errno);
|
||||
goto out_err;
|
||||
}
|
||||
if (mkdir("/var", 0) < 0) {
|
||||
test_printf("MKDIR: %d\n", errno);
|
||||
goto out_err;
|
||||
}
|
||||
if (mkdir("/bin", 0) < 0) {
|
||||
test_printf("MKDIR: %d\n", errno);
|
||||
goto out_err;
|
||||
}
|
||||
if (mkdir("/boot", 0) < 0) {
|
||||
test_printf("MKDIR: %d\n", errno);
|
||||
goto out_err;
|
||||
}
|
||||
if (mkdir("/lib", 0) < 0) {
|
||||
test_printf("MKDIR: %d\n", errno);
|
||||
goto out_err;
|
||||
}
|
||||
if (mkdir("/dev", 0) < 0) {
|
||||
test_printf("MKDIR: %d\n", errno);
|
||||
goto out_err;
|
||||
}
|
||||
if (mkdir("/usr/bin", 0) < 0) {
|
||||
test_printf("MKDIR: %d\n", errno);
|
||||
goto out_err;
|
||||
}
|
||||
if (mkdir("/home/", 0) < 0) {
|
||||
test_printf("MKDIR: %d\n", errno);
|
||||
goto out_err;
|
||||
}
|
||||
if (mkdir("/home/bahadir", 0) < 0) {
|
||||
test_printf("MKDIR: %d\n", errno);
|
||||
goto out_err;
|
||||
}
|
||||
if (chdir("/home/bahadir") < 0) {
|
||||
test_printf("MKDIR: %d\n", errno);
|
||||
goto out_err;
|
||||
}
|
||||
test_printf("Changed curdir to /home/bahadir\n");
|
||||
|
||||
if (mkdir("/usr/bin", 0) < 0)
|
||||
perror("MKDIR");
|
||||
if (mkdir("/home/", 0) < 0)
|
||||
perror("MKDIR");
|
||||
if (mkdir("/home/bahadir", 0) < 0)
|
||||
perror("MKDIR");
|
||||
if (chdir("/home/bahadir") < 0)
|
||||
perror("CHDIR");
|
||||
printf("Changed curdir to /home/bahadir\n");
|
||||
test_printf("\nlsdir root directory:\n");
|
||||
if (lsdir("/") < 0)
|
||||
goto out_err;
|
||||
|
||||
printf("\nlsdir root directory:\n");
|
||||
lsdir("/");
|
||||
test_printf("\nlsdir /usr:\n");
|
||||
if (lsdir("/usr") < 0)
|
||||
goto out_err;
|
||||
|
||||
printf("\nlsdir /usr:\n");
|
||||
lsdir("/usr");
|
||||
test_printf("\nlsdir current directory:\n");
|
||||
if (lsdir(".") < 0)
|
||||
goto out_err;
|
||||
test_printf("\nlsdir /usr/./././bin//\n");
|
||||
if (lsdir("/usr/./././bin//") < 0)
|
||||
goto out_err;
|
||||
|
||||
printf("\nlsdir current directory:\n");
|
||||
lsdir(".");
|
||||
printf("\nlsdir /usr/./././bin//\n");
|
||||
lsdir("/usr/./././bin//");
|
||||
printf("DIR TEST -- PASSED --\n");
|
||||
return 0;
|
||||
|
||||
out_err:
|
||||
printf("DIR TEST -- FAILED --\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
extern char _start_test1[];
|
||||
extern char _end_test1[];
|
||||
@@ -23,24 +23,20 @@ int exectest(void)
|
||||
char *argv[5];
|
||||
|
||||
/* First create a new file and write the executable data to that file */
|
||||
printf("%s: Creating new executable file.\n", __FUNCTION__);
|
||||
if ((fd = open("/home/bahadir/test1.axf", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU)) < 0) {
|
||||
perror("OPEN");
|
||||
return -1;
|
||||
test_printf("OPEN: %d\n", errno);
|
||||
goto out_err;
|
||||
}
|
||||
|
||||
printf("%s: Writing to the executable file.\n", __FUNCTION__);
|
||||
left = size;
|
||||
while (left != 0) {
|
||||
cnt = write(fd, exec_start, left);
|
||||
if (cnt < 0) {
|
||||
printf("Error writing to file.\n");
|
||||
return -1;
|
||||
}
|
||||
if ((cnt = write(fd, exec_start, left)) < 0)
|
||||
goto out_err;
|
||||
left -= cnt;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
if (close(fd) < 0)
|
||||
goto out_err;
|
||||
|
||||
/* Set up some arguments */
|
||||
argv[0] = "FIRST ARG";
|
||||
@@ -49,10 +45,11 @@ int exectest(void)
|
||||
argv[3] = "FOURTH ARG";
|
||||
argv[4] = 0;
|
||||
|
||||
printf("%s: Executing the file.\n", __FUNCTION__);
|
||||
/* Execute the file */
|
||||
execve("/home/bahadir/test1.axf", argv, 0);
|
||||
|
||||
out_err:
|
||||
printf("EXECVE TEST -- FAILED --\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,71 +6,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <tests.h>
|
||||
|
||||
int fileio2(void)
|
||||
{
|
||||
int fd;
|
||||
ssize_t cnt;
|
||||
int err;
|
||||
char buf[128];
|
||||
off_t offset;
|
||||
int tid = getpid();
|
||||
char *str = "I WROTE TO THIS FILE\n";
|
||||
|
||||
memset(buf, 0, 128);
|
||||
if ((fd = open("/home/bahadir/newfile2.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU)) < 0) {
|
||||
perror("OPEN");
|
||||
return -1;
|
||||
}
|
||||
printf("%d: Created newfile2.txt\n", tid);
|
||||
|
||||
printf("%d: write.\n", tid);
|
||||
if ((int)(cnt = write(fd, str, strlen(str))) < 0) {
|
||||
perror("WRITE");
|
||||
return -1;
|
||||
}
|
||||
printf("%d: close.\n", tid);
|
||||
if ((err = close(fd)) < 0) {
|
||||
printf("Close failed.\n");
|
||||
perror("CLOSE");
|
||||
return -1;
|
||||
}
|
||||
printf("%d: re-open.\n", tid);
|
||||
if ((fd = open("/home/bahadir/newfile2.txt", O_RDWR, S_IRWXU)) < 0) {
|
||||
perror("OPEN");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("%d: lseek.\n", tid);
|
||||
if ((int)(offset = lseek(fd, 0, SEEK_SET)) < 0) {
|
||||
perror("LSEEK");
|
||||
return -1;
|
||||
}
|
||||
printf("%d: read.\n", tid);
|
||||
if ((int)(cnt = read(fd, buf, strlen(str))) < 0) {
|
||||
perror("READ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("%d: Read: %d bytes from file.\n", tid, cnt);
|
||||
if (cnt) {
|
||||
printf("%d: Read string: %s\n", tid, buf);
|
||||
if (strcmp(buf, str)) {
|
||||
printf("Error: strings not the same:\n");
|
||||
printf("str: %s\n", str);
|
||||
printf("buf: %s\n", buf);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d: close.\n", tid);
|
||||
if ((err = close(fd)) < 0) {
|
||||
perror("CLOSE");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#include <errno.h>
|
||||
|
||||
int fileio(void)
|
||||
{
|
||||
@@ -81,64 +17,72 @@ int fileio(void)
|
||||
off_t offset;
|
||||
int tid = getpid();
|
||||
char *str = "I WROTE TO THIS FILE\n";
|
||||
char filename[128];
|
||||
|
||||
memset(buf, 0, 128);
|
||||
if ((fd = open("/home/bahadir/newfile.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRWXU)) < 0) {
|
||||
perror("OPEN");
|
||||
return -1;
|
||||
memset(filename, 0, 128);
|
||||
sprintf(filename, "/home/bahadir/newfile%d.txt", tid);
|
||||
if ((fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU)) < 0) {
|
||||
test_printf("OPEN: %d", errno);
|
||||
goto out_err;
|
||||
}
|
||||
printf("%d: Created newfile.txt\n", tid);
|
||||
test_printf("%d: Created %s\n", tid, filename);
|
||||
|
||||
printf("%d: write.\n", tid);
|
||||
test_printf("%d: write.\n", tid);
|
||||
if ((int)(cnt = write(fd, str, strlen(str))) < 0) {
|
||||
perror("WRITE");
|
||||
return -1;
|
||||
test_printf("WRITE: %d", errno);
|
||||
goto out_err;
|
||||
}
|
||||
|
||||
printf("%d: lseek.\n", tid);
|
||||
test_printf("%d: lseek.\n", tid);
|
||||
if ((int)(offset = lseek(fd, 0, SEEK_SET)) < 0) {
|
||||
perror("LSEEK");
|
||||
return -1;
|
||||
test_printf("LSEEK: %d", errno);
|
||||
goto out_err;
|
||||
}
|
||||
printf("%d: read.\n", tid);
|
||||
test_printf("%d: read.\n", tid);
|
||||
if ((int)(cnt = read(fd, buf, strlen(str))) < 0) {
|
||||
perror("READ");
|
||||
return -1;
|
||||
test_printf("READ: %d", errno);
|
||||
goto out_err;
|
||||
}
|
||||
|
||||
printf("%d: Read: %d bytes from file.\n", tid, cnt);
|
||||
test_printf("%d: Read: %d bytes from file.\n", tid, cnt);
|
||||
if (cnt) {
|
||||
printf("%d: Read string: %s\n", tid, buf);
|
||||
test_printf("%d: Read string: %s\n", tid, buf);
|
||||
if (strcmp(buf, str)) {
|
||||
printf("Strings not the same:\n");
|
||||
printf("Str: %s\n", str);
|
||||
printf("Buf: %s\n", buf);
|
||||
return -1;
|
||||
test_printf("Strings not the same:\n");
|
||||
test_printf("Str: %s\n", str);
|
||||
test_printf("Buf: %s\n", buf);
|
||||
goto out_err;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%d: close.\n", tid);
|
||||
if ((err = close(fd)) < 0) {
|
||||
perror("CLOSE");
|
||||
return -1;
|
||||
test_printf("CLOSE: %d", errno);
|
||||
goto out_err;
|
||||
}
|
||||
|
||||
printf("FILE IO TEST -- PASSED --\n");
|
||||
return 0;
|
||||
|
||||
out_err:
|
||||
printf("FILE IO TEST -- FAILED --\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(HOST_TESTS)
|
||||
int main(void)
|
||||
{
|
||||
printf("File IO test 1:\n");
|
||||
test_printf("File IO test 1:\n");
|
||||
if (fileio() == 0)
|
||||
printf("-- PASSED --\n");
|
||||
test_printf("-- PASSED --\n");
|
||||
else
|
||||
printf("-- FAILED --\n");
|
||||
test_printf("-- FAILED --\n");
|
||||
|
||||
printf("File IO test 2:\n");
|
||||
test_printf("File IO test 2:\n");
|
||||
if (fileio2() == 0)
|
||||
printf("-- PASSED --\n");
|
||||
test_printf("-- PASSED --\n");
|
||||
else
|
||||
printf("-- FAILED --\n");
|
||||
test_printf("-- FAILED --\n");
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -9,37 +9,45 @@
|
||||
#include <l4/macros.h>
|
||||
|
||||
int global = 0;
|
||||
extern pid_t pid;
|
||||
|
||||
static pid_t pid;
|
||||
|
||||
int forktest(void)
|
||||
{
|
||||
pid_t myid;
|
||||
pid_t parent = getpid();
|
||||
|
||||
/* 16 forks */
|
||||
for (int i = 0; i < 4; i++)
|
||||
fork();
|
||||
if (fork() < 0)
|
||||
goto out_err;
|
||||
|
||||
myid = getpid();
|
||||
pid = myid;
|
||||
if (global != 0) {
|
||||
printf("Global not zero.\n");
|
||||
printf("-- FAILED --\n");
|
||||
goto out;
|
||||
test_printf("Global not zero.\n");
|
||||
test_printf("-- FAILED --\n");
|
||||
goto out_err;
|
||||
}
|
||||
global = myid;
|
||||
global += myid;
|
||||
|
||||
if (global != myid) {
|
||||
printf("Global has not changed to myid.\n");
|
||||
printf("-- FAILED --\n");
|
||||
goto out;
|
||||
if (global != myid)
|
||||
goto out_err;
|
||||
|
||||
|
||||
if (getpid() != parent) {
|
||||
/* Successful childs return here */
|
||||
_exit(0);
|
||||
BUG();
|
||||
}
|
||||
|
||||
/* Print only when failed, otherwise too many pass messages */
|
||||
printf("PID: %d, my global: %d\n", myid, global);
|
||||
printf("-- PASSED --\n");
|
||||
out:
|
||||
printf("PID: %d exiting...\n", myid);
|
||||
_exit(0);
|
||||
BUG();
|
||||
/* Parent of all comes here if successful */
|
||||
printf("FORK TEST -- PASSED --\n");
|
||||
return 0;
|
||||
|
||||
/* Any erroneous child or parent comes here */
|
||||
out_err:
|
||||
printf("FORK TEST -- FAILED --\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <tests.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define PAGE_SIZE 0x1000
|
||||
|
||||
@@ -23,42 +24,33 @@ int mmaptest(void)
|
||||
int x = 0x1000;
|
||||
|
||||
if ((fd = open("./newfile3.txt", O_CREAT | O_TRUNC | O_RDWR, S_IRWXU)) < 0)
|
||||
perror("open:");
|
||||
else
|
||||
printf("open: Success.\n");
|
||||
goto out_err;
|
||||
|
||||
/* Extend the file */
|
||||
if ((int)lseek(fd, PAGE_SIZE*16, SEEK_SET) < 0)
|
||||
perror("lseek");
|
||||
else
|
||||
printf("lseek: Success.\n");
|
||||
goto out_err;
|
||||
|
||||
if (write(fd, &x, sizeof(x)) < 0)
|
||||
perror("write");
|
||||
else
|
||||
printf("write: Success.\n");
|
||||
goto out_err;
|
||||
|
||||
printf("%s: Calling mmap()\n", __TASKNAME__);
|
||||
if ((int)(base = mmap(0, PAGE_SIZE*16, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) < 0)
|
||||
perror("mmap");
|
||||
else
|
||||
printf("mmap: Success: %p\n", base);
|
||||
goto out_err;
|
||||
|
||||
*(unsigned int *)(base + PAGE_SIZE*2) = 0x1000;
|
||||
printf("%s: Calling msync()\n", __TASKNAME__);
|
||||
if (msync(base + PAGE_SIZE*2, PAGE_SIZE, MS_SYNC) < 0)
|
||||
perror("msync");
|
||||
else
|
||||
printf("msync: Success: %p\n", base);
|
||||
goto out_err;
|
||||
|
||||
printf("%s: Calling munmap()\n", __TASKNAME__);
|
||||
if (munmap(base + PAGE_SIZE*2, PAGE_SIZE) < 0)
|
||||
perror("munmap");
|
||||
else
|
||||
printf("munmap: Success: %p\n", base);
|
||||
goto out_err;
|
||||
|
||||
*(unsigned int *)(base + PAGE_SIZE*3) = 0x1000;
|
||||
*(unsigned int *)(base + PAGE_SIZE*1) = 0x1000;
|
||||
|
||||
printf("MMAP TEST -- PASSED --\n");
|
||||
return 0;
|
||||
|
||||
out_err:
|
||||
printf("MMAP TEST -- FAILED --\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user