Как вывести список активных портов и процессов, используя их в Linux, C Code - PullRequest
2 голосов
/ 01 января 2012

Я пытаюсь написать код C для выполнения той же работы, что и:

netstat -vatp

Список всех удаленных / локальных адресов и процессов, использующих их.Но я не знаю, какие файлы мне следует читать?

Я пытался просмотреть /proc/net/tcp и /proc/net/udp, но у них нет имени процесса или идентификатора процесса, как netstat отображает его!

Спасибо.

Ответы [ 2 ]

4 голосов
/ 01 января 2012

Вы можете проверить исходный код 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 очень приятно читать:)

Ваш ответ см. Как я могу сопоставить каждый / proc / net /запись tcp в каждый открытый сокет?

2 голосов
/ 01 января 2012

Вы можете вызвать приложение 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;
}
...