1 /* $NetBSD: optr.c,v 1.4 1996/05/18 16:16:17 jtk Exp $ */ 2 3 /*- 4 * Copyright (c) 1980, 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)optr.c 8.2 (Berkeley) 1/6/94"; 39 #else 40 static char rcsid[] = "$NetBSD: optr.c,v 1.4 1996/05/18 16:16:17 jtk Exp $"; 41 #endif 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/wait.h> 46 #include <sys/time.h> 47 48 #include <errno.h> 49 #include <fstab.h> 50 #include <grp.h> 51 #include <signal.h> 52 #include <stdio.h> 53 #ifdef __STDC__ 54 #include <stdlib.h> 55 #include <string.h> 56 #include <stdarg.h> 57 #endif 58 #include <tzfile.h> 59 #ifdef __STDC__ 60 #include <unistd.h> 61 #endif 62 #include <utmp.h> 63 #ifndef __STDC__ 64 #include <varargs.h> 65 #endif 66 67 #include "dump.h" 68 #include "pathnames.h" 69 70 void alarmcatch __P((/* int, int */)); 71 int datesort __P((const void *, const void *)); 72 static void sendmes __P((char *, char *)); 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((time_t *)0); 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((time_t *)0); 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[100]; 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 */ 146 void 147 alarmcatch() 148 { 149 if (notify == 0) { 150 if (timeout == 0) 151 (void) fprintf(stderr, 152 " DUMP: %s: (\"yes\" or \"no\") ", 153 attnmessage); 154 else 155 msgtail("\7\7"); 156 } else { 157 if (timeout) { 158 msgtail("\n"); 159 broadcast(""); /* just print last msg */ 160 } 161 (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ", 162 attnmessage); 163 } 164 signal(SIGALRM, alarmcatch); 165 (void) alarm(120); 166 timeout = 1; 167 } 168 169 /* 170 * Here if an inquisitive operator interrupts the dump program 171 */ 172 void 173 interrupt(signo) 174 int signo; 175 { 176 msg("Interrupt received.\n"); 177 if (query("Do you want to abort dump?")) 178 dumpabort(0); 179 } 180 181 /* 182 * The following variables and routines manage alerting 183 * operators to the status of dump. 184 * This works much like wall(1) does. 185 */ 186 struct group *gp; 187 188 /* 189 * Get the names from the group entry "operator" to notify. 190 */ 191 void 192 set_operators() 193 { 194 if (!notify) /*not going to notify*/ 195 return; 196 gp = getgrnam(OPGRENT); 197 (void) endgrent(); 198 if (gp == NULL) { 199 msg("No group entry for %s.\n", OPGRENT); 200 notify = 0; 201 return; 202 } 203 } 204 205 struct tm *localclock; 206 207 /* 208 * We fork a child to do the actual broadcasting, so 209 * that the process control groups are not messed up 210 */ 211 void 212 broadcast(message) 213 char *message; 214 { 215 time_t clock; 216 FILE *f_utmp; 217 struct utmp utmp; 218 char **np; 219 int pid, s; 220 221 if (!notify || gp == NULL) 222 return; 223 224 switch (pid = fork()) { 225 case -1: 226 return; 227 case 0: 228 break; 229 default: 230 while (wait(&s) != pid) 231 continue; 232 return; 233 } 234 235 clock = time((time_t *)0); 236 localclock = localtime(&clock); 237 238 if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) { 239 msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno)); 240 return; 241 } 242 243 while (!feof(f_utmp)) { 244 if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1) 245 break; 246 if (utmp.ut_name[0] == 0) 247 continue; 248 for (np = gp->gr_mem; *np; np++) { 249 if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0) 250 continue; 251 /* 252 * Do not send messages to operators on dialups 253 */ 254 if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0) 255 continue; 256 #ifdef DEBUG 257 msg("Message to %s at %s\n", *np, utmp.ut_line); 258 #endif 259 sendmes(utmp.ut_line, message); 260 } 261 } 262 (void) fclose(f_utmp); 263 Exit(0); /* the wait in this same routine will catch this */ 264 /* NOTREACHED */ 265 } 266 267 static void 268 sendmes(tty, message) 269 char *tty, *message; 270 { 271 char t[50], buf[BUFSIZ]; 272 register char *cp; 273 int lmsg = 1; 274 FILE *f_tty; 275 276 (void) strcpy(t, _PATH_DEV); 277 (void) strcat(t, tty); 278 279 if ((f_tty = fopen(t, "w")) != NULL) { 280 setbuf(f_tty, buf); 281 (void) fprintf(f_tty, 282 "\n\ 283 \7\7\7Message from the dump program to all operators at %d:%02d ...\r\n\n\ 284 DUMP: NEEDS ATTENTION: ", 285 localclock->tm_hour, localclock->tm_min); 286 for (cp = lastmsg; ; cp++) { 287 if (*cp == '\0') { 288 if (lmsg) { 289 cp = message; 290 if (*cp == '\0') 291 break; 292 lmsg = 0; 293 } else 294 break; 295 } 296 if (*cp == '\n') 297 (void) putc('\r', f_tty); 298 (void) putc(*cp, f_tty); 299 } 300 (void) fclose(f_tty); 301 } 302 } 303 304 /* 305 * print out an estimate of the amount of time left to do the dump 306 */ 307 308 time_t tschedule = 0; 309 310 void 311 timeest() 312 { 313 time_t tnow, deltat; 314 315 (void) time((time_t *) &tnow); 316 if (tnow >= tschedule) { 317 tschedule = tnow + 300; 318 if (blockswritten < 500) 319 return; 320 deltat = tstart_writing - tnow + 321 (1.0 * (tnow - tstart_writing)) 322 / blockswritten * tapesize; 323 msg("%3.2f%% done, finished in %d:%02d\n", 324 (blockswritten * 100.0) / tapesize, 325 deltat / 3600, (deltat % 3600) / 60); 326 } 327 } 328 329 void 330 #if __STDC__ 331 msg(const char *fmt, ...) 332 #else 333 msg(fmt, va_alist) 334 char *fmt; 335 va_dcl 336 #endif 337 { 338 va_list ap; 339 340 (void) fprintf(stderr," DUMP: "); 341 #ifdef TDEBUG 342 (void) fprintf(stderr, "pid=%d ", getpid()); 343 #endif 344 #if __STDC__ 345 va_start(ap, fmt); 346 #else 347 va_start(ap); 348 #endif 349 (void) vfprintf(stderr, fmt, ap); 350 (void) fflush(stdout); 351 (void) fflush(stderr); 352 (void) vsprintf(lastmsg, fmt, ap); 353 va_end(ap); 354 } 355 356 void 357 #if __STDC__ 358 msgtail(const char *fmt, ...) 359 #else 360 msgtail(fmt, va_alist) 361 char *fmt; 362 va_dcl 363 #endif 364 { 365 va_list ap; 366 #if __STDC__ 367 va_start(ap, fmt); 368 #else 369 va_start(ap); 370 #endif 371 (void) vfprintf(stderr, fmt, ap); 372 va_end(ap); 373 } 374 375 void 376 #if __STDC__ 377 quit(const char *fmt, ...) 378 #else 379 quit(fmt, va_alist) 380 char *fmt; 381 va_dcl 382 #endif 383 { 384 va_list ap; 385 386 (void) fprintf(stderr," DUMP: "); 387 #ifdef TDEBUG 388 (void) fprintf(stderr, "pid=%d ", getpid()); 389 #endif 390 #if __STDC__ 391 va_start(ap, fmt); 392 #else 393 va_start(ap); 394 #endif 395 (void) vfprintf(stderr, fmt, ap); 396 va_end(ap); 397 (void) fflush(stdout); 398 (void) fflush(stderr); 399 dumpabort(0); 400 } 401 402 /* 403 * Tell the operator what has to be done; 404 * we don't actually do it 405 */ 406 407 struct fstab * 408 allocfsent(fs) 409 register struct fstab *fs; 410 { 411 register struct fstab *new; 412 413 new = (struct fstab *)malloc(sizeof (*fs)); 414 if (new == NULL || 415 (new->fs_file = strdup(fs->fs_file)) == NULL || 416 (new->fs_type = strdup(fs->fs_type)) == NULL || 417 (new->fs_spec = strdup(fs->fs_spec)) == NULL) 418 quit("%s\n", strerror(errno)); 419 new->fs_passno = fs->fs_passno; 420 new->fs_freq = fs->fs_freq; 421 return (new); 422 } 423 424 struct pfstab { 425 struct pfstab *pf_next; 426 struct fstab *pf_fstab; 427 }; 428 429 static struct pfstab *table; 430 431 void 432 getfstab() 433 { 434 register struct fstab *fs; 435 register struct pfstab *pf; 436 437 if (setfsent() == 0) { 438 msg("Can't open %s for dump table information: %s\n", 439 _PATH_FSTAB, strerror(errno)); 440 return; 441 } 442 while ((fs = getfsent()) != NULL) { 443 if (strcmp(fs->fs_type, FSTAB_RW) && 444 strcmp(fs->fs_type, FSTAB_RO) && 445 strcmp(fs->fs_type, FSTAB_RQ)) 446 continue; 447 fs = allocfsent(fs); 448 if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL) 449 quit("%s\n", strerror(errno)); 450 pf->pf_fstab = fs; 451 pf->pf_next = table; 452 table = pf; 453 } 454 (void) endfsent(); 455 } 456 457 /* 458 * Search in the fstab for a file name. 459 * This file name can be either the special or the path file name. 460 * 461 * The entries in the fstab are the BLOCK special names, not the 462 * character special names. 463 * The caller of fstabsearch assures that the character device 464 * is dumped (that is much faster) 465 * 466 * The file name can omit the leading '/'. 467 */ 468 struct fstab * 469 fstabsearch(key) 470 char *key; 471 { 472 register struct pfstab *pf; 473 register struct fstab *fs; 474 char *rn; 475 476 for (pf = table; pf != NULL; pf = pf->pf_next) { 477 fs = pf->pf_fstab; 478 if (strcmp(fs->fs_file, key) == 0 || 479 strcmp(fs->fs_spec, key) == 0) 480 return (fs); 481 rn = rawname(fs->fs_spec); 482 if (rn != NULL && strcmp(rn, key) == 0) 483 return (fs); 484 if (key[0] != '/') { 485 if (*fs->fs_spec == '/' && 486 strcmp(fs->fs_spec + 1, key) == 0) 487 return (fs); 488 if (*fs->fs_file == '/' && 489 strcmp(fs->fs_file + 1, key) == 0) 490 return (fs); 491 } 492 } 493 return (NULL); 494 } 495 496 /* 497 * Tell the operator what to do 498 */ 499 void 500 lastdump(arg) 501 char arg; /* w ==> just what to do; W ==> most recent dumps */ 502 { 503 register int i; 504 register struct fstab *dt; 505 register struct dumpdates *dtwalk; 506 char *lastname, *date; 507 int dumpme; 508 time_t tnow; 509 510 (void) time(&tnow); 511 getfstab(); /* /etc/fstab input */ 512 initdumptimes(); /* /etc/dumpdates input */ 513 qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort); 514 515 if (arg == 'w') 516 (void) printf("Dump these file systems:\n"); 517 else 518 (void) printf("Last dump(s) done (Dump '>' file systems):\n"); 519 lastname = "??"; 520 ITITERATE(i, dtwalk) { 521 if (strncmp(lastname, dtwalk->dd_name, 522 sizeof(dtwalk->dd_name)) == 0) 523 continue; 524 date = (char *)ctime(&dtwalk->dd_ddate); 525 date[16] = '\0'; /* blast away seconds and year */ 526 lastname = dtwalk->dd_name; 527 dt = fstabsearch(dtwalk->dd_name); 528 dumpme = (dt != NULL && 529 dt->fs_freq != 0 && 530 dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY)); 531 if (arg != 'w' || dumpme) 532 (void) printf( 533 "%c %8s\t(%6s) Last dump: Level %c, Date %s\n", 534 dumpme && (arg != 'w') ? '>' : ' ', 535 dtwalk->dd_name, 536 dt ? dt->fs_file : "", 537 dtwalk->dd_level, 538 date); 539 } 540 } 541 542 int 543 datesort(a1, a2) 544 const void *a1, *a2; 545 { 546 struct dumpdates *d1 = *(struct dumpdates **)a1; 547 struct dumpdates *d2 = *(struct dumpdates **)a2; 548 int diff; 549 550 diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name)); 551 if (diff == 0) 552 return (d2->dd_ddate - d1->dd_ddate); 553 return (diff); 554 } 555