该坚持的时候坚持,该妥协的时候妥协,该放弃的时候放弃
分类: C/C++
2017-07-19 21:34:50
原文地址:chroot()使用 作者:CUDev
#include #include #include int main(void) { char chroot_path[] = "/tmp"; char *pwd; int ret; /*chroot 需要root权限*/ ret = chroot(chroot_path); if (ret != 0) { perror("chroot:"); exit(-1); } pwd = getcwd(NULL, 80); printf("After chroot,getcwd is [%s]\n",pwd); free(pwd); /*可以建立/tmp/test,测试一下是否可以改变目录 /tmp/test <==> /test*/ ret = chdir("/test"); if( ret < 0 ) { perror("chdir /test"); //exit(-1); } else /*由于chroot之后,当前工作目录还没有改变,所以需要再调用chdir来改变到/目录*/ if( chdir("/") < 0 ) { perror("chdir /"); exit(-1); } pwd = getcwd(NULL, 80); printf("After chdir /,getcwd is [%s]\n",pwd); free(pwd); return 0; } |
/* Don't need root privs anymore, so lets drop ownership * and chroot if requested.... */ if(chrootdir != NULL) { if(chdir(chrootdir) < 0) FatalError("Can not chdir to \"%s\"\n", chrootdir); if(chroot(chrootdir) < 0) FatalError("Can not chroot to %s\n", chrootdir); if(chdir("/") < 0) FatalError("Can not chdir to \"/\"\n"); free(chrootdir); chrootdir = NULL; /* we don't need chrootdir anymore so all * other routines should use fullpath. */ } |