SIGINT signal re-install in linux -
i writing program dealing linux signals. more specific, want re-install signal sigint in child process, find doesn't work.
here simpler version of code:
void handler(int sig){ //do exit(0); } void handler2(int sig){ //do exit(0); } int main(){ signal(sigint, handler); if ((pid = fork()) == 0) { signal(sigint, handler2); // re-install signal sigint // takes time printf("in child process:\n"); execve("foo", argv, environ); // foo executable in local dir exit(0); }else{ int status; waitpid(pid, &status, 0); // block waiting child procee exit } return 0; }
when shell printing "in child process:", press ctrl+c. find function handler
executed without problem, handler2
never executed.
could me bug in code?
update: want child process receive sigint signal during foo
running process, possible?
it not bug - calling execve has replaced running binary image. function handler2() (and other function of binary) no longer mapped in program memory having been replaced image of "foo" , therefore signal settings replaced default.
if wish signal handler active during "foo" run, have to: a. make sure handler function mapped memory of foo b. signal handler registered after "foo" starts.
one way create shared library contains signal handler , init function defined constructor registers said signal handler , force "foo" memory manipulating environment under execve foo (the environ variable) include ld_preload=/path/to/shared_library.so
Comments
Post a Comment