Initial import from FreeBSD RELENG_4:
[dragonfly.git] / libexec / xtend / status.c
CommitLineData
984263bc
MD
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
32#ifndef lint
33static const char rcsid[] =
34 "$FreeBSD: src/libexec/xtend/status.c,v 1.8 1999/08/28 00:10:30 peter Exp $";
35#endif /* not lint */
36
37#include <stdio.h>
38#include <unistd.h>
39#include <sys/param.h>
40#include <sys/stat.h>
41#include "xtend.h"
42#include "xten.h"
43#include "paths.h"
44
45void printstatus __P((FILE *, STATUS *));
46
47/*
48 * Initialize the status table from the status files
49 */
50
51void
52initstatus()
53{
54 if(lseek(status, 0, SEEK_SET) != 0) {
55 fprintf(Log, "%s: Seek error on status file\n", thedate());
56 return;
57 }
58 if(read(status, Status, 16*16*sizeof(STATUS)) != 16*16*sizeof(STATUS)) {
59 fprintf(Log, "%s: Read error on status file\n", thedate());
60 return;
61 }
62}
63
64/*
65 * Checkpoint status of any devices whose status has changed
66 * and notify anyone monitoring those devices.
67 */
68
69void
70checkpoint_status()
71{
72 int h, i, k, offset;
73
74 offset = 0;
75 for(h = 0; h < 16; h++) {
76 for(i = 0; i < 16; i++) {
77 if(Status[h][i].changed) {
78 if(lseek(status, offset, SEEK_SET) != offset) {
79 fprintf(Log, "%s: Seek error on status file\n", thedate());
80 } else {
81 if(write(status, &Status[h][i], sizeof(STATUS)) != sizeof(STATUS)) {
82 fprintf(Log, "%s: Write error on status file\n", thedate());
83 }
84 }
85 Status[h][i].changed = 0;
86 for(k = 0; k < MAXMON; k++) {
87 if(Monitor[k].inuse
88 && Monitor[k].house == h && Monitor[k].unit == i) {
89 /*
90 * Arrange to catch SIGPIPE in case client has gone away.
91 */
92 extern int client;
93 extern void clientgone();
94 void (*prev)();
95
96 client = k;
97 prev = signal(SIGPIPE, clientgone);
98 printstatus(Monitor[k].user, &Status[h][i]);
99 fflush(Monitor[k].user);
100 signal(SIGPIPE, prev);
101 }
102 }
103 }
104 offset += sizeof(STATUS);
105 }
106 }
107}
108
109int client;
110
111void clientgone()
112{
113 fprintf(Log, "%s: Deleting monitor table entry %d, client gone\n", thedate(), client);
114 fclose(Monitor[client].user);
115 Monitor[client].inuse = 0;
116}