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