19b50d902SRodney W. Grimes /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 49b50d902SRodney W. Grimes * Copyright (c) 1991, 1993, 1994 59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 69b50d902SRodney W. Grimes * 79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 99b50d902SRodney W. Grimes * are met: 109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 179b50d902SRodney W. Grimes * without specific prior written permission. 189b50d902SRodney W. Grimes * 199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 299b50d902SRodney W. Grimes * SUCH DAMAGE. 309b50d902SRodney W. Grimes */ 319b50d902SRodney W. Grimes 329f5b04e9SDavid Malone #if 0 339b50d902SRodney W. Grimes #ifndef lint 349f5b04e9SDavid Malone static char sccsid[] = "@(#)regular.c 8.3 (Berkeley) 4/2/94"; 356af414cbSMark Murray #endif 369f5b04e9SDavid Malone #endif 379f5b04e9SDavid Malone 389f5b04e9SDavid Malone #include <sys/cdefs.h> 399f5b04e9SDavid Malone __FBSDID("$FreeBSD$"); 409b50d902SRodney W. Grimes 419b50d902SRodney W. Grimes #include <sys/param.h> 429b50d902SRodney W. Grimes #include <sys/mman.h> 439b50d902SRodney W. Grimes #include <sys/stat.h> 449b50d902SRodney W. Grimes 452528b7e2SMark Johnston #include <capsicum_helpers.h> 469b50d902SRodney W. Grimes #include <err.h> 479b50d902SRodney W. Grimes #include <limits.h> 48d4890512SDavid Schultz #include <signal.h> 499b50d902SRodney W. Grimes #include <stdlib.h> 509b50d902SRodney W. Grimes #include <stdio.h> 51c28065c6SJoerg Wunsch #include <unistd.h> 529b50d902SRodney W. Grimes 539b50d902SRodney W. Grimes #include "extern.h" 549b50d902SRodney W. Grimes 55f1bb2cd2SWarner Losh static u_char *remmap(u_char *, int, off_t); 56d4890512SDavid Schultz static void segv_handler(int); 5760b49f05SDavid Malone #define MMAP_CHUNK (8*1024*1024) 5860b49f05SDavid Malone 59c28065c6SJoerg Wunsch #define ROUNDPAGE(i) ((i) & ~pagemask) 60c28065c6SJoerg Wunsch 619b50d902SRodney W. Grimes void 62f2e8e0daSDavid Malone c_regular(int fd1, const char *file1, off_t skip1, off_t len1, 634e380e84SKyle Evans int fd2, const char *file2, off_t skip2, off_t len2, off_t limit) 649b50d902SRodney W. Grimes { 652528b7e2SMark Johnston struct sigaction act, oact; 662528b7e2SMark Johnston cap_rights_t rights; 6760b49f05SDavid Malone u_char ch, *p1, *p2, *m1, *m2, *e1, *e2; 689b50d902SRodney W. Grimes off_t byte, length, line; 69c28065c6SJoerg Wunsch off_t pagemask, off1, off2; 70ac022265SBrian Feldman size_t pagesize; 712528b7e2SMark Johnston int dfound; 729b50d902SRodney W. Grimes 739b50d902SRodney W. Grimes if (skip1 > len1) 749b50d902SRodney W. Grimes eofmsg(file1); 759b50d902SRodney W. Grimes len1 -= skip1; 769b50d902SRodney W. Grimes if (skip2 > len2) 779b50d902SRodney W. Grimes eofmsg(file2); 789b50d902SRodney W. Grimes len2 -= skip2; 799b50d902SRodney W. Grimes 8084ad3d8fSBrian Feldman if (sflag && len1 != len2) 8184ad3d8fSBrian Feldman exit(DIFF_EXIT); 8284ad3d8fSBrian Feldman 83ac022265SBrian Feldman pagesize = getpagesize(); 84ac022265SBrian Feldman pagemask = (off_t)pagesize - 1; 85c28065c6SJoerg Wunsch off1 = ROUNDPAGE(skip1); 86c28065c6SJoerg Wunsch off2 = ROUNDPAGE(skip2); 87c28065c6SJoerg Wunsch 889b50d902SRodney W. Grimes length = MIN(len1, len2); 894e380e84SKyle Evans if (limit > 0) 904e380e84SKyle Evans length = MIN(length, limit); 919b50d902SRodney W. Grimes 9260b49f05SDavid Malone if ((m1 = remmap(NULL, fd1, off1)) == NULL) { 934e380e84SKyle Evans c_special(fd1, file1, skip1, fd2, file2, skip2, limit); 9460b49f05SDavid Malone return; 9560b49f05SDavid Malone } 962ae09ad8SJohn Dyson 9760b49f05SDavid Malone if ((m2 = remmap(NULL, fd2, off2)) == NULL) { 9860b49f05SDavid Malone munmap(m1, MMAP_CHUNK); 994e380e84SKyle Evans c_special(fd1, file1, skip1, fd2, file2, skip2, limit); 10060b49f05SDavid Malone return; 10160b49f05SDavid Malone } 1029b50d902SRodney W. Grimes 1032528b7e2SMark Johnston if (caph_rights_limit(fd1, cap_rights_init(&rights, CAP_MMAP_R)) < 0) 1042528b7e2SMark Johnston err(1, "unable to limit rights for %s", file1); 1052528b7e2SMark Johnston if (caph_rights_limit(fd2, cap_rights_init(&rights, CAP_MMAP_R)) < 0) 1062528b7e2SMark Johnston err(1, "unable to limit rights for %s", file2); 1072528b7e2SMark Johnston if (caph_enter() < 0) 1082528b7e2SMark Johnston err(ERR_EXIT, "unable to enter capability mode"); 1092528b7e2SMark Johnston 1102528b7e2SMark Johnston sigemptyset(&act.sa_mask); 1112528b7e2SMark Johnston act.sa_flags = SA_NODEFER; 1122528b7e2SMark Johnston act.sa_handler = segv_handler; 1132528b7e2SMark Johnston if (sigaction(SIGSEGV, &act, &oact)) 1142528b7e2SMark Johnston err(ERR_EXIT, "sigaction()"); 1152528b7e2SMark Johnston 1169b50d902SRodney W. Grimes dfound = 0; 11760b49f05SDavid Malone e1 = m1 + MMAP_CHUNK; 11860b49f05SDavid Malone e2 = m2 + MMAP_CHUNK; 11960b49f05SDavid Malone p1 = m1 + (skip1 - off1); 12060b49f05SDavid Malone p2 = m2 + (skip2 - off2); 12160b49f05SDavid Malone 12260b49f05SDavid Malone for (byte = line = 1; length--; ++byte) { 12396846ff6SWarner Losh if ((ch = *p1) != *p2) { 124e03983a3SPoul-Henning Kamp if (xflag) { 125e03983a3SPoul-Henning Kamp dfound = 1; 1265748d9baSDavid Malone (void)printf("%08llx %02x %02x\n", 1275748d9baSDavid Malone (long long)byte - 1, ch, *p2); 128e03983a3SPoul-Henning Kamp } else if (lflag) { 1299b50d902SRodney W. Grimes dfound = 1; 130*f66b9b40SKyle Evans if (bflag) 131*f66b9b40SKyle Evans (void)printf("%6lld %3o %c %3o %c\n", 132*f66b9b40SKyle Evans (long long)byte, ch, ch, *p2, *p2); 133*f66b9b40SKyle Evans else 1345748d9baSDavid Malone (void)printf("%6lld %3o %3o\n", 1355748d9baSDavid Malone (long long)byte, ch, *p2); 1369b50d902SRodney W. Grimes } else 137*f66b9b40SKyle Evans diffmsg(file1, file2, byte, line, ch, *p2); 1389b50d902SRodney W. Grimes /* NOTREACHED */ 13996846ff6SWarner Losh } 1409b50d902SRodney W. Grimes if (ch == '\n') 1419b50d902SRodney W. Grimes ++line; 14260b49f05SDavid Malone if (++p1 == e1) { 14360b49f05SDavid Malone off1 += MMAP_CHUNK; 14460b49f05SDavid Malone if ((p1 = m1 = remmap(m1, fd1, off1)) == NULL) { 14560b49f05SDavid Malone munmap(m2, MMAP_CHUNK); 14660b49f05SDavid Malone err(ERR_EXIT, "remmap %s", file1); 1479b50d902SRodney W. Grimes } 14860b49f05SDavid Malone e1 = m1 + MMAP_CHUNK; 14960b49f05SDavid Malone } 15060b49f05SDavid Malone if (++p2 == e2) { 15160b49f05SDavid Malone off2 += MMAP_CHUNK; 15260b49f05SDavid Malone if ((p2 = m2 = remmap(m2, fd2, off2)) == NULL) { 15360b49f05SDavid Malone munmap(m1, MMAP_CHUNK); 15460b49f05SDavid Malone err(ERR_EXIT, "remmap %s", file2); 15560b49f05SDavid Malone } 15660b49f05SDavid Malone e2 = m2 + MMAP_CHUNK; 15760b49f05SDavid Malone } 15860b49f05SDavid Malone } 15960b49f05SDavid Malone munmap(m1, MMAP_CHUNK); 16060b49f05SDavid Malone munmap(m2, MMAP_CHUNK); 1619b50d902SRodney W. Grimes 162d4890512SDavid Schultz if (sigaction(SIGSEGV, &oact, NULL)) 163d4890512SDavid Schultz err(ERR_EXIT, "sigaction()"); 164d4890512SDavid Schultz 1659b50d902SRodney W. Grimes if (len1 != len2) 1669b50d902SRodney W. Grimes eofmsg (len1 > len2 ? file2 : file1); 1679b50d902SRodney W. Grimes if (dfound) 1689b50d902SRodney W. Grimes exit(DIFF_EXIT); 1699b50d902SRodney W. Grimes } 17060b49f05SDavid Malone 17160b49f05SDavid Malone static u_char * 172f2e8e0daSDavid Malone remmap(u_char *mem, int fd, off_t offset) 17360b49f05SDavid Malone { 17460b49f05SDavid Malone if (mem != NULL) 17560b49f05SDavid Malone munmap(mem, MMAP_CHUNK); 17660b49f05SDavid Malone mem = mmap(NULL, MMAP_CHUNK, PROT_READ, MAP_SHARED, fd, offset); 17760b49f05SDavid Malone if (mem == MAP_FAILED) 17860b49f05SDavid Malone return (NULL); 17960b49f05SDavid Malone madvise(mem, MMAP_CHUNK, MADV_SEQUENTIAL); 18060b49f05SDavid Malone return (mem); 18160b49f05SDavid Malone } 182d4890512SDavid Schultz 183d4890512SDavid Schultz static void 184dd999839SPhilippe Charnier segv_handler(int sig __unused) { 185d4890512SDavid Schultz static const char msg[] = "cmp: Input/output error (caught SIGSEGV)\n"; 186d4890512SDavid Schultz 187d4890512SDavid Schultz write(STDERR_FILENO, msg, sizeof(msg)); 188d4890512SDavid Schultz _exit(EXIT_FAILURE); 189d4890512SDavid Schultz } 190