📄 00000018.htm
字号:
log_quit("unrecognized option: -%c", optopt); <BR> } <BR> } <BR> if (debug == 0) <BR> daemon_init(); <BR> loop(); /* never returns */ <BR>} <BR>_______________________________________________________________________ <BR>_______ <BR>程序18.2 main 函数 <BR> 当使用了 -d 选项后,所有对log_XXX函数的调用(见附录B)都送到标准错误。 <BR>否则它们就会被syslog记录下来。 <BR> 函数loop是服务器程序的主循环(程序18.3)。它使用了select 函数来复用 <BR>不同的描述符。 <BR>_______________________________________________________________________ <BR>_______ <BR>#include "calld.h" <BR>#include <sys/time.h> <BR>#include <errno.h> <BR>static void cli_done(int); <BR>static void child_done(int); <BR>static fd_set allset; /* one bit per client conn, plus one for listenfd <BR> */ <BR> /* modified by loop() and cli_do <BR>e() */ <BR>void <BR>loop(void) <BR>{ <BR> int i, n, maxfd, maxi, listenfd, nread; <BR> char buf[MAXLINE]; <BR> Client *cliptr; <BR> uid_t uid; <BR> fd_set rset; <BR> if (signal_intr(SIGCHLD, sig_chld) == SIG_ERR) <BR> log_sys("signal error"); <BR> /* obtain descriptor to listen for client reques <BR>s on */ <BR> if ( (listenfd = serv_listen(CS_CALL)) < 0) <BR> log_sys("serv_listen error"); <BR> FD_ZERO(&allset); <BR> FD_SET(listenfd, &allset); <BR> maxfd = listenfd; <BR> maxi = -1; <BR> for ( ; ; ) { <BR> if (chld_flag) <BR> child_done(maxi); <BR> rset = allset; /* rset gets modified each time around * <BR> <BR> if ( (n = select(maxfd + 1, &rset, NULL, NULL, NULL)) < 0) { <BR> if (errno == EINTR) { <BR> /* caught SIGCHLD, find entry with child <BR>one set */ <BR> child_done(maxi); <BR> continue; /* issue the select agai <BR> */ <BR> } else <BR> log_sys("select error"); <BR> } <BR> if (FD_ISSET(listenfd, &rset)) { <BR> /* accept new client request */ <BR> if ( (clifd = serv_accept(listenfd, &uid)) < 0) <BR> log_sys("serv_accept error: %d", clifd); <BR> i = client_add(clifd, uid); <BR> FD_SET(clifd, &allset); <BR> if (clifd > maxfd) <BR> maxfd = clifd; /* max fd for select() */ <BR> if (i > maxi) <BR> maxi = i; /* max index in client[] <BR>array */ <BR> log_msg("new connection: uid %d, fd %d", uid, clifd); <BR> continue; <BR> } <BR> /* Go through client[] array. <BR> Read any client data that has arrived. */ <BR> for (cliptr = &client[0]; cliptr <= &client[maxi]; cliptr++) { <BR> if ( (clifd = cliptr->fd) < 0) <BR> continue; <BR> if (FD_ISSET(clifd, &rset)) { <BR> /* read argument buffer from cli <BR>nt */ <BR> if ( (nread = read(clifd, buf, MAXLINE)) < 0) <BR> log_sys("read error on fd %d", clifd); <BR> else if (nread == 0) { <BR> /* The client has terminated or closed t <BR>e stream <BR> pipe. Now we can release its device <BR>ock. */ <BR> log_msg("closed: uid %d, fd %d", <BR> <BR>liptr->uid, clifd); <BR> lock_rel(cliptr->pid); <BR> cli_done(clifd); <BR> continue; <BR> } <BR> /* Data has arrived from the client. Process th <BR> <BR> client's request. */ <BR> if (buf[nread-1] != 0) { <BR> log_quit("request from uid %d not null t <BR>rminated:" <BR> " %*.*s", uid, nread, n <BR>ead, buf); <BR> cli_done(clifd); <BR> continue; <BR> } <BR> log_msg("starting: %s, from uid %d", buf, uid); <BR> /* Parse the arguments, set opti <BR>ns. Since <BR> we may need to try calling ag <BR>in for this <BR> client, save options in clien <BR>[] array. */ <BR> if (buf_args(buf, cli_args) < 0) <BR> log_quit("command line error: %s", buf); <BR>  
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -