大网数据广告活动
服务器运维问答

Why does xinetd try to close file descriptors on startup?

· 52CE 我爱测

I have xinetd running inside a docker container (base image centos:7 ) on a rockylinux 9 host machine. On starting xinetd service in the docker image, it appears to hang. So I used strace , and found the following output, rapidly…

问题原文

原帖链接 · MirroredFate · CC BY-SA 4.0

I have xinetd running inside a docker container (base image centos:7) on a rockylinux 9 host machine.

On starting xinetd service in the docker image, it appears to hang. So I used strace, and found the following output, rapidly counting up:

close(181189)                           = -1 EBADF (Bad file descriptor)
close(181190)                           = -1 EBADF (Bad file descriptor)
close(181191)                           = -1 EBADF (Bad file descriptor)
close(181192)                           = -1 EBADF (Bad file descriptor)
close(181193)                           = -1 EBADF (Bad file descriptor)
close(181194)                           = -1 EBADF (Bad file descriptor)
close(181195)                           = -1 EBADF (Bad file descriptor)
close(181196)                           = -1 EBADF (Bad file descriptor)

Running strace xinetd on another Centos 8 host I see the same behavior.

It appears that xinetd is going through all the available file descriptors, past the /proc/sys/fs/file-max (if a limit is configured, it goes past that limit) until it runs out of numbers.

What is happening here? Why would it have this behavior?

Update March 2023:

For those in similar circumstances, based on the answer detailed below, you can set ulimits via docker when running a container. This is also available in docker compose, and in nomad. Unfortunately, it is not available in k8s.

已采纳解答

原帖链接 · u1686_grawity · CC BY-SA 4.0

Many traditional daemons do this, as part of their "daemonization" sequence which is meant to avoid inheriting anything from a user's environment, as traditionally (before IPC-oriented init systems such as systemd or Upstart became popular) the administrator would just directly run an /etc/rc.d script from an interactive session, and sometimes would just directly start the daemon binary – which is then supposed to fork/double-fork, re-open stdin/out/err, shake off any controlling ttys, write its own pidfile, and so on.

For example, an old /etc/rc.d/inetd might have been as simple as this:

case $1 in
    start)
        echo -n "Starting inetd..."
        /sbin/inetd
        echo "done."
[...]

But there is no cross-platform method for determining which file descriptors are open. Some Unixes have a syscall such as closefrom(), others allow the process to iterate over /proc/self/fd/[0-9], but some don't have anything better than iterating from 3 all the way to the current RLIMIT_NOFILE value and blindly trying to every possible FD.

The latter is what xinetd does, with the assumption that its NOFILE limit will be somewhere around 1k to 4k at most. (Which was generally the case 20-30 years ago when its startup code was written – it hasn't been changed much since then.)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-02-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。