diff --git a/drivers/char/Makefile b/drivers/char/Makefile index ba5e2f20..f20cc532 100644 --- a/drivers/char/Makefile +++ b/drivers/char/Makefile @@ -1,4 +1,4 @@ -C_source := stdio.c netio.c +C_source := stdio.c socket.c MODULE := drivers_char diff --git a/drivers/char/netio.c b/drivers/char/netio.c deleted file mode 100755 index 87d27339..00000000 --- a/drivers/char/netio.c +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright 2010 Stefan Lankes, Chair for Operating Systems, - * RWTH Aachen University - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * This file is part of MetalSVM. - */ - -#include -#include -#include -#include -#include -#include -#include - -#if defined(CONFIG_LWIP) && LWIP_SOCKET -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#endif - - -/* Implementation of a simple stdout device */ - -static ssize_t netchar_read(fildes_t* file, uint8_t* buffer, size_t size) -{ - int ret = 0; -#if defined(CONFIG_LWIP) && LWIP_SOCKET - ret = lwip_read((int)file->offset, buffer, size); - - if (ret < 0) - ret = -errno; -#endif - kprintf("return size: %i", ret); - return ret; -} - -static ssize_t netchar_write(fildes_t* file, uint8_t* buffer, size_t size) -{ - int ret = 0; -#if defined(CONFIG_LWIP) && LWIP_SOCKET - ret = lwip_write(file->offset, buffer, size); - if (ret < 0) - ret = -errno; -#endif - return ret; -} - -static int netchar_open(fildes_t* file, const char* name) -{ - return 0; -} - -static int netchar_close(fildes_t* file) -{ - int ret = 0; -#if defined(CONFIG_LWIP) && LWIP_SOCKET - //ret = lwip_close(file->offset); - if (ret < 0) - ret = -errno; -#endif - return ret; -} - -int netchar_init(vfs_node_t* node, const char* name) -{ - uint32_t i, j; - vfs_node_t* new_node; - dir_block_t* blockdir; - dirent_t* dirent; - block_list_t* blist; - - if (BUILTIN_EXPECT(!node || !name, 0)) - return -EINVAL; - - if (BUILTIN_EXPECT(node->type != FS_DIRECTORY, 0)) - return -EINVAL; - - if (finddir_fs(node, name)) - return -EINVAL; - - new_node = kmalloc(sizeof(vfs_node_t)); - if (BUILTIN_EXPECT(!new_node, 0)) - return -ENOMEM; - - memset(new_node, 0x00, sizeof(vfs_node_t)); - new_node->type = FS_CHARDEVICE; - new_node->open = &netchar_open; - new_node->close = &netchar_close; - new_node->read = &netchar_read; - new_node->write = &netchar_write; - spinlock_init(&new_node->lock); - - blist= &node->block_list; - do { - for(i=0; idata[i]) { - blockdir = (dir_block_t*) blist->data[i]; - for(j=0; jentries[j]; - if (!dirent->vfs_node) { - dirent->vfs_node = new_node; - strncpy(dirent->name, name, MAX_FNAME); - return 0; - } - } - } - } - - if (!blist->next) { - blist->next = (block_list_t*) kmalloc(sizeof(block_list_t)); - if (blist->next) - memset(blist->next, 0x00, sizeof(block_list_t)); - } - - } while(blist); - - kfree(new_node, sizeof(vfs_node_t)); - - return -ENOMEM; -} diff --git a/fs/fs.c b/fs/fs.c index 85689aa8..a56c9f9d 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -17,6 +17,7 @@ * This file is part of MetalSVM. */ +#include #include #include #include diff --git a/fs/initrd.c b/fs/initrd.c index 681a0598..09719171 100644 --- a/fs/initrd.c +++ b/fs/initrd.c @@ -440,7 +440,7 @@ int initrd_init(void) /* create the standart error-output device "stderr" */ stderr_init(tmp, "stderr"); /* create the standart device "netchar" */ - netchar_init(tmp, "netchar"); + socket_init(tmp, "socket"); /* For every module.. */ diff --git a/include/metalsvm/fs.h b/include/metalsvm/fs.h index cdec219a..a4bd5c9a 100644 --- a/include/metalsvm/fs.h +++ b/include/metalsvm/fs.h @@ -245,6 +245,7 @@ int null_init(vfs_node_t* node, const char* name); int stdin_init(vfs_node_t* node, const char* name); int stdout_init(vfs_node_t* node, const char* name); int stderr_init(vfs_node_t* node, const char* name); +int socket_init(vfs_node_t* node, const char* name); int initrd_init(void); #endif diff --git a/kernel/syscall.c b/kernel/syscall.c index bb0dc6b2..529fc7f3 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -42,29 +42,40 @@ #endif +static int get_fildes(){ + int fd; + + for (fd = 0; fd < NR_OPEN; fd++) { + if (per_core(current_task)->fildes_table[fd].node == NULL) { + return fd; + } + } + + /* can't get any free fd */ + return -EMFILE; +} + static int sys_open(const char* name, int flags, int mode) { int fd, check; fildes_t* file = NULL; - for (fd = 0; fd < NR_OPEN; fd++) { - if (per_core(current_task)->fildes_table[fd].node == NULL) { - file = &(per_core(current_task)->fildes_table[fd]); - file->offset = 0; - file->mode = mode; - file->flags = flags; - check = open_fs(file, (char*) name); - if (check < 0) { - /* file doesn't exist! */ - file->node = NULL; - return check; - } - break; - } + fd = get_fildes(); + /* validate the fd */ + if (fd < 0) + return fd; + + file = &(per_core(current_task)->fildes_table[fd]); + file->offset = 0; + file->mode = mode; + file->flags = flags; + + check = open_fs(file, (char*) name); + if (check < 0) { + /* file doesn't exist! */ + file->node = NULL; + return check; } - if (fd >= NR_OPEN) { - return -EMFILE; - } return fd; } @@ -75,19 +86,16 @@ static int sys_socket(int domain, int type, int protocol) int fd; fildes_t* file = NULL; - for (fd = 0; fd < NR_OPEN; fd++) { - if (per_core(current_task)->fildes_table[fd].node == NULL) { - file = &(per_core(current_task)->fildes_table[fd]); - file->offset = lwip_socket(domain, type, protocol); - file->mode = 0; - file->flags = 0; - file->node = findnode_fs("/dev/netchar"); - break; - } - } - if (fd >= NR_OPEN) { - return -EMFILE; - } + fd = get_fildes(); + /* validate the fd */ + if (fd < 0) + return fd; + + file = &(per_core(current_task)->fildes_table[fd]); + file->offset = lwip_socket(domain, type, protocol); + file->mode = 0; + file->flags = 0; + file->node = findnode_fs("/dev/socket"); return fd; } @@ -97,19 +105,16 @@ static int sys_accept(int s, struct sockaddr* addr, socklen_t* addrlen) int fd; fildes_t* file = NULL; - for (fd = 0; fd < NR_OPEN; fd++) { - if (per_core(current_task)->fildes_table[fd].node == NULL) { - file = &(per_core(current_task)->fildes_table[fd]); - file->offset = lwip_accept(s, addr, addrlen); - file->mode = 0; - file->flags = 0; - file->node = findnode_fs("/dev/netchar"); - break; - } - } - if (fd >= NR_OPEN) { - return -EMFILE; - } + fd = get_fildes(); + /* validate the fd */ + if (fd < 0) + return fd; + + file = &(per_core(current_task)->fildes_table[fd]); + file->offset = lwip_accept(s, addr, addrlen); + file->mode = 0; + file->flags = 0; + file->node = findnode_fs("/dev/socket"); return fd; } @@ -135,9 +140,9 @@ static int sys_lseek(int fd, off_t pos, int origin) fildes_t* file = &(per_core(current_task)->fildes_table[fd]); // Disabled for testing purposes - //if (BUILTIN_EXPECT(file->node->type != FS_FILE - // && file->node->type != FS_CHARDEVICE, 0)); - // return -EINVAL; + if (BUILTIN_EXPECT(file->node->type != FS_FILE + && file->node->type != FS_CHARDEVICE, 0)); + return -EINVAL; switch(origin) { @@ -223,7 +228,7 @@ int syscall_handler(uint32_t sys_nr, ...) int fd = va_arg(vl, int); if (fd >= 0) ret = sys_close(fd); - break; + break; } case __NR_read: { int fd = va_arg(vl, int);