Simplified some SB Posix stuff.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15297 27425a3e-05d8-49a3-a47f-9c15f0e5edd8
This commit is contained in:
Giovanni Di Sirio 2021-12-31 14:06:43 +00:00
parent dd88643cce
commit a557acbcd2
4 changed files with 27 additions and 49 deletions

View File

@ -156,7 +156,7 @@ static void SBHandler(eventid_t id) {
*/ */
int main(void) { int main(void) {
event_listener_t el0, el1, el2; event_listener_t el0, el1, el2;
vfs_file_node_c *fnp; vfs_node_c *np;
msg_t ret; msg_t ret;
static const evhandler_t evhndl[] = { static const evhandler_t evhndl[] = {
sdmonInsertHandler, sdmonInsertHandler,
@ -247,28 +247,28 @@ int main(void) {
/* /*
* Associating standard input, output and error to sandbox 1. * Associating standard input, output and error to sandbox 1.
*/ */
ret = vfsDrvOpenFile((vfs_driver_c *)&sb1_root_overlay_driver, ret = vfsDrvOpen((vfs_driver_c *)&sb1_root_overlay_driver,
"/dev/VSD1", 0, &fnp); "/dev/VSD1", VO_RDWR, &np);
if (CH_RET_IS_ERROR(ret)) { if (CH_RET_IS_ERROR(ret)) {
chSysHalt("VFS"); chSysHalt("VFS");
} }
sbPosixRegisterFileDescriptor(&sbx1, STDIN_FILENO, (vfs_file_node_c *)roAddRef(fnp)); sbPosixRegisterDescriptor(&sbx1, STDIN_FILENO, (vfs_node_c *)roAddRef(np));
sbPosixRegisterFileDescriptor(&sbx1, STDOUT_FILENO, (vfs_file_node_c *)roAddRef(fnp)); sbPosixRegisterDescriptor(&sbx1, STDOUT_FILENO, (vfs_node_c *)roAddRef(np));
sbPosixRegisterFileDescriptor(&sbx1, STDERR_FILENO, (vfs_file_node_c *)roAddRef(fnp)); sbPosixRegisterDescriptor(&sbx1, STDERR_FILENO, (vfs_node_c *)roAddRef(np));
vfsCloseFile(fnp); vfsClose(np);
/* /*
* Associating standard input, output and error to sandbox 1. * Associating standard input, output and error to sandbox 1.
*/ */
ret = vfsDrvOpenFile((vfs_driver_c *)&sb2_root_overlay_driver, ret = vfsDrvOpen((vfs_driver_c *)&sb2_root_overlay_driver,
"/dev/VSD1", 0, &fnp); "/dev/VSD1", VO_RDWR, &np);
if (CH_RET_IS_ERROR(ret)) { if (CH_RET_IS_ERROR(ret)) {
chSysHalt("VFS"); chSysHalt("VFS");
} }
sbPosixRegisterFileDescriptor(&sbx2, STDIN_FILENO, (vfs_file_node_c *)roAddRef(fnp)); sbPosixRegisterDescriptor(&sbx2, STDIN_FILENO, (vfs_node_c *)roAddRef(np));
sbPosixRegisterFileDescriptor(&sbx2, STDOUT_FILENO, (vfs_file_node_c *)roAddRef(fnp)); sbPosixRegisterDescriptor(&sbx2, STDOUT_FILENO, (vfs_node_c *)roAddRef(np));
sbPosixRegisterFileDescriptor(&sbx2, STDERR_FILENO, (vfs_file_node_c *)roAddRef(fnp)); sbPosixRegisterDescriptor(&sbx2, STDERR_FILENO, (vfs_node_c *)roAddRef(np));
vfsCloseFile(fnp); vfsClose(np);
/* /*
* Creating **static** boxes using MPU. * Creating **static** boxes using MPU.

View File

@ -185,10 +185,6 @@ typedef struct {
* @brief VFS nodes associated to file descriptors. * @brief VFS nodes associated to file descriptors.
*/ */
vfs_node_c *vfs_nodes[SB_CFG_FD_NUM]; vfs_node_c *vfs_nodes[SB_CFG_FD_NUM];
/**
* @brief Extra attributes added to the VFS nodes.
*/
uint8_t attributes[SB_CFG_FD_NUM];
} sb_ioblock_t; } sb_ioblock_t;
#endif #endif

View File

@ -50,14 +50,12 @@
#if (SB_CFG_ENABLE_VFS == TRUE) || defined(__DOXYGEN__) #if (SB_CFG_ENABLE_VFS == TRUE) || defined(__DOXYGEN__)
static msg_t create_descriptor(sb_ioblock_t *iop, static msg_t create_descriptor(sb_ioblock_t *iop,
vfs_node_c *np, vfs_node_c *np) {
uint8_t attributes) {
unsigned fd; unsigned fd;
for (fd = 0U; fd < SB_CFG_FD_NUM; fd++) { for (fd = 0U; fd < SB_CFG_FD_NUM; fd++) {
if (iop->vfs_nodes[fd] == NULL) { if (iop->vfs_nodes[fd] == NULL) {
iop->vfs_nodes[fd] = np; iop->vfs_nodes[fd] = np;
iop->attributes[fd] = attributes;
return (msg_t)fd; return (msg_t)fd;
} }
@ -79,7 +77,7 @@ static bool is_valid_descriptor(sb_ioblock_t *iop, int fd) {
#if (SB_CFG_ENABLE_VFS == TRUE) || defined(__DOXYGEN__) #if (SB_CFG_ENABLE_VFS == TRUE) || defined(__DOXYGEN__)
int sb_posix_open(const char *path, int flags) { int sb_posix_open(const char *path, int flags) {
sb_class_t *sbp = (sb_class_t *)chThdGetSelfX()->ctx.syscall.p; sb_class_t *sbp = (sb_class_t *)chThdGetSelfX()->ctx.syscall.p;
vfs_file_node_c *fnp = NULL; vfs_node_c *np = NULL;
msg_t ret; msg_t ret;
if (!sb_is_valid_string_range(sbp, (void *)path, VFS_CFG_PATHLEN_MAX)) { if (!sb_is_valid_string_range(sbp, (void *)path, VFS_CFG_PATHLEN_MAX)) {
@ -87,18 +85,17 @@ int sb_posix_open(const char *path, int flags) {
} }
do { do {
ret = vfsDrvOpenFile(sbp->config->vfs_driver, path, ret = vfsDrvOpen(sbp->config->vfs_driver, path, (unsigned)flags, &np);
(unsigned)flags, &fnp);
CH_BREAK_ON_ERROR(ret); CH_BREAK_ON_ERROR(ret);
ret = create_descriptor(&sbp->io, (vfs_node_c *)fnp, 0); ret = create_descriptor(&sbp->io, np);
CH_BREAK_ON_ERROR(ret); CH_BREAK_ON_ERROR(ret);
return (int)ret; return (int)ret;
} while (true); } while (true);
if (fnp != NULL) { if (np != NULL) {
vfsCloseFile(fnp); vfsClose(np);
} }
return (int)ret; return (int)ret;
@ -111,12 +108,7 @@ int sb_posix_close(int fd) {
return CH_RET_EBADF; return CH_RET_EBADF;
} }
if (sbp->io.attributes[fd] == 0) { vfsClose(sbp->io.vfs_nodes[fd]);
vfsCloseFile((vfs_file_node_c *)sbp->io.vfs_nodes[fd]);
}
else {
vfsCloseDirectory((vfs_directory_node_c *)sbp->io.vfs_nodes[fd]);
}
sbp->io.vfs_nodes[fd] = NULL; sbp->io.vfs_nodes[fd] = NULL;
return CH_RET_SUCCESS; return CH_RET_SUCCESS;
@ -133,7 +125,7 @@ ssize_t sb_posix_read(int fd, void *buf, size_t count) {
return CH_RET_EBADF; return CH_RET_EBADF;
} }
if (sbp->io.attributes[fd] != 0) { if (VFS_MODE_S_ISDIR(sbp->io.vfs_nodes[fd]->mode)) {
return CH_RET_EISDIR; return CH_RET_EISDIR;
} }
@ -151,7 +143,7 @@ ssize_t sb_posix_write(int fd, const void *buf, size_t count) {
return CH_RET_EBADF; return CH_RET_EBADF;
} }
if (sbp->io.attributes[fd] != 0) { if (VFS_MODE_S_ISDIR(sbp->io.vfs_nodes[fd]->mode)) {
return CH_RET_EISDIR; return CH_RET_EISDIR;
} }
@ -160,8 +152,6 @@ ssize_t sb_posix_write(int fd, const void *buf, size_t count) {
off_t sb_posix_lseek(int fd, off_t offset, int whence) { off_t sb_posix_lseek(int fd, off_t offset, int whence) {
sb_class_t *sbp = (sb_class_t *)chThdGetSelfX()->ctx.syscall.p; sb_class_t *sbp = (sb_class_t *)chThdGetSelfX()->ctx.syscall.p;
msg_t ret;
vfs_stat_t stat;
if ((whence != SEEK_SET) || (whence == SEEK_CUR) || (whence != SEEK_END)) { if ((whence != SEEK_SET) || (whence == SEEK_CUR) || (whence != SEEK_END)) {
return CH_RET_EINVAL; return CH_RET_EINVAL;
@ -171,14 +161,11 @@ off_t sb_posix_lseek(int fd, off_t offset, int whence) {
return CH_RET_EBADF; return CH_RET_EBADF;
} }
if (sbp->io.attributes[fd] != 0) { if (VFS_MODE_S_ISDIR(sbp->io.vfs_nodes[fd]->mode)) {
return CH_RET_EISDIR; return CH_RET_EISDIR;
} }
ret = vfsGetStat(sbp->io.vfs_nodes[fd], &stat); if (!VFS_MODE_S_ISREG(sbp->io.vfs_nodes[fd]->mode)) {
CH_RETURN_ON_ERROR(ret);
if (!VFS_MODE_S_ISREG(stat.mode)) {
return CH_RET_ESPIPE; return CH_RET_ESPIPE;
} }
@ -187,16 +174,13 @@ off_t sb_posix_lseek(int fd, off_t offset, int whence) {
whence);; whence);;
} }
void sbPosixRegisterFileDescriptor(sb_class_t *sbp, void sbPosixRegisterDescriptor(sb_class_t *sbp, int fd, vfs_node_c *np) {
int fd,
vfs_file_node_c *fnp) {
chDbgAssert((fd >= 0) && (fd < SB_CFG_FD_NUM) && chDbgAssert((fd >= 0) && (fd < SB_CFG_FD_NUM) &&
sbp->io.vfs_nodes[fd] == NULL, sbp->io.vfs_nodes[fd] == NULL,
"invalid file descriptor"); "invalid file descriptor");
sbp->io.vfs_nodes[fd] = (vfs_node_c *)fnp; sbp->io.vfs_nodes[fd] = np;
sbp->io.attributes[fd] = 0;
} }
#else /* Fallbacks for when there is no VFS.*/ #else /* Fallbacks for when there is no VFS.*/

View File

@ -65,9 +65,7 @@ extern "C" {
ssize_t sb_posix_write(int fd, const void *buf, size_t count); ssize_t sb_posix_write(int fd, const void *buf, size_t count);
off_t sb_posix_lseek(int fd, off_t offset, int whence); off_t sb_posix_lseek(int fd, off_t offset, int whence);
#if SB_CFG_ENABLE_VFS == TRUE #if SB_CFG_ENABLE_VFS == TRUE
void sbPosixRegisterFileDescriptor(sb_class_t *sbp, void sbPosixRegisterDescriptor(sb_class_t *sbp, int fd, vfs_node_c *np);
int fd,
vfs_file_node_c *fnp);
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
} }