PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/xemacs-21.5.31/lib-src/gnuslib.c

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