simplify write_file_openat.c

This commit is contained in:
zema1 2017-08-30 08:46:48 +08:00
parent 850040e21d
commit e15806026b
2 changed files with 7 additions and 13 deletions

View File

@ -109,7 +109,7 @@ class SeccompTest(base.BaseTestCase):
config["exe_path"] = self._compile_c("write_file_openat.c") config["exe_path"] = self._compile_c("write_file_openat.c")
config["output_path"] = config["error_path"] = self.output_path() config["output_path"] = config["error_path"] = self.output_path()
path = os.path.join(self.workspace, "file3.txt") path = os.path.join(self.workspace, "file3.txt")
config["args"] = [self.workspace, "file3.txt", "w"] config["args"] = [path, "w"]
result = _judger.run(**config) result = _judger.run(**config)
# without seccomp # without seccomp
self.assertEqual(result["result"], _judger.RESULT_SUCCESS) self.assertEqual(result["result"], _judger.RESULT_SUCCESS)
@ -132,7 +132,7 @@ class SeccompTest(base.BaseTestCase):
config["exe_path"] = self._compile_c("write_file_openat.c") config["exe_path"] = self._compile_c("write_file_openat.c")
config["output_path"] = config["error_path"] = self.output_path() config["output_path"] = config["error_path"] = self.output_path()
path = os.path.join(self.workspace, "file4.txt") path = os.path.join(self.workspace, "file4.txt")
config["args"] = [self.workspace, "file4.txt", "w+"] config["args"] = [path, "w+"]
result = _judger.run(**config) result = _judger.run(**config)
# without seccomp # without seccomp
self.assertEqual(result["result"], _judger.RESULT_SUCCESS) self.assertEqual(result["result"], _judger.RESULT_SUCCESS)

View File

@ -1,26 +1,20 @@
#include <stdio.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <dirent.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
DIR *dir = opendir(argv[1]);
int dir_fd = dirfd(dir);
int flags; int flags;
if (!strcmp(argv[3], "w")) { if (!strcmp(argv[2], "w")) {
flags = O_WRONLY | O_CREAT; flags = O_WRONLY | O_CREAT;
} }
else { else {
flags = O_RDWR | O_CREAT; flags = O_RDWR | O_CREAT;
} }
int fd = openat(dir_fd, argv[2], flags, 0755); int fd = openat(0, argv[1], flags, 0755);
if (fd < 0) { if (fd < 0) {
return errno; return errno;
} }
close(fd); close(fd);
return 0; return 0;
} }