Added translation layer for all things that might be different between newlib and...
authorROS Developer <ros-dev@core0.(none)>
Thu, 1 Oct 2009 00:48:11 +0000 (02:48 +0200)
committerKevin Klues <klueska@cs.berkeley.edu>
Thu, 1 Oct 2009 20:09:52 +0000 (22:09 +0200)
kern/src/syscall.c
tools/syscall_server/Makefile
tools/syscall_server/newlib_stat.h [deleted file]
tools/syscall_server/newlib_trans.c [new file with mode: 0644]
tools/syscall_server/newlib_trans.h [new file with mode: 0644]
tools/syscall_server/syscall_server.c
tools/syscall_server/syscall_server.h
tools/syscall_server/translate_state.c [deleted file]

index 46f71e4..fd44226 100644 (file)
@@ -297,7 +297,6 @@ static ssize_t sys_cputs(env_t* e, const char *DANGEROUS s, size_t len)
        // Check that the user has permission to read memory [s, s+len).
        // Destroy the environment if not.
        pte_t* p = pgdir_walk(e->env_pgdir,s,0);
-       printk("%x %x\n",*p,PTE_USER_RO);
        char *COUNT(len) _s = user_mem_assert(e, s, len, PTE_USER_RO);
 
        // Print the string supplied by the user.
index 988719d..9ee32a2 100644 (file)
@@ -1,8 +1,8 @@
 CFLAGS += -I.
 V = @
 
-SYSCALL_SERVER_OBJS = translate_state.o syscall_server.o
-SYSCALL_SERVER_HEADS = syscall_server.h
+SYSCALL_SERVER_OBJS = newlib_trans.o syscall_server.o
+SYSCALL_SERVER_HEADS = syscall_server.h newlib_trans.h
 
 all: syscall_server_pty syscall_server_pipe
        $(V)rm -rf *.o
