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