xref: /openbsd-src/usr.bin/cvs/server.c (revision 06ccd5dad87daa7ede3ae33928857a729a8043ea)
1 /*	$OpenBSD: server.c,v 1.5 2004/08/02 17:35:37 jfb Exp $	*/
2 /*
3  * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
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
24  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/types.h>
28 
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <paths.h>
35 #include <sysexits.h>
36 
37 #include "cvs.h"
38 #include "log.h"
39 #include "sock.h"
40 #include "proto.h"
41 
42 
43 
44 /* argument vector built by the `Argument' and `Argumentx' requests */
45 char   **cvs_args;
46 u_int    cvs_nbarg = 0;
47 
48 u_int   cvs_utf8ok = 0;
49 u_int   cvs_case   = 0;
50 
51 
52 
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 
65 int
66 cvs_server(int argc, char **argv)
67 {
68 	size_t len;
69 	char reqbuf[128];
70 
71 	if (argc != 1) {
72 		return (EX_USAGE);
73 	}
74 
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 
79 	if (cvs_sock_connect(CVSD_SOCK_PATH) < 0) {
80 		cvs_log(LP_ERR, "failed to connect to CVS server socket");
81 		return (-1);
82 	}
83 
84 
85 	for (;;) {
86 		if (fgets(reqbuf, sizeof(reqbuf), stdin) == NULL) {
87 			if (feof(stdin))
88 				break;
89 			else if (ferror(stdin))
90 				return (EX_DATAERR);
91 		}
92 
93 		len = strlen(reqbuf);
94 		if (len == 0)
95 			continue;
96 		else if (reqbuf[len - 1] != '\n') {
97 			cvs_log(LP_ERR, "truncated request");
98 			return (EX_DATAERR);
99 		}
100 		reqbuf[--len] = '\0';
101 
102 		cvs_req_handle(reqbuf);
103 
104 
105 	}
106 
107 	cvs_sock_disconnect();
108 
109 	return (0);
110 }
111