Files
graphene/LibOS/shim/test/regression/device.c
Dmitrii Kuvaiskii 0c3324aa06 [Pal/{Linux,Linux-SGX}] Allow communication with host devices
Previously, Graphene supported only /dev/tty (stdin/stdout terminal)
device. This commit introduces support for other, arbitrary host
devices. The devices must be explicitly allowed in the manifest via
a mount point like this:

  fs.mount.devkmsg.type = chroot
  fs.mount.devkmsg.path = /dev/kmsg
  fs.mount.devkmsg.uri  = dev:/dev/kmsg

Currently only open/read/write/close/fstat are supported. lseek is
supported only with offset 0 (and it is not device-specific). Support
for IOCTLs will be introduced in future commits. A simple test is
added to LibOS regression tests.
2020-12-01 07:24:42 -08:00

52 lines
1.4 KiB
C

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char* arvg[]) {
int devfd = open("/dev/kmsg", O_RDONLY);
if (devfd < 0)
err(1, "/dev/kmsg open");
off_t offset;
#if 0
/* FIXME: doesn't work in Graphene because lseek() is fully emulated in LibOS and therefore
* lseek() is not aware of device-specific semantics */
offset = lseek(devfd, 0, SEEK_CUR);
if (offset != -1 || errno != EINVAL) {
errx(1, "/dev/kmsg lseek(0, SEEK_CUR) didn't return -EINVAL (returned: %ld, errno=%d)",
offset, errno);
}
offset = lseek(devfd, 1, SEEK_CUR);
if (offset != -1 || errno != ESPIPE) {
errx(1, "/dev/kmsg lseek(1, SEEK_CUR) didn't return -ESPIPE (returned: %ld, errno=%d)",
offset, errno);
}
#endif
offset = lseek(devfd, /*offset=*/0, SEEK_SET);
if (offset < 0)
err(1, "/dev/kmsg lseek(0, SEEK_SET)");
if (offset > 0)
errx(1, "/dev/kmsg lseek(0, SEEK_SET) didn't return 0 (returned: %ld)", offset);
char buf[1024];
ssize_t bytes = read(devfd, buf, sizeof(buf) - 1);
if (bytes < 0)
err(1, "/dev/kmsg read");
buf[bytes] = '\0';
printf("First line of /dev/kmsg: %s", buf);
int ret = close(devfd);
if (ret < 0)
err(1, "/dev/kmsg close");
puts("TEST OK");
return 0;
}