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