1 /* Communication subprocess for GNU Emacs acting as server.
2 Copyright (C) 1986, 1987, 1992 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* The GNU Emacs edit server process is run as a subprocess of Emacs
22 under control of the file lisp/server.el.
23 This program accepts communication from client (program emacsclient.c)
24 and passes their commands (consisting of keyboard characters)
25 up to the Emacs which then executes them. */
28 #include "../src/config.h"
35 #if !defined(HAVE_SOCKETS) && !defined(HAVE_SYSVIPC)
40 fprintf (stderr
, "Sorry, the Emacs server is supported only on systems\n");
41 fprintf (stderr
, "with Berkeley sockets or System V IPC.\n");
45 #else /* HAVE_SOCKETS or HAVE_SYSVIPC */
47 #if ! defined (HAVE_SYSVIPC)
48 /* BSD code is very different from SYSV IPC code */
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <sys/signal.h>
64 struct sockaddr_un server
, fromunix
;
66 char *str
, string
[BUFSIZ
], code
[BUFSIZ
];
75 openfiles
= (FILE **) malloc (openfiles_size
* sizeof (FILE *));
80 * Open up an AF_UNIX socket in this person's home directory
83 if ((s
= socket (AF_UNIX
, SOCK_STREAM
, 0)) < 0)
88 server
.sun_family
= AF_UNIX
;
89 #ifndef SERVER_HOME_DIR
90 gethostname (system_name
, sizeof (system_name
));
91 sprintf (server
.sun_path
, "/tmp/esrv%d-%s", geteuid (), system_name
);
93 if (unlink (server
.sun_path
) == -1 && errno
!= ENOENT
)
99 if ((homedir
= getenv ("HOME")) == NULL
)
101 fprintf (stderr
,"No home directory\n");
104 strcpy (server
.sun_path
, homedir
);
105 strcat (server
.sun_path
, "/.emacs_server");
106 /* Delete anyone else's old server. */
107 unlink (server
.sun_path
);
110 if (bind (s
, &server
, strlen (server
.sun_path
) + 2) < 0)
115 /* Only this user can send commands to this Emacs. */
116 chmod (server
.sun_path
, 0600);
118 * Now, just wait for everything to come in..
120 if (listen (s
, 5) < 0)
126 /* Disable sigpipes in case luser kills client... */
127 signal (SIGPIPE
, SIG_IGN
);
130 int rmask
= (1 << s
) + 1;
131 if (select (s
+ 1, &rmask
, 0, 0, 0) < 0)
133 if (rmask
& (1 << s
)) /* client sends list of filenames */
135 fromlen
= sizeof (fromunix
);
136 fromunix
.sun_family
= AF_UNIX
;
137 infd
= accept (s
, &fromunix
, &fromlen
); /* open socket fd */
140 if (errno
== EMFILE
|| errno
== ENFILE
)
141 printf ("Too many clients.\n");
147 if (infd
>= openfiles_size
)
150 openfiles
= (FILE **) realloc (openfiles
,
151 openfiles_size
* sizeof (FILE *));
156 infile
= fdopen (infd
, "r+"); /* open stream */
159 printf ("Too many clients.\n");
160 write (infd
, "Too many clients.\n", 18);
161 close (infd
); /* Prevent descriptor leak.. */
164 str
= fgets (string
, BUFSIZ
, infile
);
168 close (infd
); /* Prevent descriptor leak.. */
171 openfiles
[infd
] = infile
;
172 printf ("Client: %d %s", infd
, string
);
173 /* If what we read did not end in a newline,
174 it means there is more. Keep reading from the socket
175 and outputting to Emacs, until we get the newline. */
176 while (string
[strlen (string
) - 1] != '\n')
178 if (fgets (string
, BUFSIZ
, infile
) == 0)
180 printf ("%s", string
);
186 else if (rmask
& 1) /* emacs sends codeword, fd, and string message */
188 /* Read command codeword and fd */
190 scanf ("%s %d%*c", code
, &infd
);
191 if (ferror (stdin
) || feof (stdin
))
193 fprintf (stderr
, "server: error reading from standard input\n");
197 /* Transfer text from Emacs to the client, up to a newline. */
198 infile
= openfiles
[infd
];
201 if (fgets (string
, BUFSIZ
, stdin
) == 0)
203 fprintf (infile
, "%s", string
);
204 if (string
[strlen (string
) - 1] == '\n')
209 /* If command is close, close connection to client. */
210 if (strncmp (code
, "Close:", 6) == 0)
221 #else /* This is the SYSV IPC section */
223 #include <sys/types.h>
224 #include <sys/signal.h>
238 /* "THIS has to be fixed. Remember, stderr may not exist...-rlk."
239 Incorrect. This program runs as an inferior of Emacs.
240 Its stderr always exists--rms. */
245 int s
, infd
, fromlen
;
247 struct msgbuf
* msgp
=
248 (struct msgbuf
*) malloc (sizeof *msgp
+ BUFSIZ
);
249 struct msqid_ds msg_st
;
251 char *homedir
, *getenv ();
256 * Create a message queue using ~/.emacs_server as the path for ftok
258 if ((homedir
= getenv ("HOME")) == NULL
)
260 fprintf (stderr
,"No home directory\n");
263 strcpy (string
, homedir
);
264 strcat (string
, "/.emacs_server");
265 creat (string
, 0600);
266 key
= ftok (string
, 1); /* unlikely to be anyone else using it */
267 s
= msgget (key
, 0600 | IPC_CREAT
);
274 /* Fork so we can close connection even if parent dies */
278 msgctl (s
, IPC_RMID
, 0);
282 signal (SIGTERM
, msgcatch
);
283 signal (SIGINT
, msgcatch
);
284 /* If parent goes away, remove message box and exit */
288 setpgrp (); /* Gnu kills process group on exit */
293 msgctl (s
, IPC_RMID
, 0);
302 if ((fromlen
= msgrcv (s
, msgp
, BUFSIZ
- 1, 1, 0)) < 0)
309 msgctl (s
, IPC_STAT
, &msg_st
);
310 strncpy (string
, msgp
->mtext
, fromlen
);
311 string
[fromlen
] = 0; /* make sure */
312 /* Newline is part of string.. */
313 printf ("Client: %d %s", s
, string
);
315 /* Now, wait for a wakeup */
316 fgets (msgp
->mtext
, BUFSIZ
, stdin
);
317 msgp
->mtext
[strlen (msgp
->mtext
)-1] = 0;
318 /* strcpy (msgp->mtext, "done");*/
319 msgp
->mtype
= msg_st
.msg_lspid
;
320 msgsnd (s
, msgp
, strlen (msgp
->mtext
)+1, 0);
325 #endif /* HAVE_SYSVIPC */
327 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */