added AppRoot::chroot()

This commit is contained in:
hyung-hwan 2017-10-27 06:56:31 +00:00
parent 38102f659e
commit 25de06d195
2 changed files with 34 additions and 2 deletions

View File

@ -429,6 +429,12 @@
# define QSE_CHDIR(path) chdir(path) # define QSE_CHDIR(path) chdir(path)
#endif #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) #if defined(SYS_symlink) && defined(QSE_USE_SYSCALL)
# define QSE_SYMLINK(oldpath,newpath) syscall(SYS_symlink,oldpath,newpath) # define QSE_SYMLINK(oldpath,newpath) syscall(SYS_symlink,oldpath,newpath)
#else #else

View File

@ -26,6 +26,8 @@
#include <qse/si/AppRoot.hpp> #include <qse/si/AppRoot.hpp>
#include <qse/si/sinfo.h> #include <qse/si/sinfo.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/mbwc.h>
#include "../cmn/syscall.h" #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 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;
} }
int AppRoot::chroot (const qse_wchar_t* mpath) QSE_CPP_NOEXCEPT QSE_CLOSE (orgdirfd);
QSE_CHROOT ("/");
return 0;
}
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 void AppRoot::on_signal () QSE_CPP_NOEXCEPT