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