[BACK]Return to catman.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/catman.c, Revision 1.2

1.2     ! kristaps    1: /*     $Id: catman.c,v 1.1 2011/11/26 19:54:13 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
                     21: #include <sys/param.h>
                     22: #include <sys/stat.h>
                     23: #include <sys/wait.h>
                     24:
                     25: #include <assert.h>
                     26: #include <errno.h>
                     27: #include <fcntl.h>
                     28: #include <getopt.h>
                     29: #include <stdio.h>
                     30: #include <stdlib.h>
                     31: #include <string.h>
                     32: #include <unistd.h>
                     33:
                     34: #ifdef __linux__
                     35: # include <db_185.h>
                     36: #else
                     37: # include <db.h>
                     38: #endif
                     39:
                     40: #include "manpath.h"
                     41:
                     42: #define        xstrlcpy(_dst, _src, _sz) \
                     43:        do if (strlcpy((_dst), (_src), (_sz)) >= (_sz)) { \
                     44:                fprintf(stderr, "%s: Path too long", (_dst)); \
                     45:                exit(EXIT_FAILURE); \
                     46:        } while (/* CONSTCOND */0)
                     47:
                     48: #define        xstrlcat(_dst, _src, _sz) \
                     49:        do if (strlcat((_dst), (_src), (_sz)) >= (_sz)) { \
                     50:                fprintf(stderr, "%s: Path too long", (_dst)); \
                     51:                exit(EXIT_FAILURE); \
                     52:        } while (/* CONSTCOND */0)
                     53:
                     54: static int              indexhtml(char *);
1.2     ! kristaps   55: #if 0
1.1       kristaps   56: static int              jobstart(const char *, const char *, pid_t *);
                     57: static int              jobwait(pid_t);
