xref: /openbsd-src/gnu/usr.bin/cvs/windows-NT/startserver.c (revision b2346922a76a50a89e33beab4ebbc0950de8a8df)
1 /* startserver.c --- open a connection to the CVS server under Windows NT
2    Jim Blandy <jimb@cyclic.com> --- August 1995  */
3 
4 #include "cvs.h"
5 #include "rcmd.h"
6 
7 #include <stdlib.h>
8 #include <winsock.h>
9 #include <malloc.h>
10 #include <io.h>
11 #include <errno.h>
12 
13 /* Keep track of whether we've opened a socket so that wnt_shutdown_server
14    can do the correct thing.  We don't want to call shutdown or
15    closesocket on a pipe. */
16 
17 static int opened_a_socket = 0;
18 
19 void
wnt_start_server(int * tofd,int * fromfd,char * client_user,char * server_user,char * server_host,char * server_cvsroot)20 wnt_start_server (int *tofd, int *fromfd,
21 		  char *client_user,
22 		  char *server_user,
23 		  char *server_host,
24 		  char *server_cvsroot)
25 {
26     char *cvs_server;
27     char *command;
28     struct servent *sptr;
29     unsigned short port;
30     int read_fd;
31     char *portenv;
32 
33     if (! (cvs_server = getenv ("CVS_SERVER")))
34         cvs_server = "cvs";
35     command = xmalloc (strlen (cvs_server)
36 		       + strlen (server_cvsroot)
37 		       + 50);
38     sprintf (command, "%s -d %s server", cvs_server, server_cvsroot);
39 
40     portenv = getenv("CVS_RCMD_PORT");
41     if (portenv)
42 	port = atoi(portenv);
43     else if ((sptr = getservbyname("shell", "tcp")) != NULL)
44 	port = sptr->s_port;
45     else
46 	port = IPPORT_CMDSERVER; /* shell/tcp */
47 
48     read_fd = rcmd (&server_host,
49     	            port,
50     	            client_user,
51 	            (server_user ? server_user : client_user),
52 	            command,
53 	            0);
54     if (read_fd < 0)
55 	error (1, 0, "cannot start server via rcmd: %s",
56 	       SOCK_STRERROR (SOCK_ERRNO));
57 
58     *tofd = read_fd;
59     *fromfd = read_fd;
60     free (command);
61 
62     opened_a_socket = 1;
63 }
64 
65 
66 void
wnt_shutdown_server(int fd)67 wnt_shutdown_server (int fd)
68 {
69     if (opened_a_socket)
70     {
71 	SOCKET s;
72 
73 	s = fd;
74 	if (shutdown (s, 2) == SOCKET_ERROR)
75 	    error (1, 0, "couldn't shutdown server connection: %s",
76 		   SOCK_STRERROR (SOCK_ERRNO));
77 	if (closesocket (s) == SOCKET_ERROR)
78 	    error (1, 0, "couldn't close server connection: %s",
79 		   SOCK_STRERROR (SOCK_ERRNO));
80     }
81     else
82     {
83 	if (close (fd) < 0)
84 	    error (1, errno, "cannot close server connection");
85     }
86 }
87