1 /* $OpenBSD: optr.c,v 1.30 2007/06/03 20:16:08 millert Exp $ */ 2 /* $NetBSD: optr.c,v 1.11 1997/05/27 08:34:36 mrg Exp $ */ 3 4 /*- 5 * Copyright (c) 1980, 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)optr.c 8.2 (Berkeley) 1/6/94"; 36 #else 37 static const char rcsid[] = "$OpenBSD: optr.c,v 1.30 2007/06/03 20:16:08 millert Exp $"; 38 #endif 39 #endif /* not lint */ 40 41 #include <sys/param.h> 42 #include <sys/wait.h> 43 #include <sys/time.h> 44 45 #include <ufs/ufs/dinode.h> 46 47 #include <errno.h> 48 #include <fstab.h> 49 #include <grp.h> 50 #include <signal.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <stdarg.h> 55 #include <tzfile.h> 56 #include <unistd.h> 57 #include <utmp.h> 58 59 #include "dump.h" 60 #include "pathnames.h" 61 62 void alarmcatch(int); 63 int datesort(const void *, const void *); 64 65 /* 66 * Query the operator; This previously-fascist piece of code 67 * no longer requires an exact response. 68 * It is intended to protect dump aborting by inquisitive 69 * people banging on the console terminal to see what is 70 * happening which might cause dump to croak, destroying 71 * a large number of hours of work. 72 * 73 * Every 2 minutes we reprint the message, alerting others 74 * that dump needs attention. 75 */ 76 static int timeout; 77 static char *attnmessage; /* attention message */ 78 79 int 80 query(char *question) 81 { 82 char replybuffer[64]; 83 int back, errcount; 84 FILE *mytty; 85 time_t firstprompt, when_answered; 86 87 (void) time(&firstprompt); 88 89 if ((mytty = fopen(_PATH_TTY, "r")) == NULL) 90 quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno)); 91 attnmessage = question; 92 timeout = 0; 93 alarmcatch(0); 94 back = -1; 95 errcount = 0; 96 do { 97 if (fgets(replybuffer, sizeof(replybuffer), mytty) == NULL) { 98 clearerr(mytty); 99 if (++errcount > 30) /* XXX ugly */ 100 quit("excessive operator query failures\n"); 101 } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') { 102 back = 1; 103 } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') { 104 back = 0; 105 } else { 106 (void) fprintf(stderr, 107 " DUMP: \"Yes\" or \"No\"?\n"); 108 (void) fprintf(stderr, 109 " DUMP: %s: (\"yes\" or \"no\") ", question); 110 } 111 } while (back < 0); 112 113 /* 114 * Turn off the alarm, and reset the signal to trap out.. 115 */ 116 (void) alarm(0); 117 if (signal(SIGALRM, sig) == SIG_IGN) 118 signal(SIGALRM, SIG_IGN); 119 (void) fclose(mytty); 120 (void) time(&when_answered); 121 /* 122 * Adjust the base for time estimates to ignore time we spent waiting 123 * for operator input. 124 */ 125 if (when_answered - firstprompt > 0) 126 tstart_writing += (when_answered - firstprompt); 127 return (back); 128 } 129 130 char lastmsg[BUFSIZ]; 131 132 /* 133 * Alert the console operator, and enable the alarm clock to 134 * sleep for 2 minutes in case nobody comes to satisfy dump 135 * XXX not safe 136 */ 137 /* ARGSUSED */ 138 void 139 alarmcatch(int signo) 140 { 141 int save_errno = errno; 142 143 if (notify == 0) { 144 if (timeout == 0) 145 (void) fprintf(stderr, 146 " DUMP: %s: (\"yes\" or \"no\") ", 147 attnmessage); 148 else 149 msgtail("\7\7"); 150 } else { 151 if (timeout) { 152 msgtail("\n"); 153 broadcast(""); /* just print last msg */ 154 } 155 (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ", 156 attnmessage); 157 } 158 signal(SIGALRM, alarmcatch); 159 (void) alarm(120); 160 timeout = 1; 161 errno = save_errno; 162 } 163 164 /* 165 * Here if an inquisitive operator interrupts the dump program 166 */ 167 /* ARGSUSED */ 168 void 169 interrupt(int signo) 170 { 171 msg("Interrupt received.\n"); 172 if (query("Do you want to abort dump?")) 173 dumpabort(0); 174 } 175 176 /* 177 * We now use wall(1) to do the actual broadcasting. 178 */ 179 void 180 broadcast(char *message) 181 { 182 FILE *fp; 183 char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3]; 184 185 if (!notify) 186 return; 187 188 (void)snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT); 189 if ((fp = popen(buf, "w")) == NULL) 190 return; 191 192 (void) fputs("\7\7\7Message from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp); 193 if (lastmsg[0]) 194 (void) fputs(lastmsg, fp); 195 if (message[0]) 196 (void) fputs(message, fp); 197 198 (void) pclose(fp); 199 } 200 201 /* 202 * Print out an estimate of the amount of time left to do the dump 203 */ 204 205 time_t tschedule = 0; 206 207 void 208 timeest(void) 209 { 210 time_t tnow, deltat; 211 212 (void) time((time_t *) &tnow); 213 if (tnow >= tschedule) { 214 tschedule = tnow + 300; 215 if (blockswritten < 500) 216 return; 217 deltat = tstart_writing - tnow + 218 (1.0 * (tnow - tstart_writing)) 219 / blockswritten * tapesize; 220 msg("%3.2f%% done, finished in %d:%02d\n", 221 (blockswritten * 100.0) / tapesize, 222 deltat / 3600, (deltat % 3600) / 60); 223 } 224 } 225 226 void 227 msg(const char *fmt, ...) 228 { 229 va_list ap; 230 231 (void) fprintf(stderr," DUMP: "); 232 #ifdef TDEBUG 233 (void) fprintf(stderr, "pid=%d ", getpid()); 234 #endif 235 va_start(ap, fmt); 236 (void) vfprintf(stderr, fmt, ap); 237 va_end(ap); 238 (void) fflush(stdout); 239 (void) fflush(stderr); 240 va_start(ap, fmt); 241 (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap); 242 va_end(ap); 243 } 244 245 void 246 msgtail(const char *fmt, ...) 247 { 248 va_list ap; 249 250 va_start(ap, fmt); 251 (void) vfprintf(stderr, fmt, ap); 252 va_end(ap); 253 } 254 255 void 256 quit(const char *fmt, ...) 257 { 258 va_list ap; 259 260 (void) fprintf(stderr," DUMP: "); 261 #ifdef TDEBUG 262 (void) fprintf(stderr, "pid=%d ", getpid()); 263 #endif 264 va_start(ap, fmt); 265 (void) vfprintf(stderr, fmt, ap); 266 va_end(ap); 267 (void) fflush(stdout); 268 (void) fflush(stderr); 269 dumpabort(0); 270 } 271 272 /* 273 * Tell the operator what has to be done; 274 * we don't actually do it 275 */ 276 277 struct fstab * 278 allocfsent(struct fstab *fs) 279 { 280 struct fstab *new; 281 282 new = (struct fstab *)malloc(sizeof(*fs)); 283 if (new == NULL || 284 (new->fs_file = strdup(fs->fs_file)) == NULL || 285 (new->fs_type = strdup(fs->fs_type)) == NULL || 286 (new->fs_spec = strdup(fs->fs_spec)) == NULL) 287 quit("%s\n", strerror(errno)); 288 new->fs_passno = fs->fs_passno; 289 new->fs_freq = fs->fs_freq; 290 return (new); 291 } 292 293 struct pfstab { 294 struct pfstab *pf_next; 295 struct fstab *pf_fstab; 296 }; 297 298 static struct pfstab *table; 299 300 void 301 getfstab(void) 302 { 303 struct fstab *fs; 304 struct pfstab *pf; 305 306 if (setfsent() == 0) { 307 msg("Can't open %s for dump table information: %s\n", 308 _PATH_FSTAB, strerror(errno)); 309 return; 310 } 311 while ((fs = getfsent()) != NULL) { 312 if (strcmp(fs->fs_vfstype, "ffs") && 313 strcmp(fs->fs_vfstype, "ufs")) 314 continue; 315 if (strcmp(fs->fs_type, FSTAB_RW) && 316 strcmp(fs->fs_type, FSTAB_RO) && 317 strcmp(fs->fs_type, FSTAB_RQ)) 318 continue; 319 fs = allocfsent(fs); 320 if ((pf = (struct pfstab *)malloc(sizeof(*pf))) == NULL) 321 quit("%s\n", strerror(errno)); 322 pf->pf_fstab = fs; 323 pf->pf_next = table; 324 table = pf; 325 } 326 (void) endfsent(); 327 } 328 329 /* 330 * Search in the fstab for a file name. 331 * This file name can be either the special or the path file name. 332 * 333 * The entries in the fstab are the BLOCK special names, not the 334 * character special names. 335 * The caller of fstabsearch assures that the character device 336 * is dumped (that is much faster) 337 * 338 * The file name can omit the leading '/'. 339 */ 340 struct fstab * 341 fstabsearch(char *key) 342 { 343 struct pfstab *pf; 344 struct fstab *fs; 345 char *rn; 346 347 for (pf = table; pf != NULL; pf = pf->pf_next) { 348 fs = pf->pf_fstab; 349 if (strcmp(fs->fs_file, key) == 0 || 350 strcmp(fs->fs_spec, key) == 0) 351 return (fs); 352 rn = rawname(fs->fs_spec); 353 if (rn != NULL && strcmp(rn, key) == 0) 354 return (fs); 355 if (key[0] != '/') { 356 if (*fs->fs_spec == '/' && 357 strcmp(fs->fs_spec + 1, key) == 0) 358 return (fs); 359 if (*fs->fs_file == '/' && 360 strcmp(fs->fs_file + 1, key) == 0) 361 return (fs); 362 } 363 } 364 return (NULL); 365 } 366 367 /* 368 * Tell the operator what to do 369 * w ==> just what to do; W ==> most recent dumps 370 */ 371 void 372 lastdump(int arg) 373 { 374 int i; 375 struct fstab *dt; 376 struct dumpdates *dtwalk; 377 char *lastname, *date; 378 int dumpme; 379 time_t tnow; 380 381 (void) time(&tnow); 382 getfstab(); /* /etc/fstab input */ 383 initdumptimes(); /* /etc/dumpdates input */ 384 qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort); 385 386 if (arg == 'w') 387 (void) printf("Dump these file systems:\n"); 388 else 389 (void) printf("Last dump(s) done (Dump '>' file systems):\n"); 390 lastname = "??"; 391 ITITERATE(i, dtwalk) { 392 if (strncmp(lastname, dtwalk->dd_name, 393 sizeof(dtwalk->dd_name)) == 0) 394 continue; 395 date = (char *)ctime(&dtwalk->dd_ddate); 396 date[16] = '\0'; /* blast away seconds and year */ 397 lastname = dtwalk->dd_name; 398 dt = fstabsearch(dtwalk->dd_name); 399 dumpme = (dt != NULL && 400 dt->fs_freq != 0 && 401 dtwalk->dd_ddate < tnow - (dt->fs_freq * SECSPERDAY)); 402 if (arg != 'w' || dumpme) 403 (void) printf( 404 "%c %8s\t(%6s) Last dump: Level %c, Date %s\n", 405 dumpme && (arg != 'w') ? '>' : ' ', 406 dtwalk->dd_name, 407 dt ? dt->fs_file : "", 408 dtwalk->dd_level, 409 date); 410 } 411 } 412 413 int 414 datesort(const void *a1, const void *a2) 415 { 416 struct dumpdates *d1 = *(struct dumpdates **)a1; 417 struct dumpdates *d2 = *(struct dumpdates **)a2; 418 int diff; 419 420 diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name)); 421 if (diff == 0) 422 return (d2->dd_ddate - d1->dd_ddate); 423 return (diff); 424 } 425