Home | History | Annotate | Line # | Download | only in dump
      1  1.44   mlelstv /*	$NetBSD: optr.c,v 1.44 2022/03/14 18:38:11 mlelstv Exp $	*/
      2   1.3       cgd 
      3   1.1       cgd /*-
      4   1.2   mycroft  * Copyright (c) 1980, 1988, 1993
      5   1.2   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.31       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.12     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.3       cgd #if 0
     35   1.3       cgd static char sccsid[] = "@(#)optr.c	8.2 (Berkeley) 1/6/94";
     36   1.3       cgd #else
     37  1.44   mlelstv __RCSID("$NetBSD: optr.c,v 1.44 2022/03/14 18:38:11 mlelstv Exp $");
     38   1.3       cgd #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #include <sys/param.h>
     42  1.25     lukem #include <sys/queue.h>
     43   1.1       cgd #include <sys/wait.h>
     44   1.1       cgd #include <sys/time.h>
     45  1.17     lukem #include <sys/ucred.h>
     46  1.17     lukem #include <sys/mount.h>
     47   1.2   mycroft 
     48   1.2   mycroft #include <errno.h>
     49   1.1       cgd #include <fstab.h>
     50   1.1       cgd #include <grp.h>
     51   1.2   mycroft #include <signal.h>
     52   1.2   mycroft #include <stdio.h>
     53   1.1       cgd #include <stdlib.h>
     54   1.1       cgd #include <string.h>
     55   1.1       cgd #include <stdarg.h>
     56  1.13    kleink #include <time.h>
     57   1.2   mycroft #include <tzfile.h>
     58   1.2   mycroft #include <unistd.h>
     59  1.37  christos #include <util.h>
     60  1.30      fvdl 
     61   1.1       cgd #include "dump.h"
     62   1.1       cgd #include "pathnames.h"
     63   1.1       cgd 
     64  1.19     blymn extern  char *time_string;
     65  1.19     blymn extern  char default_time_string[];
     66  1.19     blymn 
     67  1.40  dholland static void alarmcatch(int);
     68  1.40  dholland static struct fstab *allocfsent(const struct fstab *);
     69  1.40  dholland static int datesort(const void *, const void *);
     70  1.34  christos static void do_timestamp(time_t, const char *);
     71   1.1       cgd 
     72   1.1       cgd /*
     73   1.1       cgd  *	Query the operator; This previously-fascist piece of code
     74   1.1       cgd  *	no longer requires an exact response.
     75   1.1       cgd  *	It is intended to protect dump aborting by inquisitive
     76   1.1       cgd  *	people banging on the console terminal to see what is
     77   1.1       cgd  *	happening which might cause dump to croak, destroying
     78   1.1       cgd  *	a large number of hours of work.
     79   1.1       cgd  *
     80   1.1       cgd  *	Every 2 minutes we reprint the message, alerting others
     81   1.1       cgd  *	that dump needs attention.
     82   1.1       cgd  */
     83   1.2   mycroft static	int timeout;
     84  1.34  christos static	const char *attnmessage;		/* attention message */
     85   1.1       cgd 
     86   1.1       cgd int
     87  1.34  christos query(const char *question)
     88   1.1       cgd {
     89   1.1       cgd 	char	replybuffer[64];
     90   1.1       cgd 	int	back, errcount;
     91   1.1       cgd 	FILE	*mytty;
     92   1.4       jtk 	time_t	firstprompt, when_answered;
     93   1.4       jtk 
     94   1.4       jtk 	firstprompt = time((time_t *)0);
     95   1.1       cgd 
     96   1.1       cgd 	if ((mytty = fopen(_PATH_TTY, "r")) == NULL)
     97  1.43  christos 		quite(errno, "fopen on %s fails", _PATH_TTY);
     98   1.1       cgd 	attnmessage = question;
     99   1.1       cgd 	timeout = 0;
    100  1.12     lukem 	alarmcatch(0);
    101   1.1       cgd 	back = -1;
    102   1.1       cgd 	errcount = 0;
    103   1.1       cgd 	do {
    104   1.1       cgd 		if (fgets(replybuffer, 63, mytty) == NULL) {
    105   1.1       cgd 			clearerr(mytty);
    106   1.1       cgd 			if (++errcount > 30)	/* XXX	ugly */
    107  1.43  christos 				quit("excessive operator query failures");
    108   1.1       cgd 		} else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') {
    109   1.1       cgd 			back = 1;
    110   1.1       cgd 		} else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') {
    111   1.1       cgd 			back = 0;
    112   1.1       cgd 		} else {
    113   1.1       cgd 			(void) fprintf(stderr,
    114   1.1       cgd 			    "  DUMP: \"Yes\" or \"No\"?\n");
    115   1.1       cgd 			(void) fprintf(stderr,
    116   1.1       cgd 			    "  DUMP: %s: (\"yes\" or \"no\") ", question);
    117   1.1       cgd 		}
    118   1.1       cgd 	} while (back < 0);
    119   1.1       cgd 
    120   1.1       cgd 	/*
    121   1.1       cgd 	 *	Turn off the alarm, and reset the signal to trap out..
    122   1.1       cgd 	 */
    123   1.1       cgd 	(void) alarm(0);
    124   1.1       cgd 	if (signal(SIGALRM, sig) == SIG_IGN)
    125   1.1       cgd 		signal(SIGALRM, SIG_IGN);
    126   1.1       cgd 	(void) fclose(mytty);
    127   1.4       jtk 	when_answered = time((time_t *)0);
    128   1.4       jtk 	/*
    129   1.4       jtk 	 * Adjust the base for time estimates to ignore time we spent waiting
    130   1.4       jtk 	 * for operator input.
    131   1.4       jtk 	 */
    132   1.4       jtk 	if (tstart_writing != 0)
    133  1.32     lukem 		tstart_writing += (when_answered - firstprompt);
    134  1.32     lukem 	if (tstart_volume != 0)
    135  1.32     lukem 		tstart_volume += (when_answered - firstprompt);
    136   1.1       cgd 	return(back);
    137   1.1       cgd }
    138   1.1       cgd 
    139  1.20     lukem char lastmsg[200];
    140   1.1       cgd 
    141   1.1       cgd /*
    142   1.1       cgd  *	Alert the console operator, and enable the alarm clock to
    143   1.1       cgd  *	sleep for 2 minutes in case nobody comes to satisfy dump
    144   1.1       cgd  */
    145  1.40  dholland static void
    146  1.36  christos alarmcatch(int dummy __unused)
    147   1.1       cgd {
    148  1.15     lukem 
    149   1.1       cgd 	if (notify == 0) {
    150   1.1       cgd 		if (timeout == 0)
    151   1.1       cgd 			(void) fprintf(stderr,
    152   1.1       cgd 			    "  DUMP: %s: (\"yes\" or \"no\") ",
    153   1.1       cgd 			    attnmessage);
    154   1.1       cgd 		else
    155  1.25     lukem 			msgtail("\a\a");
    156   1.1       cgd 	} else {
    157   1.1       cgd 		if (timeout) {
    158   1.1       cgd 			msgtail("\n");
    159   1.1       cgd 			broadcast("");		/* just print last msg */
    160   1.1       cgd 		}
    161   1.1       cgd 		(void) fprintf(stderr,"  DUMP: %s: (\"yes\" or \"no\") ",
    162   1.1       cgd 		    attnmessage);
    163   1.1       cgd 	}
    164   1.1       cgd 	signal(SIGALRM, alarmcatch);
    165   1.1       cgd 	(void) alarm(120);
    166   1.1       cgd 	timeout = 1;
    167   1.1       cgd }
    168   1.1       cgd 
    169   1.1       cgd /*
    170   1.1       cgd  *	Here if an inquisitive operator interrupts the dump program
    171   1.1       cgd  */
    172   1.1       cgd void
    173  1.36  christos interrupt(int signo __unused)
    174   1.1       cgd {
    175  1.28      yamt 	int errno_save;
    176  1.15     lukem 
    177  1.28      yamt 	errno_save = errno;
    178   1.1       cgd 	msg("Interrupt received.\n");
    179   1.1       cgd 	if (query("Do you want to abort dump?"))
    180   1.1       cgd 		dumpabort(0);
    181  1.28      yamt 	errno = errno_save;
    182   1.1       cgd }
    183   1.1       cgd 
    184   1.1       cgd /*
    185  1.29     lukem  *	Use wall(1) "-g operator" to do the actual broadcasting.
    186   1.1       cgd  */
    187   1.1       cgd void
    188  1.34  christos broadcast(const char *message)
    189   1.1       cgd {
    190  1.29     lukem 	FILE	*fp;
    191  1.29     lukem 	char	buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3];
    192   1.1       cgd 
    193  1.29     lukem 	if (!notify)
    194   1.1       cgd 		return;
    195   1.1       cgd 
    196  1.29     lukem 	(void)snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT);
    197  1.29     lukem 	if ((fp = popen(buf, "w")) == NULL)
    198   1.1       cgd 		return;
    199   1.1       cgd 
    200  1.29     lukem 	(void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp);
    201  1.29     lukem 	if (lastmsg[0])
    202  1.29     lukem 		(void) fputs(lastmsg, fp);
    203  1.29     lukem 	if (message[0])
    204  1.29     lukem 		(void) fputs(message, fp);
    205   1.1       cgd 
    206  1.29     lukem 	(void) pclose(fp);
    207   1.1       cgd }
    208   1.1       cgd 
    209   1.1       cgd /*
    210  1.22     lukem  *	print out the timestamp string to stderr.
    211  1.19     blymn  */
    212  1.19     blymn #define STAMP_LENGTH 80
    213  1.19     blymn 
    214  1.19     blymn static void
    215  1.34  christos do_timestamp(time_t thistime, const char *message)
    216  1.19     blymn {
    217  1.19     blymn 	struct tm tm_time;
    218  1.19     blymn 	char then[STAMP_LENGTH + 1];
    219  1.23     lukem 
    220  1.21     lukem 	(void) localtime_r(&thistime, &tm_time);
    221  1.19     blymn 	if (strftime(then, STAMP_LENGTH, time_string, &tm_time) == 0) {
    222  1.19     blymn 		time_string = default_time_string;
    223  1.19     blymn 		strftime(then, STAMP_LENGTH, time_string, &tm_time);
    224  1.19     blymn 		fprintf(stderr,
    225  1.19     blymn 		   "DUMP: ERROR: TIMEFORMAT too long, reverting to default\n");
    226  1.19     blymn 	}
    227  1.23     lukem 
    228  1.21     lukem 	fprintf(stderr, message, then);
    229  1.19     blymn }
    230  1.19     blymn 
    231  1.23     lukem 
    232  1.19     blymn /*
    233   1.1       cgd  *	print out an estimate of the amount of time left to do the dump
    234   1.1       cgd  */
    235   1.1       cgd 
    236   1.1       cgd time_t	tschedule = 0;
    237   1.1       cgd 
    238   1.1       cgd void
    239  1.15     lukem timeest(void)
    240   1.1       cgd {
    241   1.1       cgd 	time_t	tnow, deltat;
    242   1.1       cgd 
    243   1.1       cgd 	(void) time((time_t *) &tnow);
    244   1.1       cgd 	if (tnow >= tschedule) {
    245   1.1       cgd 		tschedule = tnow + 300;
    246   1.1       cgd 		if (blockswritten < 500)
    247  1.23     lukem 			return;
    248   1.1       cgd 		deltat = tstart_writing - tnow +
    249   1.1       cgd 			(1.0 * (tnow - tstart_writing))
    250   1.1       cgd 			/ blockswritten * tapesize;
    251  1.19     blymn 
    252  1.19     blymn 		msg("%3.2f%% done, finished in %ld:%02ld",
    253  1.14        is 		    (blockswritten * 100.0) / tapesize,
    254  1.14        is 		    (long)(deltat / 3600), (long)((deltat % 3600) / 60));
    255  1.23     lukem 
    256  1.19     blymn 		if (timestamp == 1)
    257  1.19     blymn 			do_timestamp(tnow + deltat, " (at %s)");
    258  1.19     blymn 
    259  1.19     blymn 		fprintf(stderr, "\n");
    260   1.1       cgd 	}
    261   1.1       cgd }
    262   1.1       cgd 
    263   1.1       cgd void
    264   1.1       cgd msg(const char *fmt, ...)
    265   1.1       cgd {
    266  1.22     lukem 	time_t  tnow;
    267   1.1       cgd 	va_list ap;
    268   1.1       cgd 
    269  1.19     blymn 	fprintf(stderr, "  ");
    270  1.22     lukem 	if (timestamp == 1) {
    271  1.22     lukem 		(void) time((time_t *) &tnow);
    272  1.19     blymn 		do_timestamp(tnow, "[%s] ");
    273  1.22     lukem 	}
    274  1.19     blymn 
    275  1.19     blymn 	(void) fprintf(stderr,"DUMP: ");
    276   1.1       cgd #ifdef TDEBUG
    277   1.1       cgd 	(void) fprintf(stderr, "pid=%d ", getpid());
    278   1.1       cgd #endif
    279   1.1       cgd 	va_start(ap, fmt);
    280  1.20     lukem 	(void) vsnprintf(lastmsg, sizeof lastmsg, fmt, ap);
    281  1.20     lukem 	fputs(lastmsg, stderr);
    282   1.1       cgd 	(void) fflush(stdout);
    283   1.1       cgd 	(void) fflush(stderr);
    284   1.1       cgd 	va_end(ap);
    285   1.1       cgd }
    286   1.1       cgd 
    287   1.1       cgd void
    288   1.1       cgd msgtail(const char *fmt, ...)
    289   1.1       cgd {
    290   1.1       cgd 	va_list ap;
    291  1.15     lukem 
    292   1.1       cgd 	va_start(ap, fmt);
    293   1.1       cgd 	(void) vfprintf(stderr, fmt, ap);
    294   1.1       cgd 	va_end(ap);
    295   1.1       cgd }
    296   1.1       cgd 
    297   1.1       cgd void
    298  1.43  christos quite(int e, const char *fmt, ...)
    299  1.43  christos {
    300  1.43  christos 	va_list ap;
    301  1.43  christos 
    302  1.43  christos 	(void) fprintf(stderr,"  DUMP: ");
    303  1.43  christos #ifdef TDEBUG
    304  1.43  christos 	(void) fprintf(stderr, "pid=%d ", getpid());
    305  1.43  christos #endif
    306  1.43  christos 	va_start(ap, fmt);
    307  1.43  christos 	(void) vfprintf(stderr, fmt, ap);
    308  1.43  christos 	va_end(ap);
    309  1.43  christos 	(void) fprintf(stderr, ": %s\n", strerror(e));
    310  1.43  christos 	(void) fflush(stdout);
    311  1.43  christos 	(void) fflush(stderr);
    312  1.43  christos 	dumpabort(0);
    313  1.43  christos }
    314  1.43  christos 
    315  1.43  christos void
    316   1.1       cgd quit(const char *fmt, ...)
    317   1.1       cgd {
    318   1.1       cgd 	va_list ap;
    319   1.1       cgd 
    320   1.1       cgd 	(void) fprintf(stderr,"  DUMP: ");
    321   1.1       cgd #ifdef TDEBUG
    322   1.1       cgd 	(void) fprintf(stderr, "pid=%d ", getpid());
    323   1.1       cgd #endif
    324   1.1       cgd 	va_start(ap, fmt);
    325   1.1       cgd 	(void) vfprintf(stderr, fmt, ap);
    326   1.1       cgd 	va_end(ap);
    327  1.43  christos 	(void) fprintf(stderr, "\n");
    328   1.1       cgd 	(void) fflush(stdout);
    329   1.1       cgd 	(void) fflush(stderr);
    330   1.1       cgd 	dumpabort(0);
    331   1.1       cgd }
    332   1.1       cgd 
    333   1.1       cgd /*
    334   1.1       cgd  *	Tell the operator what has to be done;
    335   1.1       cgd  *	we don't actually do it
    336   1.1       cgd  */
    337   1.1       cgd 
    338  1.40  dholland static struct fstab *
    339  1.39  dholland allocfsent(const struct fstab *fs)
    340   1.1       cgd {
    341   1.7     lukem 	struct fstab *new;
    342   1.1       cgd 
    343  1.37  christos 	new = xmalloc(sizeof (*fs));
    344  1.17     lukem 	new->fs_file = xstrdup(fs->fs_file);
    345  1.17     lukem 	new->fs_type = xstrdup(fs->fs_type);
    346  1.43  christos 	new->fs_spec = xmalloc(FILENAME_MAX);
    347  1.44   mlelstv 	if (getfsspecname(new->fs_spec, FILENAME_MAX, fs->fs_spec) == NULL) {
    348  1.44   mlelstv 		free(new);
    349  1.44   mlelstv 		return NULL;
    350  1.44   mlelstv 	}
    351   1.1       cgd 	new->fs_passno = fs->fs_passno;
    352   1.1       cgd 	new->fs_freq = fs->fs_freq;
    353  1.43  christos 	return new;
    354   1.1       cgd }
    355   1.1       cgd 
    356   1.1       cgd struct	pfstab {
    357  1.25     lukem 	SLIST_ENTRY(pfstab) pf_list;
    358   1.1       cgd 	struct	fstab *pf_fstab;
    359   1.1       cgd };
    360   1.1       cgd 
    361  1.25     lukem static	SLIST_HEAD(, pfstab) table;
    362   1.1       cgd 
    363   1.1       cgd void
    364  1.15     lukem getfstab(void)
    365   1.1       cgd {
    366   1.7     lukem 	struct fstab *fs;
    367   1.7     lukem 	struct pfstab *pf;
    368   1.1       cgd 
    369   1.1       cgd 	if (setfsent() == 0) {
    370   1.1       cgd 		msg("Can't open %s for dump table information: %s\n",
    371   1.1       cgd 		    _PATH_FSTAB, strerror(errno));
    372   1.1       cgd 		return;
    373   1.1       cgd 	}
    374   1.2   mycroft 	while ((fs = getfsent()) != NULL) {
    375   1.1       cgd 		if (strcmp(fs->fs_type, FSTAB_RW) &&
    376   1.1       cgd 		    strcmp(fs->fs_type, FSTAB_RO) &&
    377   1.1       cgd 		    strcmp(fs->fs_type, FSTAB_RQ))
    378   1.5     perry 			continue;
    379  1.24     lukem #ifdef DUMP_LFS
    380  1.24     lukem 		if (strcmp(fs->fs_vfstype, "lfs"))
    381  1.24     lukem 			continue;
    382  1.24     lukem #else
    383   1.5     perry 		if (strcmp(fs->fs_vfstype, "ufs") &&
    384   1.5     perry 		    strcmp(fs->fs_vfstype, "ffs"))
    385   1.1       cgd 			continue;
    386  1.24     lukem #endif
    387   1.1       cgd 		fs = allocfsent(fs);
    388  1.44   mlelstv 		if (fs == NULL)
    389  1.44   mlelstv 			continue;
    390  1.44   mlelstv 
    391  1.17     lukem 		pf = (struct pfstab *)xmalloc(sizeof (*pf));
    392   1.1       cgd 		pf->pf_fstab = fs;
    393  1.25     lukem 		SLIST_INSERT_HEAD(&table, pf, pf_list);
    394   1.1       cgd 	}
    395   1.1       cgd 	(void) endfsent();
    396   1.1       cgd }
    397   1.1       cgd 
    398   1.1       cgd /*
    399   1.1       cgd  * Search in the fstab for a file name.
    400   1.1       cgd  * This file name can be either the special or the path file name.
    401   1.1       cgd  *
    402   1.1       cgd  * The entries in the fstab are the BLOCK special names, not the
    403   1.1       cgd  * character special names.
    404   1.1       cgd  * The caller of fstabsearch assures that the character device
    405   1.1       cgd  * is dumped (that is much faster)
    406   1.1       cgd  *
    407   1.1       cgd  * The file name can omit the leading '/'.
    408   1.1       cgd  */
    409   1.1       cgd struct fstab *
    410  1.17     lukem fstabsearch(const char *key)
    411   1.1       cgd {
    412   1.7     lukem 	struct pfstab *pf;
    413   1.7     lukem 	struct fstab *fs;
    414  1.38  christos 	const char *rn;
    415  1.38  christos 	char buf[MAXPATHLEN];
    416   1.1       cgd 
    417  1.25     lukem 	SLIST_FOREACH(pf, &table, pf_list) {
    418   1.1       cgd 		fs = pf->pf_fstab;
    419   1.1       cgd 		if (strcmp(fs->fs_file, key) == 0 ||
    420   1.1       cgd 		    strcmp(fs->fs_spec, key) == 0)
    421   1.1       cgd 			return (fs);
    422  1.38  christos 		rn = getdiskrawname(buf, sizeof(buf), fs->fs_spec);
    423   1.1       cgd 		if (rn != NULL && strcmp(rn, key) == 0)
    424   1.1       cgd 			return (fs);
    425   1.1       cgd 		if (key[0] != '/') {
    426   1.1       cgd 			if (*fs->fs_spec == '/' &&
    427   1.1       cgd 			    strcmp(fs->fs_spec + 1, key) == 0)
    428   1.1       cgd 				return (fs);
    429   1.1       cgd 			if (*fs->fs_file == '/' &&
    430   1.1       cgd 			    strcmp(fs->fs_file + 1, key) == 0)
    431   1.1       cgd 				return (fs);
    432   1.1       cgd 		}
    433   1.1       cgd 	}
    434   1.1       cgd 	return (NULL);
    435   1.1       cgd }
    436  1.17     lukem 
    437  1.17     lukem /*
    438  1.17     lukem  * Search in the mounted file list for a file name.
    439  1.17     lukem  * This file name can be either the special or the path file name.
    440  1.17     lukem  *
    441  1.17     lukem  * The entries in the list are the BLOCK special names, not the
    442  1.17     lukem  * character special names.
    443  1.17     lukem  * The caller of mntinfosearch assures that the character device
    444  1.17     lukem  * is dumped (that is much faster)
    445  1.17     lukem  */
    446  1.33  christos struct statvfs *
    447  1.17     lukem mntinfosearch(const char *key)
    448  1.17     lukem {
    449  1.17     lukem 	int i, mntbufc;
    450  1.33  christos 	struct statvfs *mntbuf, *fs;
    451  1.38  christos 	const char *rn;
    452  1.38  christos 	char buf[MAXPATHLEN];
    453  1.17     lukem 
    454  1.17     lukem 	if ((mntbufc = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    455  1.43  christos 		quite(errno, "Can't get mount list");
    456  1.17     lukem 	for (fs = mntbuf, i = 0; i < mntbufc; i++, fs++) {
    457  1.35  perseant #ifdef DUMP_LFS
    458  1.35  perseant 		if (strcmp(fs->f_fstypename, "lfs") != 0)
    459  1.35  perseant 			continue;
    460  1.35  perseant #else /* ! DUMP_LFS */
    461  1.17     lukem 		if (strcmp(fs->f_fstypename, "ufs") != 0 &&
    462  1.17     lukem 		    strcmp(fs->f_fstypename, "ffs") != 0)
    463  1.17     lukem 			continue;
    464  1.35  perseant #endif /* ! DUMP_LFS */
    465  1.17     lukem 		if (strcmp(fs->f_mntonname, key) == 0 ||
    466  1.17     lukem 		    strcmp(fs->f_mntfromname, key) == 0)
    467  1.17     lukem 			return (fs);
    468  1.38  christos 		rn = getdiskrawname(buf, sizeof(buf), fs->f_mntfromname);
    469  1.17     lukem 		if (rn != NULL && strcmp(rn, key) == 0)
    470  1.17     lukem 			return (fs);
    471  1.17     lukem 	}
    472  1.17     lukem 	return (NULL);
    473  1.17     lukem }
    474  1.17     lukem 
    475   1.1       cgd 
    476   1.1       cgd /*
    477  1.15     lukem  *	Tell the operator what to do.
    478  1.15     lukem  *	arg:	w ==> just what to do; W ==> most recent dumps
    479   1.1       cgd  */
    480   1.1       cgd void
    481  1.15     lukem lastdump(char arg)
    482   1.1       cgd {
    483   1.7     lukem 	int i;
    484   1.7     lukem 	struct fstab *dt;
    485   1.7     lukem 	struct dumpdates *dtwalk;
    486  1.34  christos 	char *date;
    487  1.34  christos 	const char *lastname;
    488   1.2   mycroft 	int dumpme;
    489   1.1       cgd 	time_t tnow;
    490   1.1       cgd 
    491   1.1       cgd 	(void) time(&tnow);
    492   1.1       cgd 	getfstab();		/* /etc/fstab input */
    493   1.1       cgd 	initdumptimes();	/* /etc/dumpdates input */
    494   1.1       cgd 	qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort);
    495   1.1       cgd 
    496   1.1       cgd 	if (arg == 'w')
    497   1.1       cgd 		(void) printf("Dump these file systems:\n");
    498   1.1       cgd 	else
    499   1.1       cgd 		(void) printf("Last dump(s) done (Dump '>' file systems):\n");
    500   1.1       cgd 	lastname = "??";
    501   1.1       cgd 	ITITERATE(i, dtwalk) {
    502   1.1       cgd 		if (strncmp(lastname, dtwalk->dd_name,
    503   1.1       cgd 		    sizeof(dtwalk->dd_name)) == 0)
    504   1.1       cgd 			continue;
    505   1.1       cgd 		date = (char *)ctime(&dtwalk->dd_ddate);
    506   1.6     lukem 		date[24] = '\0';
    507   1.6     lukem 		strcpy(date + 16, date + 19);	/* blast away seconds */
    508   1.1       cgd 		lastname = dtwalk->dd_name;
    509   1.1       cgd 		dt = fstabsearch(dtwalk->dd_name);
    510   1.1       cgd 		dumpme = (dt != NULL &&
    511   1.1       cgd 		    dt->fs_freq != 0 &&
    512   1.1       cgd 		    dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY));
    513   1.1       cgd 		if (arg != 'w' || dumpme)
    514   1.1       cgd 			(void) printf(
    515   1.1       cgd 			    "%c %8s\t(%6s) Last dump: Level %c, Date %s\n",
    516   1.1       cgd 			    dumpme && (arg != 'w') ? '>' : ' ',
    517   1.1       cgd 			    dtwalk->dd_name,
    518   1.1       cgd 			    dt ? dt->fs_file : "",
    519   1.1       cgd 			    dtwalk->dd_level,
    520   1.1       cgd 			    date);
    521   1.1       cgd 	}
    522   1.1       cgd }
    523   1.1       cgd 
    524  1.40  dholland static int
    525  1.15     lukem datesort(const void *a1, const void *a2)
    526   1.1       cgd {
    527  1.34  christos 	const struct dumpdates *d1 = *(const struct dumpdates *const *)a1;
    528  1.34  christos 	const struct dumpdates *d2 = *(const struct dumpdates *const *)a2;
    529   1.1       cgd 	int diff;
    530   1.1       cgd 
    531   1.1       cgd 	diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name));
    532   1.1       cgd 	if (diff == 0)
    533   1.1       cgd 		return (d2->dd_ddate - d1->dd_ddate);
    534   1.1       cgd 	return (diff);
    535   1.1       cgd }
    536