리눅스에서 새로 생성 된 프로세스를 추적하는 방법?


31

나는 ps시스템에서 실행중인 현재 프로세스의 목록이나 트리를 볼 수 있음을 알고 있습니다. 그러나 내가 달성하고자하는 것은 컴퓨터를 사용할 때 만들어지는 새로운 프로세스를 "따르는 것"입니다.

비유로, tail -f파일 또는 입력에 추가 된 새 내용을 따르는 데 사용할 때 현재 작성중인 프로세스의 팔로우 목록을 유지하려고합니다.

이것도 가능합니까?

답변:


28

커널에서 kprobes가 활성화 된 경우 perf-toolsexecsnoop 에서 사용할 수 있습니다 .

첫 번째 터미널에서 :

% while true; do uptime; sleep 1; done

다른 터미널에서 :

% git clone https://github.com/brendangregg/perf-tools.git
% cd perf-tools
% sudo ./execsnoop
Tracing exec()s. Ctrl-C to end.
Instrumenting sys_execve
   PID   PPID ARGS
 83939  83937 cat -v trace_pipe
 83938  83934 gawk -v o=1 -v opt_name=0 -v name= -v opt_duration=0 [...]
 83940  76640 uptime
 83941  76640 sleep 1
 83942  76640 uptime
 83943  76640 sleep 1
 83944  76640 uptime
 83945  76640 sleep 1
^C
Ending tracing...

2
x84_64의 새 커널 버전 (> 올바르게 이해하면 4.17 이상)의 경우 Gregg의 perf-tools가 더 이상 작동하지 않습니다. 실행되지만 사용하지 않는 호출을 계측하므로 전혀 보고서가 없습니다. 다른 곳에서 Gregg의 의견에 따르면 커널 = 4.7에 대한 올바른 솔루션은 github.com/iovisor/bcc#tools 및 Ubuntu 및 최신 Linux에서 사용할 수있는 BPF 컴파일러 모음의 BPF 구현을 사용하는 것 bpfcc-tools입니다.
거스

7

가장 쉬운 방법은 시스템 호출 감사를 활성화하는 것입니다

자세한 내용은 다음 링크를 참조하십시오.

누구나 루트 프로세스 생성을 모니터링하는 간단한 방법을 알고 있습니까? 서버 결함

모든 프로세스를 모니터링하는 경우 -F uid=0부품을 제거하십시오.

로그는 /var/log/audit/audit.log


이 3 개의 링크 중 어느 것도 내 질문에 대답하지 않습니다. 처음 두 가지는 이것을 해결하기 위해 무언가를 코딩하는 것에 관한 것이고 마지막 것은 대답하지 않습니다. 내가 묻는 것은 명령에 관한 것이며 코드를 작성하지 않는 것입니다.
Pablo Matias Gomez

@ PabloMatíasGomez 업데이트
데이지

3

CONFIG_PROC_EVENTS=y

샘플 세션 :

$ su
# ./proc_events.out &
set mcast listen ok
# sleep 2 & sleep 1 &
fork: parent tid=48 pid=48 -> child tid=56 pid=56
fork: parent tid=48 pid=48 -> child tid=57 pid=57
exec: tid=57 pid=57
exec: tid=56 pid=56
exit: tid=57 pid=57 exit_code=0
exit: tid=56 pid=56 exit_code=0

CONFIG_PROC_EVENTSnetlink 소켓을 통해 이벤트를 userland에 노출합니다 .

proc_events.c

#define _XOPEN_SOURCE 700
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/connector.h>
#include <linux/cn_proc.h>
#include <signal.h>
#include <errno.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

static volatile bool need_exit = false;

static int nl_connect()
{
    int rc;
    int nl_sock;
    struct sockaddr_nl sa_nl;

    nl_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
    if (nl_sock == -1) {
        perror("socket");
        return -1;
    }
    sa_nl.nl_family = AF_NETLINK;
    sa_nl.nl_groups = CN_IDX_PROC;
    sa_nl.nl_pid = getpid();
    rc = bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl));
    if (rc == -1) {
        perror("bind");
        close(nl_sock);
        return -1;
    }
    return nl_sock;
}

static int set_proc_ev_listen(int nl_sock, bool enable)
{
    int rc;
    struct __attribute__ ((aligned(NLMSG_ALIGNTO))) {
        struct nlmsghdr nl_hdr;
        struct __attribute__ ((__packed__)) {
            struct cn_msg cn_msg;
            enum proc_cn_mcast_op cn_mcast;
        };
    } nlcn_msg;

    memset(&nlcn_msg, 0, sizeof(nlcn_msg));
    nlcn_msg.nl_hdr.nlmsg_len = sizeof(nlcn_msg);
    nlcn_msg.nl_hdr.nlmsg_pid = getpid();
    nlcn_msg.nl_hdr.nlmsg_type = NLMSG_DONE;

    nlcn_msg.cn_msg.id.idx = CN_IDX_PROC;
    nlcn_msg.cn_msg.id.val = CN_VAL_PROC;
    nlcn_msg.cn_msg.len = sizeof(enum proc_cn_mcast_op);

    nlcn_msg.cn_mcast = enable ? PROC_CN_MCAST_LISTEN : PROC_CN_MCAST_IGNORE;

    rc = send(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0);
    if (rc == -1) {
        perror("netlink send");
        return -1;
    }

    return 0;
}

