-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspec13.c
More file actions
63 lines (48 loc) · 1.59 KB
/
spec13.c
File metadata and controls
63 lines (48 loc) · 1.59 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "header.h"
/*
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>*/
void execute_ping(char * command) {
char input[strlen(command)+1];
strcpy(input,command);
char* pid_str;
char* signal_str;
strtok(input, " "); //ping
pid_str = strtok(NULL, " ");
signal_str = strtok(NULL, " "); //string
if (pid_str == NULL || signal_str == NULL) {
printf("Invalid input format. Please provide PID and signal number.\n");
return ;
}
//printf("pid %s , signal %s\n",pid_str,signal_str);
pid_t pid;
int signal_number;
// Parse the PID and signal number to int
if (sscanf(pid_str, "%d", &pid) != 1 || sscanf(signal_str, "%d", &signal_number) !=1){
printf("Invalid PID or signal number.\n");
return ;
}
//errno is a global variable,set by system calls and library functions when an error occurs.
// Check if the process with the given PID exists
if (kill(pid, 0) == -1) {
if (errno == ESRCH) {// means "No such process."
printf("No such process found\n");
} else {
perror("kill");//some other error
}
return ;
}
// Calculate the actual signal number based on modulo 32
int actual_signal_number = signal_number % 32;
// Send the signal to the process ALL THINGS GO HERE *****
if (kill(pid, actual_signal_number) == -1) {
perror("kill");
return ;
}
printf("Sent signal %d to process with pid %d\n", actual_signal_number, pid);
return ;
}