(enum syntaxcode): New members Scomment_fence, Sstring_fence.
[emacs.git] / lib-src / emacsserver.c
blob9fbf3e86516e93a30859a112fb5ce3aed490ac88
1 /* Communication subprocess for GNU Emacs acting as server.
2 Copyright (C) 1986, 1987, 1992, 1994 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* The GNU Emacs edit server process is run as a subprocess of Emacs
23 under control of the file lisp/server.el.
24 This program accepts communication from client (program emacsclient.c)
25 and passes their commands (consisting of keyboard characters)
26 up to the Emacs which then executes them. */
28 #define NO_SHORTNAMES
29 #include <signal.h>
30 #include <../src/config.h>
31 #undef read
32 #undef write
33 #undef open
34 #undef close
35 #undef signal
37 #if !defined (HAVE_SOCKETS) && !defined (HAVE_SYSVIPC)
38 #include <stdio.h>
40 main ()
42 fprintf (stderr, "Sorry, the Emacs server is supported only on systems\n");
43 fprintf (stderr, "with Berkeley sockets or System V IPC.\n");
44 exit (1);
47 #else /* HAVE_SOCKETS or HAVE_SYSVIPC */
49 #if defined (HAVE_SOCKETS) && ! defined (NO_SOCKETS_IN_FILE_SYSTEM)
50 /* BSD code is very different from SYSV IPC code */
52 #include <sys/types.h>
53 #include <sys/file.h>
54 #include <sys/socket.h>
55 #include <sys/un.h>
56 #include <stdio.h>
57 #include <errno.h>
58 #include <sys/stat.h>
60 extern int errno;
62 /* Copied from src/process.c */
63 #ifdef FD_SET
64 /* We could get this from param.h, but better not to depend on finding that.
65 And better not to risk that it might define other symbols used in this
66 file. */
67 #ifdef FD_SETSIZE
68 #define MAXDESC FD_SETSIZE
69 #else
70 #define MAXDESC 64
71 #endif
72 #define SELECT_TYPE fd_set
73 #else /* no FD_SET */
74 #define MAXDESC 32
75 #define SELECT_TYPE int
77 /* Define the macros to access a single-int bitmap of descriptors. */
78 #define FD_SET(n, p) (*(p) |= (1 << (n)))
79 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
80 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
81 #define FD_ZERO(p) (*(p) = 0)
82 #endif /* no FD_SET */
84 /* This is the file name of the socket that we made. */
86 char *socket_name;
88 /* Name of this program. */
90 char *progname;
92 /* Handle fatal signals. */
94 /* This is the handler. */
96 SIGTYPE
97 delete_socket (sig)
98 int sig;
100 signal (sig, SIG_DFL);
101 unlink (socket_name);
102 kill (getpid (), sig);
105 /* Set up to handle all the signals. */
107 handle_signals ()
109 signal (SIGHUP, delete_socket);
110 signal (SIGINT, delete_socket);
111 signal (SIGQUIT, delete_socket);
112 signal (SIGILL, delete_socket);
113 signal (SIGTRAP, delete_socket);
114 #ifdef SIGABRT
115 signal (SIGABRT, delete_socket);
116 #endif
117 #ifdef SIGHWE
118 signal (SIGHWE, delete_socket);
119 #endif
120 #ifdef SIGPRE
121 signal (SIGPRE, delete_socket);
122 #endif
123 #ifdef SIGORE
124 signal (SIGORE, delete_socket);
125 #endif
126 #ifdef SIGUME
127 signal (SIGUME, delete_socket);
128 #endif
129 #ifdef SIGDLK
130 signal (SIGDLK, delete_socket);
131 #endif
132 #ifdef SIGCPULIM
133 signal (SIGCPULIM, delete_socket);
134 #endif
135 #ifdef SIGIOT
136 /* This is missing on some systems - OS/2, for example. */
137 signal (SIGIOT, delete_socket);
138 #endif
139 #ifdef SIGEMT
140 signal (SIGEMT, delete_socket);
141 #endif
142 signal (SIGFPE, delete_socket);
143 #ifdef SIGBUS
144 signal (SIGBUS, delete_socket);
145 #endif
146 signal (SIGSEGV, delete_socket);
147 #ifdef SIGSYS
148 signal (SIGSYS, delete_socket);
149 #endif
150 signal (SIGTERM, delete_socket);
151 #ifdef SIGXCPU
152 signal (SIGXCPU, delete_socket);
153 #endif
154 #ifdef SIGXFSZ
155 signal (SIGXFSZ, delete_socket);
156 #endif /* SIGXFSZ */
158 #ifdef AIX
159 /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
160 signal (SIGXCPU, delete_socket);
161 #ifndef _I386
162 signal (SIGIOINT, delete_socket);
163 #endif
164 signal (SIGGRANT, delete_socket);
165 signal (SIGRETRACT, delete_socket);
166 signal (SIGSOUND, delete_socket);
167 signal (SIGMSG, delete_socket);
168 #endif /* AIX */
171 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
172 void
173 error (s1, s2)
174 char *s1, *s2;
176 fprintf (stderr, "%s: ", progname);
177 fprintf (stderr, s1, s2);
178 fprintf (stderr, "\n");
181 /* Print error message and exit. */
182 void
183 fatal (s1, s2)
184 char *s1, *s2;
186 error (s1, s2);
187 exit (1);
190 /* Like malloc but get fatal error if memory is exhausted. */
192 long *
193 xmalloc (size)
194 unsigned int size;
196 long *result = (long *) malloc (size);
197 if (result == NULL)
198 fatal ("virtual memory exhausted", 0);
199 return result;
203 main (argc, argv)
204 int argc;
205 char **argv;
207 char system_name[32];
208 int s, infd;
209 size_t fromlen;
210 struct sockaddr_un server, fromunix;
211 char *homedir;
212 char *str, string[BUFSIZ], code[BUFSIZ];
213 FILE *infile;
214 FILE **openfiles;
215 int openfiles_size;
216 struct stat statbuf;
218 #ifndef convex
219 char *getenv ();
220 #endif
222 progname = argv[0];
224 openfiles_size = 20;
225 openfiles = (FILE **) malloc (openfiles_size * sizeof (FILE *));
226 if (openfiles == 0)
227 abort ();
230 * Open up an AF_UNIX socket in this person's home directory
233 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
235 perror_1 ("socket");
236 exit (1);
238 server.sun_family = AF_UNIX;
239 #ifndef SERVER_HOME_DIR
240 gethostname (system_name, sizeof (system_name));
241 sprintf (server.sun_path, "/tmp/esrv%d-%s", geteuid (), system_name);
243 if (unlink (server.sun_path) == -1 && errno != ENOENT)
245 perror_1 ("unlink");
246 exit (1);
248 #else
249 if ((homedir = getenv ("HOME")) == NULL)
250 fatal_error ("No home directory\n");
252 strcpy (server.sun_path, homedir);
253 strcat (server.sun_path, "/.emacs-server-");
254 gethostname (system_name, sizeof (system_name));
255 strcat (server.sun_path, system_name);
256 /* Delete anyone else's old server. */
257 unlink (server.sun_path);
258 #endif
260 /* Save the socket name so we can delete it. */
261 socket_name = (char *) xmalloc (strlen (server.sun_path) + 1);
262 strcpy (socket_name, server.sun_path);
264 handle_signals ();
266 if (bind (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2) < 0)
268 perror_1 ("bind");
269 exit (1);
271 /* Only this user can send commands to this Emacs. */
272 if (stat (server.sun_path, &statbuf) < 0)
274 perror_1 ("bind");
275 exit (1);
278 chmod (server.sun_path, statbuf.st_mode & 0600);
280 * Now, just wait for everything to come in..
282 if (listen (s, 5) < 0)
284 perror_1 ("listen");
285 exit (1);
288 /* Disable sigpipes in case luser kills client... */
289 signal (SIGPIPE, SIG_IGN);
290 for (;;)
292 SELECT_TYPE rmask;
293 FD_ZERO (&rmask);
294 FD_SET (0, &rmask);
295 FD_SET (s, &rmask);
296 if (select (s + 1, &rmask, 0, 0, 0) < 0)
297 perror_1 ("select");
298 if (FD_ISSET (s, &rmask)) /* client sends list of filenames */
300 fromlen = sizeof (fromunix);
301 fromunix.sun_family = AF_UNIX;
302 infd = accept (s, (struct sockaddr *) &fromunix, &fromlen);
303 if (infd < 0)
305 if (errno == EMFILE || errno == ENFILE)
306 fprintf (stderr, "Error: too many clients.\n");
307 else
308 perror_1 ("accept");
309 continue;
312 if (infd >= openfiles_size)
314 openfiles_size *= 2;
315 openfiles = (FILE **) realloc (openfiles,
316 openfiles_size * sizeof (FILE *));
317 if (openfiles == 0)
318 abort ();
321 infile = fdopen (infd, "r+"); /* open stream */
322 if (infile == NULL)
324 fprintf (stderr, "Error: too many clients.\n");
325 write (infd, "Too many clients.\n", 18);
326 close (infd); /* Prevent descriptor leak.. */
327 continue;
329 str = fgets (string, BUFSIZ, infile);
330 if (str == NULL)
332 perror_1 ("fgets");
333 close (infd); /* Prevent descriptor leak.. */
334 continue;
336 openfiles[infd] = infile;
337 printf ("Client: %d %s", infd, string);
338 /* If what we read did not end in a newline,
339 it means there is more. Keep reading from the socket
340 and outputting to Emacs, until we get the newline. */
341 while (string[strlen (string) - 1] != '\n')
343 if (fgets (string, BUFSIZ, infile) == 0)
344 break;
345 printf ("%s", string);
347 fflush (stdout);
348 fflush (infile);
349 continue;
351 else if (FD_ISSET (0, &rmask)) /* emacs sends codeword, fd, and string message */
353 /* Read command codeword and fd */
354 clearerr (stdin);
355 scanf ("%s %d%*c", code, &infd);
356 if (ferror (stdin) || feof (stdin))
357 fatal_error ("server: error reading from standard input\n");
359 /* Transfer text from Emacs to the client, up to a newline. */
360 infile = openfiles[infd];
361 rewind (infile);
362 while (1)
364 if (fgets (string, BUFSIZ, stdin) == 0)
365 break;
366 fprintf (infile, "%s", string);
367 if (string[strlen (string) - 1] == '\n')
368 break;
370 fflush (infile);
372 /* If command is close, close connection to client. */
373 if (strncmp (code, "Close:", 6) == 0)
374 if (infd > 2)
376 fclose (infile);
377 close (infd);
379 continue;
384 #else /* This is the SYSV IPC section */
386 #include <sys/types.h>
387 #include <sys/ipc.h>
388 #include <sys/msg.h>
389 #include <setjmp.h>
390 #include <errno.h>
391 #include <sys/utsname.h>
393 struct utsname system_name;
395 #ifndef errno
396 extern int errno;
397 #endif
399 jmp_buf msgenv;
401 SIGTYPE
402 msgcatch ()
404 longjmp (msgenv, 1);
408 /* "THIS has to be fixed. Remember, stderr may not exist...-rlk."
409 Incorrect. This program runs as an inferior of Emacs.
410 Its stderr always exists--rms. */
411 #include <stdio.h>
413 main ()
415 int s, infd, fromlen, ioproc;
416 key_t key;
417 struct msgbuf * msgp =
418 (struct msgbuf *) malloc (sizeof *msgp + BUFSIZ);
419 struct msqid_ds msg_st;
420 int p;
421 char *homedir, *getenv ();
422 char string[BUFSIZ];
423 FILE *infile;
426 * Create a message queue using ~/.emacs-server as the path for ftok
428 if ((homedir = getenv ("HOME")) == NULL)
429 fatal_error ("No home directory\n");
431 strcpy (string, homedir);
432 #ifndef HAVE_LONG_FILE_NAMES
433 /* If file names are short, we can't fit the host name. */
434 strcat (string, "/.emacs-server");
435 #else
436 strcat (string, "/.emacs-server-");
437 uname (&system_name);
438 strcat (string, system_name.nodename);
439 #endif
440 creat (string, 0600);
441 key = ftok (string, 1); /* unlikely to be anyone else using it */
442 s = msgget (key, 0600 | IPC_CREAT);
443 if (s == -1)
445 perror_1 ("msgget");
446 exit (1);
449 /* Fork so we can close connection even if parent dies */
450 p = fork ();
451 if (setjmp (msgenv))
453 msgctl (s, IPC_RMID, 0);
454 if (p > 0)
455 kill (p, SIGKILL);
456 exit (0);
458 signal (SIGTERM, msgcatch);
459 signal (SIGINT, msgcatch);
460 signal (SIGHUP, msgcatch);
461 if (p > 0)
463 /* This is executed in the original process that did the fork above. */
464 /* Get pid of Emacs itself. */
465 p = getppid ();
466 setpgrp (); /* Gnu kills process group on exit */
467 while (1)
469 /* Is Emacs still alive? */
470 if (kill (p, 0) < 0)
472 msgctl (s, IPC_RMID, 0);
473 exit (0);
475 sleep (10);
479 /* This is executed in the child made by forking above.
480 Call it c1. Make another process, ioproc. */
482 ioproc = fork ();
483 if (ioproc == 0)
485 /* In process ioproc, wait for text from Emacs,
486 and send it to the process c1.
487 This way, c1 only has to wait for one source of input. */
488 while (fgets (msgp->mtext, BUFSIZ, stdin))
490 msgp->mtype = 1;
491 msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0);
493 exit (1);
496 /* In the process c1,
497 listen for messages from clients and pass them to Emacs. */
498 while (1)
500 if ((fromlen = msgrcv (s, msgp, BUFSIZ - 1, 1, 0)) < 0)
502 #ifdef EINTR
503 if (errno == EINTR)
504 continue;
505 #endif
506 perror_1 ("msgrcv");
507 exit (1);
509 else
511 msgctl (s, IPC_STAT, &msg_st);
513 /* Distinguish whether the message came from a client, or from
514 ioproc. */
515 if (msg_st.msg_lspid == ioproc)
517 char code[BUFSIZ];
518 int inproc;
520 /* Message from ioproc: tell a client we are done. */
521 msgp->mtext[strlen (msgp->mtext)-1] = 0;
522 sscanf (msgp->mtext, "%s %d", code, &inproc);
523 msgp->mtype = inproc;
524 msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0);
525 continue;
528 /* This is a request from a client: copy to stdout
529 so that Emacs will get it. Include msg_lspid
530 so server.el can tell us where to send the reply. */
531 strncpy (string, msgp->mtext, fromlen);
532 string[fromlen] = 0; /* make sure */
533 /* Newline is part of string.. */
534 printf ("Client: %d %s", msg_st.msg_lspid, string);
535 fflush (stdout);
540 #endif /* HAVE_SYSVIPC */
542 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */
544 /* This is like perror but puts `Error: ' at the beginning. */
546 perror_1 (string)
547 char *string;
549 char *copy = (char *) malloc (strlen (string) + 8);
550 if (copy == 0)
551 fatal_error ("Virtual memory exhausted");
553 strcpy (copy, "Error: ");
554 strcat (copy, string);
555 perror (copy);
558 fatal_error (string)
559 char *string;
561 fprintf (stderr, "%s", "Error: ");
562 fprintf (stderr, string);
563 exit (1);