Go language Gets the process PID of sending signal

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Background

Today, before the release of a program, the QA feedback program ran 10 minutes after the QA test was put out

Troubleshooting Procedures

Adding a log to the program, the Discovery program captures a sigterm signal, and then does some cleanup before exiting (the data sent before exiting is still required). And then you need to know exactly which process sent me the sigterm signal.

Code

Look, it seems that the go language is not directly sent to get to their own signal to send the PID of the process, need to embed a C language code, get to the PID, in order to more intuitively know that the executable program, you can read/proc/${pid}/exe this soft chain

 Packagemain/*#include <stdio.h>#include <signal.h>#include <string.h>#include <unistd.h>struct Sigaction old_action;void Handler (intSignum, siginfo_t*info, void*context) {printf("Sent by %d\ n", info->si_pid); Char path[1024x768]; Char res[1024x768]; memset (Path,' + ', sizeof (path)); memset (Res,' + ', 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 () ...}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.