Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- pid_t pid;
- _Bool timeout = 0;
- void handle_sig(int signum, siginfo_t *info, void *ucontext)
- {
- if(info->si_code == CLD_EXITED)
- {
- printf("ok\n");
- fflush(stdout);
- exit(0);
- }
- if(info->si_code == CLD_KILLED && timeout == 0)
- {
- printf("signaled\n");
- fflush(stdout);
- exit(1);
- }
- }
- int main(int argc, char *argv[])
- {
- pid = fork();
- if(pid == 0)
- {
- char **args = argv + 2;
- execvp(argv[2], args);
- }
- else
- {
- struct sigaction action_sig;
- memset(&action_sig, 0, sizeof(action_sig));
- action_sig.sa_sigaction = handle_sig;
- action_sig.sa_flags = SA_SIGINFO;
- sigaction(SIGCHLD, &action_sig, NULL);
- if(atoi(argv[1]) == 0)
- {
- kill(getpid(), SIGALRM);
- }
- sleep(atoi(argv[1]));
- timeout = 1;
- kill(pid, SIGTERM);
- waitpid(pid, 0, 0);
- printf("timeout\n");
- fflush(stdout);
- exit(2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement