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

背景

今天在发布一个程序之前,给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()
    ......
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值