Răsfoiți Sursa

Added sllist library + tests, fd list environment

isundil 10 ani în urmă
părinte
comite
c4e809cbff

+ 2 - 2
CMakeLists.txt

@@ -1,7 +1,7 @@
 cmake_minimum_required(VERSION 2.8)
 
-add_library(sllist STATIC lib/sllist/src/create.c)
-add_executable(sandbox src/main.c src/mem.c src/exec.c src/param.c src/sandbox.c src/environment.c
+add_library(sllist STATIC lib/sllist/src/create.c lib/sllist/src/add.c lib/sllist/src/at.c lib/sllist/src/del.c)
+add_executable(sandbox src/main.c src/mem.c src/exec.c src/param.c src/sandbox.c src/environment.c src/pathutil.c
 	src/ovr_syscall/ovr_write.c src/ovr_syscall/ovr_open.c src/ovr_syscall/ovr_close.c)
 
 set_property(TARGET sandbox PROPERTY RUNTIME_OUTPUT_DIRECTORY bin)

+ 5 - 19
lib/sllist/include/sllist.h

@@ -1,8 +1,10 @@
 #ifndef   SL_LIST_H__
 # define  SL_LIST_H__
 
+# define SL_LIST_BUFLEN 8
+
 struct sl_list_item {
-	void *data;
+	void *data[SL_LIST_BUFLEN];
 	struct sl_list_item *next;
 };
 
@@ -29,34 +31,18 @@ unsigned int sllist_count(const sl_list * const list);
 
 /**
  * Add an item at the end of the list
- * TODO
 **/
 sl_list *sllist_pushback(sl_list *list, void *item);
 
-/**
- * Add an item at the begining of the list
- * TODO
-**/
-sl_list *sllist_pushfront(sl_list *list, void *item);
-
-
 /**
  * Remove the last item of the list and return it
- * TODO
-**/
-void *sllist_popback(sl_list *list, void *item);
-
-/**
- * Remove the first item of the list and return it
- * TODO
 **/
-void *sllist_popfront(sl_list *list, void *item);
+void *sllist_popback(sl_list *list);
 
 /**
  * Get the item at position %pos
- * TODO
 **/
-void *sllist_at(sl_list *list, unsigned int pos);
+void *sllist_at(const sl_list * const list, unsigned int pos);
 
 /**
  * Remove the item at position %pos, and return it

+ 40 - 0
lib/sllist/src/add.c

@@ -0,0 +1,40 @@
+#include <stdlib.h>
+#include "sllist.h"
+
+static inline void sllist_pushitemback(struct sl_list_item *list, void *item, unsigned char index)
+{
+	list->data[index] = item;
+}
+
+sl_list *sllist_pushback(sl_list *list, void *item)
+{
+	struct sl_list_item *i;
+	unsigned int pos = sllist_count(list);
+
+	if (pos == 0 && !list->first)
+	{
+		if (!list->first)
+		{
+			list->first = malloc(sizeof(*(list->first)));
+			list->first->next = NULL;
+		}
+		sllist_pushitemback(list->first, item, 0);
+		list->count++;
+		return list;
+	}
+	for (i = list->first; i->next && pos > SL_LIST_BUFLEN; i = i->next, pos -= SL_LIST_BUFLEN);
+	if (pos == SL_LIST_BUFLEN)
+	{
+		if (!i->next)
+		{
+			i->next = malloc(sizeof(*(i->next)));
+			i->next->next = NULL;
+		}
+		i = i->next;
+		pos -= SL_LIST_BUFLEN;
+	}
+	sllist_pushitemback(i, item, pos);
+	list->count++;
+	return list;
+}
+

+ 12 - 0
lib/sllist/src/at.c

@@ -0,0 +1,12 @@
+#include "sllist.h"
+
+void *sllist_at(const sl_list * const list, unsigned int pos)
+{
+	struct sl_list_item * i;
+
+	if (pos > sllist_count(list))
+		return 0;
+	for (i = list->first; pos > SL_LIST_BUFLEN; i = i->next, pos -= SL_LIST_BUFLEN);
+	return i->data[pos];
+}
+

+ 32 - 0
lib/sllist/src/del.c

@@ -0,0 +1,32 @@
+#include <stdlib.h>
+#include "sllist.h"
+
+static inline void free_nexts(struct sl_list_item* i)
+{
+	struct sl_list_item *tmp = i->next;
+
+	while (i->next)
+	{
+		tmp = i->next;
+		i->next = tmp->next;
+		free(tmp);
+	}
+}
+
+void *sllist_popback(sl_list *list)
+{
+	void *data;
+	struct sl_list_item *i = list->first;
+	unsigned int pos = sllist_count(list) -1;
+
+	while (pos > SL_LIST_BUFLEN)
+	{
+		i = i->next;
+		pos -= SL_LIST_BUFLEN;
+	}
+	data = i->data[pos];
+	if (pos == 0 && i->next)
+		free_nexts(i);
+	list->count--;
+	return data;
+}

+ 2 - 0
src/environment.c

@@ -62,6 +62,7 @@ int init_env(t_param *params)
 {
 	if ((params->cmdpath = check_file(params->cmd[0])) == NULL)
 		return -1;
+	params->filetable = sllist_create();
 	asprintf(&(params->tmppath), "%s/sandbox_%ld:%d", params->tmpdir, time(NULL), getpid());
 	if (mkdir(params->tmppath, 0) == -1)
 	{
@@ -74,6 +75,7 @@ int init_env(t_param *params)
 void release_env(t_param *params)
 {
 	rmdir(params->tmppath);
+	sllist_destroy(params->filetable);
 
 	free(params->tmppath);
 	free(params->cmdpath);

+ 8 - 6
src/ovr_syscall/ovr_open.c

@@ -1,20 +1,22 @@
-
+#include "sandbox.h"
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <stdlib.h>
 #include <fcntl.h>
-#include "sandbox.h"
-
-/* DEBUG HEADER */
 #include <stdio.h>
 
 int ovr_open(struct s_sandboxenv *env)
 {
-	const char *pathname = getMem(env, (size_t) env->syscall_args[0], NULL);
+	char *pathname = getMem(env, (size_t) env->syscall_args[0], NULL);
 	int flags = (int) env->syscall_args[1];
 	mode_t mode = (mode_t) env->syscall_args[2];
+	int ro = !(flags & O_RDWR || flags & O_WRONLY);
 
-	printf("DO open ! ([%s], [%d], [%d])\n", pathname, flags, mode);
+	if (*pathname != '/')
+		find_fullpath(&pathname, 1);
+	printf("DO open ! ([%s], [%d], [%d])%s\n", pathname, flags, mode, ro ? "- RO" : "- RW");
 	fflush(stdout);
+	free(pathname);
 	return 0;
 }
 

+ 17 - 0
src/pathutil.c

@@ -0,0 +1,17 @@
+#include "sandbox.h"
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+void find_fullpath(char **path, int _free)
+{
+	char *result;
+	char *cwd;
+
+	cwd = get_current_dir_name();
+	asprintf(&result, "%s/%s", cwd, *path);
+	if (_free)
+		free(*path);
+	*path = result;
+}
+

+ 9 - 0
src/sandbox.h

@@ -19,11 +19,17 @@
 #  define REGISTER_TYPE long int
 # endif
 
+typedef struct {
+	char *filename;
+	sl_list *fds;
+} t_fileinfo;
+
 typedef struct {
 	const char **cmd;
 	const char *tmpdir;
 	char *cmdpath;
 	char *tmppath;
+	sl_list *filetable;
 } t_param;
 
 struct s_sandboxenv;
@@ -61,6 +67,9 @@ void *getMem(const struct s_sandboxenv *env, size_t ptr, int *readlen);
 int init_env(t_param *);
 void release_env(t_param *);
 
+/* pathutil.c */
+void find_fullpath(char **path, int free);
+
 # include "sandbox_syscall.h"
 
 #endif /* SANDBOX_H__ */

+ 40 - 0
test/sllist/main.c

@@ -6,6 +6,45 @@ static inline int test_create()
 	sl_list *a = sllist_create();
 	_assertNotNull(a);
 	_assertEqual(sllist_count(a), 0);
+	_assertEqual(sllist_count(sllist_clear(a)), 0);
+	sllist_destroy(a);
+	return 0;
+}
+
+static inline int test_add()
+{
+	unsigned long long i;
+
+	sl_list *a = sllist_create();
+	_assertNotNull(a);
+	_assertEqual(sllist_count(a), 0);
+	sllist_pushback(a, (void *)3);
+	_assertEqual(sllist_count(a), 1);
+	_assertEqual(sllist_at(a, 0), 3);
+	sllist_pushback(a, (void *)2);
+	_assertEqual(sllist_count(a), 2);
+	_assertEqual(sllist_at(a, 0), 3);
+	_assertEqual(sllist_at(a, 1), 2);
+	_assertEqual(sllist_popback(a), 2);
+	_assertEqual(sllist_count(a), 1);
+	_assertEqual(sllist_at(a, 0), 3);
+	sllist_clear(a);
+	_assertEqual(sllist_count(a), 0);
+	for (i=0; i < 1024; ++i)
+		sllist_pushback(a, (void *)(i * 2));
+	_assertEqual(sllist_count(a), 1024);
+	_assertEqual(sllist_at(a, 0), 0);
+	_assertEqual(sllist_at(a, 1023), 2046);
+	_assertEqual(sllist_at(a, 1024), 0);
+	while (sllist_count(a))
+		sllist_popback(a);
+	for (i=0; i < 1024; ++i)
+		sllist_pushback(a, (void *)(i * 2));
+	_assertEqual(sllist_count(a), 1024);
+	_assertEqual(sllist_at(a, 0), 0);
+	_assertEqual(sllist_at(a, 1023), 2046);
+	_assertEqual(sllist_at(a, 1024), 0);
+	sllist_destroy(a);
 	return 0;
 }
 
@@ -13,6 +52,7 @@ int main()
 {
 	int success = 1;
 	success &= !test_create();
+	success &= !test_add();
 	exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
 }