我一开始就尝试从一个进程派生另一个进程。为此,我尝试修改glibc (我使用的修改后的glibc )中的__libc_start_main函数,并尝试将fork放在那里,但无法编译glibc,因为每当我尝试这样做时,它都会给出一个错误。还有什么其他选择?为什么在__libc_start_main中插入fork不起作用?
再次注意,我希望以一种不需要修改程序的方式来完成,也就是说,在glibc中修改是可以的,但不是程序。
在__libc_start_main中,我尝试这样派生。
if (__builtin_expect (! not_first_call, 1))
{
struct pthread *self;
fork(); // <-- here
self = THREAD_SELF;
/* Store old info. */
unwind_buf.priv.data.prev = THREAD_GETMEM (self, cleanup_jmp_buf);
unwind_buf.priv.data.cleanup = THREAD_GETMEM (self, cleanup);
/* Store the new cleanup handler info. */
THREAD_SETMEM (self, cleanup_jmp_buf, &unwind_buf);
/* Run the program. */
result = main (argc, argv, __environ MAIN_AUXVEC_PARAM);
}我得到的错误如下所示。
file '/build/sunrpc/xbootparam_prot.T' already exists and may be overwritten
make[2]: *** [build/sunrpc/xbootparam_prot.stmp] Error 1发布于 2012-05-24 10:43:36
如果您使用主入口点静态链接到一个不可修改的对象,则可以使用符号换行将fork()放在对象的main()之前。
例如,不能修改的main.o:
#include <stdio.h>
int main( int argc, char *argv[] ) {
printf( "In main()\n" );
return 0;
}glibc中的包装器符号:
#include <unistd.h>
#include <stdio.h>
int __wrap_main( int argc, char *argv[] ) {
printf( "In wrapper\n" );
if ( fork() ) {
return __real_main( argc, argv );
} else {
printf( "Other process did something else\n" );
return 0;
}
}使用--wrap命令链接器命令:
gcc -o app main.o wrap.o -Wl,--wrap=main
$ ./app
In wrapper
In main()
$ Other process did something elsehttps://stackoverflow.com/questions/10659281
复制相似问题