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. */
29 #include <../src/config.h>
33 #if !defined (HAVE_SOCKETS) && !defined (HAVE_SYSVIPC)
39 fprintf (stderr
, "Sorry, the Emacs server is supported only on systems\n");
40 fprintf (stderr
, "with Berkeley sockets or System V IPC.\n");
44 #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>
54 #include <sys/socket.h>
68 /* Copied from src/process.c */
70 /* We could get this from param.h, but better not to depend on finding that.
71 And better not to risk that it might define other symbols used in this
74 #define MAXDESC FD_SETSIZE
78 #define SELECT_TYPE fd_set
81 #define SELECT_TYPE int
83 /* Define the macros to access a single-int bitmap of descriptors. */
84 #define FD_SET(n, p) (*(p) |= (1 << (n)))
85 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
86 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
87 #define FD_ZERO(p) (*(p) = 0)
88 #endif /* no FD_SET */
90 /* This is the file name of the socket that we made. */
94 /* Name of this program. */
98 /* Handle fatal signals. */
100 /* This is the handler. */
106 signal (sig
, SIG_DFL
);
107 unlink (socket_name
);
108 kill (getpid (), sig
);
111 /* Set up to handle all the signals. */
116 signal (SIGHUP
, delete_socket
);
117 signal (SIGINT
, delete_socket
);
118 signal (SIGQUIT
, delete_socket
);
119 signal (SIGILL
, delete_socket
);
120 signal (SIGTRAP
, delete_socket
);
122 signal (SIGABRT
, delete_socket
);
125 signal (SIGHWE
, delete_socket
);
128 signal (SIGPRE
, delete_socket
);
131 signal (SIGORE
, delete_socket
);
134 signal (SIGUME
, delete_socket
);
137 signal (SIGDLK
, delete_socket
);
140 signal (SIGCPULIM
, delete_socket
);
143 /* This is missing on some systems - OS/2, for example. */
144 signal (SIGIOT
, delete_socket
);
147 signal (SIGEMT
, delete_socket
);
149 signal (SIGFPE
, delete_socket
);
151 signal (SIGBUS
, delete_socket
);
153 signal (SIGSEGV
, delete_socket
);
155 signal (SIGSYS
, delete_socket
);
157 signal (SIGTERM
, delete_socket
);
159 signal (SIGXCPU
, delete_socket
);
162 signal (SIGXFSZ
, delete_socket
);
166 /* 20 is SIGCHLD, 21 is SIGTTIN, 22 is SIGTTOU. */
167 signal (SIGXCPU
, delete_socket
);
169 signal (SIGIOINT
, delete_socket
);
171 signal (SIGGRANT
, delete_socket
);
172 signal (SIGRETRACT
, delete_socket
);
173 signal (SIGSOUND
, delete_socket
);
174 signal (SIGMSG
, delete_socket
);
178 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
183 fprintf (stderr
, "%s: ", progname
);
184 fprintf (stderr
, s1
, s2
);
185 fprintf (stderr
, "\n");
188 /* Print error message and exit. */
197 /* Like malloc but get fatal error if memory is exhausted. */
203 long *result
= (long *) malloc (size
);
205 fatal ("virtual memory exhausted", 0);
215 int system_name_length
;
218 SOCKLEN_TYPE fromlen
;
222 struct sockaddr_un server
, fromunix
;
223 #ifdef SERVER_HOME_DIR
226 char *str
, string
[BUFSIZ
], code
[BUFSIZ
];
239 openfiles
= (FILE **) malloc (openfiles_size
* sizeof (FILE *));
244 * Open up an AF_UNIX socket in this person's home directory
247 if ((s
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0)
252 server
.sun_family
= AF_UNIX
;
254 system_name_length
= 32;
257 system_name
= (char *) xmalloc (system_name_length
+ 1);
259 /* system_name must be null-terminated string. */
260 system_name
[system_name_length
] = '\0';
262 if (gethostname (system_name
, system_name_length
) == 0)
266 system_name_length
*= 2;
269 #ifndef SERVER_HOME_DIR
270 sprintf (server
.sun_path
, "/tmp/esrv%d-%s", geteuid (), system_name
);
272 if (unlink (server
.sun_path
) == -1 && errno
!= ENOENT
)
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 strcat (server
.sun_path
, system_name
);
284 /* Delete anyone else's old server. */
285 unlink (server
.sun_path
);
288 /* Save the socket name so we can delete it. */
289 socket_name
= (char *) xmalloc (strlen (server
.sun_path
) + 1);
290 strcpy (socket_name
, server
.sun_path
);
294 if (bind (s
, (struct sockaddr
*) &server
, strlen (server
.sun_path
) + 2) < 0)
299 /* Only this user can send commands to this Emacs. */
300 if (stat (server
.sun_path
, &statbuf
) < 0)
306 chmod (server
.sun_path
, statbuf
.st_mode
& 0600);
308 * Now, just wait for everything to come in..
310 if (listen (s
, 5) < 0)
316 /* Disable sigpipes in case luser kills client... */
317 signal (SIGPIPE
, SIG_IGN
);
324 if (select (s
+ 1, &rmask
, 0, 0, 0) < 0)
326 if (FD_ISSET (s
, &rmask
)) /* client sends list of filenames */
328 fromlen
= sizeof (fromunix
);
329 fromunix
.sun_family
= AF_UNIX
;
330 infd
= accept (s
, (struct sockaddr
*) &fromunix
, &fromlen
);
333 if (errno
== EMFILE
|| errno
== ENFILE
)
334 fprintf (stderr
, "Error: too many clients.\n");
340 if (infd
>= openfiles_size
)
343 openfiles
= (FILE **) realloc (openfiles
,
344 openfiles_size
* sizeof (FILE *));
349 infile
= fdopen (infd
, "r+"); /* open stream */
352 fprintf (stderr
, "Error: too many clients.\n");
353 write (infd
, "Too many clients.\n", 18);
354 close (infd
); /* Prevent descriptor leak.. */
357 str
= fgets (string
, BUFSIZ
, infile
);
361 close (infd
); /* Prevent descriptor leak.. */
364 openfiles
[infd
] = infile
;
365 printf ("Client: %d %s", infd
, string
);
366 /* If what we read did not end in a newline,
367 it means there is more. Keep reading from the socket
368 and outputting to Emacs, until we get the newline. */
369 while (string
[strlen (string
) - 1] != '\n')
371 if (fgets (string
, BUFSIZ
, infile
) == 0)
373 printf ("%s", string
);
379 else if (FD_ISSET (0, &rmask
)) /* emacs sends codeword, fd, and string message */
381 /* Read command codeword and fd */
383 scanf ("%s %d%*c", code
, &infd
);
384 if (ferror (stdin
) || feof (stdin
))
385 fatal_error ("server: error reading from standard input\n");
387 /* Transfer text from Emacs to the client, up to a newline. */
388 infile
= openfiles
[infd
];
392 if (fgets (string
, BUFSIZ
, stdin
) == 0)
394 fprintf (infile
, "%s", string
);
395 if (string
[strlen (string
) - 1] == '\n')
400 /* If command is close, close connection to client. */
401 if (strncmp (code
, "Close:", 6) == 0)
412 #else /* This is the SYSV IPC section */
414 #include <sys/types.h>
419 #include <sys/utsname.h>
421 struct utsname system_name
;
436 /* "THIS has to be fixed. Remember, stderr may not exist...-rlk."
437 Incorrect. This program runs as an inferior of Emacs.
438 Its stderr always exists--rms. */
444 int s
, infd
, fromlen
, ioproc
;
446 struct msgbuf
* msgp
=
447 (struct msgbuf
*) malloc (sizeof *msgp
+ BUFSIZ
);
448 struct msqid_ds msg_st
;
450 char *homedir
, *getenv ();
455 * Create a message queue using ~/.emacs-server as the path for ftok
457 if ((homedir
= getenv ("HOME")) == NULL
)
458 fatal_error ("No home directory\n");
460 strcpy (string
, homedir
);
461 #ifndef HAVE_LONG_FILE_NAMES
462 /* If file names are short, we can't fit the host name. */
463 strcat (string
, "/.emacs-server");
465 strcat (string
, "/.emacs-server-");
466 uname (&system_name
);
467 strcat (string
, system_name
.nodename
);
469 creat (string
, 0600);
470 key
= ftok (string
, 1); /* unlikely to be anyone else using it */
471 s
= msgget (key
, 0600 | IPC_CREAT
);
478 /* Fork so we can close connection even if parent dies */
482 msgctl (s
, IPC_RMID
, 0);
487 signal (SIGTERM
, msgcatch
);
488 signal (SIGINT
, msgcatch
);
489 signal (SIGHUP
, msgcatch
);
492 /* This is executed in the original process that did the fork above. */
493 /* Get pid of Emacs itself. */
495 setpgrp (); /* Gnu kills process group on exit */
498 /* Is Emacs still alive? */
501 msgctl (s
, IPC_RMID
, 0);
508 /* This is executed in the child made by forking above.
509 Call it c1. Make another process, ioproc. */
514 /* In process ioproc, wait for text from Emacs,
515 and send it to the process c1.
516 This way, c1 only has to wait for one source of input. */
517 while (fgets (msgp
->mtext
, BUFSIZ
, stdin
))
520 msgsnd (s
, msgp
, strlen (msgp
->mtext
) + 1, 0);
525 /* In the process c1,
526 listen for messages from clients and pass them to Emacs. */
529 if ((fromlen
= msgrcv (s
, msgp
, BUFSIZ
- 1, 1, 0)) < 0)
540 msgctl (s
, IPC_STAT
, &msg_st
);
542 /* Distinguish whether the message came from a client, or from
544 if (msg_st
.msg_lspid
== ioproc
)
549 /* Message from ioproc: tell a client we are done. */
550 msgp
->mtext
[strlen (msgp
->mtext
)-1] = 0;
551 sscanf (msgp
->mtext
, "%s %d", code
, &inproc
);
552 msgp
->mtype
= inproc
;
553 msgsnd (s
, msgp
, strlen (msgp
->mtext
) + 1, 0);
557 /* This is a request from a client: copy to stdout
558 so that Emacs will get it. Include msg_lspid
559 so server.el can tell us where to send the reply. */
560 strncpy (string
, msgp
->mtext
, fromlen
);
561 string
[fromlen
] = 0; /* make sure */
562 /* Newline is part of string.. */
563 printf ("Client: %d %s", msg_st
.msg_lspid
, string
);
569 #endif /* HAVE_SYSVIPC */
572 /* This is like perror but puts `Error: ' at the beginning. */
578 char *copy
= (char *) malloc (strlen (string
) + 8);
580 fatal_error ("Virtual memory exhausted");
582 strcpy (copy
, "Error: ");
583 strcat (copy
, string
);
591 fprintf (stderr
, "%s", "Error: ");
592 fprintf (stderr
, string
);
595 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */