2 Common library code for the GNU Emacs server and client.
4 This file is part of GNU Emacs.
6 Copying is permitted under those conditions described by the GNU
7 General Public License.
9 Copyright (C) 1989 Free Software Foundation, Inc.
11 Author: Andy Norman (ange@hplb.hpl.hp.com), based on
12 'etc/server.c' and 'etc/emacsclient.c' from the 18.52 GNU
15 Please mail bugs and suggestions to the author at the above address.
19 * 11-Nov-1990 bristor@simba
24 * This file incorporates new features added by Bob Weiner <weiner@mot.com>,
25 * Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>.
26 * Please see the note at the end of the README file for details.
28 * (If gnuserv came bundled with your emacs, the README file is probably
29 * ../etc/gnuserv.README relative to the directory containing this file)
33 static char rcsid [] = "!Header: gnuslib.c,v 2.4 95/02/16 11:57:37 arup alpha !";
40 static int connect_to_ipc_server (void);
42 #ifdef UNIX_DOMAIN_SOCKETS
43 static int connect_to_unix_server (void);
45 #ifdef INTERNET_DOMAIN_SOCKETS
46 static int connect_to_internet_server (char *serverhost, unsigned short port);
49 /* On some systems, e.g. DGUX, inet_addr returns a 'struct in_addr'. */
50 #ifdef HAVE_BROKEN_INET_ADDR
51 # define IN_ADDR struct in_addr
52 # define NUMERIC_ADDR_ERROR (numeric_addr.s_addr == -1)
55 # define IN_ADDR unsigned int
57 # define IN_ADDR unsigned long
59 # define NUMERIC_ADDR_ERROR (numeric_addr == (IN_ADDR) -1)
64 #include <sys/types.h>
68 #endif /* HAVE_UNISTD_H */
71 #endif /* HAVE_STRING_H */
73 #include <arpa/inet.h>
77 char *progname = NULL;
80 make_connection (char *hostarg, int portarg, int *s)
82 #ifdef INTERNET_DOMAIN_SOCKETS
85 hostarg = getenv("GNU_HOST");
86 if (portarg == 0 && (ptr=getenv("GNU_PORT")) != NULL)
90 if (hostarg != NULL) {
91 /* hostname was given explicitly, via cmd line arg or GNU_HOST,
93 #ifdef UNIX_DOMAIN_SOCKETS
94 if (!strcmp(hostarg, "unix")) {
95 *s = connect_to_unix_server();
96 return (int) CONN_UNIX;
98 #endif /* UNIX_DOMAIN_SOCKETS */
99 #ifdef INTERNET_DOMAIN_SOCKETS
100 *s = connect_to_internet_server(hostarg, portarg);
101 return (int) CONN_INTERNET;
104 return -1; /* hostarg should always be NULL for SYSV_IPC */
107 /* no hostname given. Use unix-domain/sysv-ipc, or
108 * internet-domain connection to local host if they're not available. */
109 #if defined(UNIX_DOMAIN_SOCKETS)
110 *s = connect_to_unix_server();
111 return (int) CONN_UNIX;
112 #elif defined(SYSV_IPC)
113 *s = connect_to_ipc_server();
114 return (int) CONN_IPC;
115 #elif defined(INTERNET_DOMAIN_SOCKETS)
117 char localhost[HOSTNAMSZ];
118 gethostname(localhost,HOSTNAMSZ); /* use this host by default */
119 *s = connect_to_internet_server(localhost, portarg);
120 return (int) CONN_INTERNET;
122 #endif /* IPC type */
128 connect_to_ipc_server -- establish connection with server process via SYSV IPC
129 Returns msqid for server if successful.
132 connect_to_ipc_server (void)
134 int s; /* connected msqid */
135 key_t key; /* message key */
136 char buf[GSERV_BUFSZ+1]; /* buffer for filename */
138 sprintf(buf,"%s/gsrv%d",tmpdir,(int)geteuid());
140 if ((key = ftok(buf,1)) == -1) {
142 fprintf(stderr, "%s: unable to get ipc key from %s\n",
147 if ((s = msgget(key,0600)) == -1) {
149 fprintf(stderr,"%s: unable to access msg queue\n",progname);
155 } /* connect_to_ipc_server */
159 disconnect_from_ipc_server -- inform the server that sending has finished,
160 and wait for its reply.
163 disconnect_from_ipc_server (int s, struct msgbuf *msgp, int echo)
165 int len; /* length of received message */
167 send_string(s,EOT_STR); /* EOT terminates this message */
170 if(msgsnd(s,msgp,strlen(msgp->mtext)+1,0) < 0) {
172 fprintf(stderr,"%s: unable to send message to server\n",progname);
176 if((len = msgrcv(s,msgp,GSERV_BUFSZ,getpid(),0)) < 0) {
178 fprintf(stderr,"%s: unable to receive message from server\n",progname);
183 msgp->mtext[len] = '\0'; /* string terminate message */
184 fputs(msgp->mtext, stdout);
185 if (msgp->mtext[len-1] != '\n') putchar ('\n');
188 } /* disconnect_from_ipc_server */
189 #endif /* SYSV_IPC */
192 #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS)
194 send_string -- send string to socket.
197 send_string (int s, const char *msg)
200 if (send(s,msg,strlen(msg),0) < 0) {
202 fprintf(stderr,"%s: unable to send\n",progname);
206 int len, left=strlen(msg);
208 if ((len=write(s,msg,min2(left,GSERV_BUFSZ))) < 0) {
209 /* XEmacs addition: robertl@arnet.com */
210 if (errno == EPIPE) {
214 fprintf(stderr,"%s: unable to send\n",progname);
224 read_line -- read a \n terminated line from a socket
227 read_line (int s, char *dest)
231 char buffer[GSERV_BUFSZ+1];
233 while ((length=read(s,buffer+offset,1)>0) && buffer[offset]!='\n'
234 && buffer[offset] != EOT_CHR) {
236 if (offset >= GSERV_BUFSZ)
239 buffer[offset] = '\0';
243 #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */
246 #ifdef UNIX_DOMAIN_SOCKETS
248 connect_to_unix_server -- establish connection with server process via a unix-
249 domain socket. Returns socket descriptor for server
253 connect_to_unix_server (void)
255 int s; /* connected socket descriptor */
256 struct sockaddr_un server; /* for unix connections */
258 if ((s = socket(AF_UNIX,SOCK_STREAM,0)) < 0) {
260 fprintf(stderr,"%s: unable to create socket\n",progname);
264 server.sun_family = AF_UNIX;
265 #ifdef HIDE_UNIX_SOCKET
266 sprintf(server.sun_path,"%s/gsrvdir%d/gsrv",tmpdir,(int)geteuid());
267 #else /* HIDE_UNIX_SOCKET */
268 sprintf(server.sun_path,"%s/gsrv%d",tmpdir,(int)geteuid());
269 #endif /* HIDE_UNIX_SOCKET */
270 if (connect(s,(struct sockaddr *)&server,strlen(server.sun_path)+2) < 0) {
272 fprintf(stderr,"%s: unable to connect to local\n",progname);
278 } /* connect_to_unix_server */
279 #endif /* UNIX_DOMAIN_SOCKETS */
282 #ifdef INTERNET_DOMAIN_SOCKETS
284 internet_addr -- return the internet addr of the hostname or
285 internet address passed. Return -1 on error.
288 internet_addr (char *host)
290 struct hostent *hp; /* pointer to host info for remote host */
291 IN_ADDR numeric_addr; /* host address */
293 numeric_addr = inet_addr(host);
294 if (!NUMERIC_ADDR_ERROR)
296 else if ((hp = gethostbyname(host)) != NULL)
297 return ((struct in_addr *)(hp->h_addr))->s_addr;
301 } /* internet_addr */
303 #ifdef AUTH_MAGIC_COOKIE
305 # include <X11/Xauth.h>
307 static Xauth *server_xauth = NULL;
311 connect_to_internet_server -- establish connection with server process via
312 an internet domain socket. Returns socket
313 descriptor for server if successful.
316 connect_to_internet_server (char *serverhost, unsigned short port)
318 int s; /* connected socket descriptor */
319 struct servent *sp; /* pointer to service information */
320 struct sockaddr_in peeraddr_in; /* for peer socket address */
321 char buf[512]; /* temporary buffer */
325 /* clear out address structures */
326 memset((char *)&peeraddr_in,0,sizeof(struct sockaddr_in));
328 /* Set up the peer address to which we will connect. */
329 peeraddr_in.sin_family = AF_INET;
331 /* look up the server host's internet address */
332 if ((t = internet_addr(serverhost)) == -1) {
333 fprintf(stderr,"%s: unable to find %s in /etc/hosts or from YP\n",
334 progname,serverhost);
337 peeraddr_in.sin_addr.s_addr = t;
341 if ((sp = getservbyname ("gnuserv","tcp")) == NULL)
342 peeraddr_in.sin_port = htons(DEFAULT_PORT+getuid());
344 peeraddr_in.sin_port = sp->s_port;
347 peeraddr_in.sin_port = htons(port);
349 /* Create the socket. */
350 if ((s = socket (AF_INET,SOCK_STREAM, 0))== -1) {
352 fprintf(stderr,"%s: unable to create socket\n",progname);
356 /* Try to connect to the remote server at the address
357 * which was just built into peeraddr.
359 if (connect(s, (struct sockaddr *)&peeraddr_in,
360 sizeof(struct sockaddr_in)) == -1) {
362 fprintf(stderr, "%s: unable to connect to remote\n",progname);
366 #ifdef AUTH_MAGIC_COOKIE
368 /* send credentials using MIT-MAGIC-COOKIE-1 protocol */
371 XauGetAuthByAddr(FamilyInternet,
372 sizeof(peeraddr_in.sin_addr.s_addr),
373 (char *) &peeraddr_in.sin_addr.s_addr,
374 strlen(MCOOKIE_SCREEN), MCOOKIE_SCREEN,
375 strlen(MCOOKIE_X_NAME), MCOOKIE_X_NAME);
377 if (server_xauth && server_xauth->data) {
378 sprintf(buf, "%s\n%d\n", MCOOKIE_NAME, server_xauth->data_length);
379 write (s, buf, strlen(buf));
380 write (s, server_xauth->data, server_xauth->data_length);
385 #endif /* AUTH_MAGIC_COOKIE */
387 sprintf (buf, "%s\n", DEFAUTH_NAME);
388 write (s, buf, strlen(buf));
392 } /* connect_to_internet_server */
393 #endif /* INTERNET_DOMAIN_SOCKETS */
396 #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS)
398 disconnect_from_server -- inform the server that sending has finished, and wait for
402 disconnect_from_server (int s, int echo)
405 char buffer[REPLYSIZ+1];
407 char buffer[GSERV_BUFSZ+1];
412 send_string(s,EOT_STR); /* make sure server gets string */
414 #if !defined (_SCO_DS)
416 * There used to be a comment here complaining about ancient Linux
417 * versions. It is no longer relevant. I don't know why _SCO_DS is
418 * verboten here, as the original comment did not say.
421 if (shutdown(s,1) == -1) {
423 fprintf(stderr, "%s: unable to shutdown socket\n",progname);
429 while((length = recv(s,buffer,REPLYSIZ,0)) > 0) {
430 buffer[length] = '\0';
431 if (echo) fputs(buffer,stdout);
432 add_newline = (buffer[length-1] != '\n');
435 while ((length = read(s,buffer,GSERV_BUFSZ)) > 0 ||
436 (length == -1 && errno == EINTR)) {
438 buffer[length] = '\0';
440 fputs(buffer,stdout);
441 add_newline = (buffer[length-1] != '\n');
447 if (echo && add_newline) putchar('\n');
451 fprintf(stderr,"%s: unable to read the reply from the server\n",progname);
455 } /* disconnect_from_server */
456 #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */