summaryrefslogtreecommitdiff
path: root/lib/mlibc/sysdeps/aero/generic/filesystem.cpp
blob: 049f4c0fdc4d99c90b4064e373dd90a8de567e1f (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include "mlibc/fsfd_target.hpp"
#include <aero/syscall.h>

#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>

#include <unistd.h>

#include <mlibc/all-sysdeps.hpp>
#include <mlibc/debug.hpp>

namespace mlibc {
int sys_write(int fd, const void *buffer, size_t count, ssize_t *written) {
    auto result = syscall(SYS_WRITE, fd, buffer, count);

    if (result < 0) {
        return -result;
    }

    *written = result;
    return 0;
}

int sys_read(int fd, void *buf, size_t count, ssize_t *bytes_read) {
    auto result = syscall(SYS_READ, fd, buf, count);

    if (result < 0) {
        *bytes_read = 0;
        return -result;
    }

    *bytes_read = result;
    return 0;
}

// clang-format off
int sys_pwrite(int fd, const void *buffer, size_t count, off_t off,
               ssize_t *written) UNIMPLEMENTED("sys_pwrite") 

// clang-format off
int sys_pread(int fd, void *buf, size_t count,
                off_t off, ssize_t *bytes_read) UNIMPLEMENTED("sys_pread")

int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) {
    auto result = syscall(SYS_SEEK, fd, offset, whence);

    if (result < 0) {
        return -result;
    }

    *new_offset = result;
    return 0;
}

int sys_open(const char *filename, int flags, mode_t mode, int *fd) {
    auto result = syscall(SYS_OPEN, 0, filename, strlen(filename), flags);

    if (result < 0) {
        return -result;
    }

    *fd = result;
    return 0;
}

int sys_close(int fd) {
    auto result = syscall(SYS_CLOSE, fd);

    if (result < 0) {
        return -result;
    }

    return 0;
}

int sys_access(const char *filename, int mode) {
    auto result =
        syscall(SYS_ACCESS, AT_FDCWD, filename, strlen(filename), mode, 0);

    if (result < 0) {
        return -result;
    }

    return 0;
}

int sys_stat(fsfd_target fsfdt, int fd, const char *path, int flags,
             struct stat *statbuf) {
    switch (fsfdt) {
    case fsfd_target::path:
        fd = AT_FDCWD;
        break;
    case fsfd_target::fd:
        flags |= AT_EMPTY_PATH;
    
    case fsfd_target::fd_path:
        break;

    default:
        __ensure(!"Invalid fsfd_target");
        __builtin_unreachable();
    }

    auto ret = syscall(SYS_FSTAT, fd, path, strlen(path), flags, statbuf);
    if(int e = sc_error(ret); e)
        return e;
    return 0;
}

int sys_ioctl(int fd, unsigned long request, void *arg, int *result) {
    auto sys_res = syscall(SYS_IOCTL, fd, request, arg);

    if (sys_res < 0) {
        return -sys_res;
    }

    if (result)
        *result = sys_res;
    return 0;
}

int sys_isatty(int fd) {
    // NOTE: The easiest way to check if a file descriptor is a TTY is to
    // do an ioctl of TIOCGWINSZ on it and see if it succeeds :^)
    struct winsize ws;
    int result;

    if (!sys_ioctl(fd, TIOCGWINSZ, &ws, &result)) {
        return 0;
    }

    return ENOTTY;
}

int sys_tcgetattr(int fd, struct termios *attr) {
    int result;

    if (int e = sys_ioctl(fd, TCGETS, (void *)attr, &result); e)
        return e;

    return 0;
}

int sys_tcsetattr(int fd, int optional_action, const struct termios *attr) {
    int req;

    switch (optional_action) {
		case TCSANOW: req = TCSETS; break;
		case TCSADRAIN: req = TCSETSW; break;
		case TCSAFLUSH: req = TCSETSF; break;
		default: return EINVAL;
	}

    if (int e = sys_ioctl(fd, req, (void *)attr, NULL); e)
        return e;

    return 0;
}

int sys_mkdir(const char *path, mode_t) {
    auto result = syscall(SYS_MKDIR, path, strlen(path));

    if (result < 0) {
        return -result;
    }

    return 0;
}

int sys_link(const char *srcpath, const char *destpath) {
    auto result =
        syscall(SYS_LINK, srcpath, strlen(srcpath), destpath, strlen(destpath));

    if (result < 0) {
        return -result;
    }

    return 0;
}

int sys_rmdir(const char *path) {
    return sys_unlinkat(AT_FDCWD, path, AT_REMOVEDIR);
}

int sys_unlinkat(int fd, const char *path, int flags) {
	auto ret = syscall(SYS_UNLINK, fd, path, strlen(path), flags);
	if (int e = sc_error(ret); e)
		return e;
	return 0;
}

struct aero_dir_entry {
    size_t inode;
    size_t offset;
    size_t reclen;
    size_t filetyp;
    char name[];
} __attribute__((__packed__));

int sys_read_entries(int handle, void *buffer, size_t max_size,
                     size_t *bytes_read) {
    auto result = syscall(SYS_GETDENTS, handle, buffer, max_size);

    // Check if we got an error.
    if (result < 0) {
        *bytes_read = 0;
        return -result;
    }

    // Nothing to read.
    if (result == 0) {
        *bytes_read = 0;
        return 0;
    }

    auto entry = (struct aero_dir_entry *)buffer;

    struct dirent dirent = {
        .d_ino = static_cast<ino_t>(entry->inode),
        .d_off = static_cast<off_t>(entry->offset),
        .d_reclen = static_cast<unsigned short>(entry->reclen),
        .d_type = static_cast<unsigned char>(entry->filetyp),
    };

    // The reclen is the size of the dirent struct, plus the size of the name.
    auto name_size = entry->reclen - sizeof(struct aero_dir_entry);
    __ensure(name_size < 255);

    memcpy(&dirent.d_name, entry->name, name_size);
    *bytes_read = entry->reclen;

    memcpy(buffer, &dirent, sizeof(struct dirent));
    return 0;
}

int sys_open_dir(const char *path, int *handle) {
    return sys_open(path, O_DIRECTORY, 0, handle);
}

int sys_rename(const char *path, const char *new_path) {
    auto result =
        syscall(SYS_RENAME, path, strlen(path), new_path, strlen(new_path));

    if (result < 0) {
        return -result;
    }

    return 0;
}

int sys_readlink(const char *path, void *buffer, size_t max_size,
                 ssize_t *length) {
    auto result = syscall(SYS_READ_LINK, path, strlen(path), buffer, max_size);

    if (result < 0) {
        return -result;
    }

    *length = result;
    return 0;
}

int sys_dup(int fd, int flags, int *newfd) {
    auto result = syscall(SYS_DUP, fd, flags);

    if (result < 0) {
        return -result;
    }

    *newfd = result;
    return 0;
}

int sys_dup2(int fd, int flags, int newfd) {
    auto result = syscall(SYS_DUP2, fd, newfd, flags);

    if (result < 0) {
        return -result;
    }

    return 0;
}

int sys_fcntl(int fd, int request, va_list args, int *result_value) {
    auto result = syscall(SYS_FCNTL, fd, request, va_arg(args, uint64_t));

    if (result < 0) {
        return -result;
    }

    *result_value = result;
    return 0;
}

// int sys_chmod(const char *pathname, mode_t mode) UNIMPLEMENTED("sys_chmod")

int sys_pipe(int *fds, int flags) {
    auto result = syscall(SYS_PIPE, fds, flags);

    if (result < 0) {
        return -result;
    }

    return 0;
}

// epoll API syscalls:
int sys_epoll_create(int flags, int *fd) {
    auto result = syscall(SYS_EPOLL_CREATE, flags);

    if (result < 0) {
        return -result;
    }

    *fd = result;
    return 0;
}

int sys_epoll_ctl(int epfd, int mode, int fd, struct epoll_event *ev) {
    auto result = syscall(SYS_EPOLL_CTL, epfd, mode, fd, ev);

    if (result < 0) {
        return -result;
    }

    return 0;
}

int sys_epoll_pwait(int epfd, struct epoll_event *ev, int n, int timeout,
                    const sigset_t *sigmask, int *raised) {
    auto result = syscall(SYS_EPOLL_PWAIT, epfd, ev, n, timeout, sigmask);

    if (result < 0) {
        return -result;
    }

    *raised = result;
    return 0;
}

int sys_eventfd_create(unsigned int initval, int flags, int *fd) {
    auto result = syscall(SYS_EVENT_FD, initval, flags);

    if (result < 0) {
        return -result;
    }

    *fd = result;
    return 0;
}

int sys_ppoll(struct pollfd *fds, int nfds, const struct timespec *timeout,
              const sigset_t *sigmask, int *num_events) {
    auto result = syscall(SYS_POLL, fds, nfds, timeout, sigmask);

    if (result < 0) {
        return -result;
    }

    *num_events = result;
    return 0;
}

int sys_poll(struct pollfd *fds, nfds_t count, int timeout, int *num_events) {
    struct timespec ts;
    ts.tv_sec = timeout / 1000;
    ts.tv_nsec = (timeout % 1000) * 1000000;

    return sys_ppoll(fds, count, &ts, NULL, num_events);
}

#ifndef MLIBC_BUILDING_RTDL
#include <stdio.h>
int sys_ptsname(int fd, char *buffer, size_t length) {
    int index;
    if (int e = sys_ioctl(fd, TIOCGPTN, &index, NULL); e)
        return e;
    if ((size_t)snprintf(buffer, length, "/dev/pts/%d", index) >= length) {
        return ERANGE;
    }
    return 0;
}

int sys_pselect(int num_fds, fd_set *read_set, fd_set *write_set,
                fd_set *except_set, const struct timespec *timeout,
                const sigset_t *sigmask, int *num_events) {
    int fd = epoll_create1(0);
    if (fd == -1)
        return -1;

    for (int k = 0; k < FD_SETSIZE; k++) {
        struct epoll_event ev;
        memset(&ev, 0, sizeof(struct epoll_event));

        if (read_set && FD_ISSET(k, read_set))
            ev.events |= EPOLLIN;
        if (write_set && FD_ISSET(k, write_set))
            ev.events |= EPOLLOUT;
        if (except_set && FD_ISSET(k, except_set))
            ev.events |= EPOLLPRI;

        if (!ev.events)
            continue;

        ev.data.u32 = k;
        if (epoll_ctl(fd, EPOLL_CTL_ADD, k, &ev))
            return -1;
    }

    struct epoll_event evnts[16];
    int n = epoll_pwait(
        fd, evnts, 16,
        timeout ? (timeout->tv_sec * 1000 + timeout->tv_nsec / 100) : -1,
        sigmask);

    if (n == -1)
        return -1;

    fd_set res_read_set;
    fd_set res_write_set;
    fd_set res_except_set;
    FD_ZERO(&res_read_set);
    FD_ZERO(&res_write_set);
    FD_ZERO(&res_except_set);

    int m = 0;

    for (int i = 0; i < n; i++) {
        int k = evnts[i].data.u32;

        if (read_set && FD_ISSET(k, read_set) &&
            evnts[i].events & (EPOLLIN | EPOLLERR | EPOLLHUP)) {
            FD_SET(k, &res_read_set);
            m++;
        }

        if (write_set && FD_ISSET(k, write_set) &&
            evnts[i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP)) {
            FD_SET(k, &res_write_set);
            m++;
        }

        if (except_set && FD_ISSET(k, except_set) &&
            evnts[i].events & EPOLLPRI) {
            FD_SET(k, &res_except_set);
            m++;
        }
    }

    if (close(fd))
        __ensure("mlibc::pselect: close() failed on epoll file");

    if (read_set)
        memcpy(read_set, &res_read_set, sizeof(fd_set));

    if (write_set)
        memcpy(write_set, &res_write_set, sizeof(fd_set));

    if (except_set)
        memcpy(except_set, &res_except_set, sizeof(fd_set));

    *num_events = m;
    return 0;
}
#endif // #ifndef MLIBC_BUILDING_RTDL
} // namespace mlibc