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 *);
87 static void raw_cat(int);
89 #ifndef NO_UDOM_SUPPORT
90 static cap_channel_t *capnet;
92 static int udom_open(const char *path, int flags);
96 * Memory strategy threshold, in pages: if physmem is larger than this,
99 #define PHYSPAGES_THRESHOLD (32 * 1024)
101 /* Maximum buffer size in bytes - do not allow it to grow larger than this. */
102 #define BUFSIZE_MAX (2 * 1024 * 1024)
105 * Small (default) buffer size in bytes. It's inefficient for this to be
106 * smaller than MAXPHYS.
108 #define BUFSIZE_SMALL (MAXPHYS)
112 * For the bootstrapped cat binary (needed for locked appending to METALOG), we
113 * disable all flags except -l and -u to avoid non-portable function calls.
114 * In the future we may instead want to write a small portable bootstrap tool
115 * that locks the output file before writing to it. However, for now
116 * bootstrapping cat without multibyte support is the simpler solution.
119 #define SUPPORTED_FLAGS "lu"
121 #define SUPPORTED_FLAGS "belnstuv"
124 #ifndef NO_UDOM_SUPPORT
126 init_casper_net(cap_channel_t *casper)
128 cap_net_limit_t *limit;
131 capnet = cap_service_open(casper, "system.net");
133 err(EXIT_FAILURE, "unable to create network service");
135 limit = cap_net_limit_init(capnet, CAPNET_NAME2ADDR |
138 err(EXIT_FAILURE, "unable to create limits");
140 familylimit = AF_LOCAL;
141 cap_net_limit_name2addr_family(limit, &familylimit, 1);
143 if (cap_net_limit(limit) < 0)
144 err(EXIT_FAILURE, "unable to apply limits");
149 init_casper(int argc, char *argv[])
151 cap_channel_t *casper;
156 err(EXIT_FAILURE, "unable to create Casper");
158 fa = fileargs_cinit(casper, argc, argv, O_RDONLY, 0,
159 cap_rights_init(&rights, CAP_READ | CAP_FSTAT | CAP_FCNTL),
160 FA_OPEN | FA_REALPATH);
162 err(EXIT_FAILURE, "unable to create fileargs");
164 #ifndef NO_UDOM_SUPPORT
165 init_casper_net(casper);
172 main(int argc, char *argv[])
175 struct flock stdout_lock;
177 setlocale(LC_CTYPE, "");
179 while ((ch = getopt(argc, argv, SUPPORTED_FLAGS)) != -1)
182 bflag = nflag = 1; /* -b implies -n */
185 eflag = vflag = 1; /* -e implies -v */
197 tflag = vflag = 1; /* -t implies -v */
200 setbuf(stdout, NULL);
212 stdout_lock.l_len = 0;
213 stdout_lock.l_start = 0;
214 stdout_lock.l_type = F_WRLCK;
215 stdout_lock.l_whence = SEEK_SET;
216 if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1)
217 err(EXIT_FAILURE, "stdout");
220 init_casper(argc, argv);
222 caph_cache_catpages();
224 if (caph_enter_casper() < 0)
225 err(EXIT_FAILURE, "capsicum");
227 if (bflag || eflag || nflag || sflag || tflag || vflag)
241 fprintf(stderr, "usage: cat [-" SUPPORTED_FLAGS "] [file ...]\n");
247 scanfiles(char *argv[], int cooked __unused)
251 #ifndef BOOTSTRAP_CAT
257 while ((path = argv[i]) != NULL || i == 0) {
258 if (path == NULL || strcmp(path, "-") == 0) {
263 fd = fileargs_open(fa, path);
264 #ifndef NO_UDOM_SUPPORT
265 if (fd < 0 && errno == EOPNOTSUPP)
266 fd = udom_open(path, O_RDONLY);
272 #ifndef BOOTSTRAP_CAT
274 if (fd == STDIN_FILENO)
277 fp = fdopen(fd, "r");
284 if (fd != STDIN_FILENO)
293 #ifndef BOOTSTRAP_CAT
297 int ch, gobble, line, prev;
300 /* Reset EOF condition on stdin. */
301 if (fp == stdin && feof(stdin))
305 for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
316 if (!bflag || ch != '\n') {
317 (void)fprintf(stdout, "%6d\t", ++line);
321 (void)fprintf(stdout, "%6s\t", "");
328 if (eflag && putchar('$') == EOF)
330 } else if (ch == '\t') {
332 if (putchar('^') == EOF || putchar('I') == EOF)
337 (void)ungetc(ch, fp);
339 * Our getwc(3) doesn't change file position
342 if ((wch = getwc(fp)) == WEOF) {
343 if (ferror(fp) && errno == EILSEQ) {
345 /* Resync attempt. */
346 memset(&fp->_mbstate, 0, sizeof(mbstate_t));
347 if ((ch = getc(fp)) == EOF)
354 if (!iswascii(wch) && !iswprint(wch)) {
356 if (putchar('M') == EOF || putchar('-') == EOF)
362 ch = (ch == '\177') ? '?' : (ch | 0100);
363 if (putchar('^') == EOF || putchar(ch) == EOF)
367 if (putwchar(wch) == WEOF)
372 if (putchar(ch) == EOF)
376 warn("%s", filename);
383 #endif /* BOOTSTRAP_CAT */
392 static char *buf = NULL;
395 wfd = fileno(stdout);
397 if (fstat(wfd, &sbuf))
399 if (S_ISREG(sbuf.st_mode)) {
400 /* If there's plenty of RAM, use a large copy buffer */
401 if (sysconf(_SC_PHYS_PAGES) > PHYSPAGES_THRESHOLD)
402 bsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
404 bsize = BUFSIZE_SMALL;
406 bsize = sbuf.st_blksize;
407 pagesize = sysconf(_SC_PAGESIZE);
409 bsize = MAX(bsize, (size_t)pagesize);
411 if ((buf = malloc(bsize)) == NULL)
412 err(1, "malloc() failure of IO buffer");
414 while ((nr = read(rfd, buf, bsize)) > 0)
415 for (off = 0; nr; nr -= nw, off += nw)
416 if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
419 warn("%s", filename);
424 #ifndef NO_UDOM_SUPPORT
427 udom_open(const char *path, int flags)
429 struct addrinfo hints, *res, *res0;
430 char rpath[PATH_MAX];
431 int error, fd, serrno;
435 * Construct the unix domain socket address and attempt to connect.
437 bzero(&hints, sizeof(hints));
438 hints.ai_family = AF_LOCAL;
441 if (fileargs_realpath(fa, path, rpath) == NULL)
444 error = cap_getaddrinfo(capnet, rpath, NULL, &hints, &res0);
446 warn("%s", gai_strerror(error));
450 cap_rights_init(&rights, CAP_CONNECT, CAP_READ, CAP_WRITE,
451 CAP_SHUTDOWN, CAP_FSTAT, CAP_FCNTL);
452 for (res = res0; res != NULL; res = res->ai_next) {
453 fd = socket(res->ai_family, res->ai_socktype,
461 if (caph_rights_limit(fd, &rights) < 0) {
468 error = cap_connect(capnet, fd, res->ai_addr, res->ai_addrlen);
480 * handle the open flags by shutting down appropriate directions
483 switch(flags & O_ACCMODE) {
485 cap_rights_clear(&rights, CAP_WRITE);
486 if (shutdown(fd, SHUT_WR) == -1)
490 cap_rights_clear(&rights, CAP_READ);
491 if (shutdown(fd, SHUT_RD) == -1)
498 cap_rights_clear(&rights, CAP_CONNECT, CAP_SHUTDOWN);
499 if (caph_rights_limit(fd, &rights) < 0) {