I am coding a daemon in C with a http client using libcurl. I want it to be resilient to errors because the daemon will live in a risky environnement.
I added timeout control to not wait endlessly.
curl_easy_setopt(ezhandle, CURLOPT_TIMEOUT, 300); //timeout after 5 minutes (use sigalarm)
curl_easy_setopt(ezhandle, CURLOPT_CONNECTTIMEOUT, 2); //timeout after 2 seconds (use sigalarm)
curl_easy_setopt(ezhandle, CURLOPT_NOSIGNAL, 1); // Do not use signal
But when the request timeout (or other type of error) the lib is sending an abortion signal. And my daemon is killed... I got the error:
libcurl: (28) Connection timed out after 2000 milliseconds
Aborted
I tried handling the signal (with signal(6,mycallback)) but according to How to Handle SIGABRT signal? it is not possible.
According to the documentations : https://curl.se/libcurl/c/CURLOPT_TIMEOUT.html and https://curl.se/libcurl/c/CURLOPT_CONNECTTIMEOUT.html it might cause a sigalarm. I tried catching sigalarm with signal(13,myhandler) but it isn't triggered.
What would be the solution ?
PS: I believe threads might be a solution but I never used them and I would like to avoid the trouble.
This code is causing an error:
printf("%04X %04X %04X\n",LIBCURL_VERSION_MAJOR, LIBCURL_VERSION_MINOR,
LIBCURL_VERSION_PATCH );
res = curl_easy_perform(ezhandle);
if(catchError(res,ezhandle,errbuf)==-1){
printf("error \n");
fclose(downloaded_file);
//return -1; // THIS IS THE ONLY CHANGE
};
if(CURLE_OK == res){}
if(ezhandle) {
printf("ezhandle cleanup\n");
curl_easy_cleanup(ezhandle);
}
printf("Does it execute this instruction ?"); // It stops before
return 0;
}
It gives the following result:
http://192.168.174.61/token
0007 0028 0000
libcurl: (28) Operation timed out after 10000 milliseconds with 0 bytes
received
error
ezhandle cleanup
Aborted
This correct the problem:
if(catchError(res,ezhandle,errbuf)==-1){
printf("error \n");
fclose(downloaded_file);
return -1; // THIS IS THE ONLY CHANGE
};