go语言获取发送信号的进程pid

来源:互联网 发布:网络大v孙海英 编辑:程序博客网 时间:2024/05/17 01:39

背景

今天在发布一个程序之前,给qa提测的时候,qa反馈程序运行10几分钟之后,退出了

排查过程

在程序中加日志,发现程序捕获到了一个SIGTERM信号,然后做了一些退出前的清理工作(在退出之前,该发送的数据还是需要发送的)。然后就需要知道到底是那个进程向我发送SIGTERM信号

代码

查了一下,貌似go语言没有直接的发送获取向自己发送信号的进程的pid,需要嵌入一段c语言代码,获取到pid之后,为了更直观的知道是那个可执行程序,可以去读取/proc/${pid}/exe这个软链

package main/*#include <stdio.h>#include <signal.h>#include <string.h>#include <unistd.h>struct sigaction old_action;void handler(int signum, siginfo_t *info, void *context) {    printf("Sent by %d\n", info->si_pid);    char path[1024];    char res[1024];    memset(path, '\0', sizeof(path));    memset(res, '\0', sizeof(res));    snprintf(path, sizeof(path), "/proc/%d/exe", info->si_pid);    if (-1 == readlink(path, res, sizeof(res))) {        printf("fail to get the symblic link of %s\n", path);    } else {        printf("the symblic link of %s is: %s\n", path, res);    }}void test() {    struct sigaction action;    sigaction(SIGTERM, NULL, &action);    memset(&action, 0, sizeof action);    sigfillset(&action.sa_mask);    action.sa_sigaction = handler;    action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO | SA_ONSTACK;    sigaction(SIGTERM, &action, &old_action);}*/import "C"............func main() {    ......    C.test()    ......}
0 0
原创粉丝点击