(perror_1, fatal_error): Don't compile unless needed.
[emacs.git] / lib-src / emacsserver.c
blobfa3c3403655c066613bc4d6cd25edcfe5138c6f6
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 int
41 main ()
43 fprintf (stderr, "Sorry, the Emacs server is supported only on systems\n");
44 fprintf (stderr, "with Berkeley sockets or System V IPC.\n");
45 exit (1);
48 #else /* HAVE_SOCKETS or HAVE_SYSVIPC */
50 void perror_1 ();
51 void fatal_error ();
53 #if defined (HAVE_SOCKETS) && ! defined (NO_SOCKETS_IN_FILE_SYSTEM)
54 /* BSD code is very different from SYSV IPC code */
56 #include <sys/types.h>
57 #include <sys/file.h>
58 #include <sys/socket.h>
59 #include <sys/un.h>
60 #include <stdio.h>
61 #include <errno.h>
62 #include <sys/stat.h>
64 #ifdef HAVE_UNISTD_H
65 #include <unistd.h>
66 #endif
68 extern int errno;
70 /* Copied from src/process.c */
71 #ifdef FD_SET
72 /* We could get this from param.h, but better not to depend on finding that.
73 And better not to risk that it might define other symbols used in this
74 file. */
75 #ifdef FD_SETSIZE
76 #define MAXDESC FD_SETSIZE
77 #else
78 #define MAXDESC 64
79 #endif
80 #define SELECT_TYPE fd_set
81 #else /* no FD_SET */
82 #define MAXDESC 32
83 #define SELECT_TYPE int
85 /* Define the macros to access a single-int bitmap of descriptors. */
86 #define FD_SET(n, p) (*(p) |= (1 << (n)))
87 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
88 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
89 #define FD_ZERO(p) (*(p) = 0)
90 #endif /* no FD_SET */
92 /* This is the file name of the socket that we made. */
94 char *socket_name;
96 /* Name of this program. */
98 char *progname;
100 /* Handle fatal signals. */
102 /* This is the handler. */
104 SIGTYPE
105 delete_socket (sig)
106 int sig;
108 signal (sig, SIG_DFL);
109 unlink (socket_name);
110 kill (getpid (), sig);
113 /* Set up to handle all the signals. */
115 void
116 handle_signals ()
118 signal (SIGHUP, delete_socket);
119 signal (SIGINT, delete_socket);
120 signal (SIGQUIT, delete_socket);
121 signal (SIGILL, delete_socket);
122 signal (SIGTRAP, delete_socket);
123 #ifdef SIGABRT
124 signal (SIGABRT, delete_socket);
125 #endif
126 #ifdef SIGHWE
127 signal (SIGHWE, delete_socket);
128 #endif
129 #ifdef SIGPRE
130 signal (SIGPRE, delete_socket);
131 #endif
132 #ifdef SIGORE
133 signal (SIGORE, delete_socket);
134 #endif
135 #ifdef SIGUME
136 signal (SIGUME, delete_socket);
137 #endif
138 #ifdef SIGDLK
139 signal (SIGDLK, delete_socket);
140 #endif
141 #ifdef SIGCPULIM
142 signal (SIGCPULIM, delete_socket);
143 #endif
144 #ifdef SIGIOT
145 /* This is missing on some systems - OS/2, for example. */
146 signal (SIGIOT, delete_socket);
147 #endif
148 #ifdef SIGEMT
149 signal (SIGEMT, delete_socket);
150 #endif
151 signal (SIGFPE, delete_socket);
152 #ifdef SIGBUS
153 signal (SIGBUS, delete_socket);
154 #endif
155 signal (SIGSEGV, delete_socket);
156 #ifdef SIGSYS
157 signal (SIGSYS, delete_socket);
158 #endif
159 signal (SIGTERM, delete_socket);
160 #ifdef SIGXCPU
161 signal (SIGXCPU, delete_socket);
162 #endif
163 #ifdef SIGXFSZ
164 signal (SIGXFSZ, delete_socket);
165 #endif /* SIGXFSZ */
167 #ifdef AIX
168 /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
169 signal (SIGXCPU, delete_socket);
170 #ifndef _I386
171 signal (SIGIOINT, delete_socket);
172 #endif
173 signal (SIGGRANT, delete_socket);
174 signal (SIGRETRACT, delete_socket);
175 signal (SIGSOUND, delete_socket);
176 signal (SIGMSG, delete_socket);
177 #endif /* AIX */
180 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
181 void
182 error (s1, s2)
183 char *s1, *s2;
185 fprintf (stderr, "%s: ", progname);
186 fprintf (stderr, s1, s2);
187 fprintf (stderr, "\n");
190 /* Print error message and exit. */
191 void
192 fatal (s1, s2)
193 char *s1, *s2;
195 error (s1, s2);
196 exit (1);
199 /* Like malloc but get fatal error if memory is exhausted. */
201 long *
202 xmalloc (size)
203 unsigned int size;
205 long *result = (long *) malloc (size);
206 if (result == NULL)
207 fatal ("virtual memory exhausted", 0);
208 return result;
212 main (argc, argv)
213 int argc;
214 char **argv;
216 char *system_name;
217 int system_name_length;
218 int s, infd;
219 #ifdef SOCKLEN_TYPE
220 SOCKLEN_TYPE fromlen;
221 #else
222 size_t fromlen;
223 #endif
224 struct sockaddr_un server, fromunix;
225 char *homedir;
226 char *str, string[BUFSIZ], code[BUFSIZ];
227 FILE *infile;
228 FILE **openfiles;
229 int openfiles_size;
230 struct stat statbuf;
232 #ifndef convex
233 char *getenv ();
234 #endif
236 progname = argv[0];
238 openfiles_size = 20;
239 openfiles = (FILE **) malloc (openfiles_size * sizeof (FILE *));
240 if (openfiles == 0)
241 abort ();
244 * Open up an AF_UNIX socket in this person's home directory
247 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
249 perror_1 ("socket");
250 exit (1);
252 server.sun_family = AF_UNIX;
253 #ifndef SERVER_HOME_DIR
254 system_name_length = 32;
256 while (1)
258 system_name = (char *) xmalloc (system_name_length + 1);
260 /* system_name must be null-terminated string. */
261 system_name[system_name_length] = '\0';
263 if (gethostname (system_name, system_name_length) == 0)
264 break;
266 free (system_name);
267 system_name_length *= 2;
270 sprintf (server.sun_path, "/tmp/esrv%d-%s", geteuid (), system_name);
272 if (unlink (server.sun_path) == -1 && errno != ENOENT)
274 perror_1 ("unlink");
275 exit (1);
277 #else
278 if ((homedir = getenv ("HOME")) == NULL)
279 fatal_error ("No home directory\n");
281 strcpy (server.sun_path, homedir);
282 strcat (server.sun_path, "/.emacs-server-");
283 gethostname (system_name, sizeof (system_name));
284 strcat (server.sun_path, system_name);
285 /* Delete anyone else's old server. */
286 unlink (server.sun_path);
287 #endif
289 /* Save the socket name so we can delete it. */
290 socket_name = (char *) xmalloc (strlen (server.sun_path) + 1);
291 strcpy (socket_name, server.sun_path);
293 handle_signals ();
295 if (bind (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2) < 0)
297 perror_1 ("bind");
298 exit (1);
300 /* Only this user can send commands to this Emacs. */
301 if (stat (server.sun_path, &statbuf) < 0)
303 perror_1 ("bind");
304 exit (1);
307 chmod (server.sun_path, statbuf.st_mode & 0600);
309 * Now, just wait for everything to come in..
311 if (listen (s, 5) < 0)
313 perror_1 ("listen");
314 exit (1);
317 /* Disable sigpipes in case luser kills client... */
318 signal (SIGPIPE, SIG_IGN);
319 for (;;)
321 SELECT_TYPE rmask;
322 FD_ZERO (&rmask);
323 FD_SET (0, &rmask);
324 FD_SET (s, &rmask);
325 if (select (s + 1, &rmask, 0, 0, 0) < 0)
326 perror_1 ("select");
327 if (FD_ISSET (s, &rmask)) /* client sends list of filenames */
329 fromlen = sizeof (fromunix);
330 fromunix.sun_family = AF_UNIX;
331 infd = accept (s, (struct sockaddr *) &fromunix, &fromlen);
332 if (infd < 0)
334 if (errno == EMFILE || errno == ENFILE)
335 fprintf (stderr, "Error: too many clients.\n");
336 else
337 perror_1 ("accept");
338 continue;
341 if (infd >= openfiles_size)
343 openfiles_size *= 2;
344 openfiles = (FILE **) realloc (openfiles,
345 openfiles_size * sizeof (FILE *));
346 if (openfiles == 0)
347 abort ();
350 infile = fdopen (infd, "r+"); /* open stream */
351 if (infile == NULL)
353 fprintf (stderr, "Error: too many clients.\n");
354 write (infd, "Too many clients.\n", 18);
355 close (infd); /* Prevent descriptor leak.. */
356 continue;
358 str = fgets (string, BUFSIZ, infile);
359 if (str == NULL)
361 perror_1 ("fgets");
362 close (infd); /* Prevent descriptor leak.. */
363 continue;
365 openfiles[infd] = infile;
366 printf ("Client: %d %s", infd, string);
367 /* If what we read did not end in a newline,
368 it means there is more. Keep reading from the socket
369 and outputting to Emacs, until we get the newline. */
370 while (string[strlen (string) - 1] != '\n')
372 if (fgets (string, BUFSIZ, infile) == 0)
373 break;
374 printf ("%s", string);
376 fflush (stdout);
377 fflush (infile);
378 continue;
380 else if (FD_ISSET (0, &rmask)) /* emacs sends codeword, fd, and string message */
382 /* Read command codeword and fd */
383 clearerr (stdin);
384 scanf ("%s %d%*c", code, &infd);
385 if (ferror (stdin) || feof (stdin))
386 fatal_error ("server: error reading from standard input\n");
388 /* Transfer text from Emacs to the client, up to a newline. */
389 infile = openfiles[infd];
390 rewind (infile);
391 while (1)
393 if (fgets (string, BUFSIZ, stdin) == 0)
394 break;
395 fprintf (infile, "%s", string);
396 if (string[strlen (string) - 1] == '\n')
397 break;
399 fflush (infile);
401 /* If command is close, close connection to client. */
402 if (strncmp (code, "Close:", 6) == 0)
403 if (infd > 2)
405 fclose (infile);
406 close (infd);
408 continue;
413 #else /* This is the SYSV IPC section */
415 #include <sys/types.h>
416 #include <sys/ipc.h>
417 #include <sys/msg.h>
418 #include <setjmp.h>
419 #include <errno.h>
420 #include <sys/utsname.h>
422 struct utsname system_name;
424 #ifndef errno
425 extern int errno;
426 #endif
428 jmp_buf msgenv;
430 SIGTYPE
431 msgcatch ()
433 longjmp (msgenv, 1);
437 /* "THIS has to be fixed. Remember, stderr may not exist...-rlk."
438 Incorrect. This program runs as an inferior of Emacs.
439 Its stderr always exists--rms. */
440 #include <stdio.h>
443 main ()
445 int s, infd, fromlen, ioproc;
446 key_t key;
447 struct msgbuf * msgp =
448 (struct msgbuf *) malloc (sizeof *msgp + BUFSIZ);
449 struct msqid_ds msg_st;
450 int p;
451 char *homedir, *getenv ();
452 char string[BUFSIZ];
453 FILE *infile;
456 * Create a message queue using ~/.emacs-server as the path for ftok
458 if ((homedir = getenv ("HOME")) == NULL)
459 fatal_error ("No home directory\n");
461 strcpy (string, homedir);
462 #ifndef HAVE_LONG_FILE_NAMES
463 /* If file names are short, we can't fit the host name. */
464 strcat (string, "/.emacs-server");
465 #else
466 strcat (string, "/.emacs-server-");
467 uname (&system_name);
468 strcat (string, system_name.nodename);
469 #endif
470 creat (string, 0600);
471 key = ftok (string, 1); /* unlikely to be anyone else using it */
472 s = msgget (key, 0600 | IPC_CREAT);
473 if (s == -1)
475 perror_1 ("msgget");
476 exit (1);
479 /* Fork so we can close connection even if parent dies */
480 p = fork ();
481 if (setjmp (msgenv))
483 msgctl (s, IPC_RMID, 0);
484 if (p > 0)
485 kill (p, SIGKILL);
486 exit (0);
488 signal (SIGTERM, msgcatch);
489 signal (SIGINT, msgcatch);
490 signal (SIGHUP, msgcatch);
491 if (p > 0)
493 /* This is executed in the original process that did the fork above. */
494 /* Get pid of Emacs itself. */
495 p = getppid ();
496 setpgrp (); /* Gnu kills process group on exit */
497 while (1)
499 /* Is Emacs still alive? */
500 if (kill (p, 0) < 0)
502 msgctl (s, IPC_RMID, 0);
503 exit (0);
505 sleep (10);
509 /* This is executed in the child made by forking above.
510 Call it c1. Make another process, ioproc. */
512 ioproc = fork ();
513 if (ioproc == 0)
515 /* In process ioproc, wait for text from Emacs,
516 and send it to the process c1.
517 This way, c1 only has to wait for one source of input. */
518 while (fgets (msgp->mtext, BUFSIZ, stdin))
520 msgp->mtype = 1;
521 msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0);
523 exit (1);
526 /* In the process c1,
527 listen for messages from clients and pass them to Emacs. */
528 while (1)
530 if ((fromlen = msgrcv (s, msgp, BUFSIZ - 1, 1, 0)) < 0)
532 #ifdef EINTR
533 if (errno == EINTR)
534 continue;
535 #endif
536 perror_1 ("msgrcv");
537 exit (1);
539 else
541 msgctl (s, IPC_STAT, &msg_st);
543 /* Distinguish whether the message came from a client, or from
544 ioproc. */
545 if (msg_st.msg_lspid == ioproc)
547 char code[BUFSIZ];
548 int inproc;
550 /* Message from ioproc: tell a client we are done. */
551 msgp->mtext[strlen (msgp->mtext)-1] = 0;
552 sscanf (msgp->mtext, "%s %d", code, &inproc);
553 msgp->mtype = inproc;
554 msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0);
555 continue;
558 /* This is a request from a client: copy to stdout
559 so that Emacs will get it. Include msg_lspid
560 so server.el can tell us where to send the reply. */
561 strncpy (string, msgp->mtext, fromlen);
562 string[fromlen] = 0; /* make sure */
563 /* Newline is part of string.. */
564 printf ("Client: %d %s", msg_st.msg_lspid, string);
565 fflush (stdout);
570 #endif /* HAVE_SYSVIPC */
573 /* This is like perror but puts `Error: ' at the beginning. */
575 void
576 perror_1 (string)
577 char *string;
579 char *copy = (char *) malloc (strlen (string) + 8);
580 if (copy == 0)
581 fatal_error ("Virtual memory exhausted");
583 strcpy (copy, "Error: ");
584 strcat (copy, string);
585 perror (copy);
588 void
589 fatal_error (string)
590 char *string;
592 fprintf (stderr, "%s", "Error: ");
593 fprintf (stderr, string);
594 exit (1);
596 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */