【问题标题】:how to test the functionality of SOCK_CLOEXEC/ O_CLOEXEC (close on execution)如何测试 SOCK_CLOEXEC/ O_CLOEXEC 的功能(执行时关闭)
【发布时间】:2023-04-10 11:36:02
【问题描述】:

我有一个已接受的文件描述符来设置 SOCK_CLOEXEC。谁能告诉我如何测试已设置 O_CLOEXEC/SOCK_CLOEXEC 的 FD(接受的套接字不是任何 fd)的功能

accepted_fd =   accept4(lsn_fd, (struct sockaddr *)& tcp_remote, &size,  SOCK_CLOEXEC);

请通过任何方法,任何测试它的代码提供给我,除非通过测试 FD_CLOEXE 是否被返回

fdf = fcntl(accept_fd, F_GETFD);

分叉代码:

  pid_t childpid;
    childpid = fork();
    if (childpid >= 0) /* fork succeeded */
      {
        if (childpid == 0) /* fork() returns 0 to the child process */
          {
            rc = recv_gd_flow2(0, accepted_fd, count+1); /* wrapper function calls recv() */
            CHECK_VALUE("recv_gd_flow, with 0", rc, 0, goto cleanup);

            execl("/bin/ls", "ls", NULL); //EXEC ls

            //CHILD
            if(accepted_fd != INVALID_SOCKET)
              printf("I am Child and sock is closed\n");
            else
              printf("I am Child and sock is opend\n");
          }
        else{

          if(accepted_fd != INVALID_SOCKET)
            printf("I am Parent and sock is closed\n");
          else
            printf("I am Parent and sock is opend\n");
          //

        }
      }
     // The purpose of this code to see if child has a privileged to get a leaked fd, i think he shouldn't  

【问题讨论】:

  • fcntl 有什么问题?
  • 只有当这个套接字设置标志时才会得到我想测试标志它如何与接受的套接字一起工作,我尝试了 fork()、exec() 但我不知道如何和他们一起测试,或者我做错了什么..请帮助:(
  • 你觉得有什么办法吗??
  • 能否包含您用于fork() 和exec() 测试的代码?
  • pid_t childpid; childpid = fork(); if (childpid >= 0) /* fork 成功 / { if (childpid == 0) / fork() 返回 0 给子进程 */ { rc = recv_gd_flow2(0, sock, count+ 1); CHECK_VALUE("recv_gd_flow, with 0", rc, 0, goto cleanup); //CHILD if(sock != INVALID_SOCKET) printf("I am Child and sock is closed\n"); else printf("我是孩子,袜子打开了\n"); }

标签: c linux sockets unix network-programming


【解决方案1】:

更新

我注意到您在代码中添加了对execl() 的调用,并在执行后 测试套接字描述符。这是行不通的。来自execl man-page:

RETURN VALUE
       The  exec() functions return only if an error has occurred.  The return
       value is -1, and errno is set to indicate the error.

成功执行后的代码将永远不会执行。必须执行测试的是执行过程。

/更新

它被称为close-on-exec。在fork() 之后,您需要在子进程中执行某些操作。仅分叉不足以测试此标志。

这是一个示例(错误处理最少,并且有一些快捷方式):

accept4.c:

/*
  gcc -g -Wall -Wextra -std=c99 -D _GNU_SOURCE accept4.c
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>

int main (void)
{
    int ld = -1;
    int ad = -1;
    int ret = -1;
    struct sockaddr_in serv;
    int reuse = 1;
    pid_t pid = 0;
    pid_t w = 0;
    int status = 0;

    memset(&serv, 0, sizeof(serv));
    serv.sin_family = AF_INET;
    serv.sin_addr.s_addr = htonl(INADDR_ANY);
    serv.sin_port = htons(4567);

    ld = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ld < 0 )
        perror("socket");

    ret = bind(ld, (struct sockaddr *) &serv, sizeof(serv));
    if (ret < 0)
        perror("bind");

    ret = setsockopt(ld, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
    if (ret < 0)
        perror("setsockopt");  

    ret = listen(ld, 1);
    if (ret < 0)
        perror("listen");

    ad = accept4(ld, NULL, NULL,  SOCK_CLOEXEC);
    printf("Parent accepted fd %d\n", ad); fflush(stdout);

    pid = fork();

    if (pid < 0)
        perror("fork");

    if (pid == 0) {

        char adbuf[2] = {0};
        char * params [3];

        adbuf[0] = ad + '0'; /* kluge for singe digit numbers */
        params[0] = "./child";
        params[1] = adbuf;
        params[2] = NULL;

        execv("./child", params);
        /* execv() does not return on success */
        perror("execv");
    }

    /* from man waitpid */
    do {
        w = waitpid(pid, &status, WUNTRACED | WCONTINUED);
        if (w == -1) {
            perror("waitpid");
            exit(EXIT_FAILURE);
        }

        if (WIFEXITED(status)) {
            printf("exited, status=%d\n", WEXITSTATUS(status));
        } else if (WIFSIGNALED(status)) {
            printf("killed by signal %d\n", WTERMSIG(status));
        } else if (WIFSTOPPED(status)) {
            printf("stopped by signal %d\n", WSTOPSIG(status));
        } else if (WIFCONTINUED(status)) {
            printf("continued\n");
        }
    } while (!WIFEXITED(status) && !WIFSIGNALED(status));
    exit(EXIT_SUCCESS);
}

child.c:

/*
  gcc -g -Wall -Wextra -std=c99 child.c -o child
 */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main (int argc, char *argv[])
{
    char msg[] = "Hello\n";
    ssize_t written = 0;
    int ad = -1;

    if (argc < 2) {
        printf("Too few arguments to child\n");
        exit(EXIT_FAILURE);
    }
    ad = atoi(argv[1]); /* atoi() does not detect errors properly */
    printf("Child attempting to write to fd %d\n", ad); fflush(stdout);
    written = write(ad, msg, sizeof(msg));
    if (written != sizeof(msg)) {
        perror("write");
        exit(EXIT_FAILURE);
    }
    close(ad);
    exit(EXIT_SUCCESS);
}

用法:

  1. 编译成a.out和child
  2. 执行./a.out
  3. telnet 本地主机 4567
  4. 观察
  5. 额外功劳:将accept4()flags 更改为 0 并重做 1-4

ps。我希望我没有做你的功课/剥夺你的学习经验。

【讨论】:

  • 我需要注意什么??当 execl() ??
  • 你需要在你的一个进程中在 fork() 之后使用 exec()。
  • 谢谢你,现在我明白了它的功能,不,我只是在测试accept4,检查SOCK_COLEXEC只是额外的功劳......再次感谢你:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多