Merge branch 'vendor/TCPDUMP' (early part)
[dragonfly.git] / libexec / xtend / status.c
1 /*-
2  * Copyright (c) 1992, 1993, 1995 Eugene W. Stark
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Eugene W. Stark.
16  * 4. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY EUGENE W. STARK (THE AUTHOR) ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD: src/libexec/xtend/status.c,v 1.8 1999/08/28 00:10:30 peter Exp $
32  * $DragonFly: src/libexec/xtend/status.c,v 1.3 2003/11/14 03:54:32 dillon Exp $
33  */
34
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <sys/param.h>
38 #include <sys/stat.h>
39 #include "xtend.h"
40 #include "xten.h"
41 #include "paths.h"
42
43 void printstatus (FILE *, STATUS *);
44
45 /*
46  * Initialize the status table from the status files
47  */
48
49 void
50 initstatus()
51 {
52   if(lseek(status, 0, SEEK_SET) != 0) {
53     fprintf(Log, "%s:  Seek error on status file\n", thedate());
54     return;
55   }
56   if(read(status, Status, 16*16*sizeof(STATUS)) != 16*16*sizeof(STATUS)) {
57         fprintf(Log, "%s:  Read error on status file\n", thedate());
58         return;
59   }
60 }
61
62 /*
63  * Checkpoint status of any devices whose status has changed
64  * and notify anyone monitoring those devices.
65  */
66
67 void
68 checkpoint_status()
69 {
70   int h, i, k, offset;
71
72   offset = 0;
73   for(h = 0; h < 16; h++) {
74     for(i = 0; i < 16; i++) {
75       if(Status[h][i].changed) {
76         if(lseek(status, offset, SEEK_SET) != offset) {
77           fprintf(Log, "%s:  Seek error on status file\n", thedate());
78         } else {
79           if(write(status, &Status[h][i], sizeof(STATUS)) != sizeof(STATUS)) {
80             fprintf(Log, "%s:  Write error on status file\n", thedate());
81           }
82         }
83         Status[h][i].changed = 0;
84         for(k = 0; k < MAXMON; k++) {
85           if(Monitor[k].inuse
86              && Monitor[k].house == h && Monitor[k].unit == i) {
87             /*
88              * Arrange to catch SIGPIPE in case client has gone away.
89              */
90             extern int client;
91             extern void clientgone();
92             void (*prev)();
93
94             client = k;
95             prev = signal(SIGPIPE, clientgone);
96             printstatus(Monitor[k].user, &Status[h][i]);
97             fflush(Monitor[k].user);
98             signal(SIGPIPE, prev);
99           }
100         }
101       }
102       offset += sizeof(STATUS);
103     }
104   }
105 }
106
107 int client;
108
109 void clientgone()
110 {
111     fprintf(Log, "%s:  Deleting monitor table entry %d, client gone\n", thedate(), client);
112     fclose(Monitor[client].user);
113     Monitor[client].inuse = 0;
114 }