xref: /openbsd-src/usr.bin/cvs/server.c (revision 11efff7f3ac2b3cfeff0c0cddc14294d9b3aca4f)
1 /*	$OpenBSD: server.c,v 1.6 2004/12/07 17:10:56 tedu 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 /* argument vector built by the `Argument' and `Argumentx' requests */
44 char   **cvs_args;
45 u_int   cvs_nbarg = 0;
46 u_int   cvs_utf8ok = 0;
47 u_int   cvs_case   = 0;
48 
49 /*
50  * cvs_server()
51  *
52  * Implement the `cvs server' command.  As opposed to the general method of
53  * CVS client/server implementation, the cvs program merely acts as a
54  * redirector to the cvs daemon for most of the tasks.
55  *
56  * The `cvs server' command is only used on the server side of a remote
57  * cvs command.  With this command, the cvs program starts listening on
58  * standard input for CVS protocol requests.
59  */
60 int
61 cvs_server(int argc, char **argv)
62 {
63 	size_t len;
64 	char reqbuf[128];
65 
66 	if (argc != 1) {
67 		return (EX_USAGE);
68 	}
69 
70 	/* make sure standard in and standard out are line-buffered */
71 	(void)setvbuf(stdin, NULL, _IOLBF, 0);
72 	(void)setvbuf(stdout, NULL, _IOLBF, 0);
73 
74 	if (cvs_sock_connect(CVSD_SOCK_PATH) < 0) {
75 		cvs_log(LP_ERR, "failed to connect to CVS server socket");
76 		return (-1);
77 	}
78 
79 
80 	for (;;) {
81 		if (fgets(reqbuf, sizeof(reqbuf), stdin) == NULL) {
82 			if (feof(stdin))
83 				break;
84 			else if (ferror(stdin))
85 				return (EX_DATAERR);
86 		}
87 
88 		len = strlen(reqbuf);
89 		if (len == 0)
90 			continue;
91 		else if (reqbuf[len - 1] != '\n') {
92 			cvs_log(LP_ERR, "truncated request");
93 			return (EX_DATAERR);
94 		}
95 		reqbuf[--len] = '\0';
96 
97 		cvs_req_handle(reqbuf);
98 
99 
100 	}
101 
102 	cvs_sock_disconnect();
103 
104 	return (0);
105 }
106