Execute a process from memory within another process?

后端 未结 3 599
攒了一身酷
攒了一身酷 2020-12-30 12:44

I would like to have a small \"application loader\" program that receives other binary application files over TCP from an external server and runs them.

I could do

3条回答
  •  长情又很酷
    2020-12-30 13:32

    Much easier than doing it is C would just to set up a tmpfs file system. You'd have all the advantages of the interface of a harddisk, from your program / server / whatever you could just do an exec. These types of virtual filesystems are quite efficient nowadays, there would be really just one copy of the executable in the page cache.

    As Andy points out, for such scheme to be efficient you'd have to ensure that you don't use buffered writes to the file but that you "write" (in a broader sense) directly in place.

    • you'd have to know how large your executable will be
    • create a file on your tmpfs
    • scale it to that size with ftruncate
    • "map" that file into memory with mmap to obtain the addr of a buffer
    • pass that address directly to the recv call to write the data in place
    • munmap the file
    • call exec with the file
    • rm the file. can be done even when the executable is still running

提交回复
热议问题