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