* Makefile.in (exec_prefix): New variable.
[bpt/emacs.git] / lib-src / emacsserver.c
1 /* Communication subprocess for GNU Emacs acting as server.
2 Copyright (C) 1986, 1987, 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
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.
10
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.
15
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. */
19
20
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. */
26
27 #define NO_SHORTNAMES
28 #include "../src/config.h"
29 #undef read
30 #undef write
31 #undef open
32 #undef close
33
34
35 #if !defined(HAVE_SOCKETS) && !defined(HAVE_SYSVIPC)
36 #include <stdio.h>
37
38 main ()
39 {
40 fprintf (stderr, "Sorry, the Emacs server is supported only on systems\n");
41 fprintf (stderr, "with Berkeley sockets or System V IPC.\n");
42 exit (1);
43 }
44
45 #else /* HAVE_SOCKETS or HAVE_SYSVIPC */
46
47 #if ! defined (HAVE_SYSVIPC)
48 /* BSD code is very different from SYSV IPC code */
49
50 #include <sys/file.h>
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <sys/signal.h>
54 #include <sys/un.h>
55 #include <stdio.h>
56 #include <errno.h>
57
58 extern int errno;
59
60 main ()
61 {
62 char system_name[32];
63 int s, infd, fromlen;
64 struct sockaddr_un server, fromunix;
65 char *homedir;
66 char *str, string[BUFSIZ], code[BUFSIZ];
67 FILE *infile;
68 FILE **openfiles;
69 int openfiles_size;
70
71 int geteuid ();
72 char *getenv ();
73
74 openfiles_size = 20;
75 openfiles = (FILE **) malloc (openfiles_size * sizeof (FILE *));
76 if (openfiles == 0)
77 abort ();
78
79 /*
80 * Open up an AF_UNIX socket in this person's home directory
81 */
82
83 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
84 {
85 perror ("socket");
86 exit (1);
87 }
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);
92
93 if (unlink (server.sun_path) == -1 && errno != ENOENT)
94 {
95 perror ("unlink");
96 exit (1);
97 }
98 #else
99 if ((homedir = getenv ("HOME")) == NULL)
100 {
101 fprintf (stderr,"No home directory\n");
102 exit (1);
103 }
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);
108 #endif
109
110 if (bind (s, &server, strlen (server.sun_path) + 2) < 0)
111 {
112 perror ("bind");
113 exit (1);
114 }
115 /* Only this user can send commands to this Emacs. */
116 chmod (server.sun_path, 0600);
117 /*
118 * Now, just wait for everything to come in..
119 */
120 if (listen (s, 5) < 0)
121 {
122 perror ("listen");
123 exit (1);
124 }
125
126 /* Disable sigpipes in case luser kills client... */
127 signal (SIGPIPE, SIG_IGN);
128 for (;;)
129 {
130 int rmask = (1 << s) + 1;
131 if (select (s + 1, &rmask, 0, 0, 0) < 0)
132 perror ("select");
133 if (rmask & (1 << s)) /* client sends list of filenames */
134 {
135 fromlen = sizeof (fromunix);
136 fromunix.sun_family = AF_UNIX;
137 infd = accept (s, &fromunix, &fromlen); /* open socket fd */
138 if (infd < 0)
139 {
140 if (errno == EMFILE || errno == ENFILE)
141 printf ("Too many clients.\n");
142 else
143 perror ("accept");
144 continue;
145 }
146
147 if (infd >= openfiles_size)
148 {
149 openfiles_size *= 2;
150 openfiles = (FILE **) realloc (openfiles,
151 openfiles_size * sizeof (FILE *));
152 if (openfiles == 0)
153 abort ();
154 }
155
156 infile = fdopen (infd, "r+"); /* open stream */
157 if (infile == NULL)
158 {
159 printf ("Too many clients.\n");
160 write (infd, "Too many clients.\n", 18);
161 close (infd); /* Prevent descriptor leak.. */
162 continue;
163 }
164 str = fgets (string, BUFSIZ, infile);
165 if (str == NULL)
166 {
167 perror ("fgets");
168 close (infd); /* Prevent descriptor leak.. */
169 continue;
170 }
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')
177 {
178 if (fgets (string, BUFSIZ, infile) == 0)
179 break;
180 printf ("%s", string);
181 }
182 fflush (stdout);
183 fflush (infile);
184 continue;
185 }
186 else if (rmask & 1) /* emacs sends codeword, fd, and string message */
187 {
188 /* Read command codeword and fd */
189 clearerr (stdin);
190 scanf ("%s %d%*c", code, &infd);
191 if (ferror (stdin) || feof (stdin))
192 {
193 fprintf (stderr, "server: error reading from standard input\n");
194 exit (1);
195 }
196
197 /* Transfer text from Emacs to the client, up to a newline. */
198 infile = openfiles[infd];
199 while (1)
200 {
201 if (fgets (string, BUFSIZ, stdin) == 0)
202 break;
203 fprintf (infile, "%s", string);
204 if (string[strlen (string) - 1] == '\n')
205 break;
206 }
207 fflush (infile);
208
209 /* If command is close, close connection to client. */
210 if (strncmp (code, "Close:", 6) == 0)
211 if (infd > 2)
212 {
213 fclose (infile);
214 close (infd);
215 }
216 continue;
217 }
218 }
219 }
220
221 #else /* This is the SYSV IPC section */
222
223 #include <sys/types.h>
224 #include <sys/signal.h>
225 #include <sys/ipc.h>
226 #include <sys/msg.h>
227 #include <setjmp.h>
228
229 jmp_buf msgenv;
230
231 SIGTYPE
232 msgcatch ()
233 {
234 longjmp (msgenv, 1);
235 }
236
237
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. */
241 #include <stdio.h>
242
243 main ()
244 {
245 int s, infd, fromlen;
246 key_t key;
247 struct msgbuf * msgp =
248 (struct msgbuf *) malloc (sizeof *msgp + BUFSIZ);
249 struct msqid_ds msg_st;
250 int p;
251 char *homedir, *getenv ();
252 char string[BUFSIZ];
253 FILE *infile;
254
255 /*
256 * Create a message queue using ~/.emacs_server as the path for ftok
257 */
258 if ((homedir = getenv ("HOME")) == NULL)
259 {
260 fprintf (stderr,"No home directory\n");
261 exit (1);
262 }
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);
268 if (s == -1)
269 {
270 perror ("msgget");
271 exit (1);
272 }
273
274 /* Fork so we can close connection even if parent dies */
275 p = fork ();
276 if (setjmp (msgenv))
277 {
278 msgctl (s, IPC_RMID, 0);
279 kill (p, SIGKILL);
280 exit (0);
281 }
282 signal (SIGTERM, msgcatch);
283 signal (SIGINT, msgcatch);
284 /* If parent goes away, remove message box and exit */
285 if (p == 0)
286 {
287 p = getppid ();
288 setpgrp (); /* Gnu kills process group on exit */
289 while (1)
290 {
291 if (kill (p, 0) < 0)
292 {
293 msgctl (s, IPC_RMID, 0);
294 exit (0);
295 }
296 sleep (10);
297 }
298 }
299
300 while (1)
301 {
302 if ((fromlen = msgrcv (s, msgp, BUFSIZ - 1, 1, 0)) < 0)
303 {
304 perror ("msgrcv");
305 exit (1);
306 }
307 else
308 {
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);
314 fflush (stdout);
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);
321 }
322 }
323 }
324
325 #endif /* HAVE_SYSVIPC */
326
327 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */