suanmeilizhi 发表于 2012-10-02 23:33

APUE上的daemonize和ruptimed

在APUE中,daemonize函数用于初始化一个守护进程,在这个过程中,经历两次fork,同时两次退出父进程。ruptimed函数调用daemonize函数,我觉得调用daemonize函数的进程退出,所以ruptimed里面位于daemonize之后的代码都不会被执行,但明显这不是作者的原意,但我又找不出我思路错误的原因,还清各位指点。
daemonize函数:#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <fcntl.h>
#include <sys/resource.h>
#include <signal.h>

void
daemonize(const char * cmd)
{
        int i, fd0, fd1, fd2;
        pid_t pid;
        struct rlimit rl;
        struct sigaction sa;

        /*
       *clear file creation mask;
       */

        umask(0);

        /*
       * get maxmum number of file descriptors;
       */

        if(getrlimit(RLIMIT_NOFILE, &rl) < 0) {
                printf("%s can't get file limit", cmd);
                exit(1);
        }

        /*
       *become a session leader to lose controlling TTY;
       */

        if((pid = fork()) < 0) {
                perror("fork error");
                exit(1);
        } else if(pid != 0)
                exit(0);

        printf("child1");
        setsid();

        sa.sa_handler = SIG_IGN;
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = 0;
        if(sigaction(SIGHUP, &sa, NULL) < 0) {
                perror("sigaction error");
                exit(1);
        }

        if((pid = fork()) < 0) {
                perror("fork error");
                exit(1);
        } else if(pid != 0)
                exit(0);
        printf("child2");

        if(chdir("/") < 0)
                perror("can't change directory to /");

        if(rl.rlim_max == RLIM_INFINITY)
                rl.rlim_max = 1024;
        for(i = 0; i < rl.rlim_max; i++)
                close(i);

        fd0 = open("/dev/null", O_RDWR);
        fd1 = dup(0);
        fd2 = dup(0);

        /*
       * initialize the log file
       */
        openlog(cmd, LOG_CONS, LOG_DAEMON);
        if(fd0 != 0 || fd1 != 1 || fd2 != 2) {
                syslog(LOG_ERR, "unexpected descriptors %d %d %d", fd0, fd1, fd2);
                exit(1);
        }
}
ruptimed函数:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>

#define BUFLEN 128
#define QLEN 10

#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 256
#endif

extern int initserver(int, struct sockaddr *, socklen_t , int);

void
serve(int sockfd)
{
        int clfd;
        FILE *fp;
        char buf;

        for(;;) {
                clfd = accept(sockfd, NULL, NULL);
                if(clfd < 0) {
                        syslog(LOG_ERR, "ruptime: accept error:%s",
                                        strerror(errno));
                        exit(1);
                }
                if((fp = popen("/usr/bin/uptime", "r")) == NULL) {
                        sprintf(buf, "error:%s\n", strerror(errno));
                        send(clfd, buf, strlen(buf), 0);
                } else {
                        while(fgets(buf, BUFLEN, fp) != NULL)
                                send(clfd, buf, strlen(buf), 0);
                        pclose(fp);
                }
                close(clfd);
        }
}

int
main(int argc, char *argv[])
{
        struct addrinfo *ailist, *aip;
        struct addrinfo hint;

        int sockfd, err, n;
        char *host;

        if(argc != 1) {
                printf("usage: ruptimed");
                exit(1);
        }

#ifdef _SC_HOST_NAME_MAX
        n = sysconf(_SC_HOST_NAME_MAX);
        if(n < 0)
#endif

        n = HOST_NAME_MAX;
        host = malloc(n);
        if(host == NULL) {
                perror("malloc error");
                exit(1);
        }

        if(gethostname(host, n) < 0) {
                perror("gethostname error");
                exit(1);
        }
        printf("host : %s\n", host);
        printf("pid: %d\n", getpid());

        daemonize("ruptimed");
        printf("pid: %d\n", getpid());
        hint.ai_flags = AI_CANONNAME;
        hint.ai_family = 0;
        hint.ai_socktype = SOCK_STREAM;
        hint.ai_protocol = 0;
        hint.ai_addrlen = 0;
        hint.ai_canonname = NULL;
        hint.ai_next = NULL;

        printf("--------------\n");
        if((err = getaddrinfo(host, "ruptime", &hint, &ailist)) != 0) {
                syslog(LOG_ERR, "ruptimed: getaddrinfo error: %s", gai_strerror(err));
                exit(1);
        }
        printf("--------------\n");
        for(aip = ailist; aip != NULL; aip = aip->ai_next) {
                if((sockfd = initserver(SOCK_STREAM, aip->ai_addr, aip->ai_addrlen, QLEN)) >= 0) {
                        serve(sockfd);
                        exit(0);
                }
        }
        exit(1);
}
也就是说红色部分的代码都不会再执行,因为当前进程作为daemonize中的父进程已经退出。

suanmeilizhi 发表于 2012-10-02 23:52

本帖最后由 suanmeilizhi 于 2012-10-02 23:54 编辑

好吧,我二了,fork之后子进程和父进程共享正文段,所以即使父进程退出,子进程还是会按照父进程的正文执行下去。现在还没懂的是守护进程在此处的意义。

linux_c_py_php 发表于 2012-10-03 10:05

本帖最后由 linux_c_py_php 于 2012-10-03 11:14 编辑

守护进程脱离终端了, 在新的会话里边, 在你的shell(该shell是你当前会话首进程)里看起来就像是“跑到后台”(实际上, 这里与后台没有半毛钱关系)去了一样, 你可以继续做其他事了.

这个程序的先守护, 打开syslog, 之后getaddrinfo获取ruptime服务的TCP监听端口(依赖/etc/services的服务, 最好hint设置AI_PASSIVE,否则需要获取后对sin_addr赋值).

最后循环每一个监听端口, 调用initserver启动监听.

可以猜得出来, initserver是需要调用fork/pthread_create的, 否则没法同时监听N个端口.

suanmeilizhi 发表于 2012-10-03 10:39

回复 3# linux_c_py_php


    我在/etc/services中添加了内容4000 ruptime/tcp所以监听的是4000这个端口,和IP绑定为0有什么关系,不懂,麻烦您指教哈。
    另外,最后一个循环只监听了一个。

linux_c_py_php 发表于 2012-10-03 11:14

IP绑0就是绑INADDR_ANY的意思, initserver里struct sockaddr_in结构体的sin_addr赋值过吗.

正确的做法应该是:hint的flag要加上AI_PASSIVE,这样才会将结果的sockaddr_in.sin_addr = 0,这样就默认监听在所有IP上了。 如果不用这个,你需要给结果的sin_addr进行赋值。

suanmeilizhi 发表于 2012-10-03 22:26

回复 5# linux_c_py_php


initserver函数的作用是绑定和监听套接字。   
我在服务器端的hint.ai_flags设置为AI_CANONNAME,是不是说initserver函数sockaddr_in.sin_addr就没有自动设为0,导致服务器并没有监听所有IP,系统选一个地址并将其绑定到套接字呢?

linux_c_py_php 发表于 2012-10-03 22:32

按道理说是未定义的, 看返回的sin_addr是什么随机值了, 反正错误的IP是bind失败的.

suanmeilizhi 发表于 2012-10-03 23:12

回复 7# linux_c_py_php


    谢谢指教,感觉还是有很多不懂的地方,还需要加强。
    刚刚试了一下,发现在服务器端过滤之后的地址端口并不是我添加在/etc/services中的4000,请问这是怎么回事?

linux_c_py_php 发表于 2012-10-03 23:53

4000 ruptime/tcp

改成:

ruptime 4000/tcp

suanmeilizhi 发表于 2012-10-04 10:08

回复 9# linux_c_py_php


    额。。。是我抄错了。。。
在/etc/services里面我是这样写的,ruptime 4000/tcp
但是结果确实40957
页: [1] 2 3 4 5 6 7 8 9 10
查看完整版本: APUE上的daemonize和ruptimed