Unix Network Programming Episode 88

'inetd' Daemon

On a typical Unix system, there could be many servers in existence, just waiting for a client request to arrive. Examples are FTP, Telnet, Rlogin, TFTP, and so on. With systems before 4.3BSD, each of these services had a process associated with it. This process was started at boot-time from the file /etc/rc, and each process did nearly identical startup tasks: create a socket, bind the server's well-known port to the socket, wait for a connection (if TCP) or a datagram (if UDP), and then fork. The child process serviced the client and the parent waited for the next client request. There are two problems with this model:

1.All these daemons contained nearly identical startup code, first with respect to socket creation, and also with respect to becoming a daemon process (similar to our daemon_init function).

2.Each daemon took a slot in the process table, but each daemon was asleep most of the time.

The 4.3BSD release simplified this by providing an Internet superserver: the inetd daemon. This daemon can be used by servers that use either TCP or UDP. It does not handle other protocols, such as Unix domain sockets. This daemon fixes the two problems just mentioned:

1.It simplifies writing daemon processes since most of the startup details are handled by inetd. This obviates the need for each server to call our daemon_init function.

2.It allows a single process (inetd) to be waiting for incoming client requests for multiple services, instead of one process for each service. This reduces the total number of processes in the system.

Specifying the wait flag for a datagram service changes the steps done by the parent process. This flag says that inetd must wait for its child to terminate before selecting on this socket again. The following changes occur:

1.When fork returns in the parent, the parent saves the process ID of the child. This allows the parent to know when this specific child process terminates, by looking at the value returned by waitpid.

2.The parent disables the socket from future selects by using the FD_CLR macro to turn off the bit in its descriptor set. This means that the child process takes over the socket until it terminates.

3.When the child terminates, the parent is notified by a SIGCHLD signal, and the parent's signal handler obtains the process ID of the terminating child. It reenables select for the corresponding socket by turning on the bit in its descriptor set for this socket.

'daemon_inetd' Function

We can call from a server we know is invoked by inetd.

复制代码
#include "unp.h"
#include <syslog.h>

extern int daemon_proc;

void daemon_inetd(const char *pname, int facility)
{
    daemon_proc=1;
    openlog(pname, LOG_PID, facility);
}

daemon_inetd function: daemonizes process run by inetd

复制代码
#include "unp.h"
#include <time.h>

int main(int argc, char **argv)
{
    socklen_t len;
    struct sockaddr *clientaddr;
    char buff[MAXLINE];
    time_t ticks;

    daemon_inetd(argv[0],0);

    clientaddr=Malloc(sizeof(struct sockaddr_storage));
    len=sizeof(struct sockaddr_storage);
    Getpeername(0,clientaddr, &len);
    err_msg("connection from %s", Sock_ntop(clientaddr, len));

    ticks=time(NULL);
    snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
    Write(0,buff, strlen(buff));

    Close(0);
    return 0;
}

Protocol-independent daytime server that can be invoked by inetd

相关推荐
cui_ruicheng2 小时前
LangChain 应用开发(十四):Agent 上下文与记忆机制
服务器·人工智能·python·langchain
Brilliantwxx5 小时前
【Linux】 进程(10) 进程控制深度解析:创建、终止与等待
linux·运维·服务器·c语言·开发语言·网络
ai小陈5 小时前
FramePack图生视频云端部署实战:从单图输入到视频输出的完整流程
服务器·人工智能·安全·ai·音视频·gpu算力
欧米欧6 小时前
Linux基础指令上
linux·运维·服务器
sulikey7 小时前
Linux 粘滞位(Sticky Bit)完全指南:为什么共享目录需要“防删除“?
linux·服务器
mengge.cloud7 小时前
网络技术基础小白教程
linux·服务器·网络·云计算
严谨的麻辣烫7 小时前
2026 年静态 IP 为什么重新受到关注?从住宅代理安全问题看固定网络出口
运维·服务器·tcp/ip·网络安全
treacle田8 小时前
达梦数据库-Linux DM数据守护主备集群缩改为单机-记录总结
linux·服务器·数据库·主备集群改单机
shengchanxian18 小时前
2026年9月服务器装配流水线厂家技术测评:对标国标的硬件评判逻辑
运维·服务器·服务器装配流水线厂家
IT大白鼠8 小时前
CentOS 7.9 自建 Yum 源服务器搭建指南
linux·服务器·centos