Annotation of mandoc/catman.c, Revision 1.17
1.17 ! schwarze 1: /* $Id: catman.c,v 1.16 2017/02/08 16:11:40 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>
1.17 ! schwarze 43:
! 44: #ifndef O_DIRECTORY
! 45: #define O_DIRECTORY 0
! 46: #endif
1.1 kristaps 47:
1.13 schwarze 48: int process_manpage(int, int, const char *);
49: int process_tree(int, int);
1.14 schwarze 50: void run_mandocd(int, const char *, const char *)
51: __attribute__((noreturn));
1.13 schwarze 52: ssize_t sock_fd_write(int, int, int, int);
53: void usage(void) __attribute__((noreturn));
54:
55:
56: void
1.14 schwarze 57: run_mandocd(int sockfd, const char *outtype, const char* defos)
1.13 schwarze 58: {
59: char sockfdstr[10];
60:
61: if (snprintf(sockfdstr, sizeof(sockfdstr), "%d", sockfd) == -1)
62: err(1, "snprintf");
1.14 schwarze 63: if (defos == NULL)
64: execlp("mandocd", "mandocd", "-T", outtype, sockfdstr, NULL);
65: else
66: execlp("mandocd", "mandocd", "-T", outtype,
67: "-I", defos, sockfdstr, NULL);
1.13 schwarze 68: err(1, "exec");
69: }
70:
71: ssize_t
72: sock_fd_write(int fd, int fd0, int fd1, int fd2)
73: {
1.15 schwarze 74: const struct timespec timeout = { 0, 10000000 }; /* 0.01 s */
1.13 schwarze 75: struct msghdr msg;
76: struct iovec iov;
77: union {
78: struct cmsghdr cmsghdr;
79: char control[CMSG_SPACE(3 * sizeof(int))];
80: } cmsgu;
81: struct cmsghdr *cmsg;
82: int *walk;
1.15 schwarze 83: ssize_t sz;
1.13 schwarze 84: unsigned char dummy[1] = {'\0'};
85:
86: iov.iov_base = dummy;
87: iov.iov_len = sizeof(dummy);
88:
89: msg.msg_name = NULL;
90: msg.msg_namelen = 0;
91: msg.msg_iov = &iov;
92: msg.msg_iovlen = 1;
93:
94: msg.msg_control = cmsgu.control;
95: msg.msg_controllen = sizeof(cmsgu.control);
96:
97: cmsg = CMSG_FIRSTHDR(&msg);
98: cmsg->cmsg_len = CMSG_LEN(3 * sizeof(int));
99: cmsg->cmsg_level = SOL_SOCKET;
100: cmsg->cmsg_type = SCM_RIGHTS;
101:
102: walk = (int *)CMSG_DATA(cmsg);
103: *(walk++) = fd0;
104: *(walk++) = fd1;
105: *(walk++) = fd2;
1.1 kristaps 106:
1.15 schwarze 107: /*
108: * It appears that on some systems, sendmsg(3)
109: * may return EAGAIN even in blocking mode.
110: * Seen for example on Oracle Solaris 11.2.
111: * The sleeping time was chosen by experimentation,
112: * to neither cause more than a handful of retries
113: * in normal operation nor unnecessary delays.
114: */
115: for (;;) {
116: if ((sz = sendmsg(fd, &msg, 0)) != -1 ||
117: errno != EAGAIN)
118: break;
119: nanosleep(&timeout, NULL);
120: }
121: return sz;
1.13 schwarze 122: }
1.1 kristaps 123:
124: int
1.13 schwarze 125: process_manpage(int srv_fd, int dstdir_fd, const char *path)
1.1 kristaps 126: {
1.13 schwarze 127: int in_fd, out_fd;
1.14 schwarze 128: int irc;
1.1 kristaps 129:
1.13 schwarze 130: if ((in_fd = open(path, O_RDONLY)) == -1) {
131: warn("open(%s)", path);
1.14 schwarze 132: return 0;
1.13 schwarze 133: }
1.1 kristaps 134:
1.13 schwarze 135: if ((out_fd = openat(dstdir_fd, path,
136: O_WRONLY | O_NOFOLLOW | O_CREAT | O_TRUNC,
1.14 schwarze 137: S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
1.13 schwarze 138: warn("openat(%s)", path);
139: close(in_fd);
1.14 schwarze 140: return 0;
1.1 kristaps 141: }
142:
1.14 schwarze 143: irc = sock_fd_write(srv_fd, in_fd, out_fd, STDERR_FILENO);
144:
145: close(in_fd);
146: close(out_fd);
147:
148: if (irc < 0) {
1.13 schwarze 149: warn("sendmsg");
150: return -1;
151: }
152: return 0;
1.1 kristaps 153: }
154:
1.13 schwarze 155: int
156: process_tree(int srv_fd, int dstdir_fd)
1.1 kristaps 157: {
1.13 schwarze 158: FTS *ftsp;
159: FTSENT *entry;
160: const char *argv[2];
161: const char *path;
1.1 kristaps 162:
1.13 schwarze 163: argv[0] = ".";
164: argv[1] = (char *)NULL;
1.1 kristaps 165:
1.13 schwarze 166: if ((ftsp = fts_open((char * const *)argv,
167: FTS_PHYSICAL | FTS_NOCHDIR, NULL)) == NULL) {
168: warn("fts_open");
169: return -1;
170: }
171:
172: while ((entry = fts_read(ftsp)) != NULL) {
173: path = entry->fts_path + 2;
174: switch (entry->fts_info) {
175: case FTS_F:
1.14 schwarze 176: if (process_manpage(srv_fd, dstdir_fd, path) == -1) {
177: fts_close(ftsp);
178: return -1;
179: }
1.13 schwarze 180: break;
181: case FTS_D:
1.14 schwarze 182: if (*path != '\0' &&
183: mkdirat(dstdir_fd, path, S_IRWXU | S_IRGRP |
184: S_IXGRP | S_IROTH | S_IXOTH) == -1 &&
185: errno != EEXIST) {
186: warn("mkdirat(%s)", path);
187: (void)fts_set(ftsp, entry, FTS_SKIP);
188: }
189: break;
1.13 schwarze 190: case FTS_DP:
191: break;
192: default:
193: warnx("%s: not a regular file", path);
194: break;
1.1 kristaps 195: }
1.13 schwarze 196: }
1.1 kristaps 197:
1.13 schwarze 198: fts_close(ftsp);
199: return 0;
1.1 kristaps 200: }
201:
1.13 schwarze 202: int
203: main(int argc, char **argv)
1.1 kristaps 204: {
1.14 schwarze 205: const char *defos, *outtype;
1.13 schwarze 206: int srv_fds[2];
207: int dstdir_fd;
208: int opt;
1.1 kristaps 209: pid_t pid;
210:
1.14 schwarze 211: defos = NULL;
1.13 schwarze 212: outtype = "ascii";
1.14 schwarze 213: while ((opt = getopt(argc, argv, "I:T:")) != -1) {
1.13 schwarze 214: switch (opt) {
1.14 schwarze 215: case 'I':
216: defos = optarg;
217: break;
1.13 schwarze 218: case 'T':
219: outtype = optarg;
1.1 kristaps 220: break;
1.13 schwarze 221: default:
222: usage();
1.1 kristaps 223: }
1.13 schwarze 224: }
1.1 kristaps 225:
1.13 schwarze 226: if (argc > 0) {
227: argc -= optind;
228: argv += optind;
1.1 kristaps 229: }
1.13 schwarze 230: if (argc != 2)
231: usage();
1.1 kristaps 232:
1.13 schwarze 233: if (socketpair(AF_LOCAL, SOCK_STREAM, AF_UNSPEC, srv_fds) == -1)
234: err(1, "socketpair");
1.1 kristaps 235:
1.13 schwarze 236: pid = fork();
237: switch (pid) {
238: case -1:
239: err(1, "fork");
240: case 0:
241: close(srv_fds[0]);
1.14 schwarze 242: run_mandocd(srv_fds[1], outtype, defos);
1.13 schwarze 243: default:
244: break;
1.1 kristaps 245: }
1.13 schwarze 246: close(srv_fds[1]);
1.1 kristaps 247:
1.13 schwarze 248: if ((dstdir_fd = open(argv[1], O_RDONLY | O_DIRECTORY)) == -1)
249: err(1, "open(%s)", argv[1]);
1.1 kristaps 250:
1.13 schwarze 251: if (chdir(argv[0]) == -1)
252: err(1, "chdir(%s)", argv[0]);
1.1 kristaps 253:
1.13 schwarze 254: return process_tree(srv_fds[0], dstdir_fd) == -1 ? 1 : 0;
1.1 kristaps 255: }
256:
1.13 schwarze 257: void
258: usage(void)
1.1 kristaps 259: {
1.14 schwarze 260: fprintf(stderr, "usage: catman [-I os=name] [-T output] "
261: "srcdir dstdir\n");
1.13 schwarze 262: exit(1);
1.1 kristaps 263: }
CVSweb