static int handle_proc_ev(int nl_sock)
{
    int rc;
    struct __attribute__ ((aligned(NLMSG_ALIGNTO))) {
        struct nlmsghdr nl_hdr;
        struct __attribute__ ((__packed__)) {
            struct cn_msg cn_msg;
            struct proc_event proc_ev;
        };
    } nlcn_msg;
    while (!need_exit) {
        rc = recv(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0);
        if (rc == 0) {
            /* shutdown? */
            return 0;
        } else if (rc == -1) {
            if (errno == EINTR) continue;
            perror("netlink recv");
            return -1;
        }
        switch (nlcn_msg.proc_ev.what) {
            case PROC_EVENT_NONE:
                printf("set mcast listen ok\n");
                break;
            case PROC_EVENT_FORK:
                printf("fork: parent tid=%d pid=%d -> child tid=%d pid=%d\n",
                        nlcn_msg.proc_ev.event_data.fork.parent_pid,
                        nlcn_msg.proc_ev.event_data.fork.parent_tgid,
                        nlcn_msg.proc_ev.event_data.fork.child_pid,
                        nlcn_msg.proc_ev.event_data.fork.child_tgid);
                break;
            case PROC_EVENT_EXEC:
                printf("exec: tid=%d pid=%d\n",
                        nlcn_msg.proc_ev.event_data.exec.process_pid,
                        nlcn_msg.proc_ev.event_data.exec.process_tgid);
                break;
            case PROC_EVENT_UID:
                printf("uid change: tid=%d pid=%d from %d to %d\n",
                        nlcn_msg.proc_ev.event_data.id.process_pid,
                        nlcn_msg.proc_ev.event_data.id.process_tgid,
                        nlcn_msg.proc_ev.event_data.id.r.ruid,
                        nlcn_msg.proc_ev.event_data.id.e.euid);
                break;
            case PROC_EVENT_GID:
                printf("gid change: tid=%d pid=%d from %d to %d\n",
                        nlcn_msg.proc_ev.event_data.id.process_pid,
                        nlcn_msg.proc_ev.event_data.id.process_tgid,
                        nlcn_msg.proc_ev.event_data.id.r.rgid,
                        nlcn_msg.proc_ev.event_data.id.e.egid);
                break;
            case PROC_EVENT_EXIT:
                printf("exit: tid=%d pid=%d exit_code=%d\n",
                        nlcn_msg.proc_ev.event_data.exit.process_pid,
                        nlcn_msg.proc_ev.event_data.exit.process_tgid,
                        nlcn_msg.proc_ev.event_data.exit.exit_code);
                break;
            default:
                printf("unhandled proc event\n");
                break;
        }
    }

    return 0;
}

static void on_sigint(__attribute__ ((unused)) int unused)
{
    need_exit = true;
}

int main()
{
    int nl_sock;
    int rc = EXIT_SUCCESS;

    signal(SIGINT, &on_sigint);
    siginterrupt(SIGINT, true);
    nl_sock = nl_connect();
    if (nl_sock == -1)
        exit(EXIT_FAILURE);
    rc = set_proc_ev_listen(nl_sock, true);
    if (rc == -1) {
        rc = EXIT_FAILURE;
        goto out;
    }
    rc = handle_proc_ev(nl_sock);
    if (rc == -1) {
        rc = EXIT_FAILURE;
        goto out;
    }
    set_proc_ev_listen(nl_sock, false);
out:
    close(nl_sock);
    exit(rc);
}

https://bewareofgeek.livejournal.com/2945.html : GitHub upsatream

때문에 당신이 같은 UID 등의 공정 데이터 및 프로세스 인수를 얻을 수 있지만 생각하지 않는다 exec_proc_event그래서 약간의 데이터가 포함 https://github.com/torvalds/linux/blob/v4.16/include/uapi/linux/cn_proc .h # L80 에서 즉시 읽을 수는 /proc있지만 프로세스가 완료되고 다른 프로세스가 PID를 취할 위험이 있으므로 신뢰할 수 없습니다.

우분투 17.10에서 테스트 CONFIG_PROC_EVENTS=y되었으며 기본적으로 활성화되어 있습니다.


2

를 사용하여 프로세스를 따라갈 수 있습니다 strace. 프로세스의 PID를 알고 있다면 다음을 수행 할 수 있습니다.

strace -o strace-<pid>.out -f -p <pid>

통지 -f스위치를. 위 명령에서 PID가 사용 된 프로세스의 후손 인 새로 작성된 프로세스를 따르는 데 도움이됩니다. strace에 대한 자세한 내용은 질문을 참조하십시오 .


분명히 pid = 1 인 init 프로세스에 첨부하려고 했습니까? 불행히도 작동하지 않습니다. 출력에서 ​​새로운 프로세스 생성을 보지 못하고 라인 수는 수십 개이며, 새로운 프로세스에 대한 현재 pid는 수백을 통과했습니다.
Hi-Angel

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.