Mercurial > hg > xemacs-beta
annotate lib-src/gnuslib.c @ 5411:fd714e8ba81e
Converted to GPLv3 or later. Written by Stephen Turnbull hence.GPLv2 or later.
| author | Mats Lidell <matsl@xemacs.org> |
|---|---|
| date | Sun, 24 Oct 2010 00:20:52 +0200 |
| parents | 061f4f90f874 |
| children | b9167d522a9a |
| rev | line source |
|---|---|
| 428 | 1 /* -*-C-*- |
| 613 | 2 Common library code for the XEmacs server and client. |
| 428 | 3 |
| 613 | 4 This file is part of XEmacs. |
| 428 | 5 |
|
5406
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
3556
diff
changeset
|
6 XEmacs is free software: you can redistribute it and/or modify it |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
3556
diff
changeset
|
7 under the terms of the GNU General Public License as published by the |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
3556
diff
changeset
|
8 Free Software Foundation, either version 3 of the License, or (at your |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
3556
diff
changeset
|
9 option) any later version. |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
3556
diff
changeset
|
10 |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
3556
diff
changeset
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
3556
diff
changeset
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
3556
diff
changeset
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
3556
diff
changeset
|
14 for more details. |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
3556
diff
changeset
|
15 |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
3556
diff
changeset
|
16 You should have received a copy of the GNU General Public License |
|
061f4f90f874
Convert lib-src/ to GPLv3.
Mike Sperber <sperber@deinprogramm.de>
parents:
3556
diff
changeset
|
17 along with XEmacs. If not, see <http://www.gnu.org/licenses/>. |
| 428 | 18 |
| 19 Copyright (C) 1989 Free Software Foundation, Inc. | |
| 20 | |
| 21 Author: Andy Norman (ange@hplb.hpl.hp.com), based on | |
| 22 'etc/server.c' and 'etc/emacsclient.c' from the 18.52 GNU | |
| 23 Emacs distribution. | |
| 24 | |
| 25 Please mail bugs and suggestions to the author at the above address. | |
| 26 */ | |
| 27 | |
| 28 /* HISTORY | |
| 29 * 11-Nov-1990 bristor@simba | |
| 30 * Added EOT stuff. | |
| 31 */ | |
| 32 | |
| 33 /* | |
| 34 * This file incorporates new features added by Bob Weiner <weiner@mot.com>, | |
| 35 * Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>. | |
| 36 * Please see the note at the end of the README file for details. | |
| 37 * | |
| 38 * (If gnuserv came bundled with your emacs, the README file is probably | |
| 39 * ../etc/gnuserv.README relative to the directory containing this file) | |
| 40 */ | |
| 41 | |
| 42 #if 0 | |
| 43 static char rcsid [] = "!Header: gnuslib.c,v 2.4 95/02/16 11:57:37 arup alpha !"; | |
| 44 #endif | |
| 45 | |
| 46 #include "gnuserv.h" | |
| 47 #include <errno.h> | |
| 48 | |
| 49 #ifdef SYSV_IPC | |
| 50 static int connect_to_ipc_server (void); | |
| 51 #endif | |
| 52 #ifdef UNIX_DOMAIN_SOCKETS | |
| 53 static int connect_to_unix_server (void); | |
| 54 #endif | |
| 55 #ifdef INTERNET_DOMAIN_SOCKETS | |
| 458 | 56 static int connect_to_internet_server (char *serverhost, unsigned short port); |
| 428 | 57 #endif |
| 58 | |
| 59 /* On some systems, e.g. DGUX, inet_addr returns a 'struct in_addr'. */ | |
| 60 #ifdef HAVE_BROKEN_INET_ADDR | |
| 61 # define IN_ADDR struct in_addr | |
| 62 # define NUMERIC_ADDR_ERROR (numeric_addr.s_addr == -1) | |
| 63 #else | |
| 64 # if (LONGBITS > 32) | |
| 65 # define IN_ADDR unsigned int | |
| 66 # else | |
| 67 # define IN_ADDR unsigned long | |
| 68 # endif | |
| 69 # define NUMERIC_ADDR_ERROR (numeric_addr == (IN_ADDR) -1) | |
| 70 #endif | |
| 71 | |
| 72 #include <stdlib.h> | |
| 73 #include <stdio.h> | |
| 74 #include <sys/types.h> | |
| 75 #include <sys/stat.h> | |
| 76 #ifdef HAVE_UNISTD_H | |
| 77 #include <unistd.h> | |
| 78 #endif /* HAVE_UNISTD_H */ | |
| 79 #ifdef HAVE_STRING_H | |
| 80 #include <string.h> | |
| 81 #endif /* HAVE_STRING_H */ | |
| 82 | |
| 83 #include <arpa/inet.h> | |
| 84 | |
| 85 char *tmpdir = NULL; | |
| 86 | |
| 87 char *progname = NULL; | |
| 88 | |
| 440 | 89 int |
| 90 make_connection (char *hostarg, int portarg, int *s) | |
| 428 | 91 { |
| 92 #ifdef INTERNET_DOMAIN_SOCKETS | |
| 93 char *ptr; | |
| 94 if (hostarg == NULL) | |
| 95 hostarg = getenv("GNU_HOST"); | |
| 96 if (portarg == 0 && (ptr=getenv("GNU_PORT")) != NULL) | |
| 97 portarg = atoi(ptr); | |
| 98 #endif | |
| 99 | |
| 100 if (hostarg != NULL) { | |
| 101 /* hostname was given explicitly, via cmd line arg or GNU_HOST, | |
| 102 * so obey it. */ | |
| 103 #ifdef UNIX_DOMAIN_SOCKETS | |
| 104 if (!strcmp(hostarg, "unix")) { | |
| 105 *s = connect_to_unix_server(); | |
| 106 return (int) CONN_UNIX; | |
| 107 } | |
| 108 #endif /* UNIX_DOMAIN_SOCKETS */ | |
| 109 #ifdef INTERNET_DOMAIN_SOCKETS | |
| 110 *s = connect_to_internet_server(hostarg, portarg); | |
| 111 return (int) CONN_INTERNET; | |
| 112 #endif | |
| 113 #ifdef SYSV_IPC | |
| 114 return -1; /* hostarg should always be NULL for SYSV_IPC */ | |
| 115 #endif | |
| 116 } else { | |
| 117 /* no hostname given. Use unix-domain/sysv-ipc, or | |
| 118 * internet-domain connection to local host if they're not available. */ | |
| 119 #if defined(UNIX_DOMAIN_SOCKETS) | |
| 120 *s = connect_to_unix_server(); | |
| 121 return (int) CONN_UNIX; | |
| 122 #elif defined(SYSV_IPC) | |
| 123 *s = connect_to_ipc_server(); | |
| 124 return (int) CONN_IPC; | |
| 125 #elif defined(INTERNET_DOMAIN_SOCKETS) | |
| 126 { | |
| 127 char localhost[HOSTNAMSZ]; | |
| 128 gethostname(localhost,HOSTNAMSZ); /* use this host by default */ | |
| 129 *s = connect_to_internet_server(localhost, portarg); | |
| 130 return (int) CONN_INTERNET; | |
| 131 } | |
| 132 #endif /* IPC type */ | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 #ifdef SYSV_IPC | |
| 137 /* | |
| 138 connect_to_ipc_server -- establish connection with server process via SYSV IPC | |
| 139 Returns msqid for server if successful. | |
| 140 */ | |
| 440 | 141 static int |
| 142 connect_to_ipc_server (void) | |
| 428 | 143 { |
| 144 int s; /* connected msqid */ | |
| 145 key_t key; /* message key */ | |
| 146 char buf[GSERV_BUFSZ+1]; /* buffer for filename */ | |
| 147 | |
| 148 sprintf(buf,"%s/gsrv%d",tmpdir,(int)geteuid()); | |
| 149 creat(buf,0600); | |
| 150 if ((key = ftok(buf,1)) == -1) { | |
| 151 perror(progname); | |
| 152 fprintf(stderr, "%s: unable to get ipc key from %s\n", | |
| 153 progname, buf); | |
| 154 exit(1); | |
| 155 } | |
| 156 | |
| 157 if ((s = msgget(key,0600)) == -1) { | |
| 158 perror(progname); | |
| 159 fprintf(stderr,"%s: unable to access msg queue\n",progname); | |
| 160 exit(1); | |
| 161 }; /* if */ | |
| 162 | |
| 163 return(s); | |
| 164 | |
| 165 } /* connect_to_ipc_server */ | |
| 166 | |
| 167 | |
| 168 /* | |
| 169 disconnect_from_ipc_server -- inform the server that sending has finished, | |
| 170 and wait for its reply. | |
| 171 */ | |
| 440 | 172 void |
| 173 disconnect_from_ipc_server (int s, struct msgbuf *msgp, int echo) | |
| 428 | 174 { |
| 175 int len; /* length of received message */ | |
| 176 | |
| 177 send_string(s,EOT_STR); /* EOT terminates this message */ | |
| 178 msgp->mtype = 1; | |
| 179 | |
| 180 if(msgsnd(s,msgp,strlen(msgp->mtext)+1,0) < 0) { | |
| 181 perror(progname); | |
| 182 fprintf(stderr,"%s: unable to send message to server\n",progname); | |
| 183 exit(1); | |
| 184 }; /* if */ | |
| 185 | |
| 186 if((len = msgrcv(s,msgp,GSERV_BUFSZ,getpid(),0)) < 0) { | |
| 187 perror(progname); | |
| 188 fprintf(stderr,"%s: unable to receive message from server\n",progname); | |
| 189 exit(1); | |
| 190 }; /* if */ | |
| 191 | |
| 192 if (echo) { | |
| 193 msgp->mtext[len] = '\0'; /* string terminate message */ | |
| 194 fputs(msgp->mtext, stdout); | |
| 195 if (msgp->mtext[len-1] != '\n') putchar ('\n'); | |
| 196 }; /* if */ | |
| 197 | |
| 198 } /* disconnect_from_ipc_server */ | |
| 199 #endif /* SYSV_IPC */ | |
| 200 | |
| 201 | |
| 202 #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS) | |
| 203 /* | |
| 204 send_string -- send string to socket. | |
| 205 */ | |
| 440 | 206 void |
| 442 | 207 send_string (int s, const char *msg) |
| 428 | 208 { |
| 209 #if 0 | |
| 210 if (send(s,msg,strlen(msg),0) < 0) { | |
| 211 perror(progname); | |
| 212 fprintf(stderr,"%s: unable to send\n",progname); | |
| 213 exit(1); | |
| 214 }; /* if */ | |
| 215 #else | |
| 216 int len, left=strlen(msg); | |
| 217 while (left > 0) { | |
| 218 if ((len=write(s,msg,min2(left,GSERV_BUFSZ))) < 0) { | |
| 219 /* XEmacs addition: robertl@arnet.com */ | |
| 220 if (errno == EPIPE) { | |
| 221 return ; | |
| 222 } | |
| 223 perror(progname); | |
| 224 fprintf(stderr,"%s: unable to send\n",progname); | |
| 225 exit(1); | |
| 226 }; /* if */ | |
| 227 left -= len; | |
| 228 msg += len; | |
| 229 }; /* while */ | |
| 230 #endif | |
| 231 } /* send_string */ | |
| 232 | |
| 233 /* | |
| 234 read_line -- read a \n terminated line from a socket | |
| 235 */ | |
| 440 | 236 int |
| 237 read_line (int s, char *dest) | |
| 428 | 238 { |
| 239 int length; | |
| 240 int offset=0; | |
| 241 char buffer[GSERV_BUFSZ+1]; | |
| 242 | |
| 243 while ((length=read(s,buffer+offset,1)>0) && buffer[offset]!='\n' | |
| 244 && buffer[offset] != EOT_CHR) { | |
| 245 offset += length; | |
| 246 if (offset >= GSERV_BUFSZ) | |
| 247 break; | |
| 248 } | |
| 249 buffer[offset] = '\0'; | |
| 250 strcpy(dest,buffer); | |
| 251 return 1; | |
| 252 } /* read_line */ | |
| 253 #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */ | |
| 254 | |
| 255 | |
| 256 #ifdef UNIX_DOMAIN_SOCKETS | |
| 257 /* | |
| 258 connect_to_unix_server -- establish connection with server process via a unix- | |
| 259 domain socket. Returns socket descriptor for server | |
| 260 if successful. | |
| 261 */ | |
| 440 | 262 static int |
| 263 connect_to_unix_server (void) | |
| 428 | 264 { |
| 265 int s; /* connected socket descriptor */ | |
| 266 struct sockaddr_un server; /* for unix connections */ | |
| 267 | |
| 268 if ((s = socket(AF_UNIX,SOCK_STREAM,0)) < 0) { | |
| 269 perror(progname); | |
| 270 fprintf(stderr,"%s: unable to create socket\n",progname); | |
| 271 exit(1); | |
| 272 }; /* if */ | |
| 273 | |
| 274 server.sun_family = AF_UNIX; | |
| 275 #ifdef HIDE_UNIX_SOCKET | |
| 276 sprintf(server.sun_path,"%s/gsrvdir%d/gsrv",tmpdir,(int)geteuid()); | |
| 277 #else /* HIDE_UNIX_SOCKET */ | |
| 278 sprintf(server.sun_path,"%s/gsrv%d",tmpdir,(int)geteuid()); | |
| 279 #endif /* HIDE_UNIX_SOCKET */ | |
| 280 if (connect(s,(struct sockaddr *)&server,strlen(server.sun_path)+2) < 0) { | |
| 281 perror(progname); | |
| 282 fprintf(stderr,"%s: unable to connect to local\n",progname); | |
| 283 exit(1); | |
| 284 }; /* if */ | |
| 285 | |
| 286 return(s); | |
| 287 | |
| 288 } /* connect_to_unix_server */ | |
| 289 #endif /* UNIX_DOMAIN_SOCKETS */ | |
| 290 | |
| 291 | |
| 292 #ifdef INTERNET_DOMAIN_SOCKETS | |
| 293 /* | |
| 294 internet_addr -- return the internet addr of the hostname or | |
| 295 internet address passed. Return -1 on error. | |
| 296 */ | |
| 440 | 297 int |
| 298 internet_addr (char *host) | |
| 428 | 299 { |
| 300 struct hostent *hp; /* pointer to host info for remote host */ | |
| 301 IN_ADDR numeric_addr; /* host address */ | |
| 302 | |
| 303 numeric_addr = inet_addr(host); | |
| 304 if (!NUMERIC_ADDR_ERROR) | |
| 305 return numeric_addr; | |
| 306 else if ((hp = gethostbyname(host)) != NULL) | |
| 307 return ((struct in_addr *)(hp->h_addr))->s_addr; | |
| 308 else | |
| 309 return -1; | |
| 310 | |
| 311 } /* internet_addr */ | |
| 312 | |
| 313 #ifdef AUTH_MAGIC_COOKIE | |
| 314 # include <X11/X.h> | |
| 315 # include <X11/Xauth.h> | |
| 316 | |
| 317 static Xauth *server_xauth = NULL; | |
| 318 #endif | |
| 319 | |
| 320 /* | |
| 321 connect_to_internet_server -- establish connection with server process via | |
| 322 an internet domain socket. Returns socket | |
| 323 descriptor for server if successful. | |
| 324 */ | |
| 440 | 325 static int |
| 458 | 326 connect_to_internet_server (char *serverhost, unsigned short port) |
| 428 | 327 { |
| 328 int s; /* connected socket descriptor */ | |
| 329 struct servent *sp; /* pointer to service information */ | |
| 330 struct sockaddr_in peeraddr_in; /* for peer socket address */ | |
| 331 char buf[512]; /* temporary buffer */ | |
| 332 | |
| 333 /* clear out address structures */ | |
| 334 memset((char *)&peeraddr_in,0,sizeof(struct sockaddr_in)); | |
| 335 | |
| 336 /* Set up the peer address to which we will connect. */ | |
| 337 peeraddr_in.sin_family = AF_INET; | |
| 338 | |
| 339 /* look up the server host's internet address */ | |
| 647 | 340 if ((peeraddr_in.sin_addr.s_addr = internet_addr (serverhost)) == |
| 341 (unsigned int) -1) | |
| 342 { | |
| 343 fprintf (stderr, "%s: unable to find %s in /etc/hosts or from YP\n", | |
| 344 progname, serverhost); | |
| 345 exit(1); | |
| 346 } | |
| 428 | 347 |
| 348 if (port == 0) { | |
| 349 if ((sp = getservbyname ("gnuserv","tcp")) == NULL) | |
| 350 peeraddr_in.sin_port = htons(DEFAULT_PORT+getuid()); | |
| 351 else | |
| 352 peeraddr_in.sin_port = sp->s_port; | |
| 353 } /* if */ | |
| 354 else | |
| 355 peeraddr_in.sin_port = htons(port); | |
| 356 | |
| 357 /* Create the socket. */ | |
| 358 if ((s = socket (AF_INET,SOCK_STREAM, 0))== -1) { | |
| 359 perror(progname); | |
| 360 fprintf(stderr,"%s: unable to create socket\n",progname); | |
| 361 exit(1); | |
| 362 }; /* if */ | |
| 363 | |
| 364 /* Try to connect to the remote server at the address | |
| 365 * which was just built into peeraddr. | |
| 366 */ | |
| 367 if (connect(s, (struct sockaddr *)&peeraddr_in, | |
| 368 sizeof(struct sockaddr_in)) == -1) { | |
| 369 perror(progname); | |
| 370 fprintf(stderr, "%s: unable to connect to remote\n",progname); | |
| 371 exit(1); | |
| 372 }; /* if */ | |
| 373 | |
| 374 #ifdef AUTH_MAGIC_COOKIE | |
| 375 | |
| 376 /* send credentials using MIT-MAGIC-COOKIE-1 protocol */ | |
| 377 | |
| 378 server_xauth = | |
| 379 XauGetAuthByAddr(FamilyInternet, | |
| 380 sizeof(peeraddr_in.sin_addr.s_addr), | |
| 381 (char *) &peeraddr_in.sin_addr.s_addr, | |
| 382 strlen(MCOOKIE_SCREEN), MCOOKIE_SCREEN, | |
| 383 strlen(MCOOKIE_X_NAME), MCOOKIE_X_NAME); | |
| 384 | |
| 385 if (server_xauth && server_xauth->data) { | |
| 386 sprintf(buf, "%s\n%d\n", MCOOKIE_NAME, server_xauth->data_length); | |
| 387 write (s, buf, strlen(buf)); | |
| 388 write (s, server_xauth->data, server_xauth->data_length); | |
| 389 | |
| 390 return (s); | |
| 391 } | |
| 392 | |
| 393 #endif /* AUTH_MAGIC_COOKIE */ | |
| 394 | |
| 395 sprintf (buf, "%s\n", DEFAUTH_NAME); | |
| 396 write (s, buf, strlen(buf)); | |
| 397 | |
| 398 return(s); | |
| 399 | |
| 400 } /* connect_to_internet_server */ | |
| 401 #endif /* INTERNET_DOMAIN_SOCKETS */ | |
| 402 | |
| 403 | |
| 404 #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS) | |
| 405 /* | |
| 406 disconnect_from_server -- inform the server that sending has finished, and wait for | |
| 407 its reply. | |
| 408 */ | |
| 440 | 409 void |
| 410 disconnect_from_server (int s, int echo) | |
| 428 | 411 { |
| 412 #if 0 | |
| 413 char buffer[REPLYSIZ+1]; | |
| 414 #else | |
| 415 char buffer[GSERV_BUFSZ+1]; | |
| 416 #endif | |
| 417 int add_newline = 1; | |
| 418 int length; | |
| 419 | |
| 420 send_string(s,EOT_STR); /* make sure server gets string */ | |
| 421 | |
| 3556 | 422 #ifndef _SCO_DS |
| 428 | 423 /* |
| 3556 | 424 * There used to be a comment here complaining about ancient Linux |
| 425 * versions. It is no longer relevant. I don't know why _SCO_DS is | |
| 426 * verboten here, as the original comment did not say. | |
| 428 | 427 */ |
| 428 | |
| 429 if (shutdown(s,1) == -1) { | |
| 430 perror(progname); | |
| 431 fprintf(stderr, "%s: unable to shutdown socket\n",progname); | |
| 432 exit(1); | |
| 433 }; /* if */ | |
| 434 #endif | |
| 435 | |
| 436 #if 0 | |
| 437 while((length = recv(s,buffer,REPLYSIZ,0)) > 0) { | |
| 438 buffer[length] = '\0'; | |
| 439 if (echo) fputs(buffer,stdout); | |
| 440 add_newline = (buffer[length-1] != '\n'); | |
| 441 }; /* while */ | |
| 442 #else | |
| 443 while ((length = read(s,buffer,GSERV_BUFSZ)) > 0 || | |
| 444 (length == -1 && errno == EINTR)) { | |
| 3556 | 445 if (length > 0) { |
| 428 | 446 buffer[length] = '\0'; |
| 447 if (echo) { | |
| 448 fputs(buffer,stdout); | |
| 449 add_newline = (buffer[length-1] != '\n'); | |
| 450 }; /* if */ | |
| 451 }; /* if */ | |
| 452 }; /* while */ | |
| 453 #endif | |
| 454 | |
| 455 if (echo && add_newline) putchar('\n'); | |
| 456 | |
| 457 if(length < 0) { | |
| 458 perror(progname); | |
| 459 fprintf(stderr,"%s: unable to read the reply from the server\n",progname); | |
| 460 exit(1); | |
| 461 }; /* if */ | |
| 462 | |
| 463 } /* disconnect_from_server */ | |
| 464 #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */ |
