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