1 /* $NetBSD: optr.c,v 1.25 2001/12/25 12:06:26 lukem 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 #include <sys/cdefs.h> 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)optr.c 8.2 (Berkeley) 1/6/94"; 40 #else 41 __RCSID("$NetBSD: optr.c,v 1.25 2001/12/25 12:06:26 lukem Exp $"); 42 #endif 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #include <sys/queue.h> 47 #include <sys/wait.h> 48 #include <sys/time.h> 49 #include <sys/ucred.h> 50 #include <sys/mount.h> 51 52 #include <errno.h> 53 #include <fstab.h> 54 #include <grp.h> 55 #include <signal.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <stdarg.h> 60 #include <time.h> 61 #include <tzfile.h> 62 #include <unistd.h> 63 #include <utmp.h> 64 65 #include "dump.h" 66 #include "pathnames.h" 67 68 void alarmcatch(int); 69 struct fstab *allocfsent(struct fstab *); 70 int datesort(const void *, const void *); 71 static void sendmes(char *, char *); 72 extern gid_t egid; 73 extern char *time_string; 74 extern char default_time_string[]; 75 76 static void do_timestamp(time_t, char *); 77 78 /* 79 * Query the operator; This previously-fascist piece of code 80 * no longer requires an exact response. 81 * It is intended to protect dump aborting by inquisitive 82 * people banging on the console terminal to see what is 83 * happening which might cause dump to croak, destroying 84 * a large number of hours of work. 85 * 86 * Every 2 minutes we reprint the message, alerting others 87 * that dump needs attention. 88 */ 89 static int timeout; 90 static char *attnmessage; /* attention message */ 91 92 int 93 query(char *question) 94 { 95 char replybuffer[64]; 96 int back, errcount; 97 FILE *mytty; 98 time_t firstprompt, when_answered; 99 100 firstprompt = time((time_t *)0); 101 102 if ((mytty = fopen(_PATH_TTY, "r")) == NULL) 103 quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno)); 104 attnmessage = question; 105 timeout = 0; 106 alarmcatch(0); 107 back = -1; 108 errcount = 0; 109 do { 110 if (fgets(replybuffer, 63, mytty) == NULL) { 111 clearerr(mytty); 112 if (++errcount > 30) /* XXX ugly */ 113 quit("excessive operator query failures\n"); 114 } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') { 115 back = 1; 116 } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') { 117 back = 0; 118 } else { 119 (void) fprintf(stderr, 120 " DUMP: \"Yes\" or \"No\"?\n"); 121 (void) fprintf(stderr, 122 " DUMP: %s: (\"yes\" or \"no\") ", question); 123 } 124 } while (back < 0); 125 126 /* 127 * Turn off the alarm, and reset the signal to trap out.. 128 */ 129 (void) alarm(0); 130 if (signal(SIGALRM, sig) == SIG_IGN) 131 signal(SIGALRM, SIG_IGN); 132 (void) fclose(mytty); 133 when_answered = time((time_t *)0); 134 /* 135 * Adjust the base for time estimates to ignore time we spent waiting 136 * for operator input. 137 */ 138 if (tstart_writing != 0) 139 tstart_writing += (when_answered - firstprompt); 140 return(back); 141 } 142 143 char lastmsg[200]; 144 145 /* 146 * Alert the console operator, and enable the alarm clock to 147 * sleep for 2 minutes in case nobody comes to satisfy dump 148 */ 149 void 150 alarmcatch(int dummy) 151 { 152 153 if (notify == 0) { 154 if (timeout == 0) 155 (void) fprintf(stderr, 156 " DUMP: %s: (\"yes\" or \"no\") ", 157 attnmessage); 158 else 159 msgtail("\a\a"); 160 } else { 161 if (timeout) { 162 msgtail("\n"); 163 broadcast(""); /* just print last msg */ 164 } 165 (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ", 166 attnmessage); 167 } 168 signal(SIGALRM, alarmcatch); 169 (void) alarm(120); 170 timeout = 1; 171 } 172 173 /* 174 * Here if an inquisitive operator interrupts the dump program 175 */ 176 void 177 interrupt(int signo) 178 { 179 180 msg("Interrupt received.\n"); 181 if (query("Do you want to abort dump?")) 182 dumpabort(0); 183 } 184 185 /* 186 * The following variables and routines manage alerting 187 * operators to the status of dump. 188 * This works much like wall(1) does. 189 */ 190 struct group *gp; 191 192 /* 193 * Get the names from the group entry "operator" to notify. 194 */ 195 void 196 set_operators(void) 197 { 198 199 if (!notify) /*not going to notify*/ 200 return; 201 gp = getgrnam(OPGRENT); 202 (void) endgrent(); 203 if (gp == NULL) { 204 msg("No group entry for %s.\n", OPGRENT); 205 notify = 0; 206 return; 207 } 208 } 209 210 struct tm *localclock; 211 212 /* 213 * We fork a child to do the actual broadcasting, so 214 * that the process control groups are not messed up 215 */ 216 void 217 broadcast(char *message) 218 { 219 struct utmp utmp; 220 time_t now; 221 FILE *f_utmp; 222 char **np; 223 int pid, s; 224 225 if (!notify || gp == NULL) 226 return; 227 228 /* Restore 'tty' privs for the child's use only. */ 229 setegid(egid); 230 switch (pid = fork()) { 231 case -1: 232 setegid(getgid()); 233 return; 234 case 0: 235 break; 236 default: 237 setegid(getgid()); 238 while (wait(&s) != pid) 239 continue; 240 return; 241 } 242 243 now = time((time_t *)0); 244 localclock = localtime(&now); 245 246 if ((f_utmp = fopen(_PATH_UTMP, "r")) == NULL) { 247 msg("Cannot open %s: %s\n", _PATH_UTMP, strerror(errno)); 248 return; 249 } 250 251 while (!feof(f_utmp)) { 252 if (fread((char *) &utmp, sizeof (struct utmp), 1, f_utmp) != 1) 253 break; 254 if (utmp.ut_name[0] == 0) 255 continue; 256 for (np = gp->gr_mem; *np; np++) { 257 if (strncmp(*np, utmp.ut_name, sizeof(utmp.ut_name)) != 0) 258 continue; 259 /* 260 * Do not send messages to operators on dialups 261 */ 262 if (strncmp(utmp.ut_line, DIALUP, strlen(DIALUP)) == 0) 263 continue; 264 #ifdef DEBUG 265 msg("Message to %s at %s\n", *np, utmp.ut_line); 266 #endif 267 sendmes(utmp.ut_line, message); 268 } 269 } 270 (void) fclose(f_utmp); 271 Exit(0); /* the wait in this same routine will catch this */ 272 /* NOTREACHED */ 273 } 274 275 static void 276 sendmes(char *tty, char *message) 277 { 278 char t[50], buf[BUFSIZ]; 279 char *cp; 280 int lmsg = 1; 281 FILE *f_tty; 282 283 (void)strncpy(t, _PATH_DEV, sizeof(t) - 1); 284 (void)strncat(t, tty, sizeof(t) - sizeof(_PATH_DEV) - 1); 285 t[sizeof(t) - 1] = '\0'; 286 287 if ((f_tty = fopen(t, "w")) != NULL) { 288 setbuf(f_tty, buf); 289 (void) fprintf(f_tty, 290 "\n\ 291 \a\a\aMessage from the dump program to all operators at %d:%02d ...\r\n\n\ 292 DUMP: NEEDS ATTENTION: ", 293 localclock->tm_hour, localclock->tm_min); 294 for (cp = lastmsg; ; cp++) { 295 if (*cp == '\0') { 296 if (lmsg) { 297 cp = message; 298 if (*cp == '\0') 299 break; 300 lmsg = 0; 301 } else 302 break; 303 } 304 if (*cp == '\n') 305 (void) putc('\r', f_tty); 306 (void) putc(*cp, f_tty); 307 } 308 (void) fclose(f_tty); 309 } 310 } 311 312 /* 313 * print out the timestamp string to stderr. 314 */ 315 #define STAMP_LENGTH 80 316 317 static void 318 do_timestamp(time_t thistime, char *message) 319 { 320 struct tm tm_time; 321 char then[STAMP_LENGTH + 1]; 322 323 (void) localtime_r(&thistime, &tm_time); 324 if (strftime(then, STAMP_LENGTH, time_string, &tm_time) == 0) { 325 time_string = default_time_string; 326 strftime(then, STAMP_LENGTH, time_string, &tm_time); 327 fprintf(stderr, 328 "DUMP: ERROR: TIMEFORMAT too long, reverting to default\n"); 329 } 330 331 fprintf(stderr, message, then); 332 } 333 334 335 /* 336 * print out an estimate of the amount of time left to do the dump 337 */ 338 339 time_t tschedule = 0; 340 341 void 342 timeest(void) 343 { 344 time_t tnow, deltat; 345 346 (void) time((time_t *) &tnow); 347 if (tnow >= tschedule) { 348 tschedule = tnow + 300; 349 if (blockswritten < 500) 350 return; 351 deltat = tstart_writing - tnow + 352 (1.0 * (tnow - tstart_writing)) 353 / blockswritten * tapesize; 354 355 msg("%3.2f%% done, finished in %ld:%02ld", 356 (blockswritten * 100.0) / tapesize, 357 (long)(deltat / 3600), (long)((deltat % 3600) / 60)); 358 359 if (timestamp == 1) 360 do_timestamp(tnow + deltat, " (at %s)"); 361 362 fprintf(stderr, "\n"); 363 } 364 } 365 366 void 367 msg(const char *fmt, ...) 368 { 369 time_t tnow; 370 va_list ap; 371 372 fprintf(stderr, " "); 373 if (timestamp == 1) { 374 (void) time((time_t *) &tnow); 375 do_timestamp(tnow, "[%s] "); 376 } 377 378 (void) fprintf(stderr,"DUMP: "); 379 #ifdef TDEBUG 380 (void) fprintf(stderr, "pid=%d ", getpid()); 381 #endif 382 va_start(ap, fmt); 383 (void) vsnprintf(lastmsg, sizeof lastmsg, fmt, ap); 384 fputs(lastmsg, stderr); 385 (void) fflush(stdout); 386 (void) fflush(stderr); 387 va_end(ap); 388 } 389 390 void 391 msgtail(const char *fmt, ...) 392 { 393 va_list ap; 394 395 va_start(ap, fmt); 396 (void) vfprintf(stderr, fmt, ap); 397 va_end(ap); 398 } 399 400 void 401 quit(const char *fmt, ...) 402 { 403 va_list ap; 404 405 (void) fprintf(stderr," DUMP: "); 406 #ifdef TDEBUG 407 (void) fprintf(stderr, "pid=%d ", getpid()); 408 #endif 409 va_start(ap, fmt); 410 (void) vfprintf(stderr, fmt, ap); 411 va_end(ap); 412 (void) fflush(stdout); 413 (void) fflush(stderr); 414 dumpabort(0); 415 } 416 417 /* 418 * Tell the operator what has to be done; 419 * we don't actually do it 420 */ 421 422 struct fstab * 423 allocfsent(struct fstab *fs) 424 { 425 struct fstab *new; 426 427 new = (struct fstab *)xmalloc(sizeof (*fs)); 428 new->fs_file = xstrdup(fs->fs_file); 429 new->fs_type = xstrdup(fs->fs_type); 430 new->fs_spec = xstrdup(fs->fs_spec); 431 new->fs_passno = fs->fs_passno; 432 new->fs_freq = fs->fs_freq; 433 return (new); 434 } 435 436 struct pfstab { 437 SLIST_ENTRY(pfstab) pf_list; 438 struct fstab *pf_fstab; 439 }; 440 441 static SLIST_HEAD(, pfstab) table; 442 443 void 444 getfstab(void) 445 { 446 struct fstab *fs; 447 struct pfstab *pf; 448 449 if (setfsent() == 0) { 450 msg("Can't open %s for dump table information: %s\n", 451 _PATH_FSTAB, strerror(errno)); 452 return; 453 } 454 while ((fs = getfsent()) != NULL) { 455 if (strcmp(fs->fs_type, FSTAB_RW) && 456 strcmp(fs->fs_type, FSTAB_RO) && 457 strcmp(fs->fs_type, FSTAB_RQ)) 458 continue; 459 #ifdef DUMP_LFS 460 if (strcmp(fs->fs_vfstype, "lfs")) 461 continue; 462 #else 463 if (strcmp(fs->fs_vfstype, "ufs") && 464 strcmp(fs->fs_vfstype, "ffs")) 465 continue; 466 #endif 467 fs = allocfsent(fs); 468 pf = (struct pfstab *)xmalloc(sizeof (*pf)); 469 pf->pf_fstab = fs; 470 SLIST_INSERT_HEAD(&table, pf, pf_list); 471 } 472 (void) endfsent(); 473 } 474 475 /* 476 * Search in the fstab for a file name. 477 * This file name can be either the special or the path file name. 478 * 479 * The entries in the fstab are the BLOCK special names, not the 480 * character special names. 481 * The caller of fstabsearch assures that the character device 482 * is dumped (that is much faster) 483 * 484 * The file name can omit the leading '/'. 485 */ 486 struct fstab * 487 fstabsearch(const char *key) 488 { 489 struct pfstab *pf; 490 struct fstab *fs; 491 char *rn; 492 493 SLIST_FOREACH(pf, &table, pf_list) { 494 fs = pf->pf_fstab; 495 if (strcmp(fs->fs_file, key) == 0 || 496 strcmp(fs->fs_spec, key) == 0) 497 return (fs); 498 rn = rawname(fs->fs_spec); 499 if (rn != NULL && strcmp(rn, key) == 0) 500 return (fs); 501 if (key[0] != '/') { 502 if (*fs->fs_spec == '/' && 503 strcmp(fs->fs_spec + 1, key) == 0) 504 return (fs); 505 if (*fs->fs_file == '/' && 506 strcmp(fs->fs_file + 1, key) == 0) 507 return (fs); 508 } 509 } 510 return (NULL); 511 } 512 513 /* 514 * Search in the mounted file list for a file name. 515 * This file name can be either the special or the path file name. 516 * 517 * The entries in the list are the BLOCK special names, not the 518 * character special names. 519 * The caller of mntinfosearch assures that the character device 520 * is dumped (that is much faster) 521 */ 522 struct statfs * 523 mntinfosearch(const char *key) 524 { 525 int i, mntbufc; 526 struct statfs *mntbuf, *fs; 527 char *rn; 528 529 if ((mntbufc = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) 530 quit("Can't get mount list: %s", strerror(errno)); 531 for (fs = mntbuf, i = 0; i < mntbufc; i++, fs++) { 532 if (strcmp(fs->f_fstypename, "ufs") != 0 && 533 strcmp(fs->f_fstypename, "ffs") != 0) 534 continue; 535 if (strcmp(fs->f_mntonname, key) == 0 || 536 strcmp(fs->f_mntfromname, key) == 0) 537 return (fs); 538 rn = rawname(fs->f_mntfromname); 539 if (rn != NULL && strcmp(rn, key) == 0) 540 return (fs); 541 } 542 return (NULL); 543 } 544 545 546 /* 547 * Tell the operator what to do. 548 * arg: w ==> just what to do; W ==> most recent dumps 549 */ 550 void 551 lastdump(char arg) 552 { 553 int i; 554 struct fstab *dt; 555 struct dumpdates *dtwalk; 556 char *lastname, *date; 557 int dumpme; 558 time_t tnow; 559 560 (void) time(&tnow); 561 getfstab(); /* /etc/fstab input */ 562 initdumptimes(); /* /etc/dumpdates input */ 563 qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort); 564 565 if (arg == 'w') 566 (void) printf("Dump these file systems:\n"); 567 else 568 (void) printf("Last dump(s) done (Dump '>' file systems):\n"); 569 lastname = "??"; 570 ITITERATE(i, dtwalk) { 571 if (strncmp(lastname, dtwalk->dd_name, 572 sizeof(dtwalk->dd_name)) == 0) 573 continue; 574 date = (char *)ctime(&dtwalk->dd_ddate); 575 date[24] = '\0'; 576 strcpy(date + 16, date + 19); /* blast away seconds */ 577 lastname = dtwalk->dd_name; 578 dt = fstabsearch(dtwalk->dd_name); 579 dumpme = (dt != NULL && 580 dt->fs_freq != 0 && 581 dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY)); 582 if (arg != 'w' || dumpme) 583 (void) printf( 584 "%c %8s\t(%6s) Last dump: Level %c, Date %s\n", 585 dumpme && (arg != 'w') ? '>' : ' ', 586 dtwalk->dd_name, 587 dt ? dt->fs_file : "", 588 dtwalk->dd_level, 589 date); 590 } 591 } 592 593 int 594 datesort(const void *a1, const void *a2) 595 { 596 struct dumpdates *d1 = *(struct dumpdates **)a1; 597 struct dumpdates *d2 = *(struct dumpdates **)a2; 598 int diff; 599 600 diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name)); 601 if (diff == 0) 602 return (d2->dd_ddate - d1->dd_ddate); 603 return (diff); 604 } 605