aboutsummaryrefslogtreecommitdiff
path: root/kernel/include/test/vfstest/vfstest.h
blob: 4f86563766f24eafbf124a5ef43625487e0c9874 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#pragma once

/* "kernel" utility things */

/* fprintf */
#define fprintf(fd, fmt, args...) dbg(DBG_TEST, fmt, ##args)
#define printf(fmt, args...) dbg(DBG_TEST, fmt, ##args)

/* errno */
#define errno (curthr->kt_errno)

/* malloc/free */
#define malloc kmalloc
#define free kfree

/* The "kernel" system calls */
#define ksyscall(name, formal, actual) \
    static long ksys_##name formal     \
    {                                  \
        long ret = do_##name actual;   \
        if (ret < 0)                   \
        {                              \
            errno = -ret;              \
            return -1;                 \
        }                              \
        return ret;                    \
    }

ksyscall(close, (int fd), (fd))

    ksyscall(read, (int fd, void *buf, size_t nbytes), (fd, buf, nbytes))

        ksyscall(write, (int fd, const void *buf, size_t nbytes),
                 (fd, buf, nbytes))

            ksyscall(dup, (int fd), (fd))

                ksyscall(dup2, (int ofd, int nfd), (ofd, nfd))

                    ksyscall(mkdir, (const char *path), (path))

                        ksyscall(rmdir, (const char *path), (path))

                            ksyscall(link, (const char *old, const char *new),
                                     (old, new))

                                ksyscall(unlink, (const char *path), (path))

                                    ksyscall(rename,
                                             (const char *oldpath,
                                              const char *newpath),
                                             (oldpath, newpath))

                                        ksyscall(chdir, (const char *path),
                                                 (path))

                                            ksyscall(lseek,
                                                     (int fd, int offset,
                                                      int whence),
                                                     (fd, offset, whence))

                                                ksyscall(getdent,
                                                         (int fd,
                                                          struct dirent *dirp),
                                                         (fd, dirp))

                                                    ksyscall(stat,
                                                             (const char *path,
                                                              struct stat *uf),
                                                             (path, uf))

                                                        ksyscall(open,
                                                                 (const char
                                                                      *filename,
                                                                  int flags),
                                                                 (filename,
                                                                  flags))
#define ksys_exit do_exit

                                                            long ksys_getdents(
                                                                int fd,
                                                                struct dirent
                                                                    *dirp,
                                                                unsigned int
                                                                    count)
{
    size_t numbytesread = 0;
    int nbr = 0;
    dirent_t tempdirent;

    if (count < sizeof(dirent_t))
    {
        curthr->kt_errno = EINVAL;
        return -1;
    }

    while (numbytesread < count)
    {
        if ((nbr = do_getdent(fd, &tempdirent)) < 0)
        {
            curthr->kt_errno = -nbr;
            return -1;
        }
        if (nbr == 0)
        {
            return numbytesread;
        }
        memcpy(dirp, &tempdirent, sizeof(dirent_t));

        KASSERT(nbr == sizeof(dirent_t));

        dirp++;
        numbytesread += nbr;
    }
    return numbytesread;
}

/*
 * Redirect system calls to kernel system calls.
 */
#define mkdir(a, b) ksys_mkdir(a)
#define rmdir ksys_rmdir
#define mount ksys_mount
#define umount ksys_umount
#define open(a, b, c) ksys_open(a, b)
#define close ksys_close
#define link ksys_link
#define rename ksys_rename
#define unlink ksys_unlink
#define read ksys_read
#define write ksys_write
#define lseek ksys_lseek
#define dup ksys_dup
#define dup2 ksys_dup2
#define chdir ksys_chdir
#define stat(a, b) ksys_stat(a, b)
#define getdents(a, b, c) ksys_getdents(a, b, c)
#define exit(a) ksys_exit(a)

/* Random numbers */
/* Random int between lo and hi inclusive */
#define RAND_MAX INT_MAX
#define RANDOM(lo, hi)                                                        \
    ((lo) +                                                                   \
     (((hi) - (lo) + 1) * (randseed = (randseed * 4096 + 150889) % 714025)) / \
         714025)

static unsigned long long randseed = 123456L;

static unsigned long long rand(void)
{
    randseed = (randseed * 4096 + 150889) % RAND_MAX;
    return randseed;
}

static void srand(unsigned int seed) { randseed = seed; }