gpt4 book ai didi

c - 如何测试 SOCK_CLOEXEC/O_CLOEXEC 的功能(执行时关闭)

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:20:37 25 4
gpt4 key购买 nike

我有一个接受的文件描述符,它设置了 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);

fork 代码:

  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

最佳答案

更新

我注意到您在代码中添加了对 execl() 的调用,并在 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() 之后在子进程中执行某些操作。仅仅 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. 远程登录本地主机 4567
  4. 观察
  5. 额外功劳:将 accept4()flags 更改为 0 并重做 1-4

附言。我希望我没有做你的家庭作业/抢走你的学习经验。

关于c - 如何测试 SOCK_CLOEXEC/O_CLOEXEC 的功能(执行时关闭),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22323975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com