1
/* INDI Server for protocol version 1.7.
2
* Copyright (C) 2007 Elwood C. Downey ecdowney@clearskyinstitute.com
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Lesser General Public
6
License as published by the Free Software Foundation; either
7
version 2.1 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Lesser General Public License for more details.
14
You should have received a copy of the GNU Lesser General Public
15
License along with this library; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
* argv lists names of Driver programs to run or sockets to connect for Devices.
19
* Drivers are restarted if they exit or connection closes.
20
* Each local Driver's stdin/out are assumed to provide INDI traffic and are
21
* connected here via pipes. Local Drivers' stderr are connected to our
22
* stderr with date stamp and driver name prepended.
23
* We only support Drivers that advertise support for one Device. The problem
24
* with multiple Devices in one Driver is without a way to know what they
25
* _all_ are there is no way to avoid sending all messages to all Drivers.
26
* Outbound messages are limited to Devices and Properties seen inbound.
27
* Messages to Devices on sockets always include Device so the chained
28
* indiserver will only pass back info from that Device.
29
* All newXXX() received from one Client are echoed to all other Clients who
30
* have shown an interest in the same Device and property.
32
* Implementation notes:
34
* We fork each driver and open a server socket listening for INDI clients.
35
* Then forever we listen for new clients and pass traffic between clients and
36
* drivers, subject to optimizations based on sniffing messages for matching
37
* Devices and Properties. Since one message might be destined to more than
38
* one client or device, they are queued and only removed after the last
39
* consumer is finished. XMLEle are converted to linear strings before being
40
* sent to optimize write system calls and avoid blocking to slow clients.
41
* Clients that get more than maxqsiz bytes behind are shut down.
54
#include <sys/types.h>
56
#include <sys/socket.h>
57
#include <netinet/in.h>
59
#include <arpa/inet.h>
65
#define INDIPORT 7624 /* default TCP/IP port to listen */
66
#define REMOTEDVR (-1234) /* invalid PID to flag remote drivers */
67
#define MAXRBUF 4096 /* max read buffering here */
68
#define MAXWSIZ 4096 /* max bytes/write */
69
#define DEFMAXQSIZ 10 /* default max q behind, MB */
71
/* associate a usage count with queuded client or device message */
73
int count; /* number of consumers left */
74
unsigned int cl; /* content length */
75
char *cp; /* content: buf or malloced */
76
char buf[MAXWSIZ]; /* local buf for most messages */
79
/* BLOB handling, NEVER is the default */
80
typedef enum {B_NEVER=0, B_ALSO, B_ONLY} BLOBHandling;
82
/* device + property name */
84
char dev[MAXINDIDEVICE];
85
char name[MAXINDINAME];
88
/* record of each snooped property */
91
BLOBHandling blob; /* when to snoop BLOBs */
94
/* info for each connected client */
96
int active; /* 1 when this record is in use */
97
Property *props; /* malloced array of props we want */
98
int nprops; /* n entries in props[] */
99
int allprops; /* saw getProperties w/o device */
100
BLOBHandling blob; /* when to send setBLOBs */
101
int s; /* socket for this client */
102
LilXML *lp; /* XML parsing context */
103
FQ *msgq; /* Msg queue */
104
unsigned int nsent; /* bytes of current Msg sent so far */
106
static ClInfo *clinfo; /* malloced pool of clients */
107
static int nclinfo; /* n total (not active) */
109
/* info for each connected driver */
111
const char *name; /* persistent malloced name */
112
char dev[MAXINDIDEVICE]; /* device served by this driver */
113
Snoopee *sprops; /* malloced array of props we snoop */
114
int nsprops; /* n entries in sprops[] */
115
int pid; /* process id or REMOTEDVR if remote */
116
int rfd; /* read pipe fd */
117
int wfd; /* write pipe fd */
118
int efd; /* stderr from driver, if local */
119
int restarts; /* times process has been restarted */
120
LilXML *lp; /* XML parsing context */
121
FQ *msgq; /* Msg queue */
122
unsigned int nsent; /* bytes of current Msg sent so far */
124
static DvrInfo *dvrinfo; /* malloced array of drivers */
125
static int ndvrinfo; /* n total */
127
static char *me; /* our name */
128
static int port = INDIPORT; /* public INDI port */
129
static int verbose; /* chattiness */
130
static int lsocket; /* listen socket */
131
static char *ldir; /* where to log driver messages */
132
static int maxqsiz = (DEFMAXQSIZ*1024*1024); /* kill if these bytes behind */
134
static void logStartup(int ac, char *av[]);
135
static void usage (void);
136
static void noZombies (void);
137
static void noSIGPIPE (void);
138
static void indiRun (void);
139
static void indiListen (void);
140
static void newClient (void);
141
static int newClSocket (void);
142
static void shutdownClient (ClInfo *cp);
143
static int readFromClient (ClInfo *cp);
144
static void startDvr (DvrInfo *dp);
145
static void startLocalDvr (DvrInfo *dp);
146
static void startRemoteDvr (DvrInfo *dp);
147
static int openINDIServer (char host[], int indi_port);
148
static void restartDvr (DvrInfo *dp);
149
static void q2RDrivers (const char *dev, Msg *mp, XMLEle *root);
150
static void q2SDrivers (int isblob, const char *dev, const char *name, Msg *mp,
152
static int q2Clients (ClInfo *notme, int isblob, const char *dev, const char *name,
153
Msg *mp, XMLEle *root);
154
static void addSDevice (DvrInfo *dp, const char *dev, const char *name);
155
static Snoopee *findSDevice (DvrInfo *dp, const char *dev, const char *name);
156
static void addClDevice (ClInfo *cp, const char *dev, const char *name);
157
static int findClDevice (ClInfo *cp, const char *dev, const char *name);
158
static int readFromDriver (DvrInfo *dp);
159
static int stderrFromDriver (DvrInfo *dp);
160
static int msgQSize (FQ *q);
161
static void setMsgXMLEle (Msg *mp, XMLEle *root);
162
static void setMsgStr (Msg *mp, char *str);
163
static void freeMsg (Msg *mp);
164
static Msg *newMsg (void);
165
static int sendClientMsg (ClInfo *cp);
166
static int sendDriverMsg (DvrInfo *cp);
167
static void crackBLOB (char *enableBLOB, BLOBHandling *bp);
168
static void traceMsg (XMLEle *root);
169
static char *indi_tstamp (char *s);
170
static void logDMsg (XMLEle *root, const char *dev);
171
static void Bye(void);
174
main (int ac, char *av[])
183
while ((--ac > 0) && ((*++av)[0] == '-')) {
185
for (s = av[0]+1; *s != '\0'; s++)
189
fprintf (stderr, "-l requires log directory\n");
197
fprintf (stderr, "-m requires max MB behind\n");
200
maxqsiz = 1024*1024*atoi(*++av);
205
fprintf (stderr, "-p requires port value\n");
219
/* at this point there are ac args in av[] to name our drivers */
223
/* take care of some unixisms */
227
/* realloc seed for client pool */
228
clinfo = (ClInfo *) malloc (1);
231
/* create driver info array all at once since size never changes */
233
dvrinfo = (DvrInfo *) calloc (ndvrinfo, sizeof(DvrInfo));
235
/* start each driver */
237
dvrinfo[ac].name = *av++;
238
startDvr (&dvrinfo[ac]);
241
/* announce we are online */
244
/* handle new clients and all io */
249
fprintf (stderr, "unexpected return from main\n");
253
/* record we have started and our args */
255
logStartup(int ac, char *av[])
259
fprintf (stderr, "%s: startup: ", indi_tstamp(NULL));
260
for (i = 0; i < ac; i++)
261
fprintf (stderr, "%s ", av[i]);
262
fprintf (stderr, "\n");
265
/* print usage message and exit (2) */
269
fprintf (stderr, "Usage: %s [options] driver [driver ...]\n", me);
270
fprintf (stderr, "Purpose: server for local and remote INDI drivers\n");
271
fprintf (stderr, "Code %s. Protocol %g.\n", "$Revision: 726523 $", INDIV);
272
fprintf (stderr, "Options:\n");
273
fprintf (stderr, " -l d : log driver messages to <d>/YYYY-MM-DD.islog\n");
274
fprintf (stderr, " -m m : kill client if gets more than this many MB behind, default %d\n", DEFMAXQSIZ);
275
fprintf (stderr, " -p p : alternate IP port, default %d\n", INDIPORT);
276
fprintf (stderr, " -v : show key events, no traffic\n");
277
fprintf (stderr, " -vv : -v + key message content\n");
278
fprintf (stderr, " -vvv : -vv + complete xml\n");
279
fprintf (stderr, "driver : executable or device@host[:port]\n");
284
/* arrange for no zombies if drivers die */
289
sa.sa_handler = SIG_IGN;
290
sigemptyset(&sa.sa_mask);
292
sa.sa_flags = SA_NOCLDWAIT;
296
(void)sigaction(SIGCHLD, &sa, NULL);
299
/* turn off SIGPIPE on bad write so we can handle it inline */
304
sa.sa_handler = SIG_IGN;
305
sigemptyset(&sa.sa_mask);
306
(void)sigaction(SIGPIPE, &sa, NULL);
309
/* start the given INDI driver process or connection.
313
startDvr (DvrInfo *dp)
315
if (strchr (dp->name, '@'))
321
/* start the given local INDI driver process.
325
startLocalDvr (DvrInfo *dp)
329
int rp[2], wp[2], ep[2];
332
/* build three pipes: r, w and error*/
334
fprintf (stderr, "%s: read pipe: %s\n", indi_tstamp(NULL),
339
fprintf (stderr, "%s: write pipe: %s\n", indi_tstamp(NULL),
344
fprintf (stderr, "%s: stderr pipe: %s\n", indi_tstamp(NULL),
349
/* fork&exec new process */
352
fprintf (stderr, "%s: fork: %s\n", indi_tstamp(NULL), strerror(errno));
356
/* child: exec name */
360
dup2 (wp[0], 0); /* driver stdin reads from wp[0] */
361
dup2 (rp[1], 1); /* driver stdout writes to rp[1] */
362
dup2 (ep[1], 2); /* driver stderr writes to e[]1] */
363
for (fd = 3; fd < 100; fd++)
366
/* go -- should never return */
367
execlp (dp->name, dp->name, NULL);
368
fprintf (stderr, "%s: Driver %s: execlp: %s\n", indi_tstamp(NULL),
369
dp->name, strerror(errno));
370
_exit (1); /* parent will notice EOF shortly */
373
/* don't need child's side of pipes */
378
/* record pid, io channels, init lp and snoop list */
383
dp->lp = newLilXML();
385
dp->sprops = (Snoopee*) malloc (1); /* seed for realloc */
389
/* first message primes driver to report its properties -- dev known
393
pushFQ (dp->msgq, mp);
395
sprintf (buf, "<getProperties device='%s' version='%g'/>\n",
398
sprintf (buf, "<getProperties version='%g'/>\n", INDIV);
403
fprintf (stderr, "%s: Driver %s: pid=%d rfd=%d wfd=%d efd=%d\n",
404
indi_tstamp(NULL), dp->name, dp->pid, dp->rfd, dp->wfd, dp->efd);
407
/* start the given remote INDI driver connection.
411
startRemoteDvr (DvrInfo *dp)
417
int indi_port, sockfd;
419
/* extract host and port */
420
indi_port = INDIPORT;
421
if (sscanf (dp->name, "%[^@]@%[^:]:%d", dev, host, &indi_port) < 2) {
422
fprintf (stderr, "Bad remote device syntax: %s\n", dp->name);
427
sockfd = openINDIServer (host, indi_port);
429
/* record flag pid, io channels, init lp and snoop list */
433
dp->lp = newLilXML();
435
dp->sprops = (Snoopee*) malloc (1); /* seed for realloc */
439
/* N.B. storing name now is key to limiting outbound traffic to this
442
strncpy (dp->dev, dev, MAXINDIDEVICE-1);
443
dp->dev[MAXINDIDEVICE-1] = '\0';
445
/* Sending getProperties with device lets remote server limit its
446
* outbound (and our inbound) traffic on this socket to this device.
449
pushFQ (dp->msgq, mp);
450
sprintf (buf, "<getProperties device='%s' version='%g'/>\n",
456
fprintf (stderr, "%s: Driver %s: socket=%d\n", indi_tstamp(NULL),
460
/* open a connection to the given host and port or die.
464
openINDIServer (char host[], int indi_port)
466
struct sockaddr_in serv_addr;
470
/* lookup host address */
471
hp = gethostbyname (host);
473
fprintf (stderr, "gethostbyname(%s): %s\n", host, strerror(errno));
477
/* create a socket to the INDI server */
478
(void) memset ((char *)&serv_addr, 0, sizeof(serv_addr));
479
serv_addr.sin_family = AF_INET;
480
serv_addr.sin_addr.s_addr =
481
((struct in_addr *)(hp->h_addr_list[0]))->s_addr;
482
serv_addr.sin_port = htons(indi_port);
483
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
484
fprintf (stderr, "socket(%s,%d): %s\n", host, indi_port,strerror(errno));
489
if (connect (sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0){
490
fprintf (stderr, "connect(%s,%d): %s\n", host,indi_port,strerror(errno));
498
/* create the public INDI Driver endpoint lsocket on port.
499
* return server socket else exit.
504
struct sockaddr_in serv_socket;
508
/* make socket endpoint */
509
if ((sfd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
510
fprintf (stderr, "%s: socket: %s\n", indi_tstamp(NULL), strerror(errno));
514
/* bind to given port for any IP address */
515
memset (&serv_socket, 0, sizeof(serv_socket));
516
serv_socket.sin_family = AF_INET;
518
serv_socket.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
520
serv_socket.sin_addr.s_addr = htonl (INADDR_ANY);
522
serv_socket.sin_port = htons ((unsigned short)port);
523
if (setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0){
524
fprintf (stderr, "%s: setsockopt: %s\n", indi_tstamp(NULL),
528
if (bind(sfd,(struct sockaddr*)&serv_socket,sizeof(serv_socket)) < 0){
529
fprintf (stderr, "%s: bind: %s\n", indi_tstamp(NULL), strerror(errno));
533
/* willing to accept connections with a backlog of 5 pending */
534
if (listen (sfd, 5) < 0) {
535
fprintf (stderr, "%s: listen: %s\n", indi_tstamp(NULL), strerror(errno));
542
fprintf (stderr, "%s: listening to port %d on fd %d\n",
543
indi_tstamp(NULL), port, sfd);
546
/* service traffic from clients and drivers */
554
/* init with no writers or readers */
558
/* always listen for new clients */
559
FD_SET(lsocket, &rs);
562
/* add all client readers and client writers with work to send */
563
for (i = 0; i < nclinfo; i++) {
564
ClInfo *cp = &clinfo[i];
567
if (nFQ(cp->msgq) > 0)
574
/* add all driver readers and driver writers with work to send */
575
for (i = 0; i < ndvrinfo; i++) {
576
DvrInfo *dp = &dvrinfo[i];
577
FD_SET(dp->rfd, &rs);
580
if (dp->pid != REMOTEDVR) {
581
FD_SET(dp->efd, &rs);
585
if (nFQ(dp->msgq) > 0) {
586
FD_SET(dp->wfd, &ws);
592
/* wait for action */
593
s = select (maxfd+1, &rs, &ws, NULL, NULL);
595
fprintf (stderr, "%s: select(%d): %s\n", indi_tstamp(NULL), maxfd+1,
601
if (s > 0 && FD_ISSET(lsocket, &rs)) {
606
/* message to/from client? */
607
for (i = 0; s > 0 && i < nclinfo; i++) {
608
ClInfo *cp = &clinfo[i];
610
if (FD_ISSET(cp->s, &rs)) {
611
if (readFromClient(cp) < 0)
612
return; /* fds effected */
615
if (s > 0 && FD_ISSET(cp->s, &ws)) {
616
if (sendClientMsg(cp) < 0)
617
return; /* fds effected */
623
/* message to/from driver? */
624
for (i = 0; s > 0 && i < ndvrinfo; i++) {
625
DvrInfo *dp = &dvrinfo[i];
626
if (dp->pid != REMOTEDVR && FD_ISSET(dp->efd, &rs)) {
627
if (stderrFromDriver(dp) < 0)
628
return; /* fds effected */
631
if (s > 0 && FD_ISSET(dp->rfd, &rs)) {
632
if (readFromDriver(dp) < 0)
633
return; /* fds effected */
636
if (s > 0 && FD_ISSET(dp->wfd, &ws) && nFQ(dp->msgq) > 0) {
637
if (sendDriverMsg(dp) < 0)
638
return; /* fds effected */
644
/* prepare for new client arriving on lsocket.
653
/* assign new socket */
656
/* try to reuse a clinfo slot, else add one */
657
for (cli = 0; cli < nclinfo; cli++)
658
if (!(cp = &clinfo[cli])->active)
660
if (cli == nclinfo) {
662
clinfo = (ClInfo *) realloc (clinfo, (nclinfo+1)*sizeof(ClInfo));
664
fprintf (stderr, "no memory for new client\n");
667
cp = &clinfo[nclinfo++];
670
/* rig up new clinfo entry */
671
memset (cp, 0, sizeof(*cp));
674
cp->lp = newLilXML();
676
cp->props = malloc (1);
680
struct sockaddr_in addr;
681
socklen_t len = sizeof(addr);
682
getpeername(s, (struct sockaddr*)&addr, &len);
683
fprintf(stderr,"%s: Client %d: new arrival from %s:%d - welcome!\n",
684
indi_tstamp(NULL), cp->s, inet_ntoa(addr.sin_addr),
685
ntohs(addr.sin_port));
689
/* read more from the given client, send to each appropriate driver when see
690
* xml closure. also send all newXXX() to all other interested clients.
691
* return -1 if had to shut down anything, else 0.
694
readFromClient (ClInfo *cp)
701
nr = read (cp->s, buf, sizeof(buf));
704
fprintf (stderr, "%s: Client %d: read: %s\n", indi_tstamp(NULL),
705
cp->s, strerror(errno));
706
else if (verbose > 0)
707
fprintf (stderr, "%s: Client %d: read EOF\n", indi_tstamp(NULL),
713
/* process XML, sending when find closure */
714
for (i = 0; i < nr; i++) {
716
XMLEle *root = readXMLEle (cp->lp, buf[i], err);
718
char *roottag = tagXMLEle(root);
719
const char *dev = findXMLAttValu (root, "device");
720
const char *name = findXMLAttValu (root, "name");
721
int isblob = !strcmp (tagXMLEle(root), "setBLOBVector");
725
fprintf (stderr, "%s: Client %d: read ",indi_tstamp(NULL),cp->s);
727
} else if (verbose > 1) {
728
fprintf (stderr, "%s: Client %d: read <%s device='%s' name='%s'>\n",
729
indi_tstamp(NULL), cp->s, tagXMLEle(root),
730
findXMLAttValu (root, "device"),
731
findXMLAttValu (root, "name"));
734
/* snag interested properties.
735
* N.B. don't open to alldevs if seen specific dev already, else
736
* remote client connections start returning too much.
739
addClDevice (cp, dev, name);
740
else if (!strcmp (roottag, "getProperties") && !cp->nprops)
743
/* snag enableBLOB -- send to remote drivers too */
744
if (!strcmp (roottag, "enableBLOB"))
745
crackBLOB (pcdataXMLEle(root), &cp->blob);
747
/* build a new message -- set content iff anyone cares */
750
/* send message to driver(s) responsible for dev */
751
q2RDrivers (dev, mp, root);
753
/* echo new* commands back to other clients */
754
if (!strncmp (roottag, "new", 3)) {
755
if (q2Clients (cp, isblob, dev, name, mp, root) < 0)
759
/* set message content if anyone cares else forget it */
761
setMsgXMLEle (mp, root);
767
char *ts = indi_tstamp(NULL);
768
fprintf (stderr, "%s: Client %d: XML error: %s\n", ts,
770
fprintf (stderr, "%s: Client %d: XML read: %.*s\n", ts,
777
return (shutany ? -1 : 0);
780
/* read more from the given driver, send to each interested client when see
781
* xml closure. if driver dies, try restarting.
782
* return 0 if ok else -1 if had to shut down anything.
785
readFromDriver (DvrInfo *dp)
792
nr = read (dp->rfd, buf, sizeof(buf));
795
fprintf (stderr, "%s: Driver %s: stdin %s\n", indi_tstamp(NULL),
796
dp->name, strerror(errno));
798
fprintf (stderr, "%s: Driver %s: stdin EOF\n",
799
indi_tstamp(NULL), dp->name);
804
/* process XML, sending when find closure */
805
for (i = 0; i < nr; i++) {
807
XMLEle *root = readXMLEle (dp->lp, buf[i], err);
809
char *roottag = tagXMLEle(root);
810
const char *dev = findXMLAttValu (root, "device");
811
const char *name = findXMLAttValu (root, "name");
812
int isblob = !strcmp (tagXMLEle(root), "setBLOBVector");
816
fprintf(stderr, "%s: Driver %s: read ", indi_tstamp(0),dp->name);
818
} else if (verbose > 1) {
819
fprintf (stderr, "%s: Driver %s: read <%s device='%s' name='%s'>\n",
820
indi_tstamp(NULL), dp->name, tagXMLEle(root),
821
findXMLAttValu (root, "device"),
822
findXMLAttValu (root, "name"));
825
/* that's all if driver is just registering a snoop */
826
if (!strcmp (roottag, "getProperties")) {
827
addSDevice (dp, dev, name);
832
/* that's all if driver is just registering a BLOB mode */
833
if (!strcmp (roottag, "enableBLOB")) {
834
Snoopee *sp = findSDevice (dp, dev, name);
836
crackBLOB (pcdataXMLEle (root), &sp->blob);
841
/* snag device name if not known yet */
842
if (!dp->dev[0] && dev[0]) {
843
strncpy (dp->dev, dev, MAXINDIDEVICE-1);
844
dp->dev[MAXINDIDEVICE-1] = '\0';
847
/* log messages if any and wanted */
851
/* build a new message -- set content iff anyone cares */
854
/* send to interested clients */
855
if (q2Clients (NULL, isblob, dev, name, mp, root) < 0)
858
/* send to snooping drivers */
859
q2SDrivers (isblob, dev, name, mp, root);
861
/* set message content if anyone cares else forget it */
863
setMsgXMLEle (mp, root);
869
char *ts = indi_tstamp(NULL);
870
fprintf (stderr, "%s: Driver %s: XML error: %s\n", ts,
872
fprintf (stderr, "%s: Driver %s: XML read: %.*s\n", ts,
879
return (shutany ? -1 : 0);
882
/* read more from the given driver stderr, add prefix and send to our stderr.
883
* return 0 if ok else -1 if had to restart.
886
stderrFromDriver (DvrInfo *dp)
888
static char exbuf[MAXRBUF];
893
nr = read (dp->efd, exbuf+nexbuf, sizeof(exbuf)-nexbuf);
896
fprintf (stderr, "%s: Driver %s: stderr %s\n", indi_tstamp(NULL),
897
dp->name, strerror(errno));
899
fprintf (stderr, "%s: Driver %s: stderr EOF\n",
900
indi_tstamp(NULL), dp->name);
906
/* prefix each whole line to our stderr, save extra for next time */
907
for (i = 0; i < nexbuf; i++) {
908
if (exbuf[i] == '\n') {
909
fprintf (stderr, "%s: Driver %s: %.*s\n", indi_tstamp(NULL),
911
i++; /* count including nl */
912
nexbuf -= i; /* remove from nexbuf */
913
memmove (exbuf, exbuf+i, nexbuf); /* slide remaining to front */
914
i = -1; /* restart for loop scan */
921
/* close down the given client */
923
shutdownClient (ClInfo *cp)
927
/* close connection */
928
shutdown (cp->s, SHUT_RDWR);
935
/* decrement and possibly free any unsent messages for this client */
936
while ((mp = (Msg*) popFQ(cp->msgq)) != NULL)
937
if (--mp->count == 0)
941
/* ok now to recycle */
945
fprintf (stderr, "%s: Client %d: shut down complete - bye!\n",
946
indi_tstamp(NULL), cp->s);
949
/* close down the given driver and restart */
951
restartDvr (DvrInfo *dp)
955
/* make sure it's dead, reclaim resources */
956
if (dp->pid == REMOTEDVR) {
957
/* socket connection */
958
shutdown (dp->wfd, SHUT_RDWR);
959
close (dp->wfd); /* same as rfd */
961
/* local pipe connection */
962
kill (dp->pid, SIGKILL); /* we've insured there are no zombies */
972
/* decrement and possibly free any unsent messages for this client */
973
while ((mp = (Msg*) popFQ(dp->msgq)) != NULL)
974
if (--mp->count == 0)
978
fprintf (stderr, "%s: Driver %s: restart #%d\n", indi_tstamp(NULL),
979
dp->name, ++dp->restarts);
983
/* put Msg mp on queue of each driver responsible for dev, or all drivers
984
* if dev not specified.
987
q2RDrivers (const char *dev, Msg *mp, XMLEle *root)
992
/* queue message to each interested driver.
993
* N.B. don't send generic getProps to more than one remote driver,
994
* otherwise they all fan out and we get multiple responses back.
996
for (dp = dvrinfo; dp < &dvrinfo[ndvrinfo]; dp++) {
997
int isremote = (dp->pid == REMOTEDVR);
998
if (dev[0] && dp->dev[0] && strcmp (dev, dp->dev))
999
continue; /* driver known to not support this dev */
1000
if (!dev[0] && isremote && sawremote)
1001
continue; /* already sent generic to another remote */
1005
/* ok: queue message to this driver */
1007
pushFQ (dp->msgq, mp);
1009
fprintf (stderr, "%s: Driver %s: queuing responsible for <%s device='%s' name='%s'>\n",
1010
indi_tstamp(NULL), dp->name, tagXMLEle(root),
1011
findXMLAttValu (root, "device"),
1012
findXMLAttValu (root, "name"));
1016
/* put Msg mp on queue of each driver snooping dev/name.
1017
* if BLOB always honor current mode.
1020
q2SDrivers (int isblob, const char *dev, const char *name, Msg *mp, XMLEle *root)
1024
for (dp = dvrinfo; dp < &dvrinfo[ndvrinfo]; dp++) {
1025
Snoopee *sp = findSDevice (dp, dev, name);
1027
/* nothing for dp if not snooping for dev/name or wrong BLOB mode */
1030
if ((isblob && sp->blob==B_NEVER) || (!isblob && sp->blob==B_ONLY))
1033
/* ok: queue message to this device */
1035
pushFQ (dp->msgq, mp);
1037
fprintf (stderr, "%s: Driver %s: queuing snooped <%s device='%s' name='%s'>\n",
1038
indi_tstamp(NULL), dp->name, tagXMLEle(root),
1039
findXMLAttValu (root, "device"),
1040
findXMLAttValu (root, "name"));
1045
/* add dev/name to dp's snooping list.
1046
* init with blob mode set to B_NEVER.
1049
addSDevice (DvrInfo *dp, const char *dev, const char *name)
1055
sp = findSDevice (dp, dev, name);
1059
/* add dev to sdevs list */
1060
dp->sprops = (Snoopee*) realloc (dp->sprops,
1061
(dp->nsprops+1)*sizeof(Snoopee));
1062
sp = &dp->sprops[dp->nsprops++];
1065
strncpy (ip, dev, MAXINDIDEVICE-1);
1066
ip[MAXINDIDEVICE-1] = '\0';
1069
strncpy (ip, name, MAXINDINAME-1);
1070
ip[MAXINDINAME-1] = '\0';
1075
fprintf (stderr, "%s: Driver %s: snooping on %s.%s\n", indi_tstamp(NULL),
1076
dp->name, dev, name);
1079
/* return Snoopee if dp is snooping dev/name, else NULL.
1082
findSDevice (DvrInfo *dp, const char *dev, const char *name)
1086
for (i = 0; i < dp->nsprops; i++) {
1087
Snoopee *sp = &dp->sprops[i];
1088
Property *pp = &sp->prop;
1089
if (!strcmp (pp->dev, dev) &&
1090
(!pp->name[0] || !strcmp(pp->name, name)))
1097
/* put Msg mp on queue of each client interested in dev/name, except notme.
1098
* if BLOB always honor current mode.
1099
* return -1 if had to shut down any clients, else 0.
1102
q2Clients (ClInfo *notme, int isblob, const char *dev, const char *name, Msg *mp, XMLEle *root)
1108
/* queue message to each interested client */
1109
for (cp = clinfo; cp < &clinfo[nclinfo]; cp++) {
1110
/* cp in use? notme? want this dev/name? blob? */
1111
if (!cp->active || cp == notme)
1113
if (findClDevice (cp, dev, name) < 0)
1115
if ((isblob && cp->blob==B_NEVER) || (!isblob && cp->blob==B_ONLY))
1118
/* shut down this client if its q is already too large */
1119
ql = msgQSize(cp->msgq);
1122
fprintf (stderr, "%s: Client %d: %d bytes behind, shutting down\n",
1123
indi_tstamp(NULL), cp->s, ql);
1124
shutdownClient (cp);
1129
/* ok: queue message to this client */
1131
pushFQ (cp->msgq, mp);
1133
fprintf (stderr, "%s: Client %d: queuing <%s device='%s' name='%s'>\n",
1134
indi_tstamp(NULL), cp->s, tagXMLEle(root),
1135
findXMLAttValu (root, "device"),
1136
findXMLAttValu (root, "name"));
1139
return (shutany ? -1 : 0);
1142
/* return size of all Msqs on the given q */
1148
for (i = 0; i < nFQ(q); i++) {
1149
Msg *mp = (Msg *) peekiFQ(q,i);
1156
/* print root as content in Msg mp.
1159
setMsgXMLEle (Msg *mp, XMLEle *root)
1161
/* want cl to only count content, but need room for final \0 */
1162
mp->cl = sprlXMLEle (root, 0);
1163
if (mp->cl < sizeof(mp->buf))
1166
mp->cp = malloc (mp->cl+1);
1167
sprXMLEle (mp->cp, root, 0);
1170
/* save str as content in Msg mp.
1173
setMsgStr (Msg *mp, char *str)
1175
/* want cl to only count content, but need room for final \0 */
1176
mp->cl = strlen (str);
1177
if (mp->cl < sizeof(mp->buf))
1180
mp->cp = malloc (mp->cl+1);
1181
strcpy (mp->cp, str);
1184
/* return pointer to one new nulled Msg
1189
return ((Msg *) calloc (1, sizeof(Msg)));
1192
/* free Msg mp and everything it contains */
1196
if (mp->cp && mp->cp != mp->buf)
1201
/* write the next chunk of the current message in the queue to the given
1202
* client. pop message from queue when complete and free the message if we are
1203
* the last one to use it. shut down this client if trouble.
1204
* N.B. we assume we will never be called with cp->msgq empty.
1205
* return 0 if ok else -1 if had to shut down.
1208
sendClientMsg (ClInfo *cp)
1213
/* get current message */
1214
mp = (Msg *) peekFQ (cp->msgq);
1216
/* send next chunk, never more than MAXWSIZ to reduce blocking */
1217
nsend = mp->cl - cp->nsent;
1218
if (nsend > MAXWSIZ)
1220
nw = write (cp->s, &mp->cp[cp->nsent], nsend);
1222
/* shut down if trouble */
1225
fprintf (stderr, "%s: Client %d: write returned 0\n",
1226
indi_tstamp(NULL), cp->s);
1228
fprintf (stderr, "%s: Client %d: write: %s\n", indi_tstamp(NULL),
1229
cp->s, strerror(errno));
1230
shutdownClient (cp);
1236
fprintf(stderr, "%s: Client %d: sending msg copy %d nq %d:\n%.*s\n",
1237
indi_tstamp(NULL), cp->s, mp->count, nFQ(cp->msgq),
1238
nw, &mp->cp[cp->nsent]);
1239
} else if (verbose > 1) {
1240
fprintf(stderr, "%s: Client %d: sending %.50s\n", indi_tstamp(NULL),
1241
cp->s, &mp->cp[cp->nsent]);
1244
/* update amount sent. when complete: free message if we are the last
1245
* to use it and pop from our queue.
1248
if (cp->nsent == mp->cl) {
1249
if (--mp->count == 0)
1258
/* write the next chunk of the current message in the queue to the given
1259
* driver. pop message from queue when complete and free the message if we are
1260
* the last one to use it. restart this driver if touble.
1261
* N.B. we assume we will never be called with dp->msgq empty.
1262
* return 0 if ok else -1 if had to shut down.
1265
sendDriverMsg (DvrInfo *dp)
1270
/* get current message */
1271
mp = (Msg *) peekFQ (dp->msgq);
1273
/* send next chunk, never more than MAXWSIZ to reduce blocking */
1274
nsend = mp->cl - dp->nsent;
1275
if (nsend > MAXWSIZ)
1277
nw = write (dp->wfd, &mp->cp[dp->nsent], nsend);
1279
/* restart if trouble */
1282
fprintf (stderr, "%s: Driver %s: write returned 0\n",
1283
indi_tstamp(NULL), dp->name);
1285
fprintf (stderr, "%s: Driver %s: write: %s\n", indi_tstamp(NULL),
1286
dp->name, strerror(errno));
1293
fprintf(stderr, "%s: Driver %s: sending msg copy %d nq %d:\n%.*s\n",
1294
indi_tstamp(NULL), dp->name, mp->count, nFQ(dp->msgq),
1295
nw, &mp->cp[dp->nsent]);
1296
} else if (verbose > 1) {
1297
fprintf(stderr, "%s: Driver %s: sending %.50s\n", indi_tstamp(NULL),
1298
dp->name, &mp->cp[dp->nsent]);
1301
/* update amount sent. when complete: free message if we are the last
1302
* to use it and pop from our queue.
1305
if (dp->nsent == mp->cl) {
1306
if (--mp->count == 0)
1315
/* return 0 if cp may be interested in dev/name else -1
1318
findClDevice (ClInfo *cp, const char *dev, const char *name)
1322
if (cp->allprops || !dev[0])
1324
for (i = 0; i < cp->nprops; i++) {
1325
Property *pp = &cp->props[i];
1326
if (!strcmp (pp->dev, dev) &&
1327
(!pp->name[0] || !strcmp(pp->name, name)))
1333
/* add the given device and property to the devs[] list of client if new.
1336
addClDevice (ClInfo *cp, const char *dev, const char *name)
1342
if (!findClDevice (cp, dev, name))
1346
cp->props = (Property *) realloc (cp->props,
1347
(cp->nprops+1)*sizeof(Property));
1348
pp = &cp->props[cp->nprops++];
1351
strncpy (ip, dev, MAXINDIDEVICE-1);
1352
ip[MAXINDIDEVICE-1] = '\0';
1355
strncpy (ip, name, MAXINDINAME-1);
1356
ip[MAXINDINAME-1] = '\0';
1360
/* block to accept a new client arriving on lsocket.
1361
* return private nonblocking socket or exit.
1366
struct sockaddr_in cli_socket;
1370
/* get a private connection to new client */
1371
cli_len = sizeof(cli_socket);
1372
cli_fd = accept (lsocket, (struct sockaddr *)&cli_socket, &cli_len);
1374
fprintf (stderr, "accept: %s\n", strerror(errno));
1382
/* convert the string value of enableBLOB to our B_ state value.
1383
* no change if unrecognized
1386
crackBLOB (char *enableBLOB, BLOBHandling *bp)
1388
if (!strcmp (enableBLOB, "Also"))
1390
else if (!strcmp (enableBLOB, "Only"))
1392
else if (!strcmp (enableBLOB, "Never"))
1396
/* print key attributes and values of the given xml to stderr.
1399
traceMsg (XMLEle *root)
1401
static const char *prtags[] = {
1402
"defNumber", "oneNumber",
1403
"defText", "oneText",
1404
"defSwitch", "oneSwitch",
1405
"defLight", "oneLight",
1408
const char *msg, *perm, *pcd;
1411
/* print tag header */
1412
fprintf (stderr, "%s %s %s %s", tagXMLEle(root),
1413
findXMLAttValu(root,"device"),
1414
findXMLAttValu(root,"name"),
1415
findXMLAttValu(root,"state"));
1416
pcd = pcdataXMLEle (root);
1418
fprintf (stderr, " %s", pcd);
1419
perm = findXMLAttValu(root,"perm");
1421
fprintf (stderr, " %s", perm);
1422
msg = findXMLAttValu(root,"message");
1424
fprintf (stderr, " '%s'", msg);
1426
/* print each array value */
1427
for (e = nextXMLEle(root,1); e; e = nextXMLEle(root,0))
1428
for (i = 0; i < sizeof(prtags)/sizeof(prtags[0]); i++)
1429
if (strcmp (prtags[i], tagXMLEle(e)) == 0)
1430
fprintf (stderr, "\n %10s='%s'", findXMLAttValu(e,"name"),
1433
fprintf (stderr, "\n");
1436
/* fill s with current UT string.
1437
* if no s, use a static buffer
1438
* return s or buffer.
1439
* N.B. if use our buffer, be sure to use before calling again
1442
indi_tstamp (char *s)
1444
static char sbuf[64];
1452
strftime (s, sizeof(sbuf), "%Y-%m-%dT%H:%M:%S", tp);
1456
/* log message in root known to be from device dev to ldir, if any.
1459
logDMsg (XMLEle *root, const char *dev)
1463
const char *ts, *ms;
1466
/* get message, if any */
1467
ms = findXMLAttValu (root, "message");
1471
/* get timestamp now if not provided */
1472
ts = findXMLAttValu (root, "timestamp");
1475
indi_tstamp (stamp);
1479
/* append to log file, name is date portion of time stamp */
1480
sprintf (logfn, "%s/%.10s.islog", ldir, ts);
1481
fp = fopen (logfn, "a");
1483
return; /* oh well */
1484
fprintf (fp, "%s: %s: %s\n", ts, dev, ms);
1488
/* log when then exit */
1492
fprintf (stderr, "%s: good bye\n", indi_tstamp(NULL));