comparison lib-src/gnuslib.c @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ec9a17fef872
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 /* -*-C-*-
2 Common library code for the GNU Emacs server and client.
3
4 This file is part of GNU Emacs.
5
6 Copying is permitted under those conditions described by the GNU
7 General Public License.
8
9 Copyright (C) 1989 Free Software Foundation, Inc.
10
11 Author: Andy Norman (ange@hplb.hpl.hp.com), based on
12 'etc/server.c' and 'etc/emacsclient.c' from the 18.52 GNU
13 Emacs distribution.
14
15 Please mail bugs and suggestions to the author at the above address.
16 */
17
18 /* HISTORY
19 * 11-Nov-1990 bristor@simba
20 * Added EOT stuff.
21 */
22
23 /*
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.
27 *
28 * (If gnuserv came bundled with your emacs, the README file is probably
29 * ../etc/gnuserv.README relative to the directory containing this file)
30 */
31
32 #if 0
33 static char rcsid [] = "!Header: gnuslib.c,v 2.4 95/02/16 11:57:37 arup alpha !";
34 #endif
35
36 #include "gnuserv.h"
37
38 #ifdef SYSV_IPC
39 static int connect_to_ipc_server (void);
40 #endif
41 #ifdef UNIX_DOMAIN_SOCKETS
42 static int connect_to_unix_server (void);
43 #endif
44 #ifdef INTERNET_DOMAIN_SOCKETS
45 static int connect_to_internet_server (char *serverhost, u_short port);
46 #endif
47
48 /* On some systems, e.g. DGUX, inet_addr returns a 'struct in_addr'. */
49 #ifdef HAVE_BROKEN_INET_ADDR
50 # define IN_ADDR struct in_addr
51 # define NUMERIC_ADDR_ERROR (numeric_addr.s_addr == -1)
52 #else
53 # if (LONGBITS > 32)
54 # define IN_ADDR unsigned int
55 # else
56 # define IN_ADDR unsigned long
57 # endif
58 # define NUMERIC_ADDR_ERROR (numeric_addr == (IN_ADDR) -1)
59 #endif
60
61 #include <stdlib.h>
62 #include <stdio.h>
63 #include <sys/types.h>
64 #include <sys/stat.h>
65 #include <unistd.h>
66 #include <string.h>
67
68 #include <arpa/inet.h>
69
70 char *progname = NULL;
71
72 int make_connection(hostarg, portarg, s)
73 char *hostarg;
74 int portarg;
75 int *s;
76 {
77 #ifdef INTERNET_DOMAIN_SOCKETS
78 char localhost[HOSTNAMSZ];
79 char *ptr;
80 if (hostarg == NULL)
81 hostarg = getenv("GNU_HOST");
82 if (portarg == 0 && (ptr=getenv("GNU_PORT")) != NULL)
83 portarg = atoi(ptr);
84 #endif
85
86 if (hostarg != NULL) {
87 /* hostname was given explicitly, via cmd line arg or GNU_HOST,
88 * so obey it. */
89 #ifdef UNIX_DOMAIN_SOCKETS
90 if (!strcmp(hostarg, "unix")) {
91 *s = connect_to_unix_server();
92 return (int) CONN_UNIX;
93 }
94 #endif /* UNIX_DOMAIN_SOCKETS */
95 #ifdef INTERNET_DOMAIN_SOCKETS
96 *s = connect_to_internet_server(hostarg, portarg);
97 return (int) CONN_INTERNET;
98 #endif
99 #ifdef SYSV_IPC
100 return -1; /* hostarg should always be NULL for SYSV_IPC */
101 #endif
102 } else {
103 /* no hostname given. Use unix-domain/sysv-ipc, or
104 * internet-domain connection to local host if they're not available. */
105 #ifdef UNIX_DOMAIN_SOCKETS
106 *s = connect_to_unix_server();
107 return (int) CONN_UNIX;
108 #endif
109 #ifdef SYSV_IPC
110 *s = connect_to_ipc_server();
111 return (int) CONN_IPC;
112 #endif
113 #ifdef INTERNET_DOMAIN_SOCKETS
114 gethostname(localhost,HOSTNAMSZ); /* use this host by default */
115 *s = connect_to_internet_server(localhost, portarg);
116 return (int) CONN_INTERNET;
117 #endif
118 }
119 }
120
121 #ifdef SYSV_IPC
122 /*
123 connect_to_ipc_server -- establish connection with server process via SYSV IPC
124 Returns msqid for server if successful.
125 */
126 static int connect_to_ipc_server (void)
127 {
128 int s; /* connected msqid */
129 key_t key; /* message key */
130 char buf[GSERV_BUFSZ+1]; /* buffer for filename */
131
132 sprintf(buf,"/tmp/gsrv%d",(int)geteuid());
133 creat(buf,0600);
134 if ((key = ftok(buf,1)) == -1) {
135 perror(progname);
136 fprintf(stderr, "%s: unable to get ipc key from %s\n",
137 progname, buf);
138 exit(1);
139 }
140
141 if ((s = msgget(key,0600)) == -1) {
142 perror(progname);
143 fprintf(stderr,"%s: unable to access msg queue\n",progname);
144 exit(1);
145 }; /* if */
146
147 return(s);
148
149 } /* connect_to_ipc_server */
150
151
152 /*
153 disconnect_from_ipc_server -- inform the server that sending has finished,
154 and wait for its reply.
155 */
156 void disconnect_from_ipc_server(s,msgp,echo)
157 int s;
158 struct msgbuf *msgp;
159 int echo;
160 {
161 int len; /* length of received message */
162
163 send_string(s,EOT_STR); /* EOT terminates this message */
164 msgp->mtype = 1;
165
166 if(msgsnd(s,msgp,strlen(msgp->mtext)+1,0) < 0) {
167 perror(progname);
168 fprintf(stderr,"%s: unable to send message to server\n",progname);
169 exit(1);
170 }; /* if */
171
172 if((len = msgrcv(s,msgp,GSERV_BUFSZ,getpid(),0)) < 0) {
173 perror(progname);
174 fprintf(stderr,"%s: unable to receive message from server\n",progname);
175 exit(1);
176 }; /* if */
177
178 if (echo) {
179 msgp->mtext[len] = '\0'; /* string terminate message */
180 fputs(msgp->mtext, stdout);
181 if (msgp->mtext[len-1] != '\n') putchar ('\n');
182 }; /* if */
183
184 } /* disconnect_from_ipc_server */
185 #endif /* SYSV_IPC */
186
187
188 #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS)
189 /*
190 send_string -- send string to socket.
191 */
192 void send_string(s,msg)
193 int s;
194 CONST char *msg;
195 {
196 #if 0
197 if (send(s,msg,strlen(msg),0) < 0) {
198 perror(progname);
199 fprintf(stderr,"%s: unable to send\n",progname);
200 exit(1);
201 }; /* if */
202 #else
203 int len, left=strlen(msg);
204 while (left > 0) {
205 if ((len=write(s,msg,min2(left,GSERV_BUFSZ))) < 0) {
206 /* XEmacs addition: robertl@arnet.com */
207 if (errno == EPIPE) {
208 return ;
209 }
210 perror(progname);
211 fprintf(stderr,"%s: unable to send\n",progname);
212 exit(1);
213 }; /* if */
214 left -= len;
215 msg += len;
216 }; /* while */
217 #endif
218 } /* send_string */
219 #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */
220
221
222 #ifdef UNIX_DOMAIN_SOCKETS
223 /*
224 connect_to_unix_server -- establish connection with server process via a unix-
225 domain socket. Returns socket descriptor for server
226 if successful.
227 */
228 static int connect_to_unix_server (void)
229 {
230 int s; /* connected socket descriptor */
231 struct sockaddr_un server; /* for unix connections */
232
233 if ((s = socket(AF_UNIX,SOCK_STREAM,0)) < 0) {
234 perror(progname);
235 fprintf(stderr,"%s: unable to create socket\n",progname);
236 exit(1);
237 }; /* if */
238
239 server.sun_family = AF_UNIX;
240 #ifdef HIDE_UNIX_SOCKET
241 sprintf(server.sun_path,"/tmp/gsrvdir%d/gsrv",(int)geteuid());
242 #else /* HIDE_UNIX_SOCKET */
243 sprintf(server.sun_path,"/tmp/gsrv%d",(int)geteuid());
244 #endif /* HIDE_UNIX_SOCKET */
245 if (connect(s,(struct sockaddr *)&server,strlen(server.sun_path)+2) < 0) {
246 perror(progname);
247 fprintf(stderr,"%s: unable to connect to local\n",progname);
248 exit(1);
249 }; /* if */
250
251 return(s);
252
253 } /* connect_to_unix_server */
254 #endif /* UNIX_DOMAIN_SOCKETS */
255
256
257 #ifdef INTERNET_DOMAIN_SOCKETS
258 /*
259 internet_addr -- return the internet addr of the hostname or
260 internet address passed. Return -1 on error.
261 */
262 int internet_addr(host)
263 char *host;
264 {
265 struct hostent *hp; /* pointer to host info for remote host */
266 IN_ADDR numeric_addr; /* host address */
267
268 numeric_addr = inet_addr(host);
269 if (!NUMERIC_ADDR_ERROR)
270 return numeric_addr;
271 else if ((hp = gethostbyname(host)) != NULL)
272 return ((struct in_addr *)(hp->h_addr))->s_addr;
273 else
274 return -1;
275
276 } /* internet_addr */
277
278 #ifdef AUTH_MAGIC_COOKIE
279 # include <X11/X.h>
280 # include <X11/Xauth.h>
281
282 static Xauth *server_xauth = NULL;
283 #endif
284
285 /*
286 connect_to_internet_server -- establish connection with server process via
287 an internet domain socket. Returns socket
288 descriptor for server if successful.
289 */
290 static int connect_to_internet_server (char *serverhost, u_short port)
291 {
292 int s; /* connected socket descriptor */
293 struct servent *sp; /* pointer to service information */
294 struct sockaddr_in peeraddr_in; /* for peer socket address */
295 char buf[512]; /* temporary buffer */
296
297 /* clear out address structures */
298 memset((char *)&peeraddr_in,0,sizeof(struct sockaddr_in));
299
300 /* Set up the peer address to which we will connect. */
301 peeraddr_in.sin_family = AF_INET;
302
303 /* look up the server host's internet address */
304 if ((peeraddr_in.sin_addr.s_addr = internet_addr(serverhost)) == -1) {
305 fprintf(stderr,"%s: unable to find %s in /etc/hosts or from YP\n",
306 progname,serverhost);
307 exit(1);
308 }; /* if */
309
310 if (port == 0) {
311 if ((sp = getservbyname ("gnuserv","tcp")) == NULL)
312 peeraddr_in.sin_port = htons(DEFAULT_PORT+getuid());
313 else
314 peeraddr_in.sin_port = sp->s_port;
315 } /* if */
316 else
317 peeraddr_in.sin_port = htons(port);
318
319 /* Create the socket. */
320 if ((s = socket (AF_INET,SOCK_STREAM, 0))== -1) {
321 perror(progname);
322 fprintf(stderr,"%s: unable to create socket\n",progname);
323 exit(1);
324 }; /* if */
325
326 /* Try to connect to the remote server at the address
327 * which was just built into peeraddr.
328 */
329 if (connect(s, (struct sockaddr *)&peeraddr_in,
330 sizeof(struct sockaddr_in)) == -1) {
331 perror(progname);
332 fprintf(stderr, "%s: unable to connect to remote\n",progname);
333 exit(1);
334 }; /* if */
335
336 #ifdef AUTH_MAGIC_COOKIE
337
338 /* send credentials using MIT-MAGIC-COOKIE-1 protocol */
339
340 server_xauth =
341 XauGetAuthByAddr(FamilyInternet,
342 sizeof(peeraddr_in.sin_addr.s_addr),
343 (char *) &peeraddr_in.sin_addr.s_addr,
344 strlen(MCOOKIE_SCREEN), MCOOKIE_SCREEN,
345 strlen(MCOOKIE_X_NAME), MCOOKIE_X_NAME);
346
347 if (server_xauth && server_xauth->data) {
348 sprintf(buf, "%s\n%d\n", MCOOKIE_NAME, server_xauth->data_length);
349 write (s, buf, strlen(buf));
350 write (s, server_xauth->data, server_xauth->data_length);
351
352 return (s);
353 }
354
355 #endif /* AUTH_MAGIC_COOKIE */
356
357 sprintf (buf, "%s\n", DEFAUTH_NAME);
358 write (s, buf, strlen(buf));
359
360 return(s);
361
362 } /* connect_to_internet_server */
363 #endif /* INTERNET_DOMAIN_SOCKETS */
364
365
366 #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS)
367 /*
368 disconnect_from_server -- inform the server that sending has finished, and wait for
369 its reply.
370 */
371 void disconnect_from_server(s,echo)
372 int s;
373 int echo;
374 {
375 #if 0
376 char buffer[REPLYSIZ+1];
377 #else
378 char buffer[GSERV_BUFSZ+1];
379 #endif
380 int add_newline = 1;
381 int length;
382
383 send_string(s,EOT_STR); /* make sure server gets string */
384
385 #if !defined (linux) && !defined (_SCO_DS)
386 /*
387 * shutdown is completely hozed under linux. If s is a unix domain socket,
388 * you'll get EOPNOTSUPP back from it. If s is an internet socket, you get
389 * a broken pipe when you try to read a bit later. The latter
390 * problem is fixed for linux versions >= 1.1.46, but the problem
391 * with unix sockets persists. Sigh.
392 */
393
394 if (shutdown(s,1) == -1) {
395 perror(progname);
396 fprintf(stderr, "%s: unable to shutdown socket\n",progname);
397 exit(1);
398 }; /* if */
399 #endif
400
401 #if 0
402 while((length = recv(s,buffer,REPLYSIZ,0)) > 0) {
403 buffer[length] = '\0';
404 if (echo) fputs(buffer,stdout);
405 add_newline = (buffer[length-1] != '\n');
406 }; /* while */
407 #else
408 while ((length = read(s,buffer,GSERV_BUFSZ)) > 0) {
409 buffer[length] = '\0';
410 if (echo) {
411 fputs(buffer,stdout);
412 add_newline = (buffer[length-1] != '\n');
413 }; /* if */
414 }; /* while */
415 #endif
416
417 if (echo && add_newline) putchar('\n');
418
419 if(length < 0) {
420 perror(progname);
421 fprintf(stderr,"%s: unable to read the reply from the server\n",progname);
422 exit(1);
423 }; /* if */
424
425 } /* disconnect_from_server */
426 #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */