xref: /original-bsd/usr.bin/uucp/libuu/systat.c (revision 5bcf8549)
1 /*-
2  * Copyright (c) 1985, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)systat.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "uucp.h"
13 
14 #define STATNAME(f, n) sprintf(f, "%s/%s/%s", Spool, "STST", n)
15 #define S_SIZE 100
16 
17 /*LINTLIBRARY*/
18 
19 /*
20  *	make system status entry
21  *	return codes:  none
22  */
23 systat(name, type, text)
24 char *name, *text;
25 int type;
26 {
27 	char filename[MAXFULLNAME], line[S_SIZE];
28 	int count, oldtype;
29 	register FILE *fp;
30 	time_t prestime, rtry;
31 
32 	if (type == 0)
33 		return;
34 	line[0] = '\0';
35 	time(&prestime);
36 	count = 0;
37 	STATNAME(filename, name);
38 
39 	fp = fopen(filename, "r");
40 	if (fp != NULL) {
41 		fgets(line, S_SIZE, fp);
42 		sscanf(line, "%d %d", &oldtype, &count);
43 		if (count <= 0)
44 			count = 0;
45 		fclose(fp);
46 		/* If merely 'wrong time', don't change existing STST */
47 		if (type == SS_WRONGTIME && oldtype != SS_INPROGRESS)
48 			return;
49 	}
50 
51 	rtry = Retrytime;
52 	/* if failures repeat, don't try so often,
53 	 * to forstall a 'MAX RECALLS' situation.
54 	 */
55 	if (type == SS_FAIL) {
56 		count++;
57 		if (count > 5) {
58 			rtry = rtry * (count-5);
59 			if (rtry > ONEDAY/2)
60 				rtry = ONEDAY/2;
61 		}
62 	}
63 
64 
65 #ifdef VMS
66 	unlink(filename);
67 #endif VMS
68 	fp = fopen(filename, "w");
69 	if (fp == NULL) {
70 		syslog(LOG_ERR, "fopen(%s) failed: %m", filename);
71 		cleanup(FAIL);
72 	}
73 	fprintf(fp, "%d %d %ld %ld %s %s\n", type, count, prestime, rtry, text, name);
74 	fclose(fp);
75 }
76 
77 /*
78  *	remove system status entry
79  *
80  *	return codes:  none
81  */
82 rmstat(name)
83 char *name;
84 {
85 	char filename[MAXFULLNAME];
86 
87 	STATNAME(filename, name);
88 	unlink(filename);
89 }
90 
91 /*
92  *	check system status for call
93  *
94  *	return codes  0 - ok | >0 system status
95  */
96 callok(name)
97 char *name;
98 {
99 	char filename[MAXFULLNAME], line[S_SIZE];
100 	register FILE *fp;
101 	time_t lasttime, prestime, retrytime;
102 	long t1, t2;
103 	int count, type;
104 
105 	STATNAME(filename, name);
106 	fp = fopen(filename, "r");
107 	if (fp == NULL)
108 		return(SS_OK);
109 
110 	if (fgets(line, S_SIZE, fp) == NULL) {
111 		/*  no data  */
112 		fclose(fp);
113 		unlink(filename);
114 		return(SS_OK);
115 	}
116 
117 	fclose(fp);
118 	time(&prestime);
119 	sscanf(line, "%d%d%ld%ld", &type, &count, &t1, &t2);
120 	lasttime = t1;
121 	retrytime = t2;
122 
123 	switch(type) {
124 	case SS_BADSEQ:
125 	case SS_CALLBACK:
126 	case SS_NODEVICE:
127 	case SS_INPROGRESS:	/*let LCK take care of it */
128 		return(SS_OK);
129 
130 	case SS_FAIL:
131 		if (count > MAXRECALLS) {
132 			logent("MAX RECALLS", "NO CALL");
133 			DEBUG(4, "MAX RECALL COUNT %d\n", count);
134 			if (Debug) {
135 				logent("debugging", "continuing anyway");
136 				return SS_OK;
137 			}
138 			return type;
139 		}
140 
141 		if (prestime - lasttime < retrytime) {
142 			logent("RETRY TIME NOT REACHED", "NO CALL");
143 			DEBUG(4, "RETRY TIME (%ld) NOT REACHED\n", retrytime);
144 			if (Debug) {
145 				logent("debugging", "continuing anyway");
146 				return SS_OK;
147 			}
148 			return type;
149 		}
150 
151 		return SS_OK;
152 	default:
153 		return SS_OK;
154 	}
155 }
156