Here is my main.go
package main
/*
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
// Global variable to store old action
struct sigaction old_action;
// Signal handler function
void handler(int signum, siginfo_t *info, void *context) {
printf("Received signal: %dn", signum);
printf("Sent by PID: %dn", info->si_pid);
printf("Signal code: %dn", info->si_code);
printf("User ID: %dn", info->si_uid);
if (info->si_pid != 0) {
char proc_status_path[256];
snprintf(proc_status_path, sizeof(proc_status_path), "/proc/%d/status", info->si_pid);
FILE *fp = fopen(proc_status_path, "r");
if (fp == NULL) {
perror("fopen");
return;
}
char buffer[256];
while (fgets(buffer, sizeof(buffer), fp)) {
printf("%s", buffer);
}
fclose(fp);
}
}
// Function to set up signal handling
void setup_signal_handler() {
struct sigaction action;
// Initialize the action structure
memset(&action, 0, sizeof(action));
sigfillset(&action.sa_mask); // Block all signals while the handler is executing
action.sa_sigaction = handler; // Set the signal handler function
action.sa_flags = SA_SIGINFO | SA_ONSTACK; // Set flags
// Set up the signal handler for SIGTERM
if (sigaction(SIGTERM, &action, &old_action) < 0) {
perror("sigaction");
exit(EXIT_FAILURE);
}
}
*/
import "C"
import "fmt"
func main() {
C.setup_signal_handler()
for {
fmt.Println("Sleeping..., waiting for signal")
C.sleep(10)
}
}
I am trying to use C language to help me to handler signal and print out the pid of signal sender. The original Go packages(syscall, os,… etc) does not provide such information. However, when I run my code above, there are two process starts up. One is go run .
and the other one is signal
, which is the name of my go package. When I send signal to go run .
, nothing happens while to signal
, informations pop up.
What I understand is that the implicity C package fork another process to run those C code. I cannot apply the signal handler in C to my go
process.
My goal is to print out informations of any kind of signal sent to my go
process. Related informations should include at least pid
, ppid
. Maybe there is another better solution for my situation.