我试图让我的代码在 下工作chroot('/root/test1')
,但它不能正常工作。
但是当我删除chroot('/root/test1')
并修改execl("/test2", "test2", NULL)
为 时execl("/root/test1/test2", "test2", NULL)
,它会按预期工作得很好。这是为什么?
另外我想问一下,如果我把fp
redirect设置为stdin
,然后用execl
函数来运行另一个程序,子程序fp
是否会得到输入?
'/root/test1/' 中的文件:
test2
test2.cpp
test3
test3.cpp
execl 函数的返回值为 -1,errno 为 2。
测试3.cpp
int main() {
FILE *fp;
errno = 0;
fp = fopen("log.txt", "r");
dup2(fileno(fp), fileno(stdin));
cout << chdir("/root/test1") << endl;
cout << chroot("/root/test1") << endl;
DIR *dir = opendir("/");
dirent *list;
while ((list = readdir(dir)) != NULL) {
cout << list -> d_name << " ";
}
cout << endl;
closedir(dir);
errno = 0;
cout << execl("/test2", "test2", NULL) << endl;
cout << errno << endl;
cout << strerror(errno) << endl;
return 0;
}
测试2.cpp
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int a,b;
cin >> a;
scanf("%d",&b);
cout << a+b << endl;
printf("%d",a+b);
return 0;
}
日志.txt
111 222
输出*
0
0
. test3.cpp test3 .. test2 test2.cpp log.txt
-1
2
No such file or directory