diff --git a/tools/syscall_server/newlib_stat.h b/tools/syscall_server/newlib_stat.h
deleted file mode 100644 (file)
index 672fe3d..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-#ifndef NEWLIB_STRUCT_H
-#define NEWLIB_STRUCT_H
-
-#include <stdint.h>
-
-// For translating the stat structure
-typedef struct newlib_stat {
-       int16_t st_dev;
-       uint16_t st_ino;
-       uint32_t st_mode;
-       uint16_t st_nlink;
-       uint16_t st_uid;
-       uint16_t st_gid;
-       int16_t st_rdev;
-       int32_t st_size;
-       int32_t st_atim;
-       int32_t st_spare1;
-       int32_t st_mtim;
-       int32_t st_spare2;
-       int32_t st_ctim;
-       int32_t st_spare3;
-       int32_t st_blksize;
-       int32_t st_blocks;
-       int32_t st_spare4[2];
-} newlib_stat_t;
-
-// For translating the open flags
-#define NEWLIB_O_RDONLY    0x0000 
-#define NEWLIB_O_WRONLY    0x0001
-#define NEWLIB_O_RDWR      0x0002
-#define NEWLIB_O_APPEND    0x0008
-#define NEWLIB_O_CREAT     0x0200
-#define NEWLIB_O_TRUNC     0x0400
-#define NEWLIB_O_EXCL      0x0800
-
-// For translating the open modes
-#define NEWLIB_S_IRWXU     \
-        (NEWLIB_S_IRUSR | NEWLIB_S_IWUSR | NEWLIBS_IXUSR)
-#define     NEWLIB_S_IRUSR 0000400 /* read permission, owner */
-#define     NEWLIB_S_IWUSR 0000200 /* write permission, owner */
-#define     NEWLIB_S_IXUSR 0000100/* execute/search permission, owner */
-#define NEWLIB_S_IRWXG     \
-        (NEWLIB_S_IRGRP | NEWLIB_S_IWGRP | NEWLIB_S_IXGRP)
-#define     NEWLIB_S_IRGRP 0000040 /* read permission, group */
-#define     NEWLIB_S_IWGRP 0000020 /* write permission, grougroup */
-#define     NEWLIB_S_IXGRP 0000010/* execute/search permission, group */
-#define NEWLIB_S_IRWXO     \
-        (NEWLIB_S_IROTH | NEWLIB_S_IWOTH | NEWLIB_S_IXOTH)
-#define     NEWLIB_S_IROTH 0000004 /* read permission, other */
-#define     NEWLIB_S_IWOTH 0000002 /* write permission, other */
-#define     NEWLIB_S_IXOTH 0000001/* execute/search permission, other */
-
-// For translating lseek's whence
-# define    SEEK_SET    0
-# define    SEEK_CUR    1
-# define    SEEK_END    2
-
-#endif
diff --git a/tools/syscall_server/newlib_trans.c b/tools/syscall_server/newlib_trans.c
new file mode 100644 (file)
index 0000000..c60a97f
--- /dev/null
@@ -0,0 +1,106 @@
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <syscall_server.h>
+
+void translate_stat(struct stat* native, struct newlib_stat* newlib) {
+       newlib->st_dev = native->st_dev;
+       newlib->st_ino = native->st_ino;
+       newlib->st_mode = native->st_mode;
+       newlib->st_nlink = native->st_nlink;
+       newlib->st_uid = native->st_uid;
+       newlib->st_gid = native->st_gid;
+       newlib->st_rdev = native->st_rdev;
+       newlib->st_size = native->st_size;
+       newlib->st_atim = native->st_atim.tv_nsec;
+       newlib->st_mtim = native->st_mtim.tv_nsec;
+       newlib->st_ctim = native->st_ctim.tv_nsec;
+       newlib->st_blksize = native->st_blksize;
+       newlib->st_blocks = native->st_blocks;
+}
+int translate_flags(int newlib_flags) {
+       int native_flags = 0;
+       if (newlib_flags & NEWLIB_O_RDONLY) {
+               native_flags |= O_RDONLY;
+               newlib_flags &= ~NEWLIB_O_RDONLY;
+       }
+       if (newlib_flags & NEWLIB_O_WRONLY) {
+               native_flags |= O_WRONLY;
+               newlib_flags &= ~NEWLIB_O_WRONLY;
+       }
+       if (newlib_flags & NEWLIB_O_RDWR) {
+               native_flags |= O_RDWR;
+               newlib_flags &= ~NEWLIB_O_RDWR;
+       }
+       if (newlib_flags & NEWLIB_O_APPEND) {
+               native_flags |= O_APPEND;
+               newlib_flags &= ~NEWLIB_O_APPEND;
+       }
+       if (newlib_flags & NEWLIB_O_CREAT) {
+               native_flags |= O_CREAT;
+               newlib_flags &= ~NEWLIB_O_CREAT;
+       }
+       if (newlib_flags & NEWLIB_O_TRUNC) {
+               native_flags |= O_TRUNC;
+               newlib_flags &= ~NEWLIB_O_TRUNC;
+       }
+       if (newlib_flags & NEWLIB_O_EXCL) {
+               native_flags |= O_EXCL;
+               newlib_flags &= ~NEWLIB_O_EXCL;
+       }
+       if(newlib_flags != 0)
+               fprintf(stderr, "Warning: unsupported newlib flags passed to syscall...\n");
+       return native_flags;
+}
+int translate_mode(int newlib_mode) {
+       int native_mode = 0;
+       if (newlib_mode & NEWLIB_S_IRUSR) {
+               native_mode |= S_IRUSR;
+               newlib_mode &= ~NEWLIB_S_IRUSR;
+       }
+       if (newlib_mode & NEWLIB_S_IWUSR) {
+               native_mode |= S_IWUSR;
+               newlib_mode &= ~NEWLIB_S_IWUSR;
+       }
+       if (newlib_mode & NEWLIB_S_IXUSR) {
+               native_mode |= S_IXUSR;
+               newlib_mode &= ~NEWLIB_S_IXUSR;
+       }
+       if (newlib_mode & NEWLIB_S_IRGRP) {
+               native_mode |= S_IRGRP;
+               newlib_mode &= ~NEWLIB_S_IRGRP;
+       }
+       if (newlib_mode & NEWLIB_S_IWGRP) {
+               native_mode |= S_IWGRP;
+               newlib_mode &= ~NEWLIB_S_IWGRP;
+       }
+       if (newlib_mode & NEWLIB_S_IXGRP) {
+               native_mode |= S_IXGRP;
+               newlib_mode &= ~NEWLIB_S_IXGRP;
+       }
+       if (newlib_mode & NEWLIB_S_IROTH) {
+               native_mode |= S_IROTH;
+               newlib_mode &= ~NEWLIB_S_IROTH;
+       }
+       if (newlib_mode & NEWLIB_S_IWOTH) {
+               native_mode |= S_IWOTH;
+               newlib_mode &= ~NEWLIB_S_IWOTH;
+       }
+       if (newlib_mode & NEWLIB_S_IXOTH) {
+               native_mode |= S_IXOTH;
+               newlib_mode &= ~NEWLIB_S_IXOTH;
+       }
+       if(newlib_mode != 0)
+               fprintf(stderr, "Warning: unsupported newlib mode passed to syscall...\n");
+       return native_mode;     
+}
+int translate_whence(int newlib_whence) {
+       int native_whence;
+       if (newlib_whence == NEWLIB_SEEK_SET)
+               native_whence = SEEK_SET;
+       else if (newlib_whence == NEWLIB_SEEK_CUR)
+               native_whence = SEEK_CUR;
+       else if (newlib_whence == NEWLIB_SEEK_END)
+               native_whence = SEEK_END;
+       return native_whence;   
+}
diff --git a/tools/syscall_server/newlib_trans.h b/tools/syscall_server/newlib_trans.h
new file mode 100644 (file)
index 0000000..6fd80b6
--- /dev/null
@@ -0,0 +1,58 @@
+#ifndef NEWLIB_TRANS_H
+#define NEWLIB_TRANS_H
+
+#include <stdint.h>
+
+// For translating the stat structure
+typedef struct newlib_stat {
+       int16_t st_dev;
+       uint16_t st_ino;
+       uint32_t st_mode;
+       uint16_t st_nlink;
+       uint16_t st_uid;
+       uint16_t st_gid;
+       int16_t st_rdev;
+       int32_t st_size;
+       int32_t st_atim;
+       int32_t st_spare1;
+       int32_t st_mtim;
+       int32_t st_spare2;
+       int32_t st_ctim;
+       int32_t st_spare3;
+       int32_t st_blksize;
+       int32_t st_blocks;
+       int32_t st_spare4[2];
+} newlib_stat_t;
+
+// For translating the open flags
+#define NEWLIB_O_RDONLY    0x0000 
+#define NEWLIB_O_WRONLY    0x0001
+#define NEWLIB_O_RDWR      0x0002
+#define NEWLIB_O_APPEND    0x0008
+#define NEWLIB_O_CREAT     0x0200
+#define NEWLIB_O_TRUNC     0x0400
+#define NEWLIB_O_EXCL      0x0800
+
+// For translating the open modes
+#define NEWLIB_S_IRWXU     \
+        (NEWLIB_S_IRUSR | NEWLIB_S_IWUSR | NEWLIBS_IXUSR)
+#define     NEWLIB_S_IRUSR 0000400 /* read permission, owner */
+#define     NEWLIB_S_IWUSR 0000200 /* write permission, owner */
+#define     NEWLIB_S_IXUSR 0000100/* execute/search permission, owner */
+#define NEWLIB_S_IRWXG     \
+        (NEWLIB_S_IRGRP | NEWLIB_S_IWGRP | NEWLIB_S_IXGRP)
+#define     NEWLIB_S_IRGRP 0000040 /* read permission, group */
+#define     NEWLIB_S_IWGRP 0000020 /* write permission, grougroup */
+#define     NEWLIB_S_IXGRP 0000010/* execute/search permission, group */
+#define NEWLIB_S_IRWXO     \
+        (NEWLIB_S_IROTH | NEWLIB_S_IWOTH | NEWLIB_S_IXOTH)
+#define     NEWLIB_S_IROTH 0000004 /* read permission, other */
+#define     NEWLIB_S_IWOTH 0000002 /* write permission, other */
+#define     NEWLIB_S_IXOTH 0000001/* execute/search permission, other */
+
+// For translating lseek's whence
+# define    NEWLIB_SEEK_SET    0
+# define    NEWLIB_SEEK_CUR    1
+# define    NEWLIB_SEEK_END    2
+
+#endif //NEWLIB_TRANS_H
index d189a34..25623df 100644 (file)
@@ -222,7 +222,9 @@ void handle_open(syscall_req_t* req, syscall_rsp_t* rsp)
 {
        char* name = sandbox_file_name(req->payload, req->payload_len);
        open_subheader_t* o = &req->header.subheader.open;      
-       rsp->header.return_val = open(name, o->flags, o->mode);
+       int native_flags = translate_flags(o->flags);
+       int native_mode = translate_mode(o->mode);
+       rsp->header.return_val = open(name, native_flags, native_mode);
        free(name);
 }
 
