1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1990-2011 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Eclipse Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.eclipse.org/org/documents/epl-v10.html *
11 * (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * *
19 ***********************************************************************/
20 #pragma prototyped
21 /*
22 * Glenn Fowler
23 * AT&T Research
24 *
25 * cs service test
26 */
27
28 static const char id[] = "@(#)$Id: cs.tst (AT&T Research) 1997-11-11 $\0\n";
29
30 #define TST_VERSION "1.0"
31
32 #include <css.h>
33 #include <ctype.h>
34 #include <error.h>
35 #include <tm.h>
36
37 typedef struct
38 {
39 Csid_t id;
40 char* args;
41 } Connection_t;
42
43 static char buf[1024];
44 static char dat[1024];
45
46 static int
acceptf(Css_t * css,Cssfd_t * fp,Csid_t * ip,char ** av,Cssdisc_t * disc)47 acceptf(Css_t* css, Cssfd_t* fp, Csid_t* ip, char** av, Cssdisc_t* disc)
48 {
49 register Connection_t* con;
50 char* s;
51 Sfio_t* sp;
52 char** ap;
53
54 if (!(con = newof(0, Connection_t, 1, 0)))
55 return -1;
56 fp->data = con;
57 con->id = *ip;
58 con->args = 0;
59 if ((ap = av) && (sp = sfstropen()))
60 {
61 while (s = *ap++)
62 {
63 if (ap > av + 1)
64 sfputc(sp, ' ');
65 sfputr(sp, s, -1);
66 }
67 con->args = strdup(sfstruse(sp));
68 sfclose(sp);
69 }
70 return fp->fd;
71 }
72
73 static int
actionf(register Css_t * css,register Cssfd_t * fp,Cssdisc_t * disc)74 actionf(register Css_t* css, register Cssfd_t* fp, Cssdisc_t* disc)
75 {
76 register Connection_t* con;
77 int n;
78
79 switch (fp->status)
80 {
81 case CS_POLL_CLOSE:
82 if (con = (Connection_t*)fp->data)
83 {
84 if (con->args)
85 free(con->args);
86 free(con);
87 }
88 return 0;
89 case CS_POLL_READ:
90 con = (Connection_t*)fp->data;
91 if ((n = csread(css->state, fp->fd, dat, sizeof(dat), CS_LINE)) <= 0)
92 return -1;
93 dat[--n] = 0;
94 if (isalpha(dat[0]) && (dat[1] == 0 || isdigit(dat[1]))) switch (dat[0])
95 {
96 case 'd':
97 error_info.trace = -(int)strtol(dat + 1, NiL, 0);
98 n = sfsprintf(buf, sizeof(buf), "I debug level %d\n", -error_info.trace);
99 break;
100 case 'Q':
101 exit(0);
102 /*FALLTHROUGH*/
103 case 'q':
104 return -1;
105 default:
106 n = sfsprintf(buf, sizeof(buf), "E %s: unknown command\n", dat);
107 break;
108 }
109 else
110 n = sfsprintf(buf, sizeof(buf), "I [%s] server=%s version=%s %s=%s server.pid=%d pid=%d uid=%d gid=%d args=`%s'\n", fmttime(*dat ? dat : "%K", time(NiL)), csname(css->state, 0), TST_VERSION, CS_HOST_LOCAL, csntoa(css->state, con->id.hid), getpid(), con->id.pid, con->id.uid, con->id.gid, con->args);
111 if (cswrite(css->state, fp->fd, buf, n) != n)
112 return -1;
113 return 1;
114 }
115 return 0;
116 }
117
118 static int
exceptf(Css_t * css,unsigned long op,unsigned long arg,Cssdisc_t * disc)119 exceptf(Css_t* css, unsigned long op, unsigned long arg, Cssdisc_t* disc)
120 {
121 switch (op)
122 {
123 case CSS_INTERRUPT:
124 error(ERROR_SYSTEM|3, "%s: interrupt exit", fmtsignal(arg));
125 return 0;
126 case CSS_DORMANT:
127 error(2, "dormant exit");
128 exit(0);
129 case CSS_WAKEUP:
130 error(2, "wakeup");
131 return 0;
132 }
133 error(ERROR_SYSTEM|3, "poll error op=0x%08x arg=0x%08x", op, arg);
134 return -1;
135 }
136
137 int
main(int argc,char ** argv)138 main(int argc, char** argv)
139 {
140 Css_t* css;
141 Cssdisc_t disc;
142
143 NoP(argc);
144 memset(&disc, 0, sizeof(disc));
145 disc.version = CSS_VERSION;
146 disc.flags = CSS_DAEMON|CSS_LOG|CSS_ERROR|CSS_DORMANT|CSS_INTERRUPT|CSS_WAKEUP;
147 disc.timeout = 20 * 1000L;
148 disc.wakeup = 4 * 1000L;
149 disc.acceptf = acceptf;
150 disc.actionf = actionf;
151 disc.errorf = errorf;
152 disc.exceptf = exceptf;
153 if (!(css = cssopen(argv[1], &disc)))
154 exit(1);
155 error_info.id = css->id;
156 csspoll(CS_NEVER, 0);
157 exit(1);
158 }
159