1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1991, 1993, 1994 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 #if 0 33 #ifndef lint 34 static char sccsid[] = "@(#)regular.c 8.3 (Berkeley) 4/2/94"; 35 #endif 36 #endif 37 38 #include <sys/cdefs.h> 39 #include <sys/param.h> 40 #include <sys/mman.h> 41 #include <sys/stat.h> 42 43 #include <capsicum_helpers.h> 44 #include <err.h> 45 #include <limits.h> 46 #include <signal.h> 47 #include <stdlib.h> 48 #include <stdio.h> 49 #include <unistd.h> 50 51 #include "extern.h" 52 53 static u_char *remmap(u_char *, int, off_t); 54 static void segv_handler(int); 55 #define MMAP_CHUNK (8*1024*1024) 56 57 #define ROUNDPAGE(i) ((i) & ~pagemask) 58 59 void 60 c_regular(int fd1, const char *file1, off_t skip1, off_t len1, 61 int fd2, const char *file2, off_t skip2, off_t len2, off_t limit) 62 { 63 struct sigaction act, oact; 64 cap_rights_t rights; 65 u_char ch, *p1, *p2, *m1, *m2, *e1, *e2; 66 off_t byte, length, line; 67 off_t pagemask, off1, off2; 68 size_t pagesize; 69 int dfound; 70 71 if (skip1 > len1) 72 eofmsg(file1); 73 len1 -= skip1; 74 if (skip2 > len2) 75 eofmsg(file2); 76 len2 -= skip2; 77 78 if (sflag && len1 != len2) 79 exit(DIFF_EXIT); 80 81 pagesize = getpagesize(); 82 pagemask = (off_t)pagesize - 1; 83 off1 = ROUNDPAGE(skip1); 84 off2 = ROUNDPAGE(skip2); 85 86 length = MIN(len1, len2); 87 if (limit > 0) 88 length = MIN(length, limit); 89 90 if ((m1 = remmap(NULL, fd1, off1)) == NULL) { 91 c_special(fd1, file1, skip1, fd2, file2, skip2, limit); 92 return; 93 } 94 95 if ((m2 = remmap(NULL, fd2, off2)) == NULL) { 96 munmap(m1, MMAP_CHUNK); 97 c_special(fd1, file1, skip1, fd2, file2, skip2, limit); 98 return; 99 } 100 101 if (caph_rights_limit(fd1, cap_rights_init(&rights, CAP_MMAP_R)) < 0) 102 err(1, "unable to limit rights for %s", file1); 103 if (caph_rights_limit(fd2, cap_rights_init(&rights, CAP_MMAP_R)) < 0) 104 err(1, "unable to limit rights for %s", file2); 105 if (caph_enter() < 0) 106 err(ERR_EXIT, "unable to enter capability mode"); 107 108 sigemptyset(&act.sa_mask); 109 act.sa_flags = SA_NODEFER; 110 act.sa_handler = segv_handler; 111 if (sigaction(SIGSEGV, &act, &oact)) 112 err(ERR_EXIT, "sigaction()"); 113 114 dfound = 0; 115 e1 = m1 + MMAP_CHUNK; 116 e2 = m2 + MMAP_CHUNK; 117 p1 = m1 + (skip1 - off1); 118 p2 = m2 + (skip2 - off2); 119 120 for (byte = line = 1; length--; ++byte) { 121 #ifdef SIGINFO 122 if (info) { 123 (void)fprintf(stderr, "%s %s char %zu line %zu\n", 124 file1, file2, (size_t)byte, (size_t)line); 125 info = 0; 126 } 127 #endif 128 if ((ch = *p1) != *p2) { 129 if (xflag) { 130 dfound = 1; 131 (void)printf("%08llx %02x %02x\n", 132 (long long)byte - 1, ch, *p2); 133 } else if (lflag) { 134 dfound = 1; 135 if (bflag) 136 (void)printf("%6lld %3o %c %3o %c\n", 137 (long long)byte, ch, ch, *p2, *p2); 138 else 139 (void)printf("%6lld %3o %3o\n", 140 (long long)byte, ch, *p2); 141 } else 142 diffmsg(file1, file2, byte, line, ch, *p2); 143 /* NOTREACHED */ 144 } 145 if (ch == '\n') 146 ++line; 147 if (++p1 == e1) { 148 off1 += MMAP_CHUNK; 149 if ((p1 = m1 = remmap(m1, fd1, off1)) == NULL) { 150 munmap(m2, MMAP_CHUNK); 151 err(ERR_EXIT, "remmap %s", file1); 152 } 153 e1 = m1 + MMAP_CHUNK; 154 } 155 if (++p2 == e2) { 156 off2 += MMAP_CHUNK; 157 if ((p2 = m2 = remmap(m2, fd2, off2)) == NULL) { 158 munmap(m1, MMAP_CHUNK); 159 err(ERR_EXIT, "remmap %s", file2); 160 } 161 e2 = m2 + MMAP_CHUNK; 162 } 163 } 164 munmap(m1, MMAP_CHUNK); 165 munmap(m2, MMAP_CHUNK); 166 167 if (sigaction(SIGSEGV, &oact, NULL)) 168 err(ERR_EXIT, "sigaction()"); 169 170 if (len1 != len2) 171 eofmsg (len1 > len2 ? file2 : file1); 172 if (dfound) 173 exit(DIFF_EXIT); 174 } 175 176 static u_char * 177 remmap(u_char *mem, int fd, off_t offset) 178 { 179 if (mem != NULL) 180 munmap(mem, MMAP_CHUNK); 181 mem = mmap(NULL, MMAP_CHUNK, PROT_READ, MAP_SHARED, fd, offset); 182 if (mem == MAP_FAILED) 183 return (NULL); 184 madvise(mem, MMAP_CHUNK, MADV_SEQUENTIAL); 185 return (mem); 186 } 187 188 static void 189 segv_handler(int sig __unused) { 190 static const char msg[] = "cmp: Input/output error (caught SIGSEGV)\n"; 191 192 write(STDERR_FILENO, msg, sizeof(msg)); 193 _exit(EXIT_FAILURE); 194 } 195