]> git.saurik.com Git - hfs.git/blob - fsck_hfs/utilities.c
hfs-226.1.1.tar.gz
[hfs.git] / fsck_hfs / utilities.c
1 /*
2 * Copyright (c) 1999-2000, 2002, 2004, 2007-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1980, 1986, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55 #include <stddef.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <sys/errno.h>
59 #include <sys/syslimits.h>
60 #include <pwd.h>
61
62 #include <ctype.h>
63 #include <err.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <unistd.h>
67 #include <stdlib.h>
68 #include <sys/sysctl.h>
69
70 #include "fsck_hfs.h"
71
72 char *rawname __P((char *name));
73 char *unrawname __P((char *name));
74
75
76 int
77 reply(char *question)
78 {
79 int persevere;
80 char c;
81
82 if (preen)
83 pfatal("INTERNAL ERROR: GOT TO reply()");
84 persevere = !strcmp(question, "CONTINUE");
85 plog("\n");
86 if (!persevere && (nflag || fswritefd < 0)) {
87 plog("%s? no\n\n", question);
88 return (0);
89 }
90 if (yflag || (persevere && nflag)) {
91 plog("%s? yes\n\n", question);
92 return (1);
93 }
94 do {
95 plog("%s? [yn] ", question);
96 (void) fflush(stdout);
97 c = getc(stdin);
98 while (c != '\n' && getc(stdin) != '\n')
99 if (feof(stdin))
100 return (0);
101 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
102 plog("\n");
103 if (c == 'y' || c == 'Y')
104 return (1);
105 return (0);
106 }
107
108
109 void
110 ckfini(markclean)
111 int markclean;
112 {
113 // register struct bufarea *bp, *nbp;
114 // int ofsmodified, cnt = 0;
115
116 (void) CacheDestroy(&fscache);
117
118 if (fswritefd < 0) {
119 (void)close(fsreadfd);
120 return;
121 }
122 #if 0
123 flush(fswritefd, &sblk);
124 if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
125 !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
126 sblk.b_bno = SBOFF / dev_bsize;
127 sbdirty();
128 flush(fswritefd, &sblk);
129 }
130 flush(fswritefd, &cgblk);
131 free(cgblk.b_un.b_buf);
132 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
133 cnt++;
134 flush(fswritefd, bp);
135 nbp = bp->b_prev;
136 free(bp->b_un.b_buf);
137 free((char *)bp);
138 }
139 if (bufhead.b_size != cnt)
140 errx(EEXIT, "Panic: lost %d buffers", bufhead.b_size - cnt);
141 pbp = pdirbp = (struct bufarea *)0;
142 if (markclean && sblock.fs_clean == 0) {
143 sblock.fs_clean = 1;
144 sbdirty();
145 ofsmodified = fsmodified;
146 flush(fswritefd, &sblk);
147 fsmodified = ofsmodified;
148 if (!preen)
149 plog("\n***** FILE SYSTEM MARKED CLEAN *****\n");
150 }
151 if (debug)
152 plog("cache missed %ld of %ld (%d%%)\n", diskreads,
153 totalreads, (int)(diskreads * 100 / totalreads));
154 #endif
155 (void)close(fsreadfd);
156 (void)close(fswritefd);
157 }
158
159
160 char *
161 blockcheck(char *origname)
162 {
163 struct stat stslash, stblock, stchar;
164 char *newname, *raw;
165 int retried = 0;
166
167 hotroot = 0;
168 if (stat("/", &stslash) < 0) {
169 perror("/");
170 plog("Can't stat root\n");
171 return (origname);
172 }
173 newname = origname;
174 retry:
175 if (stat(newname, &stblock) < 0) {
176 perror(newname);
177 plog("Can't stat %s\n", newname);
178 return (origname);
179 }
180 if ((stblock.st_mode & S_IFMT) == S_IFBLK) {
181 if (stslash.st_dev == stblock.st_rdev)
182 hotroot++;
183 raw = rawname(newname);
184 if (stat(raw, &stchar) < 0) {
185 perror(raw);
186 plog("Can't stat %s\n", raw);
187 return (origname);
188 }
189 if ((stchar.st_mode & S_IFMT) == S_IFCHR) {
190 return (raw);
191 } else {
192 plog("%s is not a character device\n", raw);
193 return (origname);
194 }
195 } else if ((stblock.st_mode & S_IFMT) == S_IFCHR && !retried) {
196 newname = unrawname(newname);
197 retried++;
198 goto retry;
199 }
200 /*
201 * Not a block or character device, just return name and
202 * let the caller decide whether to use it.
203 */
204 return (origname);
205 }
206
207
208 char *
209 rawname(char *name)
210
211 {
212 static char rawbuf[32];
213 char *dp;
214
215 if ((dp = strrchr(name, '/')) == 0)
216 return (0);
217 *dp = 0;
218 (void)strlcpy(rawbuf, name, sizeof(rawbuf));
219 *dp = '/';
220 (void)strlcat(rawbuf, "/r", sizeof(rawbuf));
221 (void)strlcat(rawbuf, &dp[1], sizeof(rawbuf));
222
223 return (rawbuf);
224 }
225
226
227 char *
228 unrawname(char *name)
229 {
230 char *dp;
231 struct stat stb;
232
233 if ((dp = strrchr(name, '/')) == 0)
234 return (name);
235 if (stat(name, &stb) < 0)
236 return (name);
237 if ((stb.st_mode & S_IFMT) != S_IFCHR)
238 return (name);
239 if (dp[1] != 'r')
240 return (name);
241 memmove(&dp[1], &dp[2], strlen(&dp[2]) + 1);
242
243 return (name);
244 }
245
246
247 void
248 catch(sig)
249 int sig;
250 {
251 if (!upgrading)
252 ckfini(0);
253 exit(12);
254 }
255
256
257 //
258 // Logging stuff...
259 //
260 //
261 #include <stdarg.h>
262 #include <pthread.h>
263 #include <time.h>
264
265 #define FSCK_LOG_FILE "/var/log/fsck_hfs.log"
266
267 extern char lflag; // indicates if we're doing a live fsck (defined in fsck_hfs.c)
268 extern char guiControl; // indicates if we're outputting for the gui (defined in fsck_hfs.c)
269
270 FILE *log_file = NULL;
271
272 /* Variables for in-memory log for strings that will be written to log file */
273 char *in_mem_log = NULL;
274 char *cur_in_mem_log = NULL;
275 size_t in_mem_log_size = 0;
276
277 /* Variables for in-memory log for strings that will be printed on standard out */
278 char *in_mem_out = NULL;
279 char *cur_in_mem_out = NULL;
280 size_t in_mem_out_size = 0;
281
282 int live_fsck = 0;
283
284 #define DEFAULT_IN_MEM_SIZE 4096
285
286 static pthread_mutex_t mem_buf_lock = PTHREAD_MUTEX_INITIALIZER;
287 static pthread_cond_t mem_buf_cond;
288
289 static pthread_t printing_thread;
290 static pthread_t logging_thread;
291 static volatile int keep_going = 1;
292
293 #undef fprintf
294 #undef printf
295
296 // prototype
297 void print_to_mem(int type, int mem_type, const char *fmt, const char *str, va_list ap);
298
299 #define DO_VPRINT 1 // types for print_to_mem
300 #define DO_STR 2
301
302 /* Types for mem_type */
303 #define IN_MEM_LOG 1 // in-memory log strings
304 #define IN_MEM_OUT 2 // in-memory stdout strings
305
306 static void *
307 fsck_logging_thread(void *arg)
308 {
309 int copy_amt;
310 char buff[1024], *ptr;
311
312 /* Handle writing to the log file */
313 while(keep_going || cur_in_mem_log != in_mem_log) {
314
315 pthread_mutex_lock(&mem_buf_lock);
316 while (keep_going != 0 && cur_in_mem_log == in_mem_log) {
317 int err;
318
319 err = pthread_cond_wait(&mem_buf_cond, &mem_buf_lock);
320 if (err != 0) {
321 fprintf(stderr, "error %d from cond wait\n", err);
322 break;
323 }
324 }
325
326 copy_amt = (cur_in_mem_log - in_mem_log);
327 if (copy_amt == 0) {
328 pthread_mutex_unlock(&mem_buf_lock);
329 continue;
330 }
331
332 if (copy_amt >= sizeof(buff)) {
333 copy_amt = sizeof(buff) - 1;
334 memcpy(buff, in_mem_log, copy_amt);
335
336 memmove(in_mem_log, &in_mem_log[copy_amt], (cur_in_mem_log - in_mem_log) - copy_amt);
337 cur_in_mem_log -= copy_amt;
338 } else {
339 memcpy(buff, in_mem_log, copy_amt);
340 cur_in_mem_log = in_mem_log;
341 }
342
343 buff[copy_amt] = '\0';
344
345 pthread_mutex_unlock(&mem_buf_lock);
346
347 for(ptr=buff; *ptr; ) {
348 char *start;
349
350 start = ptr;
351 while(*ptr && *ptr != '\n') {
352 ptr++;
353 }
354 if (*ptr == '\n') {
355 *ptr++ = '\0';
356 if (log_file) {
357 fprintf(log_file, "%s: %s\n", cdevname ? cdevname : "UNKNOWN-DEV", start);
358 }
359 } else {
360 if (log_file) {
361 fprintf(log_file, "%s", start);
362 }
363 }
364
365 }
366
367 fflush(stdout);
368 }
369
370 return NULL;
371 }
372
373 static void *
374 fsck_printing_thread(void *arg)
375 {
376 int copy_amt;
377 char buff[1024], *ptr;
378
379 /* Handle writing to the out file */
380 while(keep_going || cur_in_mem_out != in_mem_out) {
381
382 pthread_mutex_lock(&mem_buf_lock);
383 while (keep_going != 0 && cur_in_mem_out == in_mem_out) {
384 int err;
385
386 err = pthread_cond_wait(&mem_buf_cond, &mem_buf_lock);
387 if (err != 0) {
388 fprintf(stderr, "error %d from cond wait\n", err);
389 break;
390 }
391 }
392
393 copy_amt = (cur_in_mem_out - in_mem_out);
394 if (copy_amt == 0) {
395 pthread_mutex_unlock(&mem_buf_lock);
396 continue;
397 }
398
399 if (copy_amt >= sizeof(buff)) {
400 copy_amt = sizeof(buff) - 1;
401 memcpy(buff, in_mem_out, copy_amt);
402
403 memmove(in_mem_out, &in_mem_out[copy_amt], (cur_in_mem_out - in_mem_out) - copy_amt);
404 cur_in_mem_out -= copy_amt;
405 } else {
406 memcpy(buff, in_mem_out, copy_amt);
407 cur_in_mem_out = in_mem_out;
408 }
409
410 buff[copy_amt] = '\0';
411
412 pthread_mutex_unlock(&mem_buf_lock);
413
414 for(ptr=buff; *ptr; ) {
415 char *start;
416
417 start = ptr;
418 while(*ptr && *ptr != '\n') {
419 ptr++;
420 }
421 if (*ptr == '\n') {
422 *ptr++ = '\0';
423 printf("%s\n", start);
424 } else {
425 printf("%s", start);
426 }
427
428 }
429
430 fflush(stdout);
431 }
432
433 return NULL;
434 }
435
436
437 int was_signaled = 0;
438
439 void
440 shutdown_logging(void)
441 {
442 keep_going = 0;
443 time_t t;
444
445 /* Log fsck_hfs check completion time */
446 t = time(NULL);
447 if (in_mem_log) {
448 print_to_mem(DO_STR, IN_MEM_LOG, "fsck_hfs completed at %s\n", ctime(&t), NULL);
449 } else {
450 fprintf(log_file, "%s: fsck_hfs completed at %s\n", cdevname ? cdevname : "UNKNOWN-DEV", ctime(&t));
451 }
452
453 if (was_signaled) {
454 // if we were signaled, we can't really call any of these
455 // functions from the context of a signal handler (which
456 // is how we're called if we don't have a signal handler).
457 // so we have our own signal handler which sets this var
458 // which tells us to just bail out.
459 return;
460 }
461
462 if (log_file && !live_fsck) {
463 fflush(log_file);
464 fclose(log_file);
465 log_file = NULL;
466 } else if ((in_mem_out || in_mem_log) && live_fsck && log_file) {
467 // make sure the printing and logging threads are woken up...
468 pthread_mutex_lock(&mem_buf_lock);
469 pthread_cond_broadcast(&mem_buf_cond);
470 pthread_mutex_unlock(&mem_buf_lock);
471
472 // then wait for them
473 pthread_join(printing_thread, NULL);
474 pthread_join(logging_thread, NULL);
475
476 free(in_mem_out);
477 in_mem_out = cur_in_mem_out = NULL;
478 in_mem_out_size = 0;
479
480 free(in_mem_log);
481 in_mem_log = cur_in_mem_log = NULL;
482 in_mem_log_size = 0;
483
484 if (log_file) {
485 fflush(log_file);
486 fclose(log_file);
487 log_file = NULL;
488 }
489 } else if (in_mem_log) {
490 int ret;
491
492 if (getuid() == 0) {
493 // just in case, flush any pending output
494 fflush(stdout);
495 fflush(stderr);
496
497 //
498 // fork so that the child can wait around until the
499 // root volume is mounted read-write and we can add
500 // our output to the log
501 //
502 ret = fork();
503 } else {
504 // if we're not root we don't need to fork
505 ret = 0;
506 }
507 if (ret == 0) {
508 int i;
509 char *fname = FSCK_LOG_FILE, path[PATH_MAX];
510
511 // Disk Management waits for fsck_hfs' stdout to close rather
512 // than the process death to understand if fsck_hfs has exited
513 // or not. Since we do not use stdout any further, close all
514 // the file descriptors so that Disk Management does not wait
515 // for 60 seconds unnecessarily on read-only boot volumes.
516 fclose(stdout);
517 fclose(stdin);
518 fclose(stderr);
519
520 // non-root will never be able to write to /var/log
521 // so point the file somewhere else.
522 if (getuid() != 0) {
523 struct passwd *pwd;
524 fname = NULL;
525 // each user will get their own log as ~/Library/Logs/fsck_hfs.log
526 pwd = getpwuid(getuid());
527 if (pwd) {
528 snprintf(path, sizeof(path), "%s/Library/Logs/fsck_hfs.log", pwd->pw_dir);
529 fname = &path[0];
530 }
531 }
532
533 for(i=0; i < 60; i++) {
534 log_file = fopen(fname, "a");
535 if (log_file) {
536 fwrite(in_mem_log, cur_in_mem_log - in_mem_log, 1, log_file);
537
538 fflush(log_file);
539 fclose(log_file);
540 log_file = NULL;
541
542 free(in_mem_log);
543 in_mem_log = cur_in_mem_log = NULL;
544 in_mem_log_size = 0;
545
546 break;
547 } else {
548 // hmmm, failed to open the output file so wait
549 // a while only if the fs is read-only and then
550 // try again
551 if (errno == EROFS) {
552 sleep(1);
553 } else {
554 break;
555 }
556 }
557 }
558 }
559 }
560 }
561
562 static void
563 my_sighandler(int sig)
564 {
565 was_signaled = 1;
566 cleanup_fs_fd();
567 exit(sig);
568 }
569
570
571 void
572 setup_logging(void)
573 {
574 static int at_exit_setup = 0;
575 time_t t;
576
577 // if this is set, we don't have to do anything
578 if (at_exit_setup) {
579 return;
580 }
581
582 if (guiControl) {
583 setlinebuf(stdout);
584 setlinebuf(stderr);
585 }
586
587 // our copy of this variable since we may
588 // need to change it to make the right thing
589 // happen for fsck on the root volume.
590 live_fsck = (int)lflag;
591
592 if (log_file == NULL) {
593 log_file = fopen(FSCK_LOG_FILE, "a");
594 if (log_file) {
595 setlinebuf(log_file);
596 } else {
597 //
598 // if we can't open the output file it's either because
599 // we're being run on the root volume during early boot
600 // or we were not run as the root user and so we can't
601 // write to /var/log/fsck_hfs.log. in either case we
602 // turn off "live_fsck" so that the right thing happens
603 // in here with respect to where output goes.
604 //
605 live_fsck = 0;
606 }
607
608 if (!live_fsck && log_file) {
609 t = time(NULL);
610 fprintf(log_file, "\n%s: fsck_hfs started at %s", cdevname ? cdevname : "UNKNOWN-DEV", ctime(&t));
611 fflush(log_file);
612
613 } else if (live_fsck || in_mem_log == NULL || in_mem_out == NULL) {
614 //
615 // hmm, we couldn't open the log file (or it's a
616 // live fsck). let's just squirrel away a copy
617 // of the data in memory and then deal with it
618 // later (or print it out from a separate thread
619 // if we're doing a live fsck).
620 //
621 in_mem_log = (char *)malloc(DEFAULT_IN_MEM_SIZE);
622 in_mem_out = (char *)malloc(DEFAULT_IN_MEM_SIZE);
623 if ((in_mem_log != NULL) && (in_mem_out != NULL)) {
624 in_mem_log_size = DEFAULT_IN_MEM_SIZE;
625 in_mem_log[0] = '\0';
626 cur_in_mem_log = in_mem_log;
627
628 in_mem_out_size = DEFAULT_IN_MEM_SIZE;
629 in_mem_out[0] = '\0';
630 cur_in_mem_out = in_mem_out;
631
632 t = time(NULL);
633 print_to_mem(DO_STR, IN_MEM_LOG, "\nfsck_hfs started at %s", ctime(&t), NULL);
634
635 if (live_fsck && log_file) {
636 pthread_cond_init(&mem_buf_cond, NULL);
637
638 signal(SIGINT, my_sighandler);
639 signal(SIGHUP, my_sighandler);
640 signal(SIGTERM, my_sighandler);
641 signal(SIGQUIT, my_sighandler);
642 signal(SIGBUS, my_sighandler);
643 signal(SIGSEGV, my_sighandler);
644 signal(SIGILL, my_sighandler);
645
646 pthread_create(&printing_thread, NULL, fsck_printing_thread, NULL);
647 pthread_create(&logging_thread, NULL, fsck_logging_thread, NULL);
648
649 }
650 }
651 }
652
653 if (at_exit_setup == 0 && (log_file || in_mem_log || in_mem_out)) {
654 atexit(shutdown_logging);
655 at_exit_setup = 1;
656 }
657 }
658 }
659
660
661 void
662 print_to_mem(int type, int mem_type, const char *fmt, const char *str, va_list ap)
663 {
664 int ret;
665 size_t size_remaining;
666 va_list ap_copy;
667 char *cur_in_mem;
668 char *in_mem_data;
669 size_t in_mem_data_size;
670
671 if (type == DO_VPRINT) {
672 va_copy(ap_copy, ap);
673 }
674
675 if (mem_type == IN_MEM_LOG) {
676 cur_in_mem = cur_in_mem_log;
677 in_mem_data = in_mem_log;
678 in_mem_data_size = in_mem_log_size;
679 } else {
680 cur_in_mem = cur_in_mem_out;
681 in_mem_data = in_mem_out;
682 in_mem_data_size = in_mem_out_size;
683 }
684
685 /* Grab the lock only when adding output strings to the in-memory data */
686 if (live_fsck && (mem_type == IN_MEM_OUT)) {
687 pthread_mutex_lock(&mem_buf_lock);
688 }
689
690 size_remaining = in_mem_data_size - (ptrdiff_t)(cur_in_mem - in_mem_data);
691 if (type == DO_VPRINT) {
692 ret = vsnprintf(cur_in_mem, size_remaining, fmt, ap);
693 } else {
694 ret = snprintf(cur_in_mem, size_remaining, fmt, str);
695 }
696 if (ret > size_remaining) {
697 char *new_log;
698 size_t amt;
699
700 if (ret >= DEFAULT_IN_MEM_SIZE) {
701 amt = (ret + 4095) & (~4095); // round up to a 4k boundary
702 } else {
703 amt = DEFAULT_IN_MEM_SIZE;
704 }
705
706 new_log = realloc(in_mem_data, in_mem_data_size + amt);
707 if (new_log == NULL) {
708 if (live_fsck && (mem_type == IN_MEM_OUT)) {
709 pthread_cond_signal(&mem_buf_cond);
710 pthread_mutex_unlock(&mem_buf_lock);
711 }
712 goto done;
713 }
714
715 in_mem_data_size += amt;
716 cur_in_mem = new_log + (cur_in_mem - in_mem_data);
717 in_mem_data = new_log;
718 size_remaining = in_mem_data_size - (ptrdiff_t)(cur_in_mem - new_log);
719 if (type == DO_VPRINT) {
720 ret = vsnprintf(cur_in_mem, size_remaining, fmt, ap_copy);
721 } else {
722 ret = snprintf(cur_in_mem, size_remaining, fmt, str);
723 }
724 if (ret <= size_remaining) {
725 cur_in_mem += ret;
726 }
727 } else {
728 cur_in_mem += ret;
729 }
730
731 if (live_fsck && (mem_type == IN_MEM_OUT)) {
732 pthread_cond_signal(&mem_buf_cond);
733 pthread_mutex_unlock(&mem_buf_lock);
734 }
735
736 done:
737
738 if (mem_type == IN_MEM_LOG) {
739 cur_in_mem_log = cur_in_mem;
740 in_mem_log = in_mem_data;
741 in_mem_log_size = in_mem_data_size;
742 } else {
743 cur_in_mem_out = cur_in_mem;
744 in_mem_out = in_mem_data;
745 in_mem_out_size = in_mem_data_size;
746 }
747
748 if (type == DO_VPRINT) {
749 va_end(ap_copy);
750 }
751 }
752
753
754 static int need_prefix=1;
755
756 #define LOG_PREFIX \
757 if (need_prefix) { \
758 fprintf(log_file, "%s: ", cdevname); \
759 if (strchr(fmt, '\n')) { \
760 need_prefix = 1; \
761 } else { \
762 need_prefix = 0; \
763 } \
764 } else if (strchr(fmt, '\n')) { \
765 need_prefix = 1; \
766 }
767
768 /* Print output string on given stream or store it into in-memory buffer */
769 #define VOUT(stream, fmt, ap) \
770 if (!live_fsck) { \
771 vfprintf(stream, fmt, ap); \
772 } else { \
773 print_to_mem(DO_VPRINT, IN_MEM_OUT, fmt, NULL, ap); \
774 }
775
776 #define FOUT(fmt, str) \
777 print_to_mem(DO_STR, IN_MEM_OUT, fmt, str, NULL);
778
779 /* Store output string written to fsck_hfs.log into file or in-memory buffer */
780 #define VLOG(fmt, ap) \
781 va_start(ap, fmt); \
782 VLOG_INTERNAL(fmt, ap);
783
784 #define VLOG_INTERNAL(fmt, ap) \
785 if (log_file && !live_fsck) { \
786 LOG_PREFIX \
787 vfprintf(log_file, fmt, ap); \
788 } else { \
789 print_to_mem(DO_VPRINT, IN_MEM_LOG, fmt, NULL, ap); \
790 }
791
792 #define FLOG(fmt, str) \
793 if (log_file && !live_fsck) { \
794 LOG_PREFIX; \
795 fprintf(log_file, fmt, str); \
796 } else { \
797 print_to_mem(DO_STR, IN_MEM_LOG, fmt, str, NULL); \
798 }
799
800
801 #if __STDC__
802 #include <stdarg.h>
803 #else
804 #include <varargs.h>
805 #endif
806
807 /*
808 * An unexpected inconsistency occurred.
809 * Die if preening, otherwise just print message and continue.
810 */
811 void
812 #if __STDC__
813 pfatal(const char *fmt, ...)
814 #else
815 pfatal(fmt, va_alist)
816 char *fmt;
817 va_dcl
818 #endif
819 {
820 va_list ap;
821
822 setup_logging();
823
824 #if __STDC__
825 va_start(ap, fmt);
826 #else
827 va_start(ap);
828 #endif
829 if (!preen) {
830 (void)vfprintf(stderr, fmt, ap);
831 VLOG(fmt, ap);
832 va_end(ap);
833 return;
834 }
835 if (!live_fsck)
836 (void)fprintf(stderr, "%s: ", cdevname);
837 FLOG("%s: ", cdevname);
838
839 if (!live_fsck)
840 (void)vfprintf(stderr, fmt, ap);
841 VLOG(fmt, ap);
842
843 if (!live_fsck)
844 (void)fprintf(stderr,
845 "\n%s: UNEXPECTED INCONSISTENCY; RUN fsck_hfs MANUALLY.\n",
846 cdevname);
847 FLOG("\n%s: UNEXPECTED INCONSISTENCY; RUN fsck_hfs MANUALLY.\n", cdevname);
848
849 exit(EEXIT);
850 }
851
852 /*
853 * Pwarn just prints a message when not preening,
854 * or a warning (preceded by filename) when preening.
855 */
856 void
857 #if __STDC__
858 pwarn(const char *fmt, ...)
859 #else
860 pwarn(fmt, va_alist)
861 char *fmt;
862 va_dcl
863 #endif
864 {
865 va_list ap;
866
867 setup_logging();
868
869 #if __STDC__
870 va_start(ap, fmt);
871 #else
872 va_start(ap);
873 #endif
874 if (preen) {
875 (void)fprintf(stderr, "%s: ", cdevname);
876 FLOG("%s: ", cdevname);
877 }
878 if (!live_fsck)
879 (void)vfprintf(stderr, fmt, ap);
880 VLOG(fmt, ap);
881
882 va_end(ap);
883 }
884
885 /* Write a string and parameters, if any, directly to the log file.
886 * These strings will not be printed to standard out/error.
887 */
888 void
889 logstring(void *c, const char *str)
890 {
891 llog("%s", str);
892 }
893
894 /* Write a string and parameters, if any, directly to standard out/error.
895 * These strings will not be printed to log file.
896 */
897 void
898 outstring(void *c, const char *str)
899 {
900 olog("%s", str);
901 }
902
903 /* Write to both standard out and log file */
904 void
905 plog(const char *fmt, ...)
906 {
907 va_list ap;
908 va_start(ap, fmt);
909 vplog(fmt, ap);
910 va_end(ap);
911 }
912
913 /* Write to only standard out */
914 void
915 olog(const char *fmt, ...)
916 {
917 va_list ap;
918 va_start(ap, fmt);
919
920 setup_logging();
921
922 /* For live fsck_hfs, add output strings to in-memory log,
923 * and for non-live fsck_hfs, print output to stdout.
924 */
925 VOUT(stdout, fmt, ap);
926
927 va_end(ap);
928 }
929
930 /* Write to only log file */
931 void
932 llog(const char *fmt, ...)
933 {
934 va_list ap;
935 va_start(ap, fmt);
936
937 setup_logging();
938 need_prefix = 1;
939 VLOG(fmt, ap);
940
941 va_end(ap);
942 }
943
944 /* Write to both standard out and log file */
945 void
946 vplog(const char *fmt, va_list ap)
947 {
948 va_list copy_ap;
949
950 va_copy(copy_ap, ap);
951
952 setup_logging();
953
954 /* Always print prefix to strings written to log files */
955 need_prefix = 1;
956
957 /* Handle output strings, print to stdout or store in-memory */
958 VOUT(stdout, fmt, ap);
959
960 /* Add log strings to the log file. VLOG() handles live case internally */
961 VLOG_INTERNAL(fmt, copy_ap);
962 }
963
964 /* Write to both standard out and log file */
965 void
966 fplog(FILE *stream, const char *fmt, ...)
967 {
968 va_list ap, copy_ap;
969 va_start(ap, fmt);
970 va_copy(copy_ap, ap);
971
972 setup_logging();
973 need_prefix = 1;
974
975 /* Handle output strings, print to given stream or store in-memory */
976 VOUT(stream, fmt, ap);
977
978 /* Add log strings to the log file. VLOG() handles live case internally */
979 VLOG(fmt, copy_ap);
980
981 va_end(ap);
982 }
983
984 #define kProgressToggle "kern.progressmeterenable"
985 #define kProgress "kern.progressmeter"
986
987 void
988 start_progress(void)
989 {
990 int rv;
991 int enable = 1;
992 if (hotroot == 0)
993 return;
994 rv = sysctlbyname(kProgressToggle, NULL, NULL, &enable, sizeof(enable));
995 if (debug && rv == -1 && errno != ENOENT) {
996 warn("sysctl(%s) failed", kProgressToggle);
997 }
998 }
999
1000 void
1001 draw_progress(int pct)
1002 {
1003 int rv;
1004 if (hotroot == 0)
1005 return;
1006 rv = sysctlbyname(kProgress, NULL, NULL, &pct, sizeof(pct));
1007 if (debug && rv == -1 && errno != ENOENT) {
1008 warn("sysctl(%s) failed", kProgress);
1009 }
1010 }
1011
1012 void
1013 end_progress(void)
1014 {
1015 int rv;
1016 int enable = 0;
1017 if (hotroot == 0)
1018 return;
1019 rv = sysctlbyname(kProgressToggle, NULL, NULL, &enable, sizeof(enable));
1020 if (debug && rv == -1 && errno != ENOENT) {
1021 warn("sysctl(%s) failed", kProgressToggle);
1022 }
1023 }
1024