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