📄 00000001.htm
字号:
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>发信人: ylsdd (1245), 信区: Linux <BR>标 题: Faint 0.1 文件 cover.c <BR>发信站: BBS 水木清华站 (Wed Feb 23 07:09:46 2000) <BR> <BR>#include <sys/stat.h> <BR>#include <unistd.h> <BR>#include <sys/param.h> <BR>#include <signal.h> <BR>#include <fcntl.h> <BR>#include <errno.h> <BR>#include <termios.h> <BR>#include <sys/time.h> <BR>#include <sys/types.h> <BR>#define TIMEIDLE 200 <BR>#define TIMEDELAY 6 <BR> <BR>int setup_daemon(void) <BR>{ int i; <BR> for(i=0;i<NOFILE;i++) close(i); <BR> switch(fork()) <BR> { case -1: return -1; <BR> default: exit(0); <BR> case 0: <BR> setsid(); <BR> switch(fork()) <BR> { case-1: <BR> default: exit(0); <BR> case 0: <BR> umask(0); <BR> return 0; <BR> } <BR> } <BR>} <BR> <BR>main(int argc, char **argv) <BR>{ int fdin[2], fdout[2]; <BR> int fdr0, fdw0, fdr1, fdw1, fdlock, fdlog, i, try; <BR> time_t timenow; <BR> struct tm *tmptr; <BR> fd_set rdfds, exfds; <BR> struct timeval timeout; <BR> char buf[1024], ch; <BR> char fnlock[40], fnin[40], fnout[40], fnlog[40]; <BR> <BR> if(argc<2) return; <BR> if(sscanf(argv[1],"%d",&i)!=1) return; <BR> if(i>99||i<0) return; <BR> sprintf(fnlock,"ctrl/%02dcover.lock",i); <BR> sprintf(fnin,"ctrl/%02din.fifo",i); <BR> sprintf(fnout,"ctrl/%02dout.fifo",i); <BR> timenow=time(NULL); <BR> tmptr=localtime(&timenow); <BR> sprintf(fnlog,"log/%02d%04d%02d%02d%02d%02d%02d.log", <BR> i, tmptr->tm_year+1900, tmptr->tm_mon, tmptr->tm_mday, <BR> tmptr->tm_hour, tmptr->tm_min, tmptr->tm_sec); <BR> setup_daemon(); <BR> if(pipe(fdin)==-1) exit(1); <BR> if(pipe(fdout)==-1) exit(1); <BR> fdlock=open(fnlock,O_RDONLY|O_CREAT,S_IRUSR|S_IWUSR); <BR> if(fdlock<0) exit(1); <BR> if(flock(fdlock,LOCK_EX|LOCK_NB)<0) exit(1); <BR> switch(fork()) <BR> { case -1: exit(1); <BR> case 0: close(fdin[1]); <BR> dup(fdout[1]); <BR> close(fdout[0]); <BR> close(fdout[1]); <BR> close(fdlock); <BR> execvp(argv[2], &argv[2]); <BR> exit(1); <BR> default: <BR> close(fdin[0]); <BR> fdw0=fdin[1]; <BR> close(fdout[1]); <BR> fdr1=fdout[0]; <BR> } <BR> <BR> signal(SIGPIPE, SIG_IGN); <BR> fdw1=-1; <BR> fdr0=open(fnin,O_RDWR|O_NDELAY); <BR> if(fdr0<0) exit(1); <BR> <BR> fdlog=open(fnlog,O_WRONLY|O_CREAT|O_EXCL,S_IRUSR|S_IWUSR); <BR> if(fdlog<0) exit(1); <BR> for(i=2, ch=' ';i<argc;i++) <BR> { write(fdlog, argv[i], strlen(argv[i])); <BR> write(fdlog, &ch, 1); <BR> } <BR> ch='\n'; <BR> write(fdlog,&ch,1); <BR> <BR> <BR> timeout.tv_sec=TIMEIDLE; <BR> timeout.tv_usec=0; <BR> try=1; <BR> ch=12; <BR> while(1) <BR> { <BR> if(fdw1<0&&try==1) <BR> { fdw1=open(fnout,O_WRONLY|O_NDELAY); <BR> try=0; <BR> } <BR> FD_ZERO(&rdfds); <BR> FD_ZERO(&exfds); <BR> FD_SET(fdr1,&rdfds); <BR> FD_SET(fdr1,&exfds); <BR> FD_SET(fdr0,&rdfds); <BR> if(select(1+((fdr0>fdr1)?fdr0:fdr1),&rdfds,NULL,&exfds,&timeout)<=0) <BR> { timeout.tv_sec=TIMEIDLE; <BR> timeout.tv_usec=0; <BR> write(fdw0,&ch,1); <BR> continue; <BR> } <BR> if(FD_ISSET(fdr1,&rdfds)) <BR> { i=read(fdr1,buf,1024); <BR> if(i>0) while(write(fdlog,buf,i)<0) <BR> { if(errno==EINTR) continue; <BR> else exit(1); <BR> } <BR> if(fdw1>=0&&i>0) <BR> { while(write(fdw1,buf,i)<0) <BR> { if(errno==EINTR) continue; <BR> close(fdw1); <BR> fdw1=-1; <BR> break; <BR> } <BR> } <BR> } <BR> if(FD_ISSET(fdr1,&exfds)) exit(1); <BR> if(FD_ISSET(fdr0,&rdfds)) <BR> { i=read(fdr0,buf,1024); <BR> if(i>0) <BR> { while(write(fdw0,buf,i)<0) <BR> { if(errno!=EINTR) exit(1); <BR> } <BR> timeout.tv_sec=TIMEIDLE; <BR> timeout.tv_usec=0; <BR> try=1; <BR> } <BR> } <BR> } <BR>} <BR>/*----------------End of File cover.c---------------------*/ <BR> <BR>-- <BR>要是不能说糊话, 还不如死了算了 <BR> <BR>※ 来源:·BBS 水木清华站 smth.org·[FROM: 162.105.21.92] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -