1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
/*
* Copyright (c) 2016-2024 Dmitry V. Levin <ldv@strace.io>
* All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "tests.h"
#include <limits.h>
#include <unistd.h>
#include <sys/resource.h>
static void
move_fd(int *from, int *to)
{
for (; *to > *from; --*to) {
if (dup2(*from, *to) != *to)
continue;
close(*from);
*from = *to;
break;
}
}
void
pipe_maxfd(int pipefd[2])
{
struct rlimit rlim;
if (getrlimit(RLIMIT_NOFILE, &rlim))
perror_msg_and_fail("getrlimit");
if (rlim.rlim_cur < rlim.rlim_max) {
struct rlimit rlim_new;
rlim_new.rlim_cur = rlim_new.rlim_max = rlim.rlim_max;
if (!setrlimit(RLIMIT_NOFILE, &rlim_new))
rlim.rlim_cur = rlim.rlim_max;
}
if (pipe(pipefd))
perror_msg_and_fail("pipe");
int max_fd = (rlim.rlim_cur > 0 && rlim.rlim_cur <= 0x10000)
? rlim.rlim_cur - 1 : 0x10000;
move_fd(&pipefd[1], &max_fd);
--max_fd;
move_fd(&pipefd[0], &max_fd);
}
|