我有一个特殊的Linux环境:所有文件和文件夹都是只读的,并且安装了G++。我也跑不动了sudo
。我可以在上面运行一个 C++ 程序。我想编译另一段C++代码,我写了一些这样的代码:
#include<iostream>
#include<string>
#include<cassert>
#include<unistd.h>
#include<wait.h>
#include<sys/mman.h>
constexpr char GPPPATH[]="/bin/g++";
int compile(std::string const& program){
int exefid=memfd_create("exe",0);
int subpid=fork();
assert(subpid!=-1);
if (subpid==0){
int pid=getpid();
int programfid=memfd_create("program",0);
write(programfid,program.c_str(),program.size());
std::string const programPath="/proc/"+std::to_string(pid)+"/fd/"+std::to_string(programfid);
std::string const ExePath="/proc/"+std::to_string(pid)+"/fd/"+std::to_string(exefid);
dup2(fileno(stdout),fileno(stderr));
execl(GPPPATH,GPPPATH,"-xc++",programPath.c_str(),"-o",ExePath.c_str(),NULL);
throw;
}else{
int status;
waitpid(subpid,&status,0);
if (!WIFEXITED(status)) throw;
}
return exefid;
}
int main(int argc,char** argv,char** envp){
int fid=compile("""#include<bits/stdc++.h>\nint main(){std::cout<<\"IAKIOI\";}""");
fexecve(fid,argv,envp);
}
为了保存输出文件,我使用了memfd_create
.当然,这没有用。 G++ 尝试在磁盘上创建临时文件但失败。有没有办法让g++编译C+而不需要任何磁盘写入?
我也尝试使用-pipe
,但我发现它仍然创建临时文件:
╭─ /tmp 21:12:45
╰─❯ cat tmp.cpp
#include<bits/stdc++.h>
int main(){std::cout<<114514;}
╭─ /tmp 21:12:48
╰─❯ strace g++ -pipe tmp.cpp -o tmp 2>&1 | grep "/tmp"
getcwd("/tmp", 1024) = 5
readlink("/tmp/tmp.cpp", 0x7ffc1ec38d50, 1023) = -1 EINVAL (无效的参数)
getcwd("/tmp", 1024) = 5
readlink("/tmp/tmp", 0x7ffc1ec38d50, 1023) = -1 EINVAL (无效的参数)
access("/tmp", R_OK|W_OK|X_OK) = 0
newfstatat(AT_FDCWD, "/tmp", {st_mode=S_IFDIR|S_ISVTX|0777, st_size=20504576, ...}, 0) = 0
openat(AT_FDCWD, "/tmp/ccolfqPz.o", O_RDWR|O_CREAT|O_EXCL, 0600) = 3
openat(AT_FDCWD, "/tmp/cckzgSEX.res", O_RDWR|O_CREAT|O_EXCL, 0600) = 3
newfstatat(AT_FDCWD, "/tmp/cckzgSEX.res", {st_mode=S_IFREG|0600, st_size=0, ...}, 0) = 0
unlink("/tmp/cckzgSEX.res") = 0
newfstatat(AT_FDCWD, "/tmp/ccolfqPz.o", {st_mode=S_IFREG|0600, st_size=2064, ...}, 0) = 0
unlink("/tmp/ccolfqPz.o") = 0
我的坐骑是,根据/proc/mounts
/dev/vda1 /etc/passwd ext4 ro,nosuid,relatime,errors=remount-ro 0 0
/dev/vda1 /etc/terminfo ext4 ro,nosuid,relatime,errors=remount-ro 0 0
/dev/vda1 /usr/lib/locale/locale-archive ext4 ro,nosuid,relatime,errors=remount-ro 0 0
proc /proc proc ro,nosuid,relatime 0 0
udev /dev devtmpfs ro,nosuid,relatime,size=11892204k,nr_inodes=2973051,mode=755 0 0
/dev/vda1 /nix ext4 ro,nosuid,relatime,errors=remount-ro 0 0
/dev/vda1 /tmp/exec_ir9g3uq9 ext4 ro,nosuid,relatime,errors=remount-ro 0 0
none /proc proc rw,nosuid,noexec,relatime,hidepid=invisible 0 0
none /proc/sys tmpfs ro,nosuid,relatime,size=0k 0 0
答案1
当然;只需TMPDIR
在调用gcc
(或g++
任何其他 GCC 编译器)之前设置环境变量即可。将其设置为安装上的路径tmpfs
。很可能您的/tmp
或/run
, 或/run/user/$(id -u)/
已经是这样的 tmpfs 挂载! (检查此类的输出mount
,例如mount | grep tmpfs
)
如果需要创建这样的安装座,
sudo mount -t tmpfs -o size=10M tmpfs /path/to/empty/directory