[BACK]Return to server.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/server.c, Revision 1.27

1.27    ! xsa         1: /*     $OpenBSD: server.c,v 1.26 2005/12/24 19:07:52 xsa Exp $ */
1.1       jfb         2: /*
                      3:  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.6       tedu        4:  * All rights reserved.
1.1       jfb         5:  *
1.6       tedu        6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
1.1       jfb         9:  *
1.6       tedu       10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
1.1       jfb        12:  * 2. The name of the author may not be used to endorse or promote products
1.6       tedu       13:  *    derived from this software without specific prior written permission.
1.1       jfb        14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.6       tedu       24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       jfb        25:  */
                     26:
1.27    ! xsa        27: #include "includes.h"
1.1       jfb        28:
                     29: #include "cvs.h"
                     30: #include "log.h"
1.5       jfb        31: #include "proto.h"
1.1       jfb        32:
                     33:
                     34: /* argument vector built by the `Argument' and `Argumentx' requests */
                     35: char   **cvs_args;
1.6       tedu       36: u_int   cvs_nbarg = 0;
1.1       jfb        37: u_int   cvs_utf8ok = 0;
1.17      jfb        38: u_int   cvs_case = 0;
1.8       joris      39:
1.17      jfb        40: struct cvs_cmd cvs_cmd_server = {
                     41:        CVS_OP_SERVER, 0, "server",
                     42:        { },
                     43:        "Server mode",
                     44:        "",
                     45:        "",
                     46:        NULL,
                     47:        0,
                     48:        NULL, NULL, NULL, NULL, NULL, NULL,
                     49:        0
                     50: };
1.1       jfb        51:
1.14      joris      52: char cvs_server_tmpdir[MAXPATHLEN];
                     53:
1.1       jfb        54: /*
                     55:  * cvs_server()
                     56:  *
                     57:  * Implement the `cvs server' command.  As opposed to the general method of
                     58:  * CVS client/server implementation, the cvs program merely acts as a
                     59:  * redirector to the cvs daemon for most of the tasks.
                     60:  *
                     61:  * The `cvs server' command is only used on the server side of a remote
                     62:  * cvs command.  With this command, the cvs program starts listening on
                     63:  * standard input for CVS protocol requests.
                     64:  */
                     65: int
                     66: cvs_server(int argc, char **argv)
                     67: {
1.14      joris      68:        int l, ret;
1.5       jfb        69:        size_t len;
1.9       joris      70:        char reqbuf[512];
1.1       jfb        71:
1.25      xsa        72:        if (argc != 1)
1.13      joris      73:                return (CVS_EX_USAGE);
1.1       jfb        74:
1.5       jfb        75:        /* make sure standard in and standard out are line-buffered */
1.23      xsa        76:        (void)setvbuf(stdin, NULL, _IOLBF, (size_t)0);
                     77:        (void)setvbuf(stdout, NULL, _IOLBF, (size_t)0);
1.5       jfb        78:
1.14      joris      79:        /* create the temporary directory */
                     80:        l = snprintf(cvs_server_tmpdir, sizeof(cvs_server_tmpdir),
1.21      xsa        81:            "%s/cvs-serv%d", cvs_tmpdir, getpid());
1.14      joris      82:        if (l == -1 || l >= (int)sizeof(cvs_server_tmpdir)) {
                     83:                errno = ENAMETOOLONG;
1.25      xsa        84:                fatal("cvs_server: tmpdir path too long: `%s'",
                     85:                    cvs_server_tmpdir);
1.14      joris      86:        }
                     87:
1.25      xsa        88:        if (mkdir(cvs_server_tmpdir, 0700) == -1)
                     89:                fatal("cvs_server: mkdir: `%s': %s",
                     90:                    cvs_server_tmpdir, strerror(errno));
1.14      joris      91:
1.26      xsa        92:        cvs_chdir(cvs_server_tmpdir, 1);
1.14      joris      93:
1.1       jfb        94:        for (;;) {
1.22      xsa        95:                if (fgets(reqbuf, (int)sizeof(reqbuf), stdin) == NULL) {
1.5       jfb        96:                        if (feof(stdin))
                     97:                                break;
1.24      moritz     98:                        else if (ferror(stdin)) {
1.25      xsa        99:                                (void)cvs_rmdir(cvs_server_tmpdir);
                    100:                                fatal("cvs_server: fgets failed");
1.24      moritz    101:                        }
1.1       jfb       102:                }
                    103:
1.5       jfb       104:                len = strlen(reqbuf);
                    105:                if (len == 0)
                    106:                        continue;
                    107:                else if (reqbuf[len - 1] != '\n') {
1.25      xsa       108:                        (void)cvs_rmdir(cvs_server_tmpdir);
                    109:                        fatal("cvs_server: truncated request");
1.5       jfb       110:                }
                    111:                reqbuf[--len] = '\0';
1.1       jfb       112:
1.5       jfb       113:                cvs_req_handle(reqbuf);
1.1       jfb       114:
                    115:
                    116:        }
                    117:
1.14      joris     118:        /* cleanup the temporary tree */
1.20      xsa       119:        ret = cvs_rmdir(cvs_server_tmpdir);
1.14      joris     120:
                    121:        return (ret);
1.1       jfb       122: }