xref: /freebsd/sbin/dump/optr.c (revision 32e86a82f54826f14ea381affa6674db3aa3b5ae)
1 8fae3551SRodney W. Grimes /*-
2 *8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3 *8a16b7a1SPedro F. Giffuni  *
4 8fae3551SRodney W. Grimes  * Copyright (c) 1980, 1988, 1993
5 8fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6 8fae3551SRodney W. Grimes  *
7 8fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8 8fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
9 8fae3551SRodney W. Grimes  * are met:
10 8fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11 8fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12 8fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13 8fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14 8fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15 fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
16 8fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17 8fae3551SRodney W. Grimes  *    without specific prior written permission.
18 8fae3551SRodney W. Grimes  *
19 8fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 8fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 8fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 8fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 8fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 8fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 8fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 8fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 8fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 8fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 8fae3551SRodney W. Grimes  * SUCH DAMAGE.
30 8fae3551SRodney W. Grimes  */
31 8fae3551SRodney W. Grimes 
32 8fae3551SRodney W. Grimes #include <sys/param.h>
33 941ee632SPoul-Henning Kamp #include <sys/queue.h>
34 8fae3551SRodney W. Grimes #include <sys/wait.h>
35 8fae3551SRodney W. Grimes 
36 1c85e6a3SKirk McKusick #include <ufs/ufs/dinode.h>
37 1c85e6a3SKirk McKusick 
38 8fae3551SRodney W. Grimes #include <errno.h>
39 8fae3551SRodney W. Grimes #include <fstab.h>
40 8fae3551SRodney W. Grimes #include <grp.h>
41 89fdc4e1SMike Barcroft #include <limits.h>
42 8fae3551SRodney W. Grimes #include <stdio.h>
43 8fae3551SRodney W. Grimes #include <stdlib.h>
44 8fae3551SRodney W. Grimes #include <string.h>
45 8fae3551SRodney W. Grimes #include <stdarg.h>
46 19f8080eSIan Dowse #include <signal.h>
47 7649cb00SKirk McKusick #include <time.h>
48 8fae3551SRodney W. Grimes #include <unistd.h>
49 8fae3551SRodney W. Grimes 
50 8fae3551SRodney W. Grimes #include "dump.h"
51 8fae3551SRodney W. Grimes #include "pathnames.h"
52 8fae3551SRodney W. Grimes 
53 2db673abSWarner Losh void	alarmcatch(int);
54 2db673abSWarner Losh int	datesort(const void *, const void *);
55 8fae3551SRodney W. Grimes 
56 8fae3551SRodney W. Grimes /*
57 8fae3551SRodney W. Grimes  *	Query the operator; This previously-fascist piece of code
58 8fae3551SRodney W. Grimes  *	no longer requires an exact response.
59 8fae3551SRodney W. Grimes  *	It is intended to protect dump aborting by inquisitive
60 8fae3551SRodney W. Grimes  *	people banging on the console terminal to see what is
61 8fae3551SRodney W. Grimes  *	happening which might cause dump to croak, destroying
62 8fae3551SRodney W. Grimes  *	a large number of hours of work.
63 8fae3551SRodney W. Grimes  *
64 8fae3551SRodney W. Grimes  *	Every 2 minutes we reprint the message, alerting others
65 8fae3551SRodney W. Grimes  *	that dump needs attention.
66 8fae3551SRodney W. Grimes  */
67 8fae3551SRodney W. Grimes static	int timeout;
68 2db673abSWarner Losh static	const char *attnmessage;		/* attention message */
69 8fae3551SRodney W. Grimes 
70 8fae3551SRodney W. Grimes int
query(const char * question)71 2db673abSWarner Losh query(const char *question)
72 8fae3551SRodney W. Grimes {
73 8fae3551SRodney W. Grimes 	char	replybuffer[64];
74 8fae3551SRodney W. Grimes 	int	back, errcount;
75 8fae3551SRodney W. Grimes 	FILE	*mytty;
76 8fae3551SRodney W. Grimes 
77 8fae3551SRodney W. Grimes 	if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
78 8fae3551SRodney W. Grimes 		quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno));
79 8fae3551SRodney W. Grimes 	attnmessage = question;
80 8fae3551SRodney W. Grimes 	timeout = 0;
81 2db673abSWarner Losh 	alarmcatch(0);
82 8fae3551SRodney W. Grimes 	back = -1;
83 8fae3551SRodney W. Grimes 	errcount = 0;
84 8fae3551SRodney W. Grimes 	do {
85 8fae3551SRodney W. Grimes 		if (fgets(replybuffer, 63, mytty) == NULL) {
86 8fae3551SRodney W. Grimes 			clearerr(mytty);
87 8fae3551SRodney W. Grimes 			if (++errcount > 30)	/* XXX	ugly */
88 8fae3551SRodney W. Grimes 				quit("excessive operator query failures\n");
89 8fae3551SRodney W. Grimes 		} else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
90 8fae3551SRodney W. Grimes 			back = 1;
91 8fae3551SRodney W. Grimes 		} else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
92 8fae3551SRodney W. Grimes 			back = 0;
93 8fae3551SRodney W. Grimes 		} else {
94 8fae3551SRodney W. Grimes 			(void) fprintf(stderr,
95 8fae3551SRodney W. Grimes 			    "  DUMP: \"Yes\" or \"No\"?\n");
96 8fae3551SRodney W. Grimes 			(void) fprintf(stderr,
97 8fae3551SRodney W. Grimes 			    "  DUMP: %s: (\"yes\" or \"no\") ", question);
98 8fae3551SRodney W. Grimes 		}
99 8fae3551SRodney W. Grimes 	} while (back < 0);
100 8fae3551SRodney W. Grimes 
101 8fae3551SRodney W. Grimes 	/*
102 8fae3551SRodney W. Grimes 	 *	Turn off the alarm, and reset the signal to trap out..
103 8fae3551SRodney W. Grimes 	 */
104 8fae3551SRodney W. Grimes 	(void) alarm(0);
105 8fae3551SRodney W. Grimes 	if (signal(SIGALRM, sig) == SIG_IGN)
106 8fae3551SRodney W. Grimes 		signal(SIGALRM, SIG_IGN);
107 8fae3551SRodney W. Grimes 	(void) fclose(mytty);
108 8fae3551SRodney W. Grimes 	return(back);
109 8fae3551SRodney W. Grimes }
110 8fae3551SRodney W. Grimes 
111 4a315a9aSRuslan Ermilov char lastmsg[BUFSIZ];
112 8fae3551SRodney W. Grimes 
113 8fae3551SRodney W. Grimes /*
114 8fae3551SRodney W. Grimes  *	Alert the console operator, and enable the alarm clock to
115 8fae3551SRodney W. Grimes  *	sleep for 2 minutes in case nobody comes to satisfy dump
116 8fae3551SRodney W. Grimes  */
117 8fae3551SRodney W. Grimes void
alarmcatch(int sig __unused)118 2db673abSWarner Losh alarmcatch(int sig __unused)
119 8fae3551SRodney W. Grimes {
120 8fae3551SRodney W. Grimes 	if (notify == 0) {
121 8fae3551SRodney W. Grimes 		if (timeout == 0)
122 8fae3551SRodney W. Grimes 			(void) fprintf(stderr,
123 8fae3551SRodney W. Grimes 			    "  DUMP: %s: (\"yes\" or \"no\") ",
124 8fae3551SRodney W. Grimes 			    attnmessage);
125 8fae3551SRodney W. Grimes 		else
126 bfa0b298SPoul-Henning Kamp 			msgtail("\a\a");
127 8fae3551SRodney W. Grimes 	} else {
128 8fae3551SRodney W. Grimes 		if (timeout) {
129 8fae3551SRodney W. Grimes 			msgtail("\n");
130 8fae3551SRodney W. Grimes 			broadcast("");		/* just print last msg */
131 8fae3551SRodney W. Grimes 		}
132 8fae3551SRodney W. Grimes 		(void) fprintf(stderr,"  DUMP: %s: (\"yes\" or \"no\") ",
133 8fae3551SRodney W. Grimes 		    attnmessage);
134 8fae3551SRodney W. Grimes 	}
135 8fae3551SRodney W. Grimes 	signal(SIGALRM, alarmcatch);
136 8fae3551SRodney W. Grimes 	(void) alarm(120);
137 8fae3551SRodney W. Grimes 	timeout = 1;
138 8fae3551SRodney W. Grimes }
139 8fae3551SRodney W. Grimes 
140 8fae3551SRodney W. Grimes /*
141 8fae3551SRodney W. Grimes  *	Here if an inquisitive operator interrupts the dump program
142 8fae3551SRodney W. Grimes  */
143 8fae3551SRodney W. Grimes void
interrupt(int signo __unused)144 2db673abSWarner Losh interrupt(int signo __unused)
145 8fae3551SRodney W. Grimes {
146 8fae3551SRodney W. Grimes 	msg("Interrupt received.\n");
147 8fae3551SRodney W. Grimes 	if (query("Do you want to abort dump?"))
148 8fae3551SRodney W. Grimes 		dumpabort(0);
149 8fae3551SRodney W. Grimes }
150 8fae3551SRodney W. Grimes 
151 8fae3551SRodney W. Grimes /*
152 4a315a9aSRuslan Ermilov  *	We now use wall(1) to do the actual broadcasting.
153 8fae3551SRodney W. Grimes  */
154 8fae3551SRodney W. Grimes void
broadcast(const char * message)155 2db673abSWarner Losh broadcast(const char *message)
156 8fae3551SRodney W. Grimes {
157 4a315a9aSRuslan Ermilov 	FILE *fp;
158 4a315a9aSRuslan Ermilov 	char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3];
159 8fae3551SRodney W. Grimes 
160 4a315a9aSRuslan Ermilov 	if (!notify)
161 8fae3551SRodney W. Grimes 		return;
162 8fae3551SRodney W. Grimes 
163 4a315a9aSRuslan Ermilov 	snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT);
164 4a315a9aSRuslan Ermilov 	if ((fp = popen(buf, "w")) == NULL)
165 8fae3551SRodney W. Grimes 		return;
166 8fae3551SRodney W. Grimes 
167 4a315a9aSRuslan Ermilov 	(void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp);
168 4a315a9aSRuslan Ermilov 	if (lastmsg[0])
169 4a315a9aSRuslan Ermilov 		(void) fputs(lastmsg, fp);
170 4a315a9aSRuslan Ermilov 	if (message[0])
171 4a315a9aSRuslan Ermilov 		(void) fputs(message, fp);
172 8fae3551SRodney W. Grimes 
173 4a315a9aSRuslan Ermilov 	(void) pclose(fp);
174 8fae3551SRodney W. Grimes }
175 8fae3551SRodney W. Grimes 
176 8fae3551SRodney W. Grimes /*
177 4a315a9aSRuslan Ermilov  *	Print out an estimate of the amount of time left to do the dump
178 8fae3551SRodney W. Grimes  */
179 8fae3551SRodney W. Grimes 
180 8fae3551SRodney W. Grimes time_t	tschedule = 0;
181 8fae3551SRodney W. Grimes 
182 8fae3551SRodney W. Grimes void
timeest(void)183 2db673abSWarner Losh timeest(void)
184 8fae3551SRodney W. Grimes {
185 2bb823d2SIan Dowse 	double percent;
186 bcfe1715SWarner Losh 	time_t	tnow, tdone;
187 452329adSRalf S. Engelschall 	char *tdone_str;
188 2bb823d2SIan Dowse 	int deltat, hours, mins;
189 8fae3551SRodney W. Grimes 
190 b2fb1832SIan Dowse 	(void)time(&tnow);
191 bcfe1715SWarner Losh 	if (blockswritten > tapesize) {
192 bcfe1715SWarner Losh 		setproctitle("%s: 99.99%% done, finished soon", disk);
193 b2fb1832SIan Dowse 		if (tnow >= tschedule) {
194 b2fb1832SIan Dowse 			tschedule = tnow + 300;
195 bcfe1715SWarner Losh 			msg("99.99%% done, finished soon\n");
196 b2fb1832SIan Dowse 		}
197 bcfe1715SWarner Losh 	} else {
198 2bb823d2SIan Dowse 		deltat = (blockswritten == 0) ? 0 : tstart_writing - tnow +
199 2bb823d2SIan Dowse 		    (double)(tnow - tstart_writing) / blockswritten * tapesize;
200 bcfe1715SWarner Losh 		tdone = tnow + deltat;
201 2bb823d2SIan Dowse 		percent = (blockswritten * 100.0) / tapesize;
202 2bb823d2SIan Dowse 		hours = deltat / 3600;
203 2bb823d2SIan Dowse 		mins = (deltat % 3600) / 60;
204 2bb823d2SIan Dowse 
205 452329adSRalf S. Engelschall 		tdone_str = ctime(&tdone);
206 be0ee8b3SDmitry Morozovsky 		tdone_str[strlen(tdone_str) - 1] = '\0';
207 bcfe1715SWarner Losh 		setproctitle(
208 432aab8cSDmitry Morozovsky 		    "%s: pass %d: %3.2f%% done, finished in %d:%02d at %s",
209 432aab8cSDmitry Morozovsky 		    disk, passno, percent, hours, mins, tdone_str);
210 8fae3551SRodney W. Grimes 		if (tnow >= tschedule) {
211 8fae3551SRodney W. Grimes 			tschedule = tnow + 300;
212 8fae3551SRodney W. Grimes 			if (blockswritten < 500)
213 8fae3551SRodney W. Grimes 				return;
214 432aab8cSDmitry Morozovsky 			msg("%3.2f%% done, finished in %d:%02d at %s\n", percent,
215 452329adSRalf S. Engelschall 			    hours, mins, tdone_str);
216 bcfe1715SWarner Losh 		}
217 8fae3551SRodney W. Grimes 	}
218 8fae3551SRodney W. Grimes }
219 8fae3551SRodney W. Grimes 
220 19f8080eSIan Dowse /*
221 19f8080eSIan Dowse  * Schedule a printout of the estimate in the next call to timeest().
222 19f8080eSIan Dowse  */
223 19f8080eSIan Dowse void
infosch(int signal __unused)224 2db673abSWarner Losh infosch(int signal __unused)
225 19f8080eSIan Dowse {
226 19f8080eSIan Dowse 	tschedule = 0;
227 19f8080eSIan Dowse }
228 19f8080eSIan Dowse 
229 8fae3551SRodney W. Grimes void
msg(const char * fmt,...)230 8fae3551SRodney W. Grimes msg(const char *fmt, ...)
231 8fae3551SRodney W. Grimes {
232 8fae3551SRodney W. Grimes 	va_list ap;
233 8fae3551SRodney W. Grimes 
234 8fae3551SRodney W. Grimes 	(void) fprintf(stderr,"  DUMP: ");
235 8fae3551SRodney W. Grimes #ifdef TDEBUG
236 8fae3551SRodney W. Grimes 	(void) fprintf(stderr, "pid=%d ", getpid());
237 8fae3551SRodney W. Grimes #endif
238 8fae3551SRodney W. Grimes 	va_start(ap, fmt);
239 4036f9e2SPaul Saab 	(void) vfprintf(stderr, fmt, ap);
240 c82b8ff0SDavid E. O'Brien 	va_end(ap);
241 8fae3551SRodney W. Grimes 	(void) fflush(stdout);
242 8fae3551SRodney W. Grimes 	(void) fflush(stderr);
243 70a74c07SDavid E. O'Brien 	va_start(ap, fmt);
244 70a74c07SDavid E. O'Brien 	(void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap);
245 70a74c07SDavid E. O'Brien 	va_end(ap);
246 8fae3551SRodney W. Grimes }
247 8fae3551SRodney W. Grimes 
248 8fae3551SRodney W. Grimes void
msgtail(const char * fmt,...)249 8fae3551SRodney W. Grimes msgtail(const char *fmt, ...)
250 8fae3551SRodney W. Grimes {
251 8fae3551SRodney W. Grimes 	va_list ap;
252 c82b8ff0SDavid E. O'Brien 
253 8fae3551SRodney W. Grimes 	va_start(ap, fmt);
254 8fae3551SRodney W. Grimes 	(void) vfprintf(stderr, fmt, ap);
255 8fae3551SRodney W. Grimes 	va_end(ap);
256 8fae3551SRodney W. Grimes }
257 8fae3551SRodney W. Grimes 
258 8fae3551SRodney W. Grimes void
quit(const char * fmt,...)259 8fae3551SRodney W. Grimes quit(const char *fmt, ...)
260 8fae3551SRodney W. Grimes {
261 8fae3551SRodney W. Grimes 	va_list ap;
262 8fae3551SRodney W. Grimes 
263 8fae3551SRodney W. Grimes 	(void) fprintf(stderr,"  DUMP: ");
264 8fae3551SRodney W. Grimes #ifdef TDEBUG
265 8fae3551SRodney W. Grimes 	(void) fprintf(stderr, "pid=%d ", getpid());
266 8fae3551SRodney W. Grimes #endif
267 8fae3551SRodney W. Grimes 	va_start(ap, fmt);
268 8fae3551SRodney W. Grimes 	(void) vfprintf(stderr, fmt, ap);
269 8fae3551SRodney W. Grimes 	va_end(ap);
270 8fae3551SRodney W. Grimes 	(void) fflush(stdout);
271 8fae3551SRodney W. Grimes 	(void) fflush(stderr);
272 8fae3551SRodney W. Grimes 	dumpabort(0);
273 8fae3551SRodney W. Grimes }
274 8fae3551SRodney W. Grimes 
275 8fae3551SRodney W. Grimes /*
276 8fae3551SRodney W. Grimes  *	Tell the operator what has to be done;
277 8fae3551SRodney W. Grimes  *	we don't actually do it
278 8fae3551SRodney W. Grimes  */
279 8fae3551SRodney W. Grimes 
280 8fae3551SRodney W. Grimes struct fstab *
allocfsent(const struct fstab * fs)281 2db673abSWarner Losh allocfsent(const struct fstab *fs)
282 8fae3551SRodney W. Grimes {
283 d2334e27SIan Dowse 	struct fstab *new;
284 8fae3551SRodney W. Grimes 
285 8fae3551SRodney W. Grimes 	new = (struct fstab *)malloc(sizeof (*fs));
286 8fae3551SRodney W. Grimes 	if (new == NULL ||
287 8fae3551SRodney W. Grimes 	    (new->fs_file = strdup(fs->fs_file)) == NULL ||
288 8fae3551SRodney W. Grimes 	    (new->fs_type = strdup(fs->fs_type)) == NULL ||
289 8fae3551SRodney W. Grimes 	    (new->fs_spec = strdup(fs->fs_spec)) == NULL)
290 8fae3551SRodney W. Grimes 		quit("%s\n", strerror(errno));
291 8fae3551SRodney W. Grimes 	new->fs_passno = fs->fs_passno;
292 8fae3551SRodney W. Grimes 	new->fs_freq = fs->fs_freq;
293 8fae3551SRodney W. Grimes 	return (new);
294 8fae3551SRodney W. Grimes }
295 8fae3551SRodney W. Grimes 
296 8fae3551SRodney W. Grimes struct	pfstab {
297 941ee632SPoul-Henning Kamp 	SLIST_ENTRY(pfstab) pf_list;
298 8fae3551SRodney W. Grimes 	struct	fstab *pf_fstab;
299 8fae3551SRodney W. Grimes };
300 8fae3551SRodney W. Grimes 
301 941ee632SPoul-Henning Kamp static	SLIST_HEAD(, pfstab) table;
302 8fae3551SRodney W. Grimes 
303 8fae3551SRodney W. Grimes void
dump_getfstab(void)304 a3165d16SMatthew N. Dodd dump_getfstab(void)
305 8fae3551SRodney W. Grimes {
306 d2334e27SIan Dowse 	struct fstab *fs;
307 d2334e27SIan Dowse 	struct pfstab *pf;
308 8fae3551SRodney W. Grimes 
309 8fae3551SRodney W. Grimes 	if (setfsent() == 0) {
310 8fae3551SRodney W. Grimes 		msg("Can't open %s for dump table information: %s\n",
311 8fae3551SRodney W. Grimes 		    _PATH_FSTAB, strerror(errno));
312 8fae3551SRodney W. Grimes 		return;
313 8fae3551SRodney W. Grimes 	}
314 8fae3551SRodney W. Grimes 	while ((fs = getfsent()) != NULL) {
315 062a58a1SCy Schubert 		if ((strcmp(fs->fs_type, FSTAB_RW) &&
316 8fae3551SRodney W. Grimes 		    strcmp(fs->fs_type, FSTAB_RO) &&
317 062a58a1SCy Schubert 		    strcmp(fs->fs_type, FSTAB_RQ)) ||
318 062a58a1SCy Schubert 		    strcmp(fs->fs_vfstype, "ufs"))
319 8fae3551SRodney W. Grimes 			continue;
320 8fae3551SRodney W. Grimes 		fs = allocfsent(fs);
321 8fae3551SRodney W. Grimes 		if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL)
322 8fae3551SRodney W. Grimes 			quit("%s\n", strerror(errno));
323 8fae3551SRodney W. Grimes 		pf->pf_fstab = fs;
324 941ee632SPoul-Henning Kamp 		SLIST_INSERT_HEAD(&table, pf, pf_list);
325 8fae3551SRodney W. Grimes 	}
326 8fae3551SRodney W. Grimes 	(void) endfsent();
327 8fae3551SRodney W. Grimes }
328 8fae3551SRodney W. Grimes 
329 8fae3551SRodney W. Grimes /*
330 8fae3551SRodney W. Grimes  * Search in the fstab for a file name.
331 8fae3551SRodney W. Grimes  * This file name can be either the special or the path file name.
332 8fae3551SRodney W. Grimes  *
333 8fae3551SRodney W. Grimes  * The file name can omit the leading '/'.
334 8fae3551SRodney W. Grimes  */
335 8fae3551SRodney W. Grimes struct fstab *
fstabsearch(const char * key)336 2db673abSWarner Losh fstabsearch(const char *key)
337 8fae3551SRodney W. Grimes {
338 d2334e27SIan Dowse 	struct pfstab *pf;
339 d2334e27SIan Dowse 	struct fstab *fs;
340 8fae3551SRodney W. Grimes 	char *rn;
341 8fae3551SRodney W. Grimes 
342 941ee632SPoul-Henning Kamp 	SLIST_FOREACH(pf, &table, pf_list) {
343 8fae3551SRodney W. Grimes 		fs = pf->pf_fstab;
344 8fae3551SRodney W. Grimes 		if (strcmp(fs->fs_file, key) == 0 ||
345 8fae3551SRodney W. Grimes 		    strcmp(fs->fs_spec, key) == 0)
346 8fae3551SRodney W. Grimes 			return (fs);
347 8fae3551SRodney W. Grimes 		rn = rawname(fs->fs_spec);
348 8fae3551SRodney W. Grimes 		if (rn != NULL && strcmp(rn, key) == 0)
349 8fae3551SRodney W. Grimes 			return (fs);
350 8fae3551SRodney W. Grimes 		if (key[0] != '/') {
351 8fae3551SRodney W. Grimes 			if (*fs->fs_spec == '/' &&
352 8fae3551SRodney W. Grimes 			    strcmp(fs->fs_spec + 1, key) == 0)
353 8fae3551SRodney W. Grimes 				return (fs);
354 8fae3551SRodney W. Grimes 			if (*fs->fs_file == '/' &&
355 8fae3551SRodney W. Grimes 			    strcmp(fs->fs_file + 1, key) == 0)
356 8fae3551SRodney W. Grimes 				return (fs);
357 8fae3551SRodney W. Grimes 		}
358 8fae3551SRodney W. Grimes 	}
359 8fae3551SRodney W. Grimes 	return (NULL);
360 8fae3551SRodney W. Grimes }
361 8fae3551SRodney W. Grimes 
362 8fae3551SRodney W. Grimes /*
363 8fae3551SRodney W. Grimes  *	Tell the operator what to do
364 8fae3551SRodney W. Grimes  */
365 8fae3551SRodney W. Grimes void
lastdump(int arg)366 2db673abSWarner Losh lastdump(int arg)	/* w ==> just what to do; W ==> most recent dumps */
367 8fae3551SRodney W. Grimes {
368 d2334e27SIan Dowse 	int i;
369 d2334e27SIan Dowse 	struct fstab *dt;
370 d2334e27SIan Dowse 	struct dumpdates *dtwalk;
371 8fae3551SRodney W. Grimes 	char *lastname, *date;
372 8fae3551SRodney W. Grimes 	int dumpme;
373 a741b779SJordan K. Hubbard 	time_t tnow;
374 a741b779SJordan K. Hubbard 	struct tm *tlast;
375 8fae3551SRodney W. Grimes 
376 8fae3551SRodney W. Grimes 	(void) time(&tnow);
377 a3165d16SMatthew N. Dodd 	dump_getfstab();	/* /etc/fstab input */
378 8fae3551SRodney W. Grimes 	initdumptimes();	/* /etc/dumpdates input */
379 8fae3551SRodney W. Grimes 	qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
380 8fae3551SRodney W. Grimes 
381 8fae3551SRodney W. Grimes 	if (arg == 'w')
382 8fae3551SRodney W. Grimes 		(void) printf("Dump these file systems:\n");
383 8fae3551SRodney W. Grimes 	else
384 8fae3551SRodney W. Grimes 		(void) printf("Last dump(s) done (Dump '>' file systems):\n");
385 8fae3551SRodney W. Grimes 	lastname = "??";
386 8fae3551SRodney W. Grimes 	ITITERATE(i, dtwalk) {
387 8fae3551SRodney W. Grimes 		if (strncmp(lastname, dtwalk->dd_name,
388 8fae3551SRodney W. Grimes 		    sizeof(dtwalk->dd_name)) == 0)
389 8fae3551SRodney W. Grimes 			continue;
390 a741b779SJordan K. Hubbard 		date = (char *)ctime(&dtwalk->dd_ddate);
391 8fae3551SRodney W. Grimes 		date[16] = '\0';	/* blast away seconds and year */
392 8fae3551SRodney W. Grimes 		lastname = dtwalk->dd_name;
393 8fae3551SRodney W. Grimes 		dt = fstabsearch(dtwalk->dd_name);
394 a741b779SJordan K. Hubbard 		dumpme = (dt != NULL && dt->fs_freq != 0);
395 a741b779SJordan K. Hubbard 		if (dumpme) {
396 a741b779SJordan K. Hubbard 		    tlast = localtime(&dtwalk->dd_ddate);
397 a741b779SJordan K. Hubbard 		    dumpme = tnow > (dtwalk->dd_ddate - (tlast->tm_hour * 3600)
398 a741b779SJordan K. Hubbard 				     - (tlast->tm_min * 60) - tlast->tm_sec
399 a741b779SJordan K. Hubbard 				     + (dt->fs_freq * 86400));
400 80c7cc1cSPedro F. Giffuni 		}
401 8fae3551SRodney W. Grimes 		if (arg != 'w' || dumpme)
402 8fae3551SRodney W. Grimes 			(void) printf(
403 c2a41f43SBrian Somers 			    "%c %8s\t(%6s) Last dump: Level %d, Date %s\n",
404 8fae3551SRodney W. Grimes 			    dumpme && (arg != 'w') ? '>' : ' ',
405 8fae3551SRodney W. Grimes 			    dtwalk->dd_name,
406 8fae3551SRodney W. Grimes 			    dt ? dt->fs_file : "",
407 8fae3551SRodney W. Grimes 			    dtwalk->dd_level,
408 8fae3551SRodney W. Grimes 			    date);
409 8fae3551SRodney W. Grimes 	}
410 8fae3551SRodney W. Grimes }
411 8fae3551SRodney W. Grimes 
412 8fae3551SRodney W. Grimes int
datesort(const void * a1,const void * a2)413 2db673abSWarner Losh datesort(const void *a1, const void *a2)
414 8fae3551SRodney W. Grimes {
415 8fae3551SRodney W. Grimes 	struct dumpdates *d1 = *(struct dumpdates **)a1;
416 8fae3551SRodney W. Grimes 	struct dumpdates *d2 = *(struct dumpdates **)a2;
417 8fae3551SRodney W. Grimes 	int diff;
418 8fae3551SRodney W. Grimes 
419 8fae3551SRodney W. Grimes 	diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
420 8fae3551SRodney W. Grimes 	if (diff == 0)
421 8fae3551SRodney W. Grimes 		return (d2->dd_ddate - d1->dd_ddate);
422 8fae3551SRodney W. Grimes 	return (diff);
423 8fae3551SRodney W. Grimes }
424