@@ -267,7 +269,8 @@ void handle_unlink(syscall_req_t* req, syscall_rsp_t* rsp)
 void handle_lseek(syscall_req_t* req, syscall_rsp_t* rsp)
 {
        lseek_subheader_t* l = &req->header.subheader.lseek;    
-       rsp->header.return_val = lseek(l->fd, l->ptr, l->dir);
+       int native_whence = translate_whence(l->dir); 
+       rsp->header.return_val = lseek(l->fd, l->ptr, native_whence);
 }
 
 void handle_fstat(syscall_req_t* req, syscall_rsp_t* rsp)
index 0585f14..4fa88fb 100644 (file)
@@ -3,7 +3,7 @@
 
 #include <stdint.h>
 #include <sys/stat.h>
-#include <newlib_stat.h>
+#include <newlib_trans.h>
 
 #define SANDBOX_DIR "sandbox/"
 
@@ -116,9 +116,9 @@ typedef struct syscall_rsp {
 
 void run_server();
 void translate_stat(struct stat* native, struct newlib_stat* newlib);
-void translate_flags(int native, int newlib);
-void translate_mode(int native, int newlib);
-void translate_dir(int native, int newlib);
+int translate_flags(int native_flags);
+int translate_mode(int native_mode);
+int translate_whence(int native_whence);
 void translate_errno(int native, int newlib);
 void set_syscall_req_payload_len(syscall_req_t* req);
 void read_syscall_req(int fd, syscall_req_t* req);
diff --git a/tools/syscall_server/translate_state.c b/tools/syscall_server/translate_state.c
deleted file mode 100644 (file)
index 7f2129a..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-#include <sys/stat.h>
-#include <stdio.h>
-#include <newlib_stat.h>
-#include "syscall_server.h"
-
-void translate_stat(struct stat* native, struct newlib_stat* newlib) {
-       newlib->st_dev = native->st_dev;
-       newlib->st_ino = native->st_ino;
-       newlib->st_mode = native->st_mode;
-       newlib->st_nlink = native->st_nlink;
-       newlib->st_uid = native->st_uid;
-       newlib->st_gid = native->st_gid;
-       newlib->st_rdev = native->st_rdev;
-       newlib->st_size = native->st_size;
-       newlib->st_atim = native->st_atim.tv_nsec;
-       newlib->st_mtim = native->st_mtim.tv_nsec;
-       newlib->st_ctim = native->st_ctim.tv_nsec;
-       newlib->st_blksize = native->st_blksize;
-       newlib->st_blocks = native->st_blocks;
-}
-void translate_flags(int native, int newlib) {
-}
-void translate_mode(int native, int newlib) {
-}
-void translate_dir(int native, int newlib) {
-}