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 #include <sys/capsicum.h>
50 #include <sys/param.h>
52 #ifndef NO_UDOM_SUPPORT
53 #include <sys/socket.h>
58 #include <capsicum_helpers.h>
71 #include <libcasper.h>
72 #include <casper/cap_fileargs.h>
73 #include <casper/cap_net.h>
75 static int bflag, eflag, lflag, nflag, sflag, tflag, vflag;
77 static const char *filename;
78 static fileargs_t *fa;
80 static void usage(void) __dead2;
81 static void scanfiles(char *argv[], int cooked);
83 static void cook_cat(FILE *);
84 static ssize_t in_kernel_copy(int);
86 static void raw_cat(int);
88 #ifndef NO_UDOM_SUPPORT
89 static cap_channel_t *capnet;
91 static int udom_open(const char *path, int flags);
95 * Memory strategy threshold, in pages: if physmem is larger than this,
98 #define PHYSPAGES_THRESHOLD (32 * 1024)
100 /* Maximum buffer size in bytes - do not allow it to grow larger than this. */
101 #define BUFSIZE_MAX (2 * 1024 * 1024)
104 * Small (default) buffer size in bytes. It's inefficient for this to be
105 * smaller than MAXPHYS.
107 #define BUFSIZE_SMALL (MAXPHYS)
111 * For the bootstrapped cat binary (needed for locked appending to METALOG), we
112 * disable all flags except -l and -u to avoid non-portable function calls.
113 * In the future we may instead want to write a small portable bootstrap tool
114 * that locks the output file before writing to it. However, for now
115 * bootstrapping cat without multibyte support is the simpler solution.
118 #define SUPPORTED_FLAGS "lu"
120 #define SUPPORTED_FLAGS "belnstuv"
123 #ifndef NO_UDOM_SUPPORT
125 init_casper_net(cap_channel_t *casper)
127 cap_net_limit_t *limit;
130 capnet = cap_service_open(casper, "system.net");
132 err(EXIT_FAILURE, "unable to create network service");
134 limit = cap_net_limit_init(capnet, CAPNET_NAME2ADDR |
137 err(EXIT_FAILURE, "unable to create limits");
139 familylimit = AF_LOCAL;
140 cap_net_limit_name2addr_family(limit, &familylimit, 1);
142 if (cap_net_limit(limit) < 0)
143 err(EXIT_FAILURE, "unable to apply limits");
148 init_casper(int argc, char *argv[])
150 cap_channel_t *casper;
155 err(EXIT_FAILURE, "unable to create Casper");
157 fa = fileargs_cinit(casper, argc, argv, O_RDONLY, 0,
158 cap_rights_init(&rights, CAP_READ | CAP_FSTAT | CAP_FCNTL),
159 FA_OPEN | FA_REALPATH);
161 err(EXIT_FAILURE, "unable to create fileargs");
163 #ifndef NO_UDOM_SUPPORT
164 init_casper_net(casper);
171 main(int argc, char *argv[])
174 struct flock stdout_lock;
176 setlocale(LC_CTYPE, "");
178 while ((ch = getopt(argc, argv, SUPPORTED_FLAGS)) != -1)
181 bflag = nflag = 1; /* -b implies -n */
184 eflag = vflag = 1; /* -e implies -v */
196 tflag = vflag = 1; /* -t implies -v */
199 setbuf(stdout, NULL);
211 stdout_lock.l_len = 0;
212 stdout_lock.l_start = 0;
213 stdout_lock.l_type = F_WRLCK;
214 stdout_lock.l_whence = SEEK_SET;
215 if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1)
216 err(EXIT_FAILURE, "stdout");
219 init_casper(argc, argv);
221 caph_cache_catpages();
223 if (caph_enter_casper() < 0)
224 err(EXIT_FAILURE, "capsicum");
226 if (bflag || eflag || nflag || sflag || tflag || vflag)
240 fprintf(stderr, "usage: cat [-" SUPPORTED_FLAGS "] [file ...]\n");
246 scanfiles(char *argv[], int cooked __unused)
250 #ifndef BOOTSTRAP_CAT
256 while ((path = argv[i]) != NULL || i == 0) {
257 if (path == NULL || strcmp(path, "-") == 0) {
262 fd = fileargs_open(fa, path);
263 #ifndef NO_UDOM_SUPPORT
264 if (fd < 0 && errno == EOPNOTSUPP)
265 fd = udom_open(path, O_RDONLY);
271 #ifndef BOOTSTRAP_CAT
273 if (fd == STDIN_FILENO)
276 fp = fdopen(fd, "r");
282 #ifndef BOOTSTRAP_CAT
283 if (in_kernel_copy(fd) == -1) {
284 if (errno == EINVAL || errno == EBADF)
292 if (fd != STDIN_FILENO)
301 #ifndef BOOTSTRAP_CAT
305 int ch, gobble, line, prev;
308 /* Reset EOF condition on stdin. */
309 if (fp == stdin && feof(stdin))
313 for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
324 if (!bflag || ch != '\n') {
325 (void)fprintf(stdout, "%6d\t", ++line);
329 (void)fprintf(stdout, "%6s\t", "");
336 if (eflag && putchar('$') == EOF)
338 } else if (ch == '\t') {
340 if (putchar('^') == EOF || putchar('I') == EOF)
345 (void)ungetc(ch, fp);
347 * Our getwc(3) doesn't change file position
350 if ((wch = getwc(fp)) == WEOF) {
351 if (ferror(fp) && errno == EILSEQ) {
353 /* Resync attempt. */
354 memset(&fp->_mbstate, 0, sizeof(mbstate_t));
355 if ((ch = getc(fp)) == EOF)
362 if (!iswascii(wch) && !iswprint(wch)) {
364 if (putchar('M') == EOF || putchar('-') == EOF)
370 ch = (ch == '\177') ? '?' : (ch | 0100);
371 if (putchar('^') == EOF || putchar(ch) == EOF)
375 if (putwchar(wch) == WEOF)
380 if (putchar(ch) == EOF)
384 warn("%s", filename);
393 in_kernel_copy(int rfd)
398 wfd = fileno(stdout);
402 ret = copy_file_range(rfd, NULL, wfd, NULL, SSIZE_MAX, 0);
406 #endif /* BOOTSTRAP_CAT */
415 static char *buf = NULL;
418 wfd = fileno(stdout);
420 if (fstat(wfd, &sbuf))
422 if (S_ISREG(sbuf.st_mode)) {
423 /* If there's plenty of RAM, use a large copy buffer */
424 if (sysconf(_SC_PHYS_PAGES) > PHYSPAGES_THRESHOLD)
425 bsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
427 bsize = BUFSIZE_SMALL;
429 bsize = sbuf.st_blksize;
430 pagesize = sysconf(_SC_PAGESIZE);
432 bsize = MAX(bsize, (size_t)pagesize);
434 if ((buf = malloc(bsize)) == NULL)
435 err(1, "malloc() failure of IO buffer");
437 while ((nr = read(rfd, buf, bsize)) > 0)
438 for (off = 0; nr; nr -= nw, off += nw)
439 if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
442 warn("%s", filename);
447 #ifndef NO_UDOM_SUPPORT
450 udom_open(const char *path, int flags)
452 struct addrinfo hints, *res, *res0;
453 char rpath[PATH_MAX];
454 int error, fd, serrno;
458 * Construct the unix domain socket address and attempt to connect.
460 bzero(&hints, sizeof(hints));
461 hints.ai_family = AF_LOCAL;
463 if (fileargs_realpath(fa, path, rpath) == NULL)
466 error = cap_getaddrinfo(capnet, rpath, NULL, &hints, &res0);
468 warn("%s", gai_strerror(error));
472 cap_rights_init(&rights, CAP_CONNECT, CAP_READ, CAP_WRITE,
473 CAP_SHUTDOWN, CAP_FSTAT, CAP_FCNTL);
475 /* Default error if something goes wrong. */
478 for (res = res0; res != NULL; res = res->ai_next) {
479 fd = socket(res->ai_family, res->ai_socktype,
487 if (caph_rights_limit(fd, &rights) < 0) {
494 error = cap_connect(capnet, fd, res->ai_addr, res->ai_addrlen);
510 * handle the open flags by shutting down appropriate directions
513 switch (flags & O_ACCMODE) {
515 cap_rights_clear(&rights, CAP_WRITE);
516 if (shutdown(fd, SHUT_WR) == -1)
520 cap_rights_clear(&rights, CAP_READ);
521 if (shutdown(fd, SHUT_RD) == -1)
528 cap_rights_clear(&rights, CAP_CONNECT, CAP_SHUTDOWN);
529 if (caph_rights_limit(fd, &rights) < 0) {