isundil 9 年 前
コミット
bdc0c81837
5 ファイル変更32 行追加8 行削除
  1. 6 1
      src/ovr_syscall/ovr_close.c
  2. 6 1
      src/ovr_syscall/ovr_open.c
  3. 6 1
      src/pathutil.c
  4. 2 0
      src/sandbox.h
  5. 12 5
      test/write/main.c

+ 6 - 1
src/ovr_syscall/ovr_close.c

@@ -1,5 +1,6 @@
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <stdlib.h>
 #include <fcntl.h>
 #include "sandbox.h"
 
@@ -7,11 +8,15 @@ int ovr_close(struct s_sandboxenv *env)
 {
 	REGISTER_TYPE fd = env->syscall_args[0];
 	t_fileinfo *fi;
+	int index;
+	t_fd *tfd;
 
 	fi = get_fileinfo_fd(env, fd);
 	if (!fi)
 		return 0;
-	sllist_remove(fi->fds, sllist_cmp_int, (void *)fd);
+	index = sllist_find(fi->fds, fileinfo_compare_tfd, (void*)(REGISTER_TYPE)fd);
+	tfd = sllist_removeat(fi->fds, index);
+	free(tfd);
 	return 0;
 }
 

+ 6 - 1
src/ovr_syscall/ovr_open.c

@@ -30,6 +30,7 @@ int ovr_open(struct s_sandboxenv *env)
 	mode_t mode = (mode_t) env->syscall_args[2];
 	int ro = !(flags & (O_RDWR | O_WRONLY | O_TRUNC));
 	t_fileinfo *fileentry;
+	t_fd *fd;
 
 	if (*pathname != '/')
 		find_fullpath(&pathname, 1);
@@ -48,7 +49,11 @@ int ovr_open(struct s_sandboxenv *env)
 	if (!fileentry)
 		fileentry = open_local(env, strdup(pathname));
 
-	sllist_pushback(fileentry->fds, (void *) env->syscall_no.syscall_return);
+	fd = malloc(sizeof(*fd));
+	fd->fd = (int) env->syscall_no.syscall_return;
+	fd->flags = flags;
+	fd->mod = mode;
+	sllist_pushback(fileentry->fds, fd);
 
 	printf("DO open ! ([%s], [%d], [%d])%s = %d\n", pathname, flags, mode, ro ? "- RO" : "- RW", (int) env->syscall_no.syscall_return);
 	fflush(stdout);

+ 6 - 1
src/pathutil.c

@@ -23,9 +23,14 @@ int fileinfo_compare(const void *a, const void *b)
 	return strcmp(((t_fileinfo *)a)->filename, (char *)b) == 0;
 }
 
+int fileinfo_compare_tfd(const void *a, const void *b)
+{
+	return ((t_fd*)a)->fd == (REGISTER_TYPE) b;
+}
+
 int fileinfo_compare_fd(const void *a, const void *fd)
 {
-	return sllist_find(((t_fileinfo *)a)->fds, sllist_cmp_int, fd) != -1;
+	return sllist_find(((t_fileinfo *)a)->fds, fileinfo_compare_tfd, fd) != -1;
 }
 
 t_fileinfo *get_fileinfo(const struct s_sandboxenv *env, const char * filename)

+ 2 - 0
src/sandbox.h

@@ -25,6 +25,7 @@
 
 typedef struct fd {
 	int fd;
+	int flags;
 	int mod;
 } t_fd;
 
@@ -87,6 +88,7 @@ t_fileinfo *get_fileinfo(const struct s_sandboxenv *env, const char *filename);
 t_fileinfo *get_fileinfo_fd(const struct s_sandboxenv *env, const int fd);
 unsigned long long int hash_path(const char *filename);
 int file_exists(const char *filename);
+int fileinfo_compare_tfd(const void *a, const void *b);
 
 /* applay.c */
 void prompt_sandbox(struct s_sandboxenv *env);

+ 12 - 5
test/write/main.c

@@ -37,9 +37,10 @@ int fakeclose(int fd, struct s_sandboxenv *env)
 }
 
 
-int test_open(struct s_sandboxenv *env)
+int test_open(struct s_sandboxenv *env, int *final_fd)
 {
 	t_fileinfo *fi;
+	t_fd *fd_struct;
 
 	close(open("_test", O_CREAT, 0644));
 	_assertEqual(sllist_count(env->filetable), 0);
@@ -48,7 +49,7 @@ int test_open(struct s_sandboxenv *env)
 	_assertEqual(env->syscall_no.syscall_return, fd);
 	_assertEqual(sllist_count(env->filetable), 0);
 	close(fd);
-	fd = fakeopen("_test", O_WRONLY, 0, env);
+	fd = fakeopen("_test", O_WRONLY, 0646, env);
 	_assertDiff(fd, -1);
 	_assertEqual(env->syscall_no.syscall_return, fd);
 	_assertEqual(sllist_count(env->filetable), 1);
@@ -61,8 +62,11 @@ int test_open(struct s_sandboxenv *env)
 	_assertEqual(sllist_count(fi->fds), 2);
 	fakeclose(fd2, env);
 	_assertEqual(sllist_count(fi->fds), 1);
-	fakeclose(fd, env);
-	unlink("_test");
+	fd_struct = sllist_at(fi->fds, 0);
+	_assertNotNull(fd_struct);
+	_assertEqual(fd_struct->fd, fd);
+	_assertEqual(fd_struct->flags, O_WRONLY);
+	_assertEqual(fd_struct->mod, 0646);
 	return 0;
 }
 
@@ -75,14 +79,17 @@ int main()
 {
 	int success = 1;
 	struct s_sandboxenv env;
+	int fd;
 	t_param params;
 
 	tests_init_env(&env, &params);
 
-	success &= !test_open(&env);
+	success &= !test_open(&env, &fd);
 	success &= !test_write(&env);
 
 	tests_release_env(&env, &params);
+	fakeclose(fd, &env);
+	unlink("_test");
 	exit(success ? EXIT_SUCCESS: EXIT_FAILURE);
 }