Rev 3836 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
10 | reyssat | 1 | /* Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis |
2 | * |
||
3 | * This program is free software; you can redistribute it and/or modify |
||
4 | * it under the terms of the GNU General Public License as published by |
||
5 | * the Free Software Foundation; either version 2 of the License, or |
||
6 | * (at your option) any later version. |
||
7 | * |
||
8 | * This program is distributed in the hope that it will be useful, |
||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
11 | * GNU General Public License for more details. |
||
12 | * |
||
13 | * You should have received a copy of the GNU General Public License |
||
14 | * along with this program; if not, write to the Free Software |
||
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
||
16 | */ |
||
17 | |||
18 | /* WIMS log daemon, socket routines */ |
||
19 | |||
20 | void errorquit(char *p) |
||
21 | { |
||
22 | error(p); exit(1); |
||
23 | } |
||
24 | |||
25 | void opensock(void) |
||
26 | { |
||
27 | struct stat st; |
||
28 | struct sockaddr_un sun; |
||
29 | if(stat(sockfile,&st)==0) unlink(sockfile); |
||
30 | commsock=socket(PF_UNIX,SOCK_STREAM,0); |
||
31 | if(commsock<0) errorquit("Unable to open socket."); |
||
32 | sun.sun_family=PF_UNIX; |
||
33 | snprintf(sun.sun_path,sizeof(sun.sun_path),"%s",sockfile); |
||
34 | if(bind(commsock,(struct sockaddr *)&sun, sizeof(sun))==-1) |
||
35 | errorquit("Unable to bind to socket file."); |
||
36 | if(listen(commsock,SOCKET_QUEUE)==-1) errorquit("listen() error."); |
||
37 | } |
||
38 | |||
39 | void output(int fh) |
||
40 | { |
||
41 | int l, *ip; |
||
42 | if(answerlen<0) l=strlen(textbuf); else l=answerlen; |
||
43 | ip=(int *) commbuf; *ip=l; |
||
44 | write(fh,commbuf,l+sizeof(int)); |
||
45 | close(fh); |
||
46 | if(debugging) { |
||
47 | if(textbuf[0]=='O') debug("%.2s %d bytes.",textbuf,l); |
||
48 | else debug("%s",textbuf); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | void sockerror(int type, char *p,...) |
||
53 | { |
||
54 | char *estr, buf[MAX_LINELEN+1]; |
||
55 | va_list vp; |
||
56 | va_start(vp,p); |
||
57 | vsnprintf(buf,sizeof(buf),p,vp); |
||
58 | va_end(vp); |
||
59 | if(errno==0) estr=""; else estr=strerror(errno); |
||
60 | snprintf(textbuf,BUFFERLEN-16,"ERROR %d\n%s\n%s",type,buf,estr); |
||
61 | } |
||
62 | |||
63 | void sockok(char *p) |
||
64 | { |
||
65 | snprintf(textbuf,BUFFERLEN-16,"OK\n%s",p); |
||
66 | } |
||
67 | |||
68 | #include "options.c" |
||
69 | #include "score.c" |
||
70 | #include "cmd.c" |
||
71 | |||
72 | void answer(int fh) |
||
73 | { |
||
74 | int t,l, *ip; |
||
75 | |||
76 | t=read(fh,commbuf,BUFFERLEN-sizeof(int)); if(t<sizeof(int)) { |
||
77 | bad: sockerror(3,"Daemon socket read error."); |
||
78 | goto end; |
||
79 | } |
||
80 | ip=(int *) commbuf; l=*ip; |
||
81 | if(l<=0 || l>=BUFFERLEN-sizeof(int)) goto bad; |
||
82 | while(t<l+sizeof(int)) { |
||
83 | struct timeval tv; |
||
84 | fd_set rset; |
||
85 | int t2; |
||
86 | tv.tv_sec=0; tv.tv_usec=20*1000; |
||
87 | FD_ZERO(&rset); FD_SET(fh,&rset); |
||
88 | if(select(fh+1,&rset,NULL,NULL,&tv)<=0) goto bad; |
||
89 | t2=read(fh,commbuf+t,l+sizeof(int)-t); |
||
90 | if(t2<=0 || t2>l+sizeof(int)-t) goto bad; |
||
91 | t+=t2; |
||
92 | } |
||
93 | textbuf[l]=0; textptr=textbuf; |
||
94 | answerlen=-1; |
||
95 | if(debugging) debug("> %s",textbuf); |
||
96 | if(options()>=0) cmd(); |
||
97 | end: chdir(cwd); cwdtype=dir_home; output(fh); |
||
98 | } |
||
99 |