Modified some stuff to cleanup the syscall_server build process
authorROS Developer <ros-dev@core0.(none)>
Mon, 28 Sep 2009 05:05:37 +0000 (07:05 +0200)
committerKevin Klues <klueska@cs.berkeley.edu>
Thu, 1 Oct 2009 20:09:51 +0000 (22:09 +0200)
This also involved adding a sandbox directory under the serial_server directory.  All files accessed by ROS will be relative to this sandbox.  There is currently no suport for preventing one from using ".." to break out of the sandbox.  Presumably a real server would run with itself as a user with no priveleges other than to access files in the sandbox directory and nowhere else.

scripts/mergedep.pl
tools/syscall_server/.ros_slave_pty [deleted file]
tools/syscall_server/Makefile
tools/syscall_server/pipe_init.c [new file with mode: 0644]
tools/syscall_server/pty_init.c [new file with mode: 0644]
tools/syscall_server/sandbox/sample [new file with mode: 0644]
tools/syscall_server/syscall_server.c
tools/syscall_server/syscall_server.h
tools/syscall_server/test/input [deleted file]
user/apps/parlib/file_error.c
user/apps/parlib/file_io.c

index 7accc2b..180b2b9 100644 (file)
@@ -58,7 +58,17 @@ sub readdeps {
                        #print "DEPENDENCY [[$target]]: [[$deplines]]\n";
                        my $new_deplines = $deplines;
                        if($use_filename) {
-                               my $new_deplines = " ".makedirname($filename)."/Makefrag".$deplines;
+                               my $makelocal = "Makelocal";
+                               my $sim_makelocal = "sim/Makelocal";
+                               if (! -e $makelocal) {
+                                       $makelocal = ""
+                               }
+                               if (! -e $sim_makelocal) {
+                                       $sim_makelocal = ""
+                               }
+                               $new_deplines = 
+                                     " " . $makelocal . " " . $sim_makelocal . " " 
+                                         . makedirname($filename) . "/Makefrag" . $deplines;
                        }
                        $dephash{$target} = $new_deplines;
                } elsif (/^[#]?[ \t]*$/) {
diff --git a/tools/syscall_server/.ros_slave_pty b/tools/syscall_server/.ros_slave_pty
deleted file mode 100644 (file)
index 5bc6e41..0000000
+++ /dev/null
@@ -1 +0,0 @@
-/dev/pts/2
\ No newline at end of file
index d6ad21d..988719d 100644 (file)
@@ -1,12 +1,38 @@
+CFLAGS += -I.
+V = @
 
-all: syscall_server
+SYSCALL_SERVER_OBJS = translate_state.o syscall_server.o
+SYSCALL_SERVER_HEADS = syscall_server.h
 
-translate_state.o: translate_state.c syscall_server.h
-       gcc -c -I. -o translate_state.o translate_state.c 
+all: syscall_server_pty syscall_server_pipe
+       $(V)rm -rf *.o
 
-syscall_server: translate_state.o syscall_server.h syscall_server.c
-       gcc -I. -o syscall_server translate_state.o syscall_server.c 
+.syscall_server_pipe.in: 
+       $(V)mkfifo $@
+       $(V)chmod ug+rw $@
+
+.syscall_server_pipe.out: 
+       $(V)mkfifo $@
+       $(V)chmod ug+rw $@
+
+syscall_server_pipes: .syscall_server_pipe.in .syscall_server_pipe.out 
+
+%.o: %.c $(SYSCALL_SERVER_HEADS)
+       @echo + cc [SYSCALL_SERVER] $<
+       $(V)$(CC) -c $(CFLAGS) -o $@ $<
+
+SYSCALL_SERVER_PTY_OBJS = $(SYSCALL_SERVER_OBJS) pty_init.o
+syscall_server_pty: $(SYSCALL_SERVER_PTY_OBJS)
+       @echo + cc [SYSCALL_SERVER] $@
+       $(V)$(CC) $(CFLAGS) -o $@ $(SYSCALL_SERVER_PTY_OBJS)
+        
+SYSCALL_SERVER_PIPE_OBJS = $(SYSCALL_SERVER_OBJS) pipe_init.o
+syscall_server_pipe: $(SYSCALL_SERVER_PIPE_OBJS) syscall_server_pipes
+       @echo + cc [SYSCALL_SERVER] $@
+       $(V)$(CC) $(CFLAGS) -o $@ $(SYSCALL_SERVER_PIPE_OBJS)
 
 clean:
        rm -rf *.o
-       rm -rf syscall_server
+       rm -rf syscall_server_*
+       rm -rf .syscall_server_*
+
diff --git a/tools/syscall_server/pipe_init.c b/tools/syscall_server/pipe_init.c
new file mode 100644 (file)
index 0000000..0555cfb
--- /dev/null
@@ -0,0 +1,25 @@
+#include <fcntl.h>
+#include <stdio.h>
+
+// These definitions seem backwards, but they are not.
+//  They are assigned from the persepctive of how qemu sees them
+#define SYSCALL_SERVER_PIPE_IN  ".syscall_server_pipe.out"
+#define SYSCALL_SERVER_PIPE_OUT ".syscall_server_pipe.in"
+
+int init_syscall_server(int* fd_read, int* fd_write) {
+
+       printf("Waiting for other end of pipe to connect...\n");
+       int write = open(SYSCALL_SERVER_PIPE_OUT, O_WRONLY);
+       if(write < 0)
+               return write;
+
+       int read = open(SYSCALL_SERVER_PIPE_IN, O_RDONLY);
+       if(read < 0) {
+               close(write);
+               return read;
+       }
+
+    *fd_read = read;
+       *fd_write = write;
+    return read+write;
+}
diff --git a/tools/syscall_server/pty_init.c b/tools/syscall_server/pty_init.c
new file mode 100644 (file)
index 0000000..6896774
--- /dev/null
@@ -0,0 +1,24 @@
+#define _XOPEN_SOURCE 600
+#include <stdlib.h>
+#include <fcntl.h>
+
+#define SYSCALL_SERVER_PTY ".syscall_server_pty"
+
+int init_syscall_server(int* fd_read, int* fd_write) {
+       // File descriptor of our open serial port
+       
+       int fd = posix_openpt(O_RDWR | O_NOCTTY);
+       if(fd < 0)
+               return fd;
+       grantpt (fd);
+    unlockpt (fd);
+       char* pty_dev = ptsname(fd);
+       
+       //Output the newly allocated slave device into a file
+       int pty_fd = open(SYSCALL_SERVER_PTY, 
+                         O_RDWR | O_CREAT | O_TRUNC, 
+                         S_IRUSR | S_IWUSR);
+       write(pty_fd, pty_dev, strnlen(pty_dev));
+       *fd_read = *fd_write = fd;
+       return fd;
+}
diff --git a/tools/syscall_server/sandbox/sample b/tools/syscall_server/sandbox/sample
new file mode 100644 (file)
index 0000000..602f8ee
--- /dev/null
@@ -0,0 +1 @@
+If you can see this text, then your server should be working!
index cc4f82a..d189a34 100644 (file)
@@ -5,6 +5,8 @@
 #include <stdio.h>
 #include <sys/stat.h>
 #include <malloc.h>
+#include <termios.h>
+#include <strings.h>
 #include "syscall_server.h"
 
 #define debug(...) printf(__VA_ARGS__)  
@@ -21,23 +23,13 @@ void run_server()
        // Struct for reading the syscall over the serial port
        syscall_req_t syscall_req;
        syscall_rsp_t syscall_rsp;
-
-       // File descriptor of our open serial port
-       int fd = posix_openpt(O_RDWR | O_NOCTTY);
-       grantpt (fd);
-    unlockpt (fd);
-       char* slave_dev = ptsname(fd);
-       printf("Master PTY fd: %d\n", fd);
-       printf("Slave  TTY device name: %s\n", slave_dev);
-       printf("\nStarting server.....\n");
-       fflush(stdout);
        
-       //Output the newly allocated slave device into a file
-       int slave_fd = open(ROS_SLAVE_PTY, 
-                      O_RDWR | O_CREAT | O_TRUNC, 
-                      S_IRUSR | S_IWUSR);
-       write(slave_fd, slave_dev, strnlen(slave_dev));
+       int fd_read, fd_write;
+       int ret = init_syscall_server(&fd_read, &fd_write);
+       if(ret < 0)
+               error(ret, "Could not open file desciptors for communication\n");
 
+       printf("Server started....");
        // Continuously read in data from the serial port socket
        while(1) {
                syscall_req.payload_len = 0;
@@ -46,11 +38,13 @@ void run_server()
                syscall_rsp.payload = NULL;
        
                debug("\nWaiting for syscall...\n");
-               read_syscall_req(fd, &syscall_req);     
+               read_syscall_req(fd_read, &syscall_req);        
+
                debug("Processing syscall: %d\n", syscall_req.header.id);
                handle_syscall(&syscall_req, &syscall_rsp);
+
                debug("Writing response: %d\n", syscall_req.header.id);
-               write_syscall_rsp(fd, &syscall_rsp);
+               write_syscall_rsp(fd_write, &syscall_rsp);
 
                if(syscall_req.payload != NULL)
                        free(syscall_req.payload);
@@ -62,10 +56,8 @@ void run_server()
 void read_syscall_req(int fd, syscall_req_t* req) 
 {
        read_syscall_req_header(fd, req);
-       debug("Read Header for :%d...\n", req->header.id);
        set_syscall_req_payload_len(req);
        read_syscall_req_payload(fd, req);
-       debug("Read Payload...\n");
 }
 
 void set_syscall_req_payload_len(syscall_req_t* req)
@@ -100,14 +92,12 @@ void read_syscall_req_header(int fd, syscall_req_t* req)
        // If no data, or the ID we got is bad, terminate process.
        uint32_t id = req->header.id;
        if ((bytes_read < 0) || (id < 0) || (id > NUM_SYSCALLS)) {
-               printf("Bytes Read: %d\n", bytes_read);
                perror("Problems reading the id from the serial port...");
        }
 
        // Otherwise, start grabbing the rest of the data
        bytes_read = read_from_serial(fd, &req->header.subheader, 
                                   sizeof(req->header.subheader) , 0);
-
        if(bytes_read < 0)
                error(fd, "Problems reading header from the serial port...");
 }
@@ -149,9 +139,9 @@ int read_from_serial(int fd, void* buf, int len, int peek)
 // Send CONNECTION_TERMINATED over the FD (if possible)
 void error(int fd, const char* s)
 {
-       printf("Error: Spawned Process, FD: %i\n",fd);
+       fprintf(stderr, "Error: Spawned Process, FD: %i\n",fd);
        perror(s);
-    printf("Sending CONNECTION_TERMINATED.... \n");
+    fprintf(stderr, "Sending CONNECTION_TERMINATED.... \n");
        close(fd);
        exit(-1);
 }
@@ -221,17 +211,19 @@ void write_syscall_rsp_payload(int fd, syscall_rsp_t* rsp)
                error(fd, "Problems writing all bytes in the response payload...");     
 }
 
+char* sandbox_file_name(char* name, uint32_t len) {
+       char* new_name = malloc(len + strnlen(SANDBOX_DIR));
+       sprintf(new_name, "%s%s", SANDBOX_DIR, name);
+       printf("%s\n", new_name);
+       return new_name;
+}
+
 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;      
-       debug("Flags: %d\n", o->flags);
-       debug("Newlib Flags: %d\n", O_RDWR | O_CREAT | O_TRUNC);
-       debug("Mode: %d\n", o->mode);
-       debug("Newlib Mode: %d\n", 
-           S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
-       rsp->header.return_val = open(req->payload, o->flags, o->mode);
-       debug("Return Value: %d\n", rsp->header.return_val);
-       debug("Payload: %s\n", (char*)req->payload);
+       rsp->header.return_val = open(name, o->flags, o->mode);
+       free(name);
 }
 
 void handle_close(syscall_req_t* req, syscall_rsp_t* rsp)
@@ -244,8 +236,6 @@ void handle_read(syscall_req_t* req, syscall_rsp_t* rsp)
 {
        read_subheader_t* r = &req->header.subheader.read;      
        rsp->payload = malloc(r->len);
-       debug("File descriptor: %d\n", r->fd);
-       debug("Length to read: %d\n", r->len);
        rsp->header.return_val = read(r->fd, rsp->payload, r->len);
        if(rsp->header.return_val >= 0)
                rsp->payload_len = rsp->header.return_val;
@@ -260,13 +250,18 @@ void handle_write(syscall_req_t* req, syscall_rsp_t* rsp)
 void handle_link(syscall_req_t* req, syscall_rsp_t* rsp)
 {
        link_subheader_t* l = &req->header.subheader.link;      
-       rsp->header.return_val = link(req->payload, 
-                                  req->payload + l->old_len);
+       char* old_name = sandbox_file_name(req->payload, l->old_len);
+       char* new_name = sandbox_file_name(req->payload + l->old_len, l->new_len);
+       rsp->header.return_val = link(old_name, new_name); 
+       free(old_name);
+       free(new_name);
 }
 
 void handle_unlink(syscall_req_t* req, syscall_rsp_t* rsp)
 {
-       rsp->header.return_val = unlink(req->payload); 
+       char* name = sandbox_file_name(req->payload, req->payload_len);
+       rsp->header.return_val = unlink(name); 
+       free(name);
 }
 
 void handle_lseek(syscall_req_t* req, syscall_rsp_t* rsp)
index 2427436..0585f14 100644 (file)
@@ -2,9 +2,10 @@
 #define SERIAL_SERVER_H
 
 #include <stdint.h>
+#include <sys/stat.h>
 #include <newlib_stat.h>
 
-#define ROS_SLAVE_PTY ".ros_slave_pty"
+#define SANDBOX_DIR "sandbox/"
 
 #define OPEN_ID                        0
 #define CLOSE_ID               1
@@ -128,6 +129,7 @@ void write_syscall_rsp_header(int fd, syscall_rsp_t* rsp);
 void write_syscall_rsp_payload(int fd, syscall_rsp_t* rsp);
 int read_from_serial(int fd, void* buf, int len, int peek); 
 void error(int fd, const char* s);
+char* sandbox_file_name(char* name, uint32_t len);
 
 void handle_syscall(syscall_req_t* req, syscall_rsp_t* rsp);
 void handle_open(syscall_req_t* req, syscall_rsp_t* rsp);
diff --git a/tools/syscall_server/test/input b/tools/syscall_server/test/input
deleted file mode 100644 (file)
index fb1bcb5..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Shit for brains
index 90df8ec..a0bb30f 100644 (file)
@@ -20,7 +20,7 @@ void file_error()
        printf("Starting error testing....\n\n");
 
        errno = 0;
-       int bad_fd = open("./test/DNE", O_RDWR, 0);
+       int bad_fd = open("DNE", O_RDWR, 0);
        printf("Opened:       DNE\n");
         printf("FD:           %d\n", bad_fd);
         printf("ERRNO:        %s\n", strerror(errno));
index 63b3486..b6a2e19 100644 (file)
@@ -13,8 +13,8 @@ extern char * readline(const char *prompt);
 void file_io()
 {      
        printf("Beginning Ethernet Based File IO Test...\n\n");
-       int in_fd = open("./test/input", O_RDWR, 0);
-       printf("Opened:       input\n");
+       int in_fd = open("sample", O_RDWR, 0);
+       printf("Opened:       sample\n");
        printf("FD:           %d\n", in_fd);
        char buf[IN_BUF_SIZE];
        int read_amt = read(in_fd, buf, IN_BUF_SIZE - 1);
@@ -48,7 +48,7 @@ void file_io()
 
        unsigned int ofp_len = strlen(file_name) + 8;
        char * output_full_path = malloc(ofp_len);
-       snprintf(output_full_path, ofp_len, "./test/%s", file_name);
+       snprintf(output_full_path, ofp_len, "%s", file_name);
 
        int out_fd = open(output_full_path, O_RDWR | O_CREAT | O_TRUNC,
                       S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);