1 /* Communication subprocess for GNU Emacs acting as server.
2 Copyright (C) 1986, 1987, 1992, 1994, 1999 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)
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. */
37 #if !defined (HAVE_SOCKETS) && !defined (HAVE_SYSVIPC)
43 fprintf (stderr
, "Sorry, the Emacs server is supported only on systems\n");
44 fprintf (stderr
, "with Berkeley sockets or System V IPC.\n");
48 #else /* HAVE_SOCKETS or HAVE_SYSVIPC */
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>
58 #include <sys/socket.h>
72 /* Copied from src/process.c */
74 /* We could get this from param.h, but better not to depend on finding that.
75 And better not to risk that it might define other symbols used in this
78 #define MAXDESC FD_SETSIZE
82 #define SELECT_TYPE fd_set
85 #define SELECT_TYPE int
87 /* Define the macros to access a single-int bitmap of descriptors. */
88 #define FD_SET(n, p) (*(p) |= (1 << (n)))
89 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
90 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
91 #define FD_ZERO(p) (*(p) = 0)
92 #endif /* no FD_SET */
94 /* This is the file name of the socket that we made. */
98 /* Name of this program. */
102 /* Handle fatal signals. */
104 /* This is the handler. */
110 signal (sig
, SIG_DFL
);
111 unlink (socket_name
);
112 kill (getpid (), sig
);
115 /* Set up to handle all the signals. */
120 signal (SIGHUP
, delete_socket
);
121 signal (SIGINT
, delete_socket
);
122 signal (SIGQUIT
, delete_socket
);
123 signal (SIGILL
, delete_socket
);
124 signal (SIGTRAP
, delete_socket
);
126 signal (SIGABRT
, delete_socket
);
129 signal (SIGHWE
, delete_socket
);
132 signal (SIGPRE
, delete_socket
);
135 signal (SIGORE
, delete_socket
);
138 signal (SIGUME
, delete_socket
);
141 signal (SIGDLK
, delete_socket
);
144 signal (SIGCPULIM
, delete_socket
);
147 /* This is missing on some systems - OS/2, for example. */
148 signal (SIGIOT
, delete_socket
);
151 signal (SIGEMT
, delete_socket
);
153 signal (SIGFPE
, delete_socket
);
155 signal (SIGBUS
, delete_socket
);
157 signal (SIGSEGV
, delete_socket
);
159 signal (SIGSYS
, delete_socket
);
161 signal (SIGTERM
, delete_socket
);
163 signal (SIGXCPU
, delete_socket
);
166 signal (SIGXFSZ
, delete_socket
);
170 /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
171 signal (SIGXCPU
, delete_socket
);
173 signal (SIGIOINT
, delete_socket
);
175 signal (SIGGRANT
, delete_socket
);
176 signal (SIGRETRACT
, delete_socket
);
177 signal (SIGSOUND
, delete_socket
);
178 signal (SIGMSG
, delete_socket
);
182 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
187 fprintf (stderr
, "%s: ", progname
);
188 fprintf (stderr
, s1
, s2
);
189 fprintf (stderr
, "\n");
192 /* Print error message and exit. */
201 /* Like malloc but get fatal error if memory is exhausted. */
207 long *result
= (long *) malloc (size
);
209 fatal ("virtual memory exhausted", 0);
219 int system_name_length
;
222 SOCKLEN_TYPE fromlen
;
226 struct sockaddr_un server
, fromunix
;
227 #ifdef SERVER_HOME_DIR
230 char *str
, string
[BUFSIZ
], code
[BUFSIZ
];
243 openfiles
= (FILE **) malloc (openfiles_size
* sizeof (FILE *));
248 * Open up an AF_UNIX socket in this person's home directory
251 if ((s
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0)
256 server
.sun_family
= AF_UNIX
;
258 system_name_length
= 32;
261 system_name
= (char *) xmalloc (system_name_length
+ 1);
263 /* system_name must be null-terminated string. */
264 system_name
[system_name_length
] = '\0';
266 if (gethostname (system_name
, system_name_length
) == 0)
270 system_name_length
*= 2;
273 #ifndef SERVER_HOME_DIR
274 sprintf (server
.sun_path
, "/tmp/esrv%d-%s", (int) geteuid (), system_name
);
276 if (unlink (server
.sun_path
) == -1 && errno
!= ENOENT
)
282 if ((homedir
= getenv ("HOME")) == NULL
)
283 fatal_error ("No home directory\n");
285 strcpy (server
.sun_path
, homedir
);
286 strcat (server
.sun_path
, "/.emacs-server-");
287 strcat (server
.sun_path
, system_name
);
288 /* Delete anyone else's old server. */
289 unlink (server
.sun_path
);
292 /* Save the socket name so we can delete it. */
293 socket_name
= (char *) xmalloc (strlen (server
.sun_path
) + 1);
294 strcpy (socket_name
, server
.sun_path
);
298 if (bind (s
, (struct sockaddr
*) &server
, strlen (server
.sun_path
) + 2) < 0)
303 /* Only this user can send commands to this Emacs. */
304 if (stat (server
.sun_path
, &statbuf
) < 0)
310 chmod (server
.sun_path
, statbuf
.st_mode
& 0600);
312 * Now, just wait for everything to come in..
314 if (listen (s
, 5) < 0)
320 /* Disable sigpipes in case luser kills client... */
321 signal (SIGPIPE
, SIG_IGN
);
328 if (select (s
+ 1, &rmask
, 0, 0, 0) < 0)
330 if (FD_ISSET (s
, &rmask
)) /* client sends list of filenames */
332 fromlen
= sizeof (fromunix
);
333 fromunix
.sun_family
= AF_UNIX
;
334 infd
= accept (s
, (struct sockaddr
*) &fromunix
, &fromlen
);
337 if (errno
== EMFILE
|| errno
== ENFILE
)
338 fprintf (stderr
, "Error: too many clients.\n");
344 if (infd
>= openfiles_size
)
347 openfiles
= (FILE **) realloc (openfiles
,
348 openfiles_size
* sizeof (FILE *));
353 infile
= fdopen (infd
, "r+"); /* open stream */
356 fprintf (stderr
, "Error: too many clients.\n");
357 write (infd
, "Too many clients.\n", 18);
358 close (infd
); /* Prevent descriptor leak.. */
361 str
= fgets (string
, BUFSIZ
, infile
);
365 close (infd
); /* Prevent descriptor leak.. */
368 openfiles
[infd
] = infile
;
369 printf ("Client: %d %s", infd
, string
);
370 /* If what we read did not end in a newline,
371 it means there is more. Keep reading from the socket
372 and outputting to Emacs, until we get the newline. */
373 while (string
[strlen (string
) - 1] != '\n')
375 if (fgets (string
, BUFSIZ
, infile
) == 0)
377 printf ("%s", string
);
383 else if (FD_ISSET (0, &rmask
)) /* emacs sends codeword, fd, and string message */
385 /* Read command codeword and fd */
387 scanf ("%s %d%*c", code
, &infd
);
388 if (ferror (stdin
) || feof (stdin
))
389 fatal_error ("server: error reading from standard input\n");
391 /* Transfer text from Emacs to the client, up to a newline. */
392 infile
= openfiles
[infd
];
396 if (fgets (string
, BUFSIZ
, stdin
) == 0)
398 fprintf (infile
, "%s", string
);
399 if (string
[strlen (string
) - 1] == '\n')
404 /* If command is close, close connection to client. */
405 if (strncmp (code
, "Close:", 6) == 0)
416 #else /* This is the SYSV IPC section */
418 #include <sys/types.h>
423 #include <sys/utsname.h>
425 struct utsname system_name
;
440 /* "THIS has to be fixed. Remember, stderr may not exist...-rlk."
441 Incorrect. This program runs as an inferior of Emacs.
442 Its stderr always exists--rms. */
448 int s
, infd
, fromlen
, ioproc
;
450 struct msgbuf
* msgp
=
451 (struct msgbuf
*) malloc (sizeof *msgp
+ BUFSIZ
);
452 struct msqid_ds msg_st
;
454 char *homedir
, *getenv ();
459 * Create a message queue using ~/.emacs-server as the path for ftok
461 if ((homedir
= getenv ("HOME")) == NULL
)
462 fatal_error ("No home directory\n");
464 strcpy (string
, homedir
);
465 #ifndef HAVE_LONG_FILE_NAMES
466 /* If file names are short, we can't fit the host name. */
467 strcat (string
, "/.emacs-server");
469 strcat (string
, "/.emacs-server-");
470 uname (&system_name
);
471 strcat (string
, system_name
.nodename
);
473 creat (string
, 0600);
474 key
= ftok (string
, 1); /* unlikely to be anyone else using it */
475 s
= msgget (key
, 0600 | IPC_CREAT
);
482 /* Fork so we can close connection even if parent dies */
486 msgctl (s
, IPC_RMID
, 0);
491 signal (SIGTERM
, msgcatch
);
492 signal (SIGINT
, msgcatch
);
493 signal (SIGHUP
, msgcatch
);
496 /* This is executed in the original process that did the fork above. */
497 /* Get pid of Emacs itself. */
499 setpgrp (); /* Gnu kills process group on exit */
502 /* Is Emacs still alive? */
505 msgctl (s
, IPC_RMID
, 0);
512 /* This is executed in the child made by forking above.
513 Call it c1. Make another process, ioproc. */
518 /* In process ioproc, wait for text from Emacs,
519 and send it to the process c1.
520 This way, c1 only has to wait for one source of input. */
521 while (fgets (msgp
->mtext
, BUFSIZ
, stdin
))
524 msgsnd (s
, msgp
, strlen (msgp
->mtext
) + 1, 0);
529 /* In the process c1,
530 listen for messages from clients and pass them to Emacs. */
533 if ((fromlen
= msgrcv (s
, msgp
, BUFSIZ
- 1, 1, 0)) < 0)
544 msgctl (s
, IPC_STAT
, &msg_st
);
546 /* Distinguish whether the message came from a client, or from
548 if (msg_st
.msg_lspid
== ioproc
)
553 /* Message from ioproc: tell a client we are done. */
554 msgp
->mtext
[strlen (msgp
->mtext
)-1] = 0;
555 sscanf (msgp
->mtext
, "%s %d", code
, &inproc
);
556 msgp
->mtype
= inproc
;
557 msgsnd (s
, msgp
, strlen (msgp
->mtext
) + 1, 0);
561 /* This is a request from a client: copy to stdout
562 so that Emacs will get it. Include msg_lspid
563 so server.el can tell us where to send the reply. */
564 strncpy (string
, msgp
->mtext
, fromlen
);
565 string
[fromlen
] = 0; /* make sure */
566 /* Newline is part of string.. */
567 printf ("Client: %d %s", msg_st
.msg_lspid
, string
);
573 #endif /* HAVE_SYSVIPC */
576 /* This is like perror but puts `Error: ' at the beginning. */
582 char *copy
= (char *) malloc (strlen (string
) + 8);
584 fatal_error ("Virtual memory exhausted");
586 strcpy (copy
, "Error: ");
587 strcat (copy
, string
);
595 fprintf (stderr
, "%s", "Error: ");
596 fprintf (stderr
, string
);
599 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */