Subversion Repositories wimsdev

Rev

Rev 8195 | Go to most recent revision | Details | Compare with Previous | 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 */
8185 bpr 19
#include "wimslogd.h"
10 reyssat 20
 
21
void errorquit(char *p)
22
{
8195 bpr 23
    wimslogd_error(p); exit(1);
10 reyssat 24
}
25
 
26
void opensock(void)
27
{
28
    struct stat st;
29
    struct sockaddr_un sun;
30
    if(stat(sockfile,&st)==0) unlink(sockfile);
31
    commsock=socket(PF_UNIX,SOCK_STREAM,0);
32
    if(commsock<0) errorquit("Unable to open socket.");
33
    sun.sun_family=PF_UNIX;
34
    snprintf(sun.sun_path,sizeof(sun.sun_path),"%s",sockfile);
35
    if(bind(commsock,(struct sockaddr *)&sun, sizeof(sun))==-1)
36
      errorquit("Unable to bind to socket file.");
37
    if(listen(commsock,SOCKET_QUEUE)==-1) errorquit("listen() error.");
38
}
39
 
40
void output(int fh)
41
{
42
    int l, *ip;
43
    if(answerlen<0) l=strlen(textbuf); else l=answerlen;
44
    ip=(int *) commbuf; *ip=l;
3836 kbelabas 45
    (void)write(fh,commbuf,l+sizeof(int));
10 reyssat 46
    close(fh);
47
    if(debugging) {
8849 bpr 48
      if(textbuf[0]=='O') debug("%.2s %d bytes.",textbuf,l);
49
      else debug("%s",textbuf);
10 reyssat 50
    }
51
}
52
 
53
void sockerror(int type, char *p,...)
54
{
55
    char *estr, buf[MAX_LINELEN+1];
56
    va_list vp;
57
    va_start(vp,p);
58
    vsnprintf(buf,sizeof(buf),p,vp);
59
    va_end(vp);
60
    if(errno==0) estr=""; else estr=strerror(errno);
61
    snprintf(textbuf,BUFFERLEN-16,"ERROR %d\n%s\n%s",type,buf,estr);
62
}
63
 
64
void sockok(char *p)
65
{
66
    snprintf(textbuf,BUFFERLEN-16,"OK\n%s",p);
67
}
68
 
69
void answer(int fh)
70
{
71
    int t,l, *ip;
72
 
73
    t=read(fh,commbuf,BUFFERLEN-sizeof(int)); if(t<sizeof(int)) {
8849 bpr 74
      bad: sockerror(3,"Daemon socket read error.");
75
      goto end;
10 reyssat 76
    }
77
    ip=(int *) commbuf; l=*ip;
78
    if(l<=0 || l>=BUFFERLEN-sizeof(int)) goto bad;
79
    while(t<l+sizeof(int)) {
8849 bpr 80
      struct timeval tv;
81
      fd_set rset;
82
      int t2;
83
      tv.tv_sec=0; tv.tv_usec=20*1000;
84
      FD_ZERO(&rset); FD_SET(fh,&rset);
85
      if(select(fh+1,&rset,NULL,NULL,&tv)<=0) goto bad;
86
      t2=read(fh,commbuf+t,l+sizeof(int)-t);
87
      if(t2<=0 || t2>l+sizeof(int)-t) goto bad;
88
      t+=t2;
10 reyssat 89
    }
90
    textbuf[l]=0; textptr=textbuf;
91
    answerlen=-1;
92
    if(debugging) debug("> %s",textbuf);
93
    if(options()>=0) cmd();
3836 kbelabas 94
    end: (void)chdir(cwd); cwdtype=dir_home; output(fh);
10 reyssat 95
}