Annotation of mandoc/catman.c, Revision 1.16
1.16 ! schwarze 1: /* $Id: catman.c,v 1.15 2017/02/08 14:50:53 schwarze Exp $ */
1.1 kristaps 2: /*
1.13 schwarze 3: * Copyright (c) 2017 Michael Stapelberg <stapelberg@debian.org>
4: * Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
1.13 schwarze 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1 kristaps 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.13 schwarze 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1 kristaps 13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18: #include "config.h"
1.16 ! schwarze 19:
! 20: #if HAVE_CMSG_XPG42
! 21: #define _XPG4_2
! 22: #endif
1.1 kristaps 23:
1.13 schwarze 24: #include <sys/types.h>
25: #include <sys/socket.h>
1.1 kristaps 26: #include <sys/stat.h>
27:
1.13 schwarze 28: #if HAVE_ERR
29: #include <err.h>
30: #endif
1.14 schwarze 31: #include <errno.h>
1.1 kristaps 32: #include <fcntl.h>
1.13 schwarze 33: #if HAVE_FTS
34: #include <fts.h>
35: #else
36: #include "compat_fts.h"
37: #endif
1.1 kristaps 38: #include <stdio.h>
39: #include <stdlib.h>
40: #include <string.h>
1.15 schwarze 41: #include <time.h>
1.1 kristaps 42: #include <unistd.h>
43:
1.13 schwarze 44: int process_manpage(int, int, const char *);
45: int process_tree(int, int);
1.14 schwarze 46: void run_mandocd(int, const char *, const char *)
47: __attribute__((noreturn));
1.13 schwarze 48: ssize_t sock_fd_write(int, int, int, int);
49: void usage(void) __attribute__((noreturn));
50:
51:
52: void
1.14 schwarze 53: run_mandocd(int sockfd, const char *outtype, const char* defos)
1.13 schwarze 54: {
55: char sockfdstr[10];
56:
57: if (snprintf(sockfdstr, sizeof(sockfdstr), "%d", sockfd) == -1)
58: err(1, "snprintf");
1.14 schwarze 59: if (defos == NULL)
60: execlp("mandocd", "mandocd", "-T", outtype, sockfdstr, NULL);
61: else
62: execlp("mandocd", "mandocd", "-T", outtype,
63: "-I", defos, sockfdstr, NULL);
1.13 schwarze 64: err(1, "exec");
65: }
66:
67: ssize_t
68: sock_fd_write(int fd, int fd0, int fd1, int fd2)
69: {
1.15 schwarze 70: const struct timespec timeout = { 0, 10000000 }; /* 0.01 s */
1.13 schwarze 71: struct msghdr msg;
72: struct iovec iov;
73: union {
74: struct cmsghdr cmsghdr;
75: char control[CMSG_SPACE(3 * sizeof(int))];
76: } cmsgu;
77: struct cmsghdr *cmsg;
78: int *walk;
1.15 schwarze 79: ssize_t sz;
1.13 schwarze 80: unsigned char dummy[1] = {'\0'};
81:
82: iov.iov_base = dummy;
83: iov.iov_len = sizeof(dummy);
84:
85: msg.msg_name = NULL;
86: msg.msg_namelen = 0;
87: msg.msg_iov = &iov;
88: msg.msg_iovlen = 1;
89:
90: msg.msg_control = cmsgu.control;
91: msg.msg_controllen = sizeof(cmsgu.control);
92:
93: cmsg = CMSG_FIRSTHDR(&msg);
94: cmsg->cmsg_len = CMSG_LEN(3 * sizeof(int));
95: cmsg->cmsg_level = SOL_SOCKET;
96: cmsg->cmsg_type = SCM_RIGHTS;
97:
98: walk = (int *)CMSG_DATA(cmsg);
99: *(walk++) = fd0;
100: *(walk++) = fd1;
101: *(walk++) = fd2;
1.1 kristaps 102:
1.15 schwarze 103: /*
104: * It appears that on some systems, sendmsg(3)
105: * may return EAGAIN even in blocking mode.
106: * Seen for example on Oracle Solaris 11.2.
107: * The sleeping time was chosen by experimentation,
108: * to neither cause more than a handful of retries
109: * in normal operation nor unnecessary delays.
110: */
111: for (;;) {
112: if ((sz = sendmsg(fd, &msg, 0)) != -1 ||
113: errno != EAGAIN)
114: break;
115: nanosleep(&timeout, NULL);
116: }
117: return sz;
1.13 schwarze 118: }
1.1 kristaps 119:
120: int
1.13 schwarze 121: process_manpage(int srv_fd, int dstdir_fd, const char *path)
1.1 kristaps 122: {
1.13 schwarze 123: int in_fd, out_fd;
1.14 schwarze 124: int irc;
1.1 kristaps 125:
1.13 schwarze 126: if ((in_fd = open(path, O_RDONLY)) == -1) {
127: warn("open(%s)", path);
1.14 schwarze 128: return 0;
1.13 schwarze 129: }
1.1 kristaps 130:
1.13 schwarze 131: if ((out_fd = openat(dstdir_fd, path,
132: O_WRONLY | O_NOFOLLOW | O_CREAT | O_TRUNC,
1.14 schwarze 133: S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
1.13 schwarze 134: warn("openat(%s)", path);
135: close(in_fd);
1.14 schwarze 136: return 0;
1.1 kristaps 137: }
138:
1.14 schwarze 139: irc = sock_fd_write(srv_fd, in_fd, out_fd, STDERR_FILENO);
140:
141: close(in_fd);
142: close(out_fd);
143:
144: if (irc < 0) {
1.13 schwarze 145: warn("sendmsg");
146: return -1;
147: }
148: return 0;
1.1 kristaps 149: }
150:
1.13 schwarze 151: int
152: process_tree(int srv_fd, int dstdir_fd)
1.1 kristaps 153: {
1.13 schwarze 154: FTS *ftsp;
155: FTSENT *entry;
156: const char *argv[2];
157: const char *path;
1.1 kristaps 158:
1.13 schwarze 159: argv[0] = ".";
160: argv[1] = (char *)NULL;
1.1 kristaps 161:
1.13 schwarze 162: if ((ftsp = fts_open((char * const *)argv,
163: FTS_PHYSICAL | FTS_NOCHDIR, NULL)) == NULL) {
164: warn("fts_open");
165: return -1;
166: }
167:
168: while ((entry = fts_read(ftsp)) != NULL) {
169: path = entry->fts_path + 2;
170: switch (entry->fts_info) {
171: case FTS_F:
1.14 schwarze 172: if (process_manpage(srv_fd, dstdir_fd, path) == -1) {
173: fts_close(ftsp);
174: return -1;
175: }
1.13 schwarze 176: break;
177: case FTS_D:
1.14 schwarze 178: if (*path != '\0' &&
179: mkdirat(dstdir_fd, path, S_IRWXU | S_IRGRP |
180: S_IXGRP | S_IROTH | S_IXOTH) == -1 &&
181: errno != EEXIST) {
182: warn("mkdirat(%s)", path);
183: (void)fts_set(ftsp, entry, FTS_SKIP);
184: }
185: break;
1.13 schwarze 186: case FTS_DP:
187: break;
188: default:
189: warnx("%s: not a regular file", path);
190: break;
1.1 kristaps 191: }
1.13 schwarze 192: }
1.1 kristaps 193:
1.13 schwarze 194: fts_close(ftsp);
195: return 0;
1.1 kristaps 196: }
197:
1.13 schwarze 198: int
199: main(int argc, char **argv)
1.1 kristaps 200: {
1.14 schwarze 201: const char *defos, *outtype;
1.13 schwarze 202: int srv_fds[2];
203: int dstdir_fd;
204: int opt;
1.1 kristaps 205: pid_t pid;
206:
1.14 schwarze 207: defos = NULL;
1.13 schwarze 208: outtype = "ascii";
1.14 schwarze 209: while ((opt = getopt(argc, argv, "I:T:")) != -1) {
1.13 schwarze 210: switch (opt) {
1.14 schwarze 211: case 'I':
212: defos = optarg;
213: break;
1.13 schwarze 214: case 'T':
215: outtype = optarg;
1.1 kristaps 216: break;
1.13 schwarze 217: default:
218: usage();
1.1 kristaps 219: }
1.13 schwarze 220: }
1.1 kristaps 221:
1.13 schwarze 222: if (argc > 0) {
223: argc -= optind;
224: argv += optind;
1.1 kristaps 225: }
1.13 schwarze 226: if (argc != 2)
227: usage();
1.1 kristaps 228:
1.13 schwarze 229: if (socketpair(AF_LOCAL, SOCK_STREAM, AF_UNSPEC, srv_fds) == -1)
230: err(1, "socketpair");
1.1 kristaps 231:
1.13 schwarze 232: pid = fork();
233: switch (pid) {
234: case -1:
235: err(1, "fork");
236: case 0:
237: close(srv_fds[0]);
1.14 schwarze 238: run_mandocd(srv_fds[1], outtype, defos);
1.13 schwarze 239: default:
240: break;
1.1 kristaps 241: }
1.13 schwarze 242: close(srv_fds[1]);
1.1 kristaps 243:
1.13 schwarze 244: if ((dstdir_fd = open(argv[1], O_RDONLY | O_DIRECTORY)) == -1)
245: err(1, "open(%s)", argv[1]);
1.1 kristaps 246:
1.13 schwarze 247: if (chdir(argv[0]) == -1)
248: err(1, "chdir(%s)", argv[0]);
1.1 kristaps 249:
1.13 schwarze 250: return process_tree(srv_fds[0], dstdir_fd) == -1 ? 1 : 0;
1.1 kristaps 251: }
252:
1.13 schwarze 253: void
254: usage(void)
1.1 kristaps 255: {
1.14 schwarze 256: fprintf(stderr, "usage: catman [-I os=name] [-T output] "
257: "srcdir dstdir\n");
1.13 schwarze 258: exit(1);
1.1 kristaps 259: }
CVSweb