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