Back to index

wims  3.65+svn20090927
socket.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018 /* WIMS log daemon, socket routines */
00019 
00020 void errorquit(char *p)
00021 {
00022     error(p); exit(1);
00023 }
00024 
00025 void opensock(void)
00026 {
00027     struct stat st;
00028     struct sockaddr_un sun;
00029     if(stat(sockfile,&st)==0) unlink(sockfile);
00030     commsock=socket(PF_UNIX,SOCK_STREAM,0);
00031     if(commsock<0) errorquit("Unable to open socket.");
00032     sun.sun_family=PF_UNIX;
00033     snprintf(sun.sun_path,sizeof(sun.sun_path),"%s",sockfile);
00034     if(bind(commsock,(struct sockaddr *)&sun, sizeof(sun))==-1)
00035       errorquit("Unable to bind to socket file.");
00036     if(listen(commsock,SOCKET_QUEUE)==-1) errorquit("listen() error.");
00037 }
00038 
00039 void output(int fh)
00040 {
00041     int l, *ip;
00042     if(answerlen<0) l=strlen(textbuf); else l=answerlen;
00043     ip=(int *) commbuf; *ip=l;
00044     write(fh,commbuf,l+sizeof(int));
00045     close(fh);
00046     if(debugging) {
00047        if(textbuf[0]=='O') debug("%.2s %d bytes.",textbuf,l);
00048        else debug("%s",textbuf);
00049     }
00050 }
00051 
00052 void sockerror(int type, char *p,...)
00053 {
00054     char *estr, buf[MAX_LINELEN+1];
00055     va_list vp;
00056     va_start(vp,p);
00057     vsnprintf(buf,sizeof(buf),p,vp);
00058     va_end(vp);
00059     if(errno==0) estr=""; else estr=strerror(errno);
00060     snprintf(textbuf,BUFFERLEN-16,"ERROR %d\n%s\n%s",type,buf,estr);
00061 }
00062 
00063 void sockok(char *p)
00064 {
00065     snprintf(textbuf,BUFFERLEN-16,"OK\n%s",p);
00066 }
00067 
00068 #include "options.c"
00069 #include "score.c"
00070 #include "cmd.c"
00071 
00072 void answer(int fh)
00073 {
00074     int t,l, *ip;
00075 
00076     t=read(fh,commbuf,BUFFERLEN-sizeof(int)); if(t<sizeof(int)) {
00077        bad: sockerror(3,"Daemon socket read error.");
00078        goto end;
00079     }
00080     ip=(int *) commbuf; l=*ip;
00081     if(l<=0 || l>=BUFFERLEN-sizeof(int)) goto bad;
00082     while(t<l+sizeof(int)) {
00083        struct timeval tv;
00084        fd_set rset;
00085        int t2;
00086        tv.tv_sec=0; tv.tv_usec=20*1000;
00087        FD_ZERO(&rset); FD_SET(fh,&rset);
00088        if(select(fh+1,&rset,NULL,NULL,&tv)<=0) goto bad;
00089        t2=read(fh,commbuf+t,l+sizeof(int)-t);
00090        if(t2<=0 || t2>l+sizeof(int)-t) goto bad;
00091        t+=t2;
00092     }
00093     textbuf[l]=0; textptr=textbuf;
00094     answerlen=-1;
00095     if(debugging) debug("> %s",textbuf);
00096     if(options()>=0) cmd();
00097     end: chdir(cwd); cwdtype=dir_home; output(fh);
00098 }
00099