There is a simple program below that takes a HUP and continues with execv. The problem is when the execv call is executed, after that the code continues from the places where the HUP comes.I want, after execv the code must exit from main and than start again from the beginning of main.
char* a;
char** args;
void hup_func(){
printf("aaaa\n");
if(execv(a,args))
printf("bbb\n");
}
int main(int argc,char* argv[]){
signal(SIGHUP,hup_func);
args=argv;
a=args[0];
printf("%s\n",a);
while(1){
printf("test\n");
sleep(10);
}
return 0;
}
The name of the program is deneme1. The output of the code is when I sent HUP;
./deneme1
test
test
aaa
bbbb
test.
But I want it, when I sent HUP, it must start with the start of the main. like that
./deneme1
test
test
aaa
bbb
./deneme1
test
...
I want it to return at the beginning of the main after HUP, not the place where it was before
You’re not sending the correct arguments to
execv, and therefore the function is exiting with an error. So you’re simply exiting the signal handler rather than overlaying the process with a new copy of the process.The argument signature for
execvis:The second argument should be an array of
constpointers tochar, with the last element being aNULLpointer. Thechar* athat you’re passing for the second argument is not the correct type.Finally,
execvis not considered an asynchronous signal-safe function, where-as it’s sister-function,execveis. You should use the latter. The same is also true for any of theprintffamily of functions … if you need to write to the terminal, usewriteinstead.Update: I re-ran your updated code. The program is restarting properly, but the problem is that the signal mask is being inherited from the original process that was overlaid by the call to
execv. Since you are runningexecvfrom inside a signal-handler, theSIGHUPwill be blocked in the overlaid process, and you won’t be able to send any otherSIGHUPsignals to the process. To undo this, you need to unblock theSIGHUPsignal usingsigprocmask()right after you assign your signal-handler usingsignal().For instance, change your
mainto:I’ve compiled and run this on Ubuntu 12.04, and it now runs perfectly.