如何在Linux,C代码中列出使用它们的活动端口和进程

时间:2012-01-01 10:56:11

标签: c linux netstat

我正在尝试编写一个C代码来执行相同的工作:

netstat -vatp

使用它们列出所有远程/本地地址和进程。但我不知道我应该阅读哪些文件?

我尝试查看/proc/net/tcp/proc/net/udp,但他们没有netstat显示的进程名称或进程标识符!

感谢。

2 个答案:

答案 0 :(得分:4)

您可以查看源代码http://freecode.com/projects/net-tools。只需下载,解压缩bz2文件,你就会找到netstat.c源代码

快速分析:

/ proc / net / tcp例如有一个inode选项卡,在/ proc中有每个inode的子文件夹,其中包含您需要的信息。

进一步分析:

我认为情况更糟。 netstat只是循环遍历/ proc目录并检查数字子目录的内容以找到与inode匹配的实际进程。不确定,因为我只是在分析

http://linux.die.net/man/5/proc非常好读:)

要获得答案,请参阅How can i match each /proc/net/tcp entry to each opened socket?

答案 1 :(得分:2)

您可以在代码中调用netstat应用程序。看看execve来捕获stdout和stderr。

修改 因为代码不仅仅是单词:

<强> IEFTask.h

#ifndef IEFTASK_H
#define IEFTASK_H

#include <sys/wait.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <signal.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* MARK: Structure */
struct IEFTask {
    const char **arguments; /* last argument should be NULL */
    int standardInput;
    void *callbackArgument;

    void (*callback)(int term, char *out, size_t outLen, 
        char *err, size_t errLen, void *arg);
};
typedef struct IEFTask IEFTask;

/* MARK: Running */
int
IEFTaskRun(IEFTask *theTask);

#endif /* IEFTASK_H */

<强> IEFTask.c

#include "IEFTask.h"

/* MARK: DECLARATION: Data Conversion */
char *
IEFTaskCreateBufferFromPipe(int fd, size_t *bufLen);

/* MARK: Running */
int
IEFTaskRun(IEFTask *myTask) {
    pid_t pid;
    int exitStatus, status;
    int outPipe[2], errPipe[2];

    assert(myTask != NULL);

    /* Create stdout and stderr pipes */
    {
        status = pipe(outPipe);
        if(status != 0) {
            return -1;
        }

        status = pipe(errPipe);
        if(status != 0) {
            close(errPipe[0]);
            close(errPipe[1]);
            return -1;
        }
    }

    /* Fork the process and wait pid */
    {

        pid = fork();

        if(pid < 0) { /* error */
            return -1;

        } else if(pid > 0) { /* parent */
            waitpid(pid, &exitStatus, 0);
            exitStatus = WEXITSTATUS(exitStatus);

        } else { /* child */
            /* close unneeded pipes */
            close(outPipe[0]);
            close(errPipe[0]);

            /* redirect stdout, stdin, stderr */
            if(myTask->standardInput >= 0) {
                close(STDIN_FILENO);
                dup2(myTask->standardInput, STDIN_FILENO);
                close(myTask->standardInput);
            }

            close(STDOUT_FILENO);
            dup2(outPipe[1], STDOUT_FILENO);
            close(outPipe[1]);

            close(STDERR_FILENO);
            dup2(errPipe[1], STDERR_FILENO);
            close(errPipe[1]);

            execve(myTask->arguments[0], 
                (char *const *)myTask->arguments, NULL);
            exit(127);
        }
    }

    /* Parent continues */
    {
        char *output, *error;
        size_t outLen, errLen;

        /* 127 = execve failed */
        if(exitStatus == 127) {
            close(errPipe[0]);
            close(errPipe[1]);
            close(outPipe[0]);
            close(outPipe[1]);
            return -1;
        }

        /* Read in data */
        close(errPipe[1]);
        close(outPipe[1]);

        output = IEFTaskCreateBufferFromPipe(outPipe[0], &outLen);
        error = IEFTaskCreateBufferFromPipe(errPipe[0], &errLen);

        close(errPipe[0]);
        close(outPipe[0]);

        /* Call callback */
        (*myTask->callback)(exitStatus, 
            output, outLen,
            error, errLen, myTask->callbackArgument);

        if(output) free(output);
        if(error) free(error);
    }

    return 0;
}

/* MARK: Data Conversion */
#define READ_BUF_SIZE (128)
char *
IEFTaskCreateBufferFromPipe(int fd, size_t *bufLen) {
    ssize_t totalRead = 0, nowRead;
    char readBuffer[READ_BUF_SIZE], *myBuffer = NULL;
    char *ptr;

    while(1) {
        nowRead = read(fd, readBuffer, READ_BUF_SIZE);
        if(nowRead == -1) {
            free(myBuffer);
            return NULL;

        } else if(nowRead == 0) {
            break;

        } else {
            ptr = realloc(myBuffer, totalRead + nowRead);
            if(ptr == NULL) {
                free(myBuffer);
                return NULL;
            }
            myBuffer = ptr;
            memcpy(&(myBuffer[totalRead]), readBuffer, nowRead);
            totalRead += nowRead;
        }
    }

    if(bufLen) *bufLen = (size_t)totalRead;

    return myBuffer;
}

<强>的main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "IEFTask.h"


void taskCallback(int term,
    char *out, size_t outlen,
    char *err, size_t errlen)
{
    char *ptr;
    printf("Task terminated: %d\n", term);

    ptr = malloc(outlen + 1);
    memcpy(ptr, out, outlen);
    ptr[outlen] = '\0';
    printf("***STDOUT:\n%s\n***END\n", ptr);
    free(ptr);

    ptr = malloc(errlen + 1);
    memcpy(ptr, err, errlen);
    ptr[errlen] = '\0';
    printf("***STDERR:\n%s\n***END\n", ptr);
    free(ptr);
}

int main() {
    const char *arguments[] = {
        "/bin/echo",
        "Hello",
        "World",
        NULL
    };

    IEFTask myTask;
    myTask.arguments = arguments;
    myTask.standardInput = -1;
    myTask.callback = &taskCallback;

    int status;
    status = IEFTaskRun(&myTask);

    if(status != 0) {
        printf("Failed: %s\n", strerror(errno));
    }

    return 0;
}