-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping.c
More file actions
46 lines (40 loc) · 1.24 KB
/
ping.c
File metadata and controls
46 lines (40 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "headers.h"
int ping(char** argument_tokens, int no_of_arguments) {
if (no_of_arguments < 2) {
fprintf(stderr, "\033[1;31mping : Invalid Arguments\033[1;0m\n");
return 0;
} else {
int pid = atoi(argument_tokens[1]);
int sig = atoi(argument_tokens[2]);
return ping_sig(pid, sig);
}
return 1;
}
int ping_sig(int pid, int sig) {
// checking if the process exists
int result = kill(pid, 0);
if (result == 0) {
// process exists
int response = kill(pid, sig);
if (response == 0) {
printf("Sent signal %d to process with pid %d\n", sig, pid);
LL_Node trav = LL->first;
while (trav != NULL) {
int cstatus;
if (waitpid(trav->pid, &cstatus, WNOHANG) == trav->pid) {
free_node(trav);
break;
}
trav = trav->next;
}
} else {
fprintf(stderr, "\033[1;31mping : kill: could not send signal\033[1;0m\n");
return 0;
}
} else {
// process does not exist
fprintf(stderr, "\033[1;31mping : No such process exists\033[1;0m\n");
return 0;
}
return 1;
}