From 25de06d1954bf773b1ef1912ae60d3766e0f3c90 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 27 Oct 2017 06:56:31 +0000 Subject: [PATCH] added AppRoot::chroot() --- qse/lib/cmn/syscall.h | 6 ++++++ qse/lib/si/AppRoot.cpp | 30 ++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/qse/lib/cmn/syscall.h b/qse/lib/cmn/syscall.h index c45e3c9d..d502e0a0 100644 --- a/qse/lib/cmn/syscall.h +++ b/qse/lib/cmn/syscall.h @@ -429,6 +429,12 @@ # define QSE_CHDIR(path) chdir(path) #endif +#if defined(SYS_fchdir) && defined(QSE_USE_SYSCALL) +# define QSE_FCHDIR(handle) syscall(SYS_fchdir,handle) +#else +# define QSE_FCHDIR(handle) fchdir(handle) +#endif + #if defined(SYS_symlink) && defined(QSE_USE_SYSCALL) # define QSE_SYMLINK(oldpath,newpath) syscall(SYS_symlink,oldpath,newpath) #else diff --git a/qse/lib/si/AppRoot.cpp b/qse/lib/si/AppRoot.cpp index 9ece85c7..cb7dcea9 100644 --- a/qse/lib/si/AppRoot.cpp +++ b/qse/lib/si/AppRoot.cpp @@ -26,6 +26,8 @@ #include #include +#include +#include #include "../cmn/syscall.h" ///////////////////////////////// @@ -124,11 +126,35 @@ int AppRoot::switchUser () QSE_CPP_NOEXCEPT int AppRoot::chroot (const qse_mchar_t* mpath) QSE_CPP_NOEXCEPT { - QSE_CHROOT (mpath); + int orgdirfd; + + orgdirfd = QSE_OPEN (".", O_RDONLY, 0); + if (orgdirfd == -1) return -1; + + if (QSE_CHDIR(mpath) == -1) return -1; + if (QSE_CHROOT(mpath) == -1) + { + QSE_FCHDIR (orgdirfd); + QSE_CLOSE (orgdirfd); + return -1; + } + + QSE_CLOSE (orgdirfd); + QSE_CHROOT ("/"); + + return 0; } -int AppRoot::chroot (const qse_wchar_t* mpath) QSE_CPP_NOEXCEPT +int AppRoot::chroot (const qse_wchar_t* wpath) QSE_CPP_NOEXCEPT { + qse_mchar_t* mpath; + + mpath = qse_wcstombsdup (wpath, QSE_NULL, QSE_MMGR_GETDFL()); + if (!mpath) return -1; + + int n = AppRoot::chroot (mpath); + QSE_MMGR_FREE (QSE_MMGR_GETDFL(), mpath); + return n; } void AppRoot::on_signal () QSE_CPP_NOEXCEPT