2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char const copyright[] =
38 "@(#) Copyright (c) 1989, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)cat.c 8.2 (Berkeley) 4/27/95";
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 #include <sys/capsicum.h>
52 #include <sys/param.h>
54 #ifndef NO_UDOM_SUPPORT
55 #include <sys/socket.h>
60 #include <capsicum_helpers.h>
73 #include <libcasper.h>
74 #include <casper/cap_fileargs.h>
75 #include <casper/cap_net.h>
77 static int bflag, eflag, lflag, nflag, sflag, tflag, vflag;
79 static const char *filename;
80 static fileargs_t *fa;
82 static void usage(void) __dead2;
83 static void scanfiles(char *argv[], int cooked);
85 static void cook_cat(FILE *);
86 static ssize_t in_kernel_copy(int);
88 static void raw_cat(int);
90 #ifndef NO_UDOM_SUPPORT
91 static cap_channel_t *capnet;
93 static int udom_open(const char *path, int flags);
97 * Memory strategy threshold, in pages: if physmem is larger than this,
100 #define PHYSPAGES_THRESHOLD (32 * 1024)
102 /* Maximum buffer size in bytes - do not allow it to grow larger than this. */
103 #define BUFSIZE_MAX (2 * 1024 * 1024)
106 * Small (default) buffer size in bytes. It's inefficient for this to be
107 * smaller than MAXPHYS.
109 #define BUFSIZE_SMALL (MAXPHYS)
113 * For the bootstrapped cat binary (needed for locked appending to METALOG), we
114 * disable all flags except -l and -u to avoid non-portable function calls.
115 * In the future we may instead want to write a small portable bootstrap tool
116 * that locks the output file before writing to it. However, for now
117 * bootstrapping cat without multibyte support is the simpler solution.
120 #define SUPPORTED_FLAGS "lu"
122 #define SUPPORTED_FLAGS "belnstuv"
125 #ifndef NO_UDOM_SUPPORT
127 init_casper_net(cap_channel_t *casper)
129 cap_net_limit_t *limit;
132 capnet = cap_service_open(casper, "system.net");
134 err(EXIT_FAILURE, "unable to create network service");
136 limit = cap_net_limit_init(capnet, CAPNET_NAME2ADDR |
139 err(EXIT_FAILURE, "unable to create limits");
141 familylimit = AF_LOCAL;
142 cap_net_limit_name2addr_family(limit, &familylimit, 1);
144 if (cap_net_limit(limit) < 0)
145 err(EXIT_FAILURE, "unable to apply limits");
150 init_casper(int argc, char *argv[])
152 cap_channel_t *casper;
157 err(EXIT_FAILURE, "unable to create Casper");
159 fa = fileargs_cinit(casper, argc, argv, O_RDONLY, 0,
160 cap_rights_init(&rights, CAP_READ | CAP_FSTAT | CAP_FCNTL),
161 FA_OPEN | FA_REALPATH);
163 err(EXIT_FAILURE, "unable to create fileargs");
165 #ifndef NO_UDOM_SUPPORT
166 init_casper_net(casper);
173 main(int argc, char *argv[])
176 struct flock stdout_lock;
178 setlocale(LC_CTYPE, "");
180 while ((ch = getopt(argc, argv, SUPPORTED_FLAGS)) != -1)
183 bflag = nflag = 1; /* -b implies -n */
186 eflag = vflag = 1; /* -e implies -v */
198 tflag = vflag = 1; /* -t implies -v */
201 setbuf(stdout, NULL);
213 stdout_lock.l_len = 0;
214 stdout_lock.l_start = 0;
215 stdout_lock.l_type = F_WRLCK;
216 stdout_lock.l_whence = SEEK_SET;
217 if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1)
218 err(EXIT_FAILURE, "stdout");
221 init_casper(argc, argv);
223 caph_cache_catpages();
225 if (caph_enter_casper() < 0)
226 err(EXIT_FAILURE, "capsicum");
228 if (bflag || eflag || nflag || sflag || tflag || vflag)
242 fprintf(stderr, "usage: cat [-" SUPPORTED_FLAGS "] [file ...]\n");
248 scanfiles(char *argv[], int cooked __unused)
252 #ifndef BOOTSTRAP_CAT
258 while ((path = argv[i]) != NULL || i == 0) {
259 if (path == NULL || strcmp(path, "-") == 0) {
264 fd = fileargs_open(fa, path);
265 #ifndef NO_UDOM_SUPPORT
266 if (fd < 0 && errno == EOPNOTSUPP)
267 fd = udom_open(path, O_RDONLY);
273 #ifndef BOOTSTRAP_CAT
275 if (fd == STDIN_FILENO)
278 fp = fdopen(fd, "r");
284 #ifndef BOOTSTRAP_CAT
285 if (in_kernel_copy(fd) == -1) {
286 if (errno == EINVAL || errno == EBADF)
294 if (fd != STDIN_FILENO)
303 #ifndef BOOTSTRAP_CAT
307 int ch, gobble, line, prev;
310 /* Reset EOF condition on stdin. */
311 if (fp == stdin && feof(stdin))
315 for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
326 if (!bflag || ch != '\n') {
327 (void)fprintf(stdout, "%6d\t", ++line);
331 (void)fprintf(stdout, "%6s\t", "");
338 if (eflag && putchar('$') == EOF)
340 } else if (ch == '\t') {
342 if (putchar('^') == EOF || putchar('I') == EOF)
347 (void)ungetc(ch, fp);
349 * Our getwc(3) doesn't change file position
352 if ((wch = getwc(fp)) == WEOF) {
353 if (ferror(fp) && errno == EILSEQ) {
355 /* Resync attempt. */
356 memset(&fp->_mbstate, 0, sizeof(mbstate_t));
357 if ((ch = getc(fp)) == EOF)
364 if (!iswascii(wch) && !iswprint(wch)) {
366 if (putchar('M') == EOF || putchar('-') == EOF)
372 ch = (ch == '\177') ? '?' : (ch | 0100);
373 if (putchar('^') == EOF || putchar(ch) == EOF)
377 if (putwchar(wch) == WEOF)
382 if (putchar(ch) == EOF)
386 warn("%s", filename);
395 in_kernel_copy(int rfd)
400 wfd = fileno(stdout);
404 ret = copy_file_range(rfd, NULL, wfd, NULL, SSIZE_MAX, 0);
408 #endif /* BOOTSTRAP_CAT */
417 static char *buf = NULL;
420 wfd = fileno(stdout);
422 if (fstat(wfd, &sbuf))
424 if (S_ISREG(sbuf.st_mode)) {
425 /* If there's plenty of RAM, use a large copy buffer */
426 if (sysconf(_SC_PHYS_PAGES) > PHYSPAGES_THRESHOLD)
427 bsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
429 bsize = BUFSIZE_SMALL;
431 bsize = sbuf.st_blksize;
432 pagesize = sysconf(_SC_PAGESIZE);
434 bsize = MAX(bsize, (size_t)pagesize);
436 if ((buf = malloc(bsize)) == NULL)
437 err(1, "malloc() failure of IO buffer");
439 while ((nr = read(rfd, buf, bsize)) > 0)
440 for (off = 0; nr; nr -= nw, off += nw)
441 if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
444 warn("%s", filename);
449 #ifndef NO_UDOM_SUPPORT
452 udom_open(const char *path, int flags)
454 struct addrinfo hints, *res, *res0;
455 char rpath[PATH_MAX];
456 int error, fd, serrno;
460 * Construct the unix domain socket address and attempt to connect.
462 bzero(&hints, sizeof(hints));
463 hints.ai_family = AF_LOCAL;
465 if (fileargs_realpath(fa, path, rpath) == NULL)
468 error = cap_getaddrinfo(capnet, rpath, NULL, &hints, &res0);
470 warn("%s", gai_strerror(error));
474 cap_rights_init(&rights, CAP_CONNECT, CAP_READ, CAP_WRITE,
475 CAP_SHUTDOWN, CAP_FSTAT, CAP_FCNTL);
477 /* Default error if something goes wrong. */
480 for (res = res0; res != NULL; res = res->ai_next) {
481 fd = socket(res->ai_family, res->ai_socktype,
489 if (caph_rights_limit(fd, &rights) < 0) {
496 error = cap_connect(capnet, fd, res->ai_addr, res->ai_addrlen);
512 * handle the open flags by shutting down appropriate directions
515 switch (flags & O_ACCMODE) {
517 cap_rights_clear(&rights, CAP_WRITE);
518 if (shutdown(fd, SHUT_WR) == -1)
522 cap_rights_clear(&rights, CAP_READ);
523 if (shutdown(fd, SHUT_RD) == -1)
530 cap_rights_clear(&rights, CAP_CONNECT, CAP_SHUTDOWN);
531 if (caph_rights_limit(fd, &rights) < 0) {