另一个进程中执行从内存中的程序? [英] Execute a process from memory within another process?

查看:128
本文介绍了另一个进程中执行从内存中的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有通过TCP从外部服务器接收其他二进制应用程序文件,并运行他们一个小的应用程序加载器程序。

我可以通过保存所发送的文件到硬盘和使用该系统()调用来运行它这样做。不过,我想知道是否有可能推出从内存中新的应用程序,而不它曾经接触硬盘。

加载应用程序的状态加载新的应用程序后并不重要。我preFER坚持C,而C ++的解决方案,欢迎为好。我也想如果可能的话要坚持标准Linux C函数和不使用任何外部库。


解决方案

简短的回答:没有

龙答:这是可能的,但相当棘手这样做没有写出来到磁盘。理论上可以编写自己的小精灵装载机,读取二进制文件,映射了一些记忆,需要处理的动态链接,然后将控制权移交但是这是一个可怕的很多工作,这是未落将是值得的。

下一个最好的解决方案是将其写入到磁盘,并呼吁尽快断开链接。磁盘甚至不必须是真正的盘,也可以是tmpfs的或相似的。

我最近使用的另一种方法是无法通过完整的编译的二进制文件左右,但通过LLVM字节code来代替,然后可以JIT'd /间preTED /另存为契合。这也有使在异构环境中应用程序的工作的优势。

这可能是很有诱惑力的尝试 fmemopen 的fileno 的组合和 fexecve ,但是这不会,原因有两个工作:


  1. fexecve()手册页:


      

    文件描述符fd必须只读方式打开,调用者必须有权限执行,它指的是文件


    即。它需要的是一个文件一个FD。


  2. fmemopen()手册页:


      

    没有与这些函数(即的fileno(3)将如呼吁返回返回一个错误返回的文件流关联的文件描述符流)



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 this by saving the transmitted file to the hard disk and using the system() call to run it. However, I am wondering if it would be possible to launch the new application from memory without it ever touching the hard drive.

The state of the loader application does not matter after loading a new application. I prefer to stick to C, but C++ solutions are welcome as well. I would also like to stick to standard Linux C functions and not use any external libraries, if possible.

解决方案

Short answer: no.

Long answer: It's possible but rather tricky to do this without writing it out to disk. You can theoretically write your own elf loader that reads the binary, maps some memory, handles the dynamic linking as required, and then transfers control but that's an awful lot of work, that's hardly ever going to be worth the effort.

The next best solution is to write it to disk and call unlink ASAP. The disk doesn't even have to be "real" disk, it can be tmpfs or similar.

The alternative I've been using recently is to not pass complete compiled binaries around, but to pass LLVM bytecode instead, which can then be JIT'd/interpreted/saved as fit. This also has the advantage of making your application work in heterogeneous environments.

It may be tempting to try a combination of fmemopen, fileno and fexecve, but this won't work for two reasons:

  1. From fexecve() manpage:

    "The file descriptor fd must be opened read-only, and the caller must have permission to execute the file that it refers to"

    I.e. it needs to be a fd that refers to a file.

  2. From fmemopen() manpage:

    "There is no file descriptor associated with the file stream returned by these functions (i.e., fileno(3) will return an error if called on the returned stream)"

这篇关于另一个进程中执行从内存中的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