1.2     ! kristaps   58: #endif
1.1       kristaps   59: static int              manup(const struct manpaths *, const char *);
                     60: static int              mkpath(char *, mode_t, mode_t);
                     61: static int              treecpy(char *, char *);
                     62: static int              update(char *, char *);
                     63: static void             usage(void);
                     64:
                     65: static const char      *progname;
                     66: static int              verbose;
                     67: static int              force;
                     68:
                     69: int
                     70: main(int argc, char *argv[])
                     71: {
                     72:        int              ch;
                     73:        char            *aux, *base;
                     74:        const char      *dir;
                     75:        struct manpaths  dirs;
                     76:        extern char     *optarg;
                     77:        extern int       optind;
                     78:
                     79:        progname = strrchr(argv[0], '/');
                     80:        if (progname == NULL)
                     81:                progname = argv[0];
                     82:        else
                     83:                ++progname;
                     84:
                     85:        aux = base = NULL;
                     86:        dir = "/var/www/cache/man.cgi";
                     87:
                     88:        while (-1 != (ch = getopt(argc, argv, "fm:M:o:v")))
                     89:                switch (ch) {
                     90:                case ('f'):
                     91:                        force = 1;
                     92:                        break;
                     93:                case ('m'):
                     94:                        aux = optarg;
                     95:                        break;
                     96:                case ('M'):
                     97:                        base = optarg;
                     98:                        break;
                     99:                case ('o'):
                    100:                        dir = optarg;
                    101:                        break;
                    102:                case ('v'):
                    103:                        verbose++;
                    104:                        break;
                    105:                default:
                    106:                        usage();
                    107:                        return(EXIT_FAILURE);
                    108:                }
                    109:
                    110:        argc -= optind;
                    111:        argv += optind;
                    112:
                    113:        if (argc > 0) {
                    114:                usage();
                    115:                return(EXIT_FAILURE);
                    116:        }
                    117:
                    118:        memset(&dirs, 0, sizeof(struct manpaths));
                    119:        manpath_parse(&dirs, base, aux);
                    120:        ch = manup(&dirs, dir);
                    121:        manpath_free(&dirs);
                    122:        return(ch ? EXIT_SUCCESS : EXIT_FAILURE);
                    123: }
                    124:
                    125: static void
                    126: usage(void)
                    127: {
                    128:
                    129:        fprintf(stderr, "usage: %s "
                    130:                        "[-fv] "
                    131:                        "[-o path] "
                    132:                        "[-m manpath] "
                    133:                        "[-M manpath]\n",
                    134:                        progname);
                    135: }
                    136:
                    137: /*
                    138:  * If "src" file doesn't exist (errors out), return -1.  Otherwise,
                    139:  * return 1 if "src" is newer (which also happens "dst" doesn't exist)
                    140:  * and 0 otherwise.
                    141:  */
                    142: static int
                    143: isnewer(const char *dst, const char *src)
                    144: {
                    145:        struct stat      s1, s2;
                    146:
                    147:        if (-1 == stat(src, &s1))
                    148:                return(-1);
                    149:        if (force)
                    150:                return(1);
                    151:
                    152:        return(-1 == stat(dst, &s2) ? 1 : s1.st_mtime > s2.st_mtime);
                    153: }
                    154:
                    155: /*
                    156:  * Copy the contents of one file into another.
                    157:  * Returns 0 on failure, 1 on success.
                    158:  */
                    159: static int
                    160: filecpy(const char *dst, const char *src)
                    161: {
                    162:        char             buf[BUFSIZ];
                    163:        int              sfd, dfd, rc;
                    164:        ssize_t          rsz, wsz;
                    165:
                    166:        sfd = dfd = -1;
                    167:        rc = 0;
                    168:
                    169:        if (-1 == (dfd = open(dst, O_CREAT|O_TRUNC|O_WRONLY, 0644))) {
                    170:                perror(dst);
                    171:                goto out;
                    172:        } else if (-1 == (sfd = open(src, O_RDONLY, 0))) {
                    173:                perror(src);
                    174:                goto out;
                    175:        }
                    176:
                    177:        while ((rsz = read(sfd, buf, BUFSIZ)) > 0)
                    178:                if (-1 == (wsz = write(dfd, buf, (size_t)rsz))) {
                    179:                        perror(dst);
                    180:                        goto out;
                    181:                } else if (wsz < rsz) {
                    182:                        fprintf(stderr, "%s: Short write\n", dst);
                    183:                        goto out;
                    184:                }
                    185:
                    186:        if (rsz < 0)
                    187:                perror(src);
                    188:        else
                    189:                rc = 1;
                    190: out:
                    191:        if (-1 != sfd)
                    192:                close(sfd);
                    193:        if (-1 != dfd)
                    194:                close(dfd);
                    195:
                    196:        return(rc);
                    197: }
                    198:
1.2     ! kristaps  199: #if 0
1.1       kristaps  200: /*
                    201:  * Clean up existing child.
                    202:  * Return 1 if cleaned up fine (or none was started) and 0 otherwise.
                    203:  */
                    204: static int
                    205: jobwait(pid_t pid)
                    206: {
                    207:        int              st;
                    208:
                    209:        if (-1 == pid)
                    210:                return(1);
                    211:
                    212:        if (-1 == waitpid(pid, &st, 0)) {
                    213:                perror(NULL);
                    214:                exit(EXIT_FAILURE);
                    215:        }
                    216:
                    217:        return(WIFEXITED(st) && 0 == WEXITSTATUS(st));
                    218: }
                    219:
                    220: /*
                    221:  * Start a job (child process), first making sure that the prior one has
                    222:  * finished.
                    223:  * Return 1 if the prior child exited and the new one started, else 0.
                    224:  */
                    225: static int
                    226: jobstart(const char *dst, const char *src, pid_t *pid)
                    227: {
                    228:        int              fd;
                    229:
                    230:        if ( ! jobwait(*pid))
                    231:                return(0);
                    232:
                    233:        if (-1 == (*pid = fork())) {
                    234:                perror(NULL);
                    235:                exit(EXIT_FAILURE);
                    236:        } else if (*pid > 0)
                    237:                return(1);
                    238:
                    239:        if (-1 == (fd = open(dst, O_WRONLY|O_TRUNC|O_CREAT, 0644))) {
                    240:                perror(dst);
                    241:                exit(EXIT_FAILURE);
                    242:        }
                    243:
                    244:        if (-1 == dup2(fd, STDOUT_FILENO)) {
                    245:                perror(NULL);
                    246:                exit(EXIT_FAILURE);
                    247:        }
                    248:
                    249:        execlp("mandoc", "mandoc", "-T", "html",
                    250:                        "-O", "fragment",
                    251:                        "-O", "man=man.cgi?expr=%N&sec=%S",
                    252:                        src, (char *)NULL);
                    253:
                    254:        perror("mandoc");
                    255:        exit(EXIT_FAILURE);
                    256:        /* NOTREACHED */
                    257: }
1.2     ! kristaps  258: #endif
1.1       kristaps  259:
                    260: /*
                    261:  * Pass over the recno database and re-create HTML pages if they're
                    262:  * found to be out of date.
                    263:  * Returns -1 on fatal error, 1 on success.
                    264:  */
                    265: static int
                    266: indexhtml(char *dst)
                    267: {
                    268:        DB              *db;
                    269:        DBT              key, val;
                    270:        size_t           sz;
                    271:        int              c, rc;
                    272:        unsigned int     fl;
                    273:        const char      *f;
                    274:        char            *d;
                    275:        char             fname[MAXPATHLEN];
                    276:        pid_t            pid;
                    277:
                    278:        sz = strlen(dst);
                    279:        pid = -1;
                    280:
                    281:        xstrlcpy(fname, dst, MAXPATHLEN);
                    282:        xstrlcat(fname, "/mandoc.index", MAXPATHLEN);
                    283:
                    284:        db = dbopen(fname, O_RDONLY, 0, DB_RECNO, NULL);
                    285:        if (NULL == db) {
                    286:                perror(fname);
                    287:                return(-1);
                    288:        }
                    289:
                    290:        fl = R_FIRST;
                    291:        while (0 == (c = (*db->seq)(db, &key, &val, fl))) {
                    292:                fl = R_NEXT;
                    293:                f = (const char *)val.data;
                    294:
                    295:                dst[(int)sz] = '\0';
                    296:
                    297:                xstrlcat(dst, "/", MAXPATHLEN);
                    298:                xstrlcat(dst, f, MAXPATHLEN);
1.2     ! kristaps  299:                /*xstrlcat(dst, ".html", MAXPATHLEN);*/
1.1       kristaps  300:
                    301:                if (-1 == (rc = isnewer(dst, f))) {
                    302:                        fprintf(stderr, "%s: Manpage missing\n", f);
                    303:                        break;
                    304:                } else if (0 == rc)
                    305:                        continue;
                    306:
                    307:                d = strrchr(dst, '/');
                    308:                assert(NULL != d);
                    309:                *d = '\0';
                    310:
                    311:                if (-1 == mkpath(dst, 0755, 0755)) {
                    312:                        perror(dst);
                    313:                        break;
                    314:                }
                    315:
                    316:                *d = '/';
1.2     ! kristaps  317:
        !           318:                if ( ! filecpy(dst, f))
1.1       kristaps  319:                        break;
1.2     ! kristaps  320:
        !           321:                /*if ( ! jobstart(dst, f, &pid))
        !           322:                        break;*/
1.1       kristaps  323:                if (verbose)
                    324:                        printf("%s\n", dst);
                    325:        }
                    326:
                    327:        (*db->close)(db);
                    328:
                    329:        if (c < 0)
                    330:                perror(fname);
1.2     ! kristaps  331:        /*if ( ! jobwait(pid))
        !           332:                c = -1;*/
1.1       kristaps  333:
                    334:        return(1 == c ? 1 : -1);
                    335: }
                    336:
                    337: /*
                    338:  * Copy both recno and btree databases into the destination.
                    339:  * Call in to begin recreating HTML files.
                    340:  * Return -1 on fatal error and 1 if the update went well.
                    341:  */
                    342: static int
                    343: update(char *dst, char *src)
                    344: {
                    345:        size_t           dsz, ssz;
                    346:
                    347:        dsz = strlen(dst);
                    348:        ssz = strlen(src);
                    349:
                    350:        xstrlcat(src, "/mandoc.db", MAXPATHLEN);
                    351:        xstrlcat(dst, "/mandoc.db", MAXPATHLEN);
                    352:
                    353:        if ( ! filecpy(dst, src))
                    354:                return(-1);
                    355:        if (verbose)
                    356:                printf("%s\n", dst);
                    357:
                    358:        dst[(int)dsz] = src[(int)ssz] = '\0';
                    359:
                    360:        xstrlcat(src, "/mandoc.index", MAXPATHLEN);
                    361:        xstrlcat(dst, "/mandoc.index", MAXPATHLEN);
                    362:
                    363:        if ( ! filecpy(dst, src))
                    364:                return(-1);
                    365:        if (verbose)
                    366:                printf("%s\n", dst);
                    367:
                    368:        dst[(int)dsz] = '\0';
                    369:
                    370:        return(indexhtml(dst));
                    371: }
                    372:
                    373: /*
                    374:  * See if btree or recno databases in the destination are out of date
                    375:  * with respect to a single manpath component.
                    376:  * Return -1 on fatal error, 0 if the source is no longer valid (and
                    377:  * shouldn't be listed), and 1 if the update went well.
                    378:  */
                    379: static int
                    380: treecpy(char *dst, char *src)
                    381: {
                    382:        size_t           dsz, ssz;
                    383:        int              rc;
                    384:
                    385:        dsz = strlen(dst);
                    386:        ssz = strlen(src);
                    387:
                    388:        xstrlcat(src, "/mandoc.index", MAXPATHLEN);
                    389:        xstrlcat(dst, "/mandoc.index", MAXPATHLEN);
                    390:
                    391:        if (-1 == (rc = isnewer(dst, src)))
                    392:                return(0);
                    393:
                    394:        dst[(int)dsz] = src[(int)ssz] = '\0';
                    395:
                    396:        if (1 == rc)
                    397:                return(update(dst, src));
                    398:
                    399:        xstrlcat(src, "/mandoc.db", MAXPATHLEN);
                    400:        xstrlcat(dst, "/mandoc.db", MAXPATHLEN);
                    401:
                    402:        if (-1 == (rc = isnewer(dst, src)))
                    403:                return(0);
                    404:        else if (rc == 0)
                    405:                return(1);
                    406:
                    407:        dst[(int)dsz] = src[(int)ssz] = '\0';
                    408:
                    409:        return(update(dst, src));
                    410: }
                    411:
                    412: /*
                    413:  * Update the destination's file-tree with respect to changes in the
                    414:  * source manpath components.
                    415:  * "Change" is defined by an updated index or btree database.
                    416:  * Returns 1 on success, 0 on failure.
                    417:  */
                    418: static int
                    419: manup(const struct manpaths *dirs, const char *dir)
                    420: {
                    421:        char             dst[MAXPATHLEN],
                    422:                         src[MAXPATHLEN];
                    423:        const char      *path;
                    424:        int              i, c;
                    425:        size_t           sz;
                    426:        FILE            *f;
                    427:
                    428:        xstrlcpy(dst, dir, MAXPATHLEN);
                    429:        xstrlcat(dst, "/etc", MAXPATHLEN);
                    430:
                    431:        if (-1 == mkpath(dst, 0755, 0755)) {
                    432:                perror(dst);
                    433:                return(0);
                    434:        }
                    435:
                    436:        xstrlcat(dst, "/man.conf", MAXPATHLEN);
                    437:
                    438:        if (verbose)
                    439:                printf("%s\n", dst);
                    440:
                    441:        if (NULL == (f = fopen(dst, "w"))) {
                    442:                perror(dst);
                    443:                return(0);
                    444:        }
                    445:
                    446:        xstrlcpy(dst, dir, MAXPATHLEN);
                    447:        sz = strlen(dst);
                    448:
                    449:        for (i = 0; i < dirs->sz; i++) {
                    450:                path = dirs->paths[i];
                    451:
                    452:                dst[(int)sz] = '\0';
                    453:                xstrlcat(dst, path, MAXPATHLEN);
                    454:
                    455:                if (-1 == mkpath(dst, 0755, 0755)) {
                    456:                        perror(dst);
                    457:                        break;
                    458:                }
                    459:
                    460:                xstrlcpy(src, path, MAXPATHLEN);
                    461:
                    462:                if (-1 == (c = treecpy(dst, src)))
                    463:                        break;
                    464:                else if (0 == c)
                    465:                        continue;
                    466:
                    467:                /*
                    468:                 * We want to use a relative path here because manpath.h
                    469:                 * will realpath() when invoked with man.cgi, and we'll
                    470:                 * make sure to chdir() into the cache directory before.
                    471:                 *
                    472:                 * This allows the cache directory to be in an arbitrary
                    473:                 * place, working in both chroot() and non-chroot()
                    474:                 * "safe" modes.
                    475:                 */
                    476:                assert('/' == path[0]);
                    477:                fprintf(f, "_whatdb %s/whatis.db\n", path + 1);
                    478:        }
                    479:
                    480:        fclose(f);
                    481:        return(i == dirs->sz);
                    482: }
                    483:
                    484: /*
                    485:  * Copyright (c) 1983, 1992, 1993
                    486:  *     The Regents of the University of California.  All rights reserved.
                    487:  *
                    488:  * Redistribution and use in source and binary forms, with or without
                    489:  * modification, are permitted provided that the following conditions
                    490:  * are met:
                    491:  * 1. Redistributions of source code must retain the above copyright
                    492:  *    notice, this list of conditions and the following disclaimer.
                    493:  * 2. Redistributions in binary form must reproduce the above copyright
                    494:  *    notice, this list of conditions and the following disclaimer in the
                    495:  *    documentation and/or other materials provided with the distribution.
                    496:  * 3. Neither the name of the University nor the names of its contributors
                    497:  *    may be used to endorse or promote products derived from this software
                    498:  *    without specific prior written permission.
                    499:  *
                    500:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                    501:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                    502:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                    503:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                    504:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                    505:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                    506:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                    507:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                    508:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                    509:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                    510:  * SUCH DAMAGE.
                    511:  */
                    512: static int
                    513: mkpath(char *path, mode_t mode, mode_t dir_mode)
                    514: {
                    515:        struct stat sb;
                    516:        char *slash;
                    517:        int done, exists;
                    518:
                    519:        slash = path;
                    520:
                    521:        for (;;) {
                    522:                /* LINTED */
                    523:                slash += strspn(slash, "/");
                    524:                /* LINTED */
                    525:                slash += strcspn(slash, "/");
                    526:
                    527:                done = (*slash == '\0');
                    528:                *slash = '\0';
                    529:
                    530:                /* skip existing path components */
                    531:                exists = !stat(path, &sb);
                    532:                if (!done && exists && S_ISDIR(sb.st_mode)) {
                    533:                        *slash = '/';
                    534:                        continue;
                    535:                }
                    536:
                    537:                if (mkdir(path, done ? mode : dir_mode) == 0) {
                    538:                        if (mode > 0777 && chmod(path, mode) < 0)
                    539:                                return (-1);
                    540:                } else {
                    541:                        if (!exists) {
                    542:                                /* Not there */
                    543:                                return (-1);
                    544:                        }
                    545:                        if (!S_ISDIR(sb.st_mode)) {
                    546:                                /* Is there, but isn't a directory */
                    547:                                errno = ENOTDIR;
                    548:                                return (-1);
                    549:                        }
                    550:                }
                    551:
                    552:                if (done)
                    553:                        break;
                    554:
                    555:                *slash = '/';
                    556:        }
                    557:
                    558:        return (0);
                    559: }

CVSweb