xref: /openbsd-src/gnu/usr.bin/cvs/windows-NT/startserver.c (revision b2346922a76a50a89e33beab4ebbc0950de8a8df)
11e72d8d2Sderaadt /* startserver.c --- open a connection to the CVS server under Windows NT
21e72d8d2Sderaadt    Jim Blandy <jimb@cyclic.com> --- August 1995  */
31e72d8d2Sderaadt 
41e72d8d2Sderaadt #include "cvs.h"
51e72d8d2Sderaadt #include "rcmd.h"
61e72d8d2Sderaadt 
71e72d8d2Sderaadt #include <stdlib.h>
81e72d8d2Sderaadt #include <winsock.h>
91e72d8d2Sderaadt #include <malloc.h>
101e72d8d2Sderaadt #include <io.h>
111e72d8d2Sderaadt #include <errno.h>
121e72d8d2Sderaadt 
13*b2346922Stholo /* Keep track of whether we've opened a socket so that wnt_shutdown_server
14*b2346922Stholo    can do the correct thing.  We don't want to call shutdown or
15*b2346922Stholo    closesocket on a pipe. */
16*b2346922Stholo 
17*b2346922Stholo static int opened_a_socket = 0;
18*b2346922Stholo 
191e72d8d2Sderaadt void
wnt_start_server(int * tofd,int * fromfd,char * client_user,char * server_user,char * server_host,char * server_cvsroot)201e72d8d2Sderaadt wnt_start_server (int *tofd, int *fromfd,
211e72d8d2Sderaadt 		  char *client_user,
221e72d8d2Sderaadt 		  char *server_user,
231e72d8d2Sderaadt 		  char *server_host,
241e72d8d2Sderaadt 		  char *server_cvsroot)
251e72d8d2Sderaadt {
261e72d8d2Sderaadt     char *cvs_server;
271e72d8d2Sderaadt     char *command;
28c2c61682Stholo     struct servent *sptr;
291e72d8d2Sderaadt     unsigned short port;
3050bf276cStholo     int read_fd;
31c2c61682Stholo     char *portenv;
321e72d8d2Sderaadt 
331e72d8d2Sderaadt     if (! (cvs_server = getenv ("CVS_SERVER")))
341e72d8d2Sderaadt         cvs_server = "cvs";
35c2c61682Stholo     command = xmalloc (strlen (cvs_server)
361e72d8d2Sderaadt 		       + strlen (server_cvsroot)
371e72d8d2Sderaadt 		       + 50);
381e72d8d2Sderaadt     sprintf (command, "%s -d %s server", cvs_server, server_cvsroot);
391e72d8d2Sderaadt 
40c2c61682Stholo     portenv = getenv("CVS_RCMD_PORT");
41c2c61682Stholo     if (portenv)
42c2c61682Stholo 	port = atoi(portenv);
43c2c61682Stholo     else if ((sptr = getservbyname("shell", "tcp")) != NULL)
44c2c61682Stholo 	port = sptr->s_port;
451e72d8d2Sderaadt     else
46c2c61682Stholo 	port = IPPORT_CMDSERVER; /* shell/tcp */
471e72d8d2Sderaadt 
481e72d8d2Sderaadt     read_fd = rcmd (&server_host,
491e72d8d2Sderaadt     	            port,
501e72d8d2Sderaadt     	            client_user,
511e72d8d2Sderaadt 	            (server_user ? server_user : client_user),
521e72d8d2Sderaadt 	            command,
531e72d8d2Sderaadt 	            0);
541e72d8d2Sderaadt     if (read_fd < 0)
55b6c02222Stholo 	error (1, 0, "cannot start server via rcmd: %s",
56b6c02222Stholo 	       SOCK_STRERROR (SOCK_ERRNO));
571e72d8d2Sderaadt 
5850bf276cStholo     *tofd = read_fd;
591e72d8d2Sderaadt     *fromfd = read_fd;
60c2c61682Stholo     free (command);
61*b2346922Stholo 
62*b2346922Stholo     opened_a_socket = 1;
631e72d8d2Sderaadt }
641e72d8d2Sderaadt 
651e72d8d2Sderaadt 
661e72d8d2Sderaadt void
wnt_shutdown_server(int fd)671e72d8d2Sderaadt wnt_shutdown_server (int fd)
681e72d8d2Sderaadt {
69*b2346922Stholo     if (opened_a_socket)
70*b2346922Stholo     {
711e72d8d2Sderaadt 	SOCKET s;
721e72d8d2Sderaadt 
7350bf276cStholo 	s = fd;
741e72d8d2Sderaadt 	if (shutdown (s, 2) == SOCKET_ERROR)
75b6c02222Stholo 	    error (1, 0, "couldn't shutdown server connection: %s",
76b6c02222Stholo 		   SOCK_STRERROR (SOCK_ERRNO));
771e72d8d2Sderaadt 	if (closesocket (s) == SOCKET_ERROR)
78b6c02222Stholo 	    error (1, 0, "couldn't close server connection: %s",
79b6c02222Stholo 		   SOCK_STRERROR (SOCK_ERRNO));
801e72d8d2Sderaadt     }
81*b2346922Stholo     else
82*b2346922Stholo     {
83*b2346922Stholo 	if (close (fd) < 0)
84*b2346922Stholo 	    error (1, errno, "cannot close server connection");
85*b2346922Stholo     }
86*b2346922Stholo }
87