0
|
1 /* Communication subprocess for GNU Emacs acting as server.
|
|
2 Copyright (C) 1986, 1987, 1993 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of GNU Emacs.
|
|
5
|
|
6 GNU Emacs is free software; you can redistribute it and/or modify
|
|
7 it under the terms of the GNU General Public License as published by
|
|
8 the Free Software Foundation; either version 2, or (at your option)
|
|
9 any later version.
|
|
10
|
|
11 GNU Emacs is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 GNU General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with GNU Emacs; see the file COPYING. If not, write to
|
|
18 the Free the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: FSF 19.28. */
|
|
22
|
|
23 /* The GNU Emacs edit server process is run as a subprocess of Emacs
|
|
24 under control of the file lisp/server.el.
|
|
25 This program accepts communication from client (program emacsclient.c)
|
|
26 and passes their commands (consisting of keyboard characters)
|
|
27 up to the Emacs which then executes them. */
|
|
28
|
|
29 #define NO_SHORTNAMES
|
|
30 #include <../src/config.h>
|
|
31 #undef read
|
|
32 #undef write
|
|
33 #undef open
|
|
34 #undef close
|
|
35 #undef signal
|
|
36
|
|
37 #if __STDC__ || defined(STDC_HEADERS)
|
|
38 # include <stdlib.h>
|
|
39 # include <unistd.h>
|
|
40 # include <fcntl.h> /* for creat() */
|
|
41 # include <string.h>
|
|
42 #else
|
|
43 extern int errno;
|
|
44 #endif
|
|
45
|
|
46 #if !defined(HAVE_SOCKETS) && !defined(HAVE_SYSVIPC)
|
|
47 #include <stdio.h>
|
|
48
|
|
49 void
|
|
50 main ()
|
|
51 {
|
|
52 fprintf (stderr, "Sorry, the Emacs server is supported only on systems\n");
|
|
53 fprintf (stderr, "with Berkeley sockets or System V IPC.\n");
|
|
54 exit (1);
|
|
55 }
|
|
56
|
|
57 #else /* HAVE_SOCKETS or HAVE_SYSVIPC */
|
|
58
|
|
59 #if ! defined (HAVE_SYSVIPC)
|
|
60 /* BSD code is very different from SYSV IPC code */
|
|
61
|
|
62 #include <sys/types.h>
|
|
63 #include <sys/file.h>
|
|
64 #include <sys/signal.h>
|
|
65 #include <sys/stat.h> /* Needed for chmod, at least on Linux */
|
|
66 #include <sys/un.h>
|
|
67 #include <stdio.h>
|
|
68 #include <errno.h>
|
70
|
69 #include "../src/sysproc.h" /* Needed for select */
|
114
|
70 #ifndef SOCK_STREAM
|
|
71 /* this is normally included by src/sysproc.h. might be safe to omit
|
|
72 * it entirely. lousy ultrix's sys/socket.h chokes if it's included
|
|
73 * twice, so we can't include unconditionally. */
|
|
74 #include <sys/socket.h>
|
|
75 #endif
|
0
|
76
|
|
77 void
|
|
78 main ()
|
|
79 {
|
|
80 char system_name[256];
|
|
81 int s, infd, fromlen;
|
|
82 struct sockaddr_un server, fromunix;
|
|
83 char *str, string[BUFSIZ], code[BUFSIZ];
|
|
84 FILE *infile;
|
|
85 FILE **openfiles;
|
|
86 int openfiles_size;
|
|
87
|
|
88 openfiles_size = 20;
|
|
89 openfiles = (FILE **) malloc (openfiles_size * sizeof (FILE *));
|
|
90 if (openfiles == 0)
|
|
91 abort ();
|
|
92
|
|
93 /*
|
|
94 * Open up an AF_UNIX socket in this person's home directory
|
|
95 */
|
|
96
|
|
97 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
|
|
98 {
|
|
99 perror ("socket");
|
|
100 exit (1);
|
|
101 }
|
|
102 memset (&server, 0, sizeof (server));
|
|
103 server.sun_family = AF_UNIX;
|
|
104 #ifndef SERVER_HOME_DIR
|
|
105 gethostname (system_name, sizeof (system_name));
|
|
106 sprintf (server.sun_path, "/tmp/esrv%d-%s", (int) geteuid (), system_name);
|
|
107
|
|
108 if (unlink (server.sun_path) == -1 && errno != ENOENT)
|
|
109 {
|
|
110 perror ("unlink");
|
|
111 exit (1);
|
|
112 }
|
|
113 #else
|
|
114 {
|
|
115 char *homedir;
|
|
116
|
|
117 if ((homedir = getenv ("HOME")) == NULL)
|
|
118 {
|
|
119 fprintf (stderr,"No home directory\n");
|
|
120 exit (1);
|
|
121 }
|
|
122 sprintf (server.sun_path, "/tmp/esrv%d", geteuid ());
|
|
123 #if 0
|
|
124 strcpy (server.sun_path, homedir);
|
|
125 strcat (server.sun_path, "/.emacs_server");
|
|
126 #endif
|
|
127 /* Delete anyone else's old server. */
|
|
128 unlink (server.sun_path);
|
|
129 }
|
|
130 #endif /* SERVER_HOME_DIR */
|
|
131
|
|
132 {
|
|
133 int bindlen;
|
|
134
|
|
135 #ifdef HAVE_SOCKADDR_SUN_LEN
|
|
136 /* See W. R. Stevens "Advanced Programming in the Unix Environment"
|
|
137 p. 502 */
|
|
138 bindlen = (sizeof (server.sun_len) + sizeof (server.sun_family)
|
|
139 + strlen (server.sun_path) + 1);
|
|
140 server.sun_len = bindlen;
|
|
141 #else
|
|
142 bindlen = strlen (server.sun_path) + sizeof (server.sun_family);
|
|
143 #endif
|
|
144
|
|
145 if (bind (s, (struct sockaddr *) &server, bindlen) < 0)
|
|
146 {
|
|
147 perror ("bind");
|
|
148 exit (1);
|
|
149 }
|
|
150 }
|
|
151 /* Only this user can send commands to this Emacs. */
|
|
152 chmod (server.sun_path, 0600);
|
|
153 /*
|
|
154 * Now, just wait for everything to come in..
|
|
155 */
|
|
156 if (listen (s, 5) < 0)
|
|
157 {
|
|
158 perror ("listen");
|
|
159 exit (1);
|
|
160 }
|
|
161
|
|
162 /* Disable sigpipes in case luser kills client... */
|
|
163 signal (SIGPIPE, SIG_IGN);
|
|
164 for (;;)
|
|
165 {
|
|
166 int rmask = (1 << s) + 1;
|
|
167 if (select (s + 1, (fd_set *)&rmask, 0, 0, 0) < 0)
|
|
168 perror ("select");
|
|
169 if (rmask & (1 << s)) /* client sends list of filenames */
|
|
170 {
|
|
171 fromlen = sizeof (fromunix);
|
|
172 fromunix.sun_family = AF_UNIX;
|
|
173 infd = accept (s, (struct sockaddr *) &fromunix,
|
|
174 #ifdef HAVE_POSIX_ACCEPT
|
|
175 /* Posix dictates that this arg should be
|
|
176 a size_t *, but all known existing
|
|
177 implementations want an int *. */
|
|
178 (size_t *) &fromlen
|
|
179 #else
|
|
180 &fromlen
|
|
181 #endif
|
|
182 );
|
|
183 if (infd < 0)
|
|
184 {
|
|
185 if (errno == EMFILE || errno == ENFILE)
|
|
186 printf ("Too many clients.\n");
|
|
187 else
|
|
188 perror ("accept");
|
|
189 continue;
|
|
190 }
|
|
191
|
|
192 if (infd >= openfiles_size)
|
|
193 {
|
|
194 openfiles_size *= 2;
|
|
195 openfiles = (FILE **) realloc (openfiles,
|
|
196 openfiles_size * sizeof (FILE *));
|
|
197 if (openfiles == 0)
|
|
198 abort ();
|
|
199 }
|
|
200
|
|
201 infile = (FILE *) fdopen (infd, "r+"); /* open stream */
|
|
202 if (infile == NULL)
|
|
203 {
|
|
204 printf ("Too many clients.\n");
|
|
205 write (infd, "Too many clients.\n", 18);
|
|
206 close (infd); /* Prevent descriptor leak.. */
|
|
207 continue;
|
|
208 }
|
|
209 str = fgets (string, BUFSIZ, infile);
|
|
210 if (str == NULL)
|
|
211 {
|
|
212 perror ("fgets");
|
|
213 close (infd); /* Prevent descriptor leak.. */
|
|
214 continue;
|
|
215 }
|
|
216 openfiles[infd] = infile;
|
|
217 printf ("Client: %d %s", infd, string);
|
|
218 /* If what we read did not end in a newline,
|
|
219 it means there is more. Keep reading from the socket
|
|
220 and outputting to Emacs, until we get the newline. */
|
|
221 while (string[strlen (string) - 1] != '\n')
|
|
222 {
|
|
223 if (fgets (string, BUFSIZ, infile) == 0)
|
|
224 break;
|
|
225 printf ("%s", string);
|
|
226 }
|
|
227 fflush (stdout);
|
|
228 fflush (infile);
|
|
229 continue;
|
|
230 }
|
|
231 else if (rmask & 1) /* emacs sends codeword, fd, and string message */
|
|
232 {
|
|
233 /* Read command codeword and fd */
|
|
234 clearerr (stdin);
|
|
235 scanf ("%s %d%*c", code, &infd);
|
|
236
|
|
237 if (ferror (stdin) || feof (stdin))
|
|
238 {
|
|
239 fprintf (stderr, "server: error reading from standard input\n");
|
|
240 exit (1);
|
|
241 }
|
|
242
|
|
243 /* Transfer text from Emacs to the client, up to a newline. */
|
|
244 infile = openfiles[infd];
|
|
245 while (1)
|
|
246 {
|
|
247 if (fgets (string, BUFSIZ, stdin) == 0)
|
|
248 break;
|
|
249 fprintf (infile, "%s", string);
|
|
250 if (string[strlen (string) - 1] == '\n')
|
|
251 break;
|
|
252 }
|
|
253 fflush (infile);
|
|
254
|
|
255 /* If command is close, close connection to client. */
|
|
256 if (strncmp (code, "Close:", 6) == 0)
|
|
257 if (infd > 2)
|
|
258 {
|
|
259 fclose (infile);
|
|
260 close (infd);
|
|
261 }
|
|
262 continue;
|
|
263 }
|
|
264 }
|
|
265 }
|
|
266
|
|
267 #else /* This is the SYSV IPC section */
|
|
268
|
|
269 #include <sys/types.h>
|
|
270 #include <sys/signal.h>
|
|
271 #include <sys/ipc.h>
|
|
272 #include <sys/msg.h>
|
|
273 #include <setjmp.h>
|
|
274
|
|
275 jmp_buf msgenv;
|
|
276
|
|
277 static SIGTYPE
|
|
278 msgcatch (int arg)
|
|
279 {
|
|
280 longjmp (msgenv, 1);
|
|
281 }
|
|
282
|
|
283
|
|
284 /* "THIS has to be fixed. Remember, stderr may not exist...-rlk."
|
|
285 Incorrect. This program runs as an inferior of Emacs.
|
|
286 Its stderr always exists--rms. */
|
|
287 #include <stdio.h>
|
|
288
|
|
289 #if !__STDC__ && !defined(STDC_HEADERS)
|
|
290 #ifndef convex
|
|
291 char *getenv ();
|
|
292 #endif
|
|
293 #endif
|
|
294
|
|
295 void
|
|
296 main ()
|
|
297 {
|
|
298 int s, fromlen, ioproc;
|
|
299 #if 0
|
|
300 int infd;
|
|
301 #endif /* 0 */
|
|
302 key_t key;
|
|
303 struct msgbuf * msgp =
|
|
304 (struct msgbuf *) malloc (sizeof *msgp + BUFSIZ);
|
|
305 struct msqid_ds msg_st;
|
|
306 int p;
|
|
307 char *homedir;
|
|
308 char string[BUFSIZ];
|
|
309 #if 0
|
|
310 FILE *infile;
|
|
311 #endif /* 0 */
|
|
312
|
|
313 /*
|
|
314 * Create a message queue using ~/.emacs_server as the path for ftok
|
|
315 */
|
|
316 if ((homedir = getenv ("HOME")) == NULL)
|
|
317 {
|
|
318 fprintf (stderr,"No home directory\n");
|
|
319 exit (1);
|
|
320 }
|
|
321 strcpy (string, homedir);
|
|
322 strcat (string, "/.emacs_server");
|
|
323 creat (string, 0600);
|
|
324 key = ftok (string, 1); /* unlikely to be anyone else using it */
|
|
325 s = msgget (key, 0600 | IPC_CREAT);
|
|
326 if (s == -1)
|
|
327 {
|
|
328 perror ("msgget");
|
|
329 exit (1);
|
|
330 }
|
|
331
|
|
332 /* Fork so we can close connection even if parent dies */
|
|
333 p = fork ();
|
|
334 if (setjmp (msgenv))
|
|
335 {
|
|
336 msgctl (s, IPC_RMID, 0);
|
|
337 kill (p, SIGKILL);
|
|
338 exit (0);
|
|
339 }
|
|
340 signal (SIGTERM, msgcatch);
|
|
341 signal (SIGINT, msgcatch);
|
|
342 if (p > 0)
|
|
343 {
|
|
344 /* This is executed in the original process that did the fork above. */
|
|
345 /* Get pid of Emacs itself. */
|
|
346 p = getppid ();
|
|
347 setpgrp (); /* Gnu kills process group on exit */
|
|
348 while (1)
|
|
349 {
|
|
350 /* Is Emacs still alive? */
|
|
351 if (kill (p, 0) < 0)
|
|
352 {
|
|
353 msgctl (s, IPC_RMID, 0);
|
|
354 exit (0);
|
|
355 }
|
|
356 sleep (10);
|
|
357 }
|
|
358 }
|
|
359
|
|
360 /* This is executed in the child made by forking above.
|
|
361 Call it c1. Make another process, ioproc. */
|
|
362
|
|
363 ioproc = fork ();
|
|
364 if (ioproc == 0)
|
|
365 {
|
|
366 /* In process ioproc, wait for text from Emacs,
|
|
367 and send it to the process c1.
|
|
368 This way, c1 only has to wait for one source of input. */
|
|
369 while (fgets (msgp->mtext, BUFSIZ, stdin))
|
|
370 {
|
|
371 msgp->mtype = 1;
|
|
372 msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0);
|
|
373 }
|
|
374 exit (1);
|
|
375 }
|
|
376
|
|
377 /* In the process c1,
|
|
378 listen for messages from clients and pass them to Emacs. */
|
|
379 while (1)
|
|
380 {
|
|
381 if ((fromlen = msgrcv (s, msgp, BUFSIZ - 1, 1, 0)) < 0)
|
|
382 {
|
|
383 perror ("msgrcv");
|
|
384 exit (1);
|
|
385 }
|
|
386 else
|
|
387 {
|
|
388 msgctl (s, IPC_STAT, &msg_st);
|
|
389
|
|
390 /* Distinguish whether the message came from a client, or from
|
|
391 ioproc. */
|
|
392 if (msg_st.msg_lspid == ioproc)
|
|
393 {
|
|
394 char code[BUFSIZ];
|
|
395 int inproc;
|
|
396
|
|
397 /* Message from ioproc: tell a client we are done. */
|
|
398 msgp->mtext[strlen (msgp->mtext)-1] = 0;
|
|
399 sscanf (msgp->mtext, "%s %d", code, &inproc);
|
|
400 msgp->mtype = inproc;
|
|
401 msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0);
|
|
402 continue;
|
|
403 }
|
|
404
|
|
405 /* This is a request from a client: copy to stdout
|
|
406 so that Emacs will get it. Include msg_lspid
|
|
407 so server.el can tell us where to send the reply. */
|
|
408 strncpy (string, msgp->mtext, fromlen);
|
|
409 string[fromlen] = 0; /* make sure */
|
|
410 /* Newline is part of string.. */
|
|
411 printf ("Client: %d %s", (int) msg_st.msg_lspid, string);
|
|
412 fflush (stdout);
|
|
413 /* Now, wait for a wakeup */
|
|
414 }
|
|
415 }
|
|
416 }
|
|
417
|
|
418 #endif /* HAVE_SYSVIPC */
|
|
419
|
|
420 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */
|