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