Smarter exec error handling

This commit is contained in:
Bandie 2018-03-18 14:37:29 +01:00
parent f37ec9e797
commit 731e893580
Signed by: Bandie
GPG Key ID: C1E133BC65A822DD
1 changed files with 21 additions and 20 deletions

View File

@ -36,6 +36,15 @@ void help(){
"}\n\n", PROGRAM, VRFFILE, VRFFILE); "}\n\n", PROGRAM, VRFFILE, VRFFILE);
} }
void exec_failed(){
fprintf(stderr, "ERROR: It is not possible to execute %s: ", PROGRAM);
if(access(PROGRAM, F_OK)) fprintf(stderr, "It does not exist (in your PATH variable).\n");
else fprintf(stderr, "Access denied. (Are you root?)\n");
if(access(VRFFILE, F_OK) != -1)
unlink(VRFFILE);
exit(EXIT_FAILURE);
}
void sig_handler(int signo){ void sig_handler(int signo){
if(signo == SIGTERM || signo == SIGINT){ if(signo == SIGTERM || signo == SIGINT){
// Unverify // Unverify
@ -69,31 +78,23 @@ int main(int argc, char *argv[]){
if(fork() == 0){ if(fork() == 0){
// Check if program is even executable // Exec program silently by default
if(!access(PROGRAM, F_OK) && !access(PROGRAM, R_OK) && !access(PROGRAM, X_OK)){ if(!verbose){
int fd = open("/dev/null", O_WRONLY | O_CREAT, 0666);
// Exec program silently by default dup2(fd, 1);
if(!verbose){ dup2(fd, 2);
int fd = open("/dev/null", O_WRONLY | O_CREAT, 0666); if(execlp(PROGRAM, PROGRAM, NULL) < 0){
exec_failed();
dup2(fd, 1);
dup2(fd, 2);
execlp(PROGRAM, PROGRAM, NULL);
close(fd);
} }
else close(fd);
execlp(PROGRAM, PROGRAM, NULL);
} }
else{ else{
if(execlp(PROGRAM, PROGRAM, NULL) < 0){
fprintf(stderr, "ERROR: It is not possible to execute %s: ", PROGRAM); exec_failed();
if(access(PROGRAM, F_OK)) fprintf(stderr, "It does not exist.\n"); }
else fprintf(stderr, "Access denied. (Are you root?)\n");
return 1;
} }
} }
else { else {
if(verbose) if(verbose)