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 "$FreeBSD$"; 40 #endif /* not lint */ 41 42 #include <sys/param.h> 43 #include <sys/queue.h> 44 #include <sys/wait.h> 45 #include <sys/time.h> 46 47 #include <errno.h> 48 #include <fstab.h> 49 #include <grp.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <stdarg.h> 54 #include <unistd.h> 55 #include <utmp.h> 56 57 #include "dump.h" 58 #include "pathnames.h" 59 60 void alarmcatch __P((/* int, int */)); 61 int datesort __P((const void *, const void *)); 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[BUFSIZ]; 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("\a\a"); 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 * We now use wall(1) to do the actual broadcasting. 162 */ 163 void 164 broadcast(message) 165 char *message; 166 { 167 FILE *fp; 168 char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3]; 169 170 if (!notify) 171 return; 172 173 snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT); 174 if ((fp = popen(buf, "w")) == NULL) 175 return; 176 177 (void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp); 178 if (lastmsg[0]) 179 (void) fputs(lastmsg, fp); 180 if (message[0]) 181 (void) fputs(message, fp); 182 183 (void) pclose(fp); 184 } 185 186 /* 187 * Print out an estimate of the amount of time left to do the dump 188 */ 189 190 time_t tschedule = 0; 191 192 void 193 timeest() 194 { 195 time_t tnow, deltat; 196 197 (void) time((time_t *) &tnow); 198 if (tnow >= tschedule) { 199 tschedule = tnow + 300; 200 if (blockswritten < 500) 201 return; 202 deltat = tstart_writing - tnow + 203 (1.0 * (tnow - tstart_writing)) 204 / blockswritten * tapesize; 205 msg("%3.2f%% done, finished in %d:%02d\n", 206 (blockswritten * 100.0) / tapesize, 207 deltat / 3600, (deltat % 3600) / 60); 208 } 209 } 210 211 void 212 #if __STDC__ 213 msg(const char *fmt, ...) 214 #else 215 msg(fmt, va_alist) 216 char *fmt; 217 va_dcl 218 #endif 219 { 220 va_list ap; 221 222 (void) fprintf(stderr," DUMP: "); 223 #ifdef TDEBUG 224 (void) fprintf(stderr, "pid=%d ", getpid()); 225 #endif 226 #if __STDC__ 227 va_start(ap, fmt); 228 #else 229 va_start(ap); 230 #endif 231 (void) vfprintf(stderr, fmt, ap); 232 (void) fflush(stdout); 233 (void) fflush(stderr); 234 (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap); 235 va_end(ap); 236 } 237 238 void 239 #if __STDC__ 240 msgtail(const char *fmt, ...) 241 #else 242 msgtail(fmt, va_alist) 243 char *fmt; 244 va_dcl 245 #endif 246 { 247 va_list ap; 248 #if __STDC__ 249 va_start(ap, fmt); 250 #else 251 va_start(ap); 252 #endif 253 (void) vfprintf(stderr, fmt, ap); 254 va_end(ap); 255 } 256 257 void 258 #if __STDC__ 259 quit(const char *fmt, ...) 260 #else 261 quit(fmt, va_alist) 262 char *fmt; 263 va_dcl 264 #endif 265 { 266 va_list ap; 267 268 (void) fprintf(stderr," DUMP: "); 269 #ifdef TDEBUG 270 (void) fprintf(stderr, "pid=%d ", getpid()); 271 #endif 272 #if __STDC__ 273 va_start(ap, fmt); 274 #else 275 va_start(ap); 276 #endif 277 (void) vfprintf(stderr, fmt, ap); 278 va_end(ap); 279 (void) fflush(stdout); 280 (void) fflush(stderr); 281 dumpabort(0); 282 } 283 284 /* 285 * Tell the operator what has to be done; 286 * we don't actually do it 287 */ 288 289 struct fstab * 290 allocfsent(fs) 291 register struct fstab *fs; 292 { 293 register struct fstab *new; 294 295 new = (struct fstab *)malloc(sizeof (*fs)); 296 if (new == NULL || 297 (new->fs_file = strdup(fs->fs_file)) == NULL || 298 (new->fs_type = strdup(fs->fs_type)) == NULL || 299 (new->fs_spec = strdup(fs->fs_spec)) == NULL) 300 quit("%s\n", strerror(errno)); 301 new->fs_passno = fs->fs_passno; 302 new->fs_freq = fs->fs_freq; 303 return (new); 304 } 305 306 struct pfstab { 307 SLIST_ENTRY(pfstab) pf_list; 308 struct fstab *pf_fstab; 309 }; 310 311 static SLIST_HEAD(, pfstab) table; 312 313 void 314 getfstab() 315 { 316 register struct fstab *fs; 317 register struct pfstab *pf; 318 319 if (setfsent() == 0) { 320 msg("Can't open %s for dump table information: %s\n", 321 _PATH_FSTAB, strerror(errno)); 322 return; 323 } 324 while ((fs = getfsent()) != NULL) { 325 if (strcmp(fs->fs_type, FSTAB_RW) && 326 strcmp(fs->fs_type, FSTAB_RO) && 327 strcmp(fs->fs_type, FSTAB_RQ)) 328 continue; 329 fs = allocfsent(fs); 330 if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL) 331 quit("%s\n", strerror(errno)); 332 pf->pf_fstab = fs; 333 SLIST_INSERT_HEAD(&table, pf, pf_list); 334 } 335 (void) endfsent(); 336 } 337 338 /* 339 * Search in the fstab for a file name. 340 * This file name can be either the special or the path file name. 341 * 342 * The file name can omit the leading '/'. 343 */ 344 struct fstab * 345 fstabsearch(key) 346 char *key; 347 { 348 register struct pfstab *pf; 349 register struct fstab *fs; 350 char *rn; 351 352 SLIST_FOREACH(pf, &table, pf_list) { 353 fs = pf->pf_fstab; 354 if (strcmp(fs->fs_file, key) == 0 || 355 strcmp(fs->fs_spec, key) == 0) 356 return (fs); 357 rn = rawname(fs->fs_spec); 358 if (rn != NULL && strcmp(rn, key) == 0) 359 return (fs); 360 if (key[0] != '/') { 361 if (*fs->fs_spec == '/' && 362 strcmp(fs->fs_spec + 1, key) == 0) 363 return (fs); 364 if (*fs->fs_file == '/' && 365 strcmp(fs->fs_file + 1, key) == 0) 366 return (fs); 367 } 368 } 369 return (NULL); 370 } 371 372 /* 373 * Tell the operator what to do 374 */ 375 void 376 lastdump(arg) 377 char arg; /* w ==> just what to do; W ==> most recent dumps */ 378 { 379 register int i; 380 register struct fstab *dt; 381 register struct dumpdates *dtwalk; 382 char *lastname, *date; 383 int dumpme; 384 time_t tnow; 385 struct tm *tlast; 386 387 (void) time(&tnow); 388 getfstab(); /* /etc/fstab input */ 389 initdumptimes(); /* /etc/dumpdates input */ 390 qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort); 391 392 if (arg == 'w') 393 (void) printf("Dump these file systems:\n"); 394 else 395 (void) printf("Last dump(s) done (Dump '>' file systems):\n"); 396 lastname = "??"; 397 ITITERATE(i, dtwalk) { 398 if (strncmp(lastname, dtwalk->dd_name, 399 sizeof(dtwalk->dd_name)) == 0) 400 continue; 401 date = (char *)ctime(&dtwalk->dd_ddate); 402 date[16] = '\0'; /* blast away seconds and year */ 403 lastname = dtwalk->dd_name; 404 dt = fstabsearch(dtwalk->dd_name); 405 dumpme = (dt != NULL && dt->fs_freq != 0); 406 if (dumpme) { 407 tlast = localtime(&dtwalk->dd_ddate); 408 dumpme = tnow > (dtwalk->dd_ddate - (tlast->tm_hour * 3600) 409 - (tlast->tm_min * 60) - tlast->tm_sec 410 + (dt->fs_freq * 86400)); 411 }; 412 if (arg != 'w' || dumpme) 413 (void) printf( 414 "%c %8s\t(%6s) Last dump: Level %c, Date %s\n", 415 dumpme && (arg != 'w') ? '>' : ' ', 416 dtwalk->dd_name, 417 dt ? dt->fs_file : "", 418 dtwalk->dd_level, 419 date); 420 } 421 } 422 423 int 424 datesort(a1, a2) 425 const void *a1, *a2; 426 { 427 struct dumpdates *d1 = *(struct dumpdates **)a1; 428 struct dumpdates *d2 = *(struct dumpdates **)a2; 429 int diff; 430 431 diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name)); 432 if (diff == 0) 433 return (d2->dd_ddate - d1->dd_ddate); 434 return (diff); 435 } 436