(widget-specify-secret): New function.
[bpt/emacs.git] / lib-src / emacsserver.c
CommitLineData
d953f306 1/* Communication subprocess for GNU Emacs acting as server.
86af296b 2 Copyright (C) 1986, 1987, 1992, 1994 Free Software Foundation, Inc.
d953f306
RS
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
d4327fec 8the Free Software Foundation; either version 2, or (at your option)
d953f306
RS
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
d953f306
RS
20
21
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. */
27
28#define NO_SHORTNAMES
3bd8b59d 29#include <signal.h>
18160b98 30#include <../src/config.h>
d953f306
RS
31#undef read
32#undef write
33#undef open
34#undef close
5ed8e09c 35#undef signal
d953f306 36
021eac48 37#if !defined (HAVE_SOCKETS) && !defined (HAVE_SYSVIPC)
d953f306
RS
38#include <stdio.h>
39
4e7ef1d6 40int
d953f306
RS
41main ()
42{
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);
46}
47
48#else /* HAVE_SOCKETS or HAVE_SYSVIPC */
49
4e7ef1d6
AS
50void perror_1 ();
51void fatal_error ();
52
1e523e3d 53#if defined (HAVE_SOCKETS) && ! defined (NO_SOCKETS_IN_FILE_SYSTEM)
d953f306
RS
54/* BSD code is very different from SYSV IPC code */
55
d953f306 56#include <sys/types.h>
908d6b87 57#include <sys/file.h>
d953f306 58#include <sys/socket.h>
d953f306
RS
59#include <sys/un.h>
60#include <stdio.h>
61#include <errno.h>
cbf5f47c 62#include <sys/stat.h>
d953f306 63
4e7ef1d6
AS
64#ifdef HAVE_UNISTD_H
65#include <unistd.h>
66#endif
67
d953f306
RS
68extern int errno;
69
b38bab94
KH
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
84
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 */
91
d8888efa
RS
92/* This is the file name of the socket that we made. */
93
94char *socket_name;
95
96/* Name of this program. */
97
98char *progname;
99\f
100/* Handle fatal signals. */
101
102/* This is the handler. */
103
104SIGTYPE
105delete_socket (sig)
106 int sig;
107{
108 signal (sig, SIG_DFL);
109 unlink (socket_name);
110 kill (getpid (), sig);
111}
112
113/* Set up to handle all the signals. */
114
4e7ef1d6 115void
d8888efa
RS
116handle_signals ()
117{
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 */
166
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 */
178}
179\f
180/* Print error message. `s1' is printf control string, `s2' is arg for it. */
181void
182error (s1, s2)
183 char *s1, *s2;
184{
185 fprintf (stderr, "%s: ", progname);
186 fprintf (stderr, s1, s2);
187 fprintf (stderr, "\n");
188}
189
190/* Print error message and exit. */
191void
192fatal (s1, s2)
193 char *s1, *s2;
194{
195 error (s1, s2);
196 exit (1);
197}
198
199/* Like malloc but get fatal error if memory is exhausted. */
200
201long *
202xmalloc (size)
203 unsigned int size;
204{
205 long *result = (long *) malloc (size);
206 if (result == NULL)
207 fatal ("virtual memory exhausted", 0);
208 return result;
209}
210\f
c275f129 211int
d8888efa
RS
212main (argc, argv)
213 int argc;
214 char **argv;
d953f306 215{
1464f5d5 216 char system_name[32];
4893a437 217 int s, infd;
57b1f720
RS
218#ifdef SOCKLEN_TYPE
219 SOCKLEN_TYPE fromlen;
220#else
4893a437 221 size_t fromlen;
57b1f720 222#endif
4893a437 223 struct sockaddr_un server, fromunix;
d953f306
RS
224 char *homedir;
225 char *str, string[BUFSIZ], code[BUFSIZ];
226 FILE *infile;
227 FILE **openfiles;
228 int openfiles_size;
cbf5f47c 229 struct stat statbuf;
d953f306 230
f662f9b0 231#ifndef convex
d953f306 232 char *getenv ();
f662f9b0 233#endif
d953f306 234
d8888efa
RS
235 progname = argv[0];
236
d953f306
RS
237 openfiles_size = 20;
238 openfiles = (FILE **) malloc (openfiles_size * sizeof (FILE *));
239 if (openfiles == 0)
240 abort ();
241
242 /*
243 * Open up an AF_UNIX socket in this person's home directory
244 */
245
246 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
247 {
ccd318fb 248 perror_1 ("socket");
d953f306
RS
249 exit (1);
250 }
251 server.sun_family = AF_UNIX;
1464f5d5
JB
252#ifndef SERVER_HOME_DIR
253 gethostname (system_name, sizeof (system_name));
254 sprintf (server.sun_path, "/tmp/esrv%d-%s", geteuid (), system_name);
255
256 if (unlink (server.sun_path) == -1 && errno != ENOENT)
257 {
ccd318fb 258 perror_1 ("unlink");
1464f5d5
JB
259 exit (1);
260 }
261#else
262 if ((homedir = getenv ("HOME")) == NULL)
ccd318fb
RS
263 fatal_error ("No home directory\n");
264
d953f306 265 strcpy (server.sun_path, homedir);
021eac48
RS
266 strcat (server.sun_path, "/.emacs-server-");
267 gethostname (system_name, sizeof (system_name));
268 strcat (server.sun_path, system_name);
d953f306
RS
269 /* Delete anyone else's old server. */
270 unlink (server.sun_path);
1464f5d5
JB
271#endif
272
d8888efa
RS
273 /* Save the socket name so we can delete it. */
274 socket_name = (char *) xmalloc (strlen (server.sun_path) + 1);
275 strcpy (socket_name, server.sun_path);
276
277 handle_signals ();
278
2fdfe079 279 if (bind (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2) < 0)
d953f306 280 {
ccd318fb 281 perror_1 ("bind");
d953f306
RS
282 exit (1);
283 }
284 /* Only this user can send commands to this Emacs. */
cbf5f47c
KH
285 if (stat (server.sun_path, &statbuf) < 0)
286 {
287 perror_1 ("bind");
288 exit (1);
289 }
290
291 chmod (server.sun_path, statbuf.st_mode & 0600);
d953f306
RS
292 /*
293 * Now, just wait for everything to come in..
294 */
295 if (listen (s, 5) < 0)
296 {
ccd318fb 297 perror_1 ("listen");
d953f306
RS
298 exit (1);
299 }
300
301 /* Disable sigpipes in case luser kills client... */
302 signal (SIGPIPE, SIG_IGN);
303 for (;;)
304 {
b38bab94 305 SELECT_TYPE rmask;
86af296b
RM
306 FD_ZERO (&rmask);
307 FD_SET (0, &rmask);
308 FD_SET (s, &rmask);
b38bab94 309 if (select (s + 1, &rmask, 0, 0, 0) < 0)
ccd318fb 310 perror_1 ("select");
86af296b 311 if (FD_ISSET (s, &rmask)) /* client sends list of filenames */
d953f306
RS
312 {
313 fromlen = sizeof (fromunix);
314 fromunix.sun_family = AF_UNIX;
b38bab94 315 infd = accept (s, (struct sockaddr *) &fromunix, &fromlen);
d953f306
RS
316 if (infd < 0)
317 {
318 if (errno == EMFILE || errno == ENFILE)
ccd318fb 319 fprintf (stderr, "Error: too many clients.\n");
d953f306 320 else
ccd318fb 321 perror_1 ("accept");
d953f306
RS
322 continue;
323 }
324
325 if (infd >= openfiles_size)
326 {
327 openfiles_size *= 2;
328 openfiles = (FILE **) realloc (openfiles,
329 openfiles_size * sizeof (FILE *));
330 if (openfiles == 0)
331 abort ();
332 }
333
334 infile = fdopen (infd, "r+"); /* open stream */
335 if (infile == NULL)
336 {
ccd318fb 337 fprintf (stderr, "Error: too many clients.\n");
d953f306
RS
338 write (infd, "Too many clients.\n", 18);
339 close (infd); /* Prevent descriptor leak.. */
340 continue;
341 }
342 str = fgets (string, BUFSIZ, infile);
343 if (str == NULL)
344 {
ccd318fb 345 perror_1 ("fgets");
d953f306
RS
346 close (infd); /* Prevent descriptor leak.. */
347 continue;
348 }
349 openfiles[infd] = infile;
350 printf ("Client: %d %s", infd, string);
351 /* If what we read did not end in a newline,
352 it means there is more. Keep reading from the socket
353 and outputting to Emacs, until we get the newline. */
354 while (string[strlen (string) - 1] != '\n')
355 {
356 if (fgets (string, BUFSIZ, infile) == 0)
357 break;
358 printf ("%s", string);
359 }
360 fflush (stdout);
361 fflush (infile);
362 continue;
363 }
86af296b 364 else if (FD_ISSET (0, &rmask)) /* emacs sends codeword, fd, and string message */
d953f306
RS
365 {
366 /* Read command codeword and fd */
367 clearerr (stdin);
368 scanf ("%s %d%*c", code, &infd);
369 if (ferror (stdin) || feof (stdin))
ccd318fb 370 fatal_error ("server: error reading from standard input\n");
d953f306
RS
371
372 /* Transfer text from Emacs to the client, up to a newline. */
373 infile = openfiles[infd];
cbd1aee2 374 rewind (infile);
d953f306
RS
375 while (1)
376 {
377 if (fgets (string, BUFSIZ, stdin) == 0)
378 break;
379 fprintf (infile, "%s", string);
380 if (string[strlen (string) - 1] == '\n')
381 break;
382 }
383 fflush (infile);
384
385 /* If command is close, close connection to client. */
386 if (strncmp (code, "Close:", 6) == 0)
387 if (infd > 2)
388 {
389 fclose (infile);
390 close (infd);
391 }
392 continue;
393 }
394 }
395}
396
397#else /* This is the SYSV IPC section */
398
399#include <sys/types.h>
d953f306
RS
400#include <sys/ipc.h>
401#include <sys/msg.h>
402#include <setjmp.h>
b8a0f2d8 403#include <errno.h>
021eac48
RS
404#include <sys/utsname.h>
405
406struct utsname system_name;
b8a0f2d8
RS
407
408#ifndef errno
409extern int errno;
410#endif
d953f306
RS
411
412jmp_buf msgenv;
413
d4327fec 414SIGTYPE
d953f306
RS
415msgcatch ()
416{
417 longjmp (msgenv, 1);
418}
419
420
421/* "THIS has to be fixed. Remember, stderr may not exist...-rlk."
422 Incorrect. This program runs as an inferior of Emacs.
423 Its stderr always exists--rms. */
424#include <stdio.h>
425
4e7ef1d6 426int
d953f306
RS
427main ()
428{
2f317998 429 int s, infd, fromlen, ioproc;
d953f306
RS
430 key_t key;
431 struct msgbuf * msgp =
432 (struct msgbuf *) malloc (sizeof *msgp + BUFSIZ);
433 struct msqid_ds msg_st;
434 int p;
435 char *homedir, *getenv ();
436 char string[BUFSIZ];
437 FILE *infile;
438
439 /*
021eac48 440 * Create a message queue using ~/.emacs-server as the path for ftok
d953f306
RS
441 */
442 if ((homedir = getenv ("HOME")) == NULL)
ccd318fb
RS
443 fatal_error ("No home directory\n");
444
d953f306 445 strcpy (string, homedir);
021eac48
RS
446#ifndef HAVE_LONG_FILE_NAMES
447 /* If file names are short, we can't fit the host name. */
448 strcat (string, "/.emacs-server");
449#else
450 strcat (string, "/.emacs-server-");
451 uname (&system_name);
452 strcat (string, system_name.nodename);
453#endif
d953f306
RS
454 creat (string, 0600);
455 key = ftok (string, 1); /* unlikely to be anyone else using it */
456 s = msgget (key, 0600 | IPC_CREAT);
457 if (s == -1)
458 {
ccd318fb 459 perror_1 ("msgget");
d953f306
RS
460 exit (1);
461 }
462
463 /* Fork so we can close connection even if parent dies */
464 p = fork ();
465 if (setjmp (msgenv))
466 {
467 msgctl (s, IPC_RMID, 0);
b8a0f2d8
RS
468 if (p > 0)
469 kill (p, SIGKILL);
d953f306
RS
470 exit (0);
471 }
472 signal (SIGTERM, msgcatch);
473 signal (SIGINT, msgcatch);
b8a0f2d8 474 signal (SIGHUP, msgcatch);
9e3891de 475 if (p > 0)
d953f306 476 {
9e3891de
RS
477 /* This is executed in the original process that did the fork above. */
478 /* Get pid of Emacs itself. */
d953f306
RS
479 p = getppid ();
480 setpgrp (); /* Gnu kills process group on exit */
481 while (1)
482 {
9e3891de 483 /* Is Emacs still alive? */
d953f306
RS
484 if (kill (p, 0) < 0)
485 {
486 msgctl (s, IPC_RMID, 0);
487 exit (0);
488 }
489 sleep (10);
490 }
491 }
492
2f317998
RS
493 /* This is executed in the child made by forking above.
494 Call it c1. Make another process, ioproc. */
495
496 ioproc = fork ();
497 if (ioproc == 0)
498 {
499 /* In process ioproc, wait for text from Emacs,
500 and send it to the process c1.
501 This way, c1 only has to wait for one source of input. */
502 while (fgets (msgp->mtext, BUFSIZ, stdin))
503 {
504 msgp->mtype = 1;
505 msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0);
506 }
507 exit (1);
508 }
509
510 /* In the process c1,
511 listen for messages from clients and pass them to Emacs. */
d953f306
RS
512 while (1)
513 {
514 if ((fromlen = msgrcv (s, msgp, BUFSIZ - 1, 1, 0)) < 0)
515 {
b8a0f2d8
RS
516#ifdef EINTR
517 if (errno == EINTR)
518 continue;
519#endif
ccd318fb 520 perror_1 ("msgrcv");
1464f5d5 521 exit (1);
d953f306
RS
522 }
523 else
524 {
525 msgctl (s, IPC_STAT, &msg_st);
2f317998
RS
526
527 /* Distinguish whether the message came from a client, or from
528 ioproc. */
529 if (msg_st.msg_lspid == ioproc)
530 {
531 char code[BUFSIZ];
532 int inproc;
533
534 /* Message from ioproc: tell a client we are done. */
535 msgp->mtext[strlen (msgp->mtext)-1] = 0;
536 sscanf (msgp->mtext, "%s %d", code, &inproc);
537 msgp->mtype = inproc;
538 msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0);
539 continue;
540 }
541
542 /* This is a request from a client: copy to stdout
543 so that Emacs will get it. Include msg_lspid
544 so server.el can tell us where to send the reply. */
d953f306
RS
545 strncpy (string, msgp->mtext, fromlen);
546 string[fromlen] = 0; /* make sure */
547 /* Newline is part of string.. */
2f317998 548 printf ("Client: %d %s", msg_st.msg_lspid, string);
d953f306 549 fflush (stdout);
d953f306
RS
550 }
551 }
552}
553
554#endif /* HAVE_SYSVIPC */
555
556#endif /* HAVE_SOCKETS or HAVE_SYSVIPC */
ccd318fb
RS
557\f
558/* This is like perror but puts `Error: ' at the beginning. */
559
4e7ef1d6 560void
ccd318fb
RS
561perror_1 (string)
562 char *string;
563{
564 char *copy = (char *) malloc (strlen (string) + 8);
565 if (copy == 0)
566 fatal_error ("Virtual memory exhausted");
567
568 strcpy (copy, "Error: ");
569 strcat (copy, string);
570 perror (copy);
571}
572
4e7ef1d6 573void
ccd318fb
RS
574fatal_error (string)
575 char *string;
576{
577 fprintf (stderr, "%s", "Error: ");
578 fprintf (stderr, string);
579 exit (1);
580}