Skip to content

Instantly share code, notes, and snippets.

@oz123
Created April 15, 2014 08:10
Show Gist options
  • Save oz123/10712641 to your computer and use it in GitHub Desktop.
Save oz123/10712641 to your computer and use it in GitHub Desktop.
/* Test for hanged mounts */
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
int interrupted;
int spid, tpid;
int killres;
main(int argc, char ** argv)
{
int i, j, first_arg, timeout_sec, pst, rpid;
struct stat statb;
struct timeval timeout;
struct sigaction sigact;
sigset_t sigmask;
timeout_sec = 2;
first_arg = 1;
if(argc > 2){
if(!strcmp(argv[1], "-t")){
first_arg = 3;
timeout_sec = atoi(argv[2]);
}
}
for(i = first_arg; i < argc; i++){
spid = fork();
if(spid == 0){
stat(argv[i], &statb);
exit(0);
}
tpid = fork();
if(tpid == 0){
memset(&timeout, 0, sizeof(timeout));
timeout.tv_sec = timeout_sec;
select(1, NULL, NULL, NULL, &timeout);
exit(0);
}
rpid = -1;
while(tpid != waitpid(tpid, &pst, WNOHANG)){
rpid = waitpid(spid, &pst, WNOHANG);
if(rpid == spid){
break;
}
memset(&timeout, 0, sizeof(timeout));
timeout.tv_usec = 20;
select(1, NULL, NULL, NULL, &timeout);
}
if(rpid == spid){
kill(tpid, SIGKILL);
waitpid(tpid, &pst, 0);
}
else{
kill(spid, SIGKILL);
waitpid(spid, &pst, 0);
fprintf(stdout, "%s\n", argv[i]);
}
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment