Annotation of src/usr.bin/cvs/server.c, Revision 1.23
1.23 ! xsa 1: /* $OpenBSD: server.c,v 1.22 2005/08/10 14:49:20 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:
27: #include <sys/types.h>
1.14 joris 28: #include <sys/stat.h>
1.1 jfb 29:
1.18 xsa 30: #include <errno.h>
31: #include <stdio.h>
1.1 jfb 32: #include <stdlib.h>
1.18 xsa 33: #include <string.h>
1.1 jfb 34: #include <unistd.h>
35:
36: #include "cvs.h"
37: #include "log.h"
1.5 jfb 38: #include "proto.h"
1.1 jfb 39:
40:
41: /* argument vector built by the `Argument' and `Argumentx' requests */
42: char **cvs_args;
1.6 tedu 43: u_int cvs_nbarg = 0;
1.1 jfb 44: u_int cvs_utf8ok = 0;
1.17 jfb 45: u_int cvs_case = 0;
1.8 joris 46:
1.17 jfb 47: struct cvs_cmd cvs_cmd_server = {
48: CVS_OP_SERVER, 0, "server",
49: { },
50: "Server mode",
51: "",
52: "",
53: NULL,
54: 0,
55: NULL, NULL, NULL, NULL, NULL, NULL,
56: 0
57: };
1.1 jfb 58:
1.14 joris 59: char cvs_server_tmpdir[MAXPATHLEN];
60:
1.1 jfb 61: /*
62: * cvs_server()
63: *
64: * Implement the `cvs server' command. As opposed to the general method of
65: * CVS client/server implementation, the cvs program merely acts as a
66: * redirector to the cvs daemon for most of the tasks.
67: *
68: * The `cvs server' command is only used on the server side of a remote
69: * cvs command. With this command, the cvs program starts listening on
70: * standard input for CVS protocol requests.
71: */
72: int
73: cvs_server(int argc, char **argv)
74: {
1.14 joris 75: int l, ret;
1.5 jfb 76: size_t len;
1.9 joris 77: char reqbuf[512];
1.1 jfb 78:
79: if (argc != 1) {
1.13 joris 80: return (CVS_EX_USAGE);
1.1 jfb 81: }
82:
1.5 jfb 83: /* make sure standard in and standard out are line-buffered */
1.23 ! xsa 84: (void)setvbuf(stdin, NULL, _IOLBF, (size_t)0);
! 85: (void)setvbuf(stdout, NULL, _IOLBF, (size_t)0);
1.5 jfb 86:
1.14 joris 87: /* create the temporary directory */
88: l = snprintf(cvs_server_tmpdir, sizeof(cvs_server_tmpdir),
1.21 xsa 89: "%s/cvs-serv%d", cvs_tmpdir, getpid());
1.14 joris 90: if (l == -1 || l >= (int)sizeof(cvs_server_tmpdir)) {
91: errno = ENAMETOOLONG;
92: cvs_log(LP_ERRNO, "%s", cvs_server_tmpdir);
93: return (CVS_EX_DATA);
94: }
95:
96: if (mkdir(cvs_server_tmpdir, 0700) == -1) {
97: cvs_log(LP_ERRNO, "failed to create temporary directory '%s'",
98: cvs_server_tmpdir);
1.16 joris 99: return (CVS_EX_FILE);
1.14 joris 100: }
101:
1.19 xsa 102: if (cvs_chdir(cvs_server_tmpdir) == -1)
1.16 joris 103: return (CVS_EX_FILE);
1.14 joris 104:
1.1 jfb 105: for (;;) {
1.22 xsa 106: if (fgets(reqbuf, (int)sizeof(reqbuf), stdin) == NULL) {
1.5 jfb 107: if (feof(stdin))
108: break;
109: else if (ferror(stdin))
1.13 joris 110: return (CVS_EX_DATA);
1.1 jfb 111: }
112:
1.5 jfb 113: len = strlen(reqbuf);
114: if (len == 0)
115: continue;
116: else if (reqbuf[len - 1] != '\n') {
117: cvs_log(LP_ERR, "truncated request");
1.13 joris 118: return (CVS_EX_PROTO);
1.5 jfb 119: }
120: reqbuf[--len] = '\0';
1.1 jfb 121:
1.5 jfb 122: cvs_req_handle(reqbuf);
1.1 jfb 123:
124:
125: }
126:
1.14 joris 127: /* cleanup the temporary tree */
1.20 xsa 128: ret = cvs_rmdir(cvs_server_tmpdir);
1.14 joris 129:
130: return (ret);
1.1 jfb 131: }