204
|
1 /* Communication subprocess for GNU Emacs acting as server.
|
|
2 Copyright (C) 1986, 1987 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 1, 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 {
|
470
|
62 char system_name[32];
|
204
|
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;
|
470
|
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)
|
204
|
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");
|
470
|
106 /* Delete anyone else's old server. */
|
|
107 unlink (server.sun_path);
|
204
|
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 msgcatch ()
|
|
232 {
|
|
233 longjmp (msgenv, 1);
|
|
234 }
|
|
235
|
|
236
|
|
237 /* "THIS has to be fixed. Remember, stderr may not exist...-rlk."
|
|
238 Incorrect. This program runs as an inferior of Emacs.
|
|
239 Its stderr always exists--rms. */
|
|
240 #include <stdio.h>
|
|
241
|
|
242 main ()
|
|
243 {
|
|
244 int s, infd, fromlen;
|
|
245 key_t key;
|
|
246 struct msgbuf * msgp =
|
|
247 (struct msgbuf *) malloc (sizeof *msgp + BUFSIZ);
|
|
248 struct msqid_ds msg_st;
|
|
249 int p;
|
|
250 char *homedir, *getenv ();
|
|
251 char string[BUFSIZ];
|
|
252 FILE *infile;
|
|
253
|
|
254 /*
|
|
255 * Create a message queue using ~/.emacs_server as the path for ftok
|
|
256 */
|
|
257 if ((homedir = getenv ("HOME")) == NULL)
|
|
258 {
|
|
259 fprintf (stderr,"No home directory\n");
|
|
260 exit (1);
|
|
261 }
|
|
262 strcpy (string, homedir);
|
|
263 strcat (string, "/.emacs_server");
|
|
264 creat (string, 0600);
|
|
265 key = ftok (string, 1); /* unlikely to be anyone else using it */
|
|
266 s = msgget (key, 0600 | IPC_CREAT);
|
|
267 if (s == -1)
|
|
268 {
|
|
269 perror ("msgget");
|
|
270 exit (1);
|
|
271 }
|
|
272
|
|
273 /* Fork so we can close connection even if parent dies */
|
|
274 p = fork ();
|
|
275 if (setjmp (msgenv))
|
|
276 {
|
|
277 msgctl (s, IPC_RMID, 0);
|
|
278 kill (p, SIGKILL);
|
|
279 exit (0);
|
|
280 }
|
|
281 signal (SIGTERM, msgcatch);
|
|
282 signal (SIGINT, msgcatch);
|
|
283 /* If parent goes away, remove message box and exit */
|
|
284 if (p == 0)
|
|
285 {
|
|
286 p = getppid ();
|
|
287 setpgrp (); /* Gnu kills process group on exit */
|
|
288 while (1)
|
|
289 {
|
|
290 if (kill (p, 0) < 0)
|
|
291 {
|
|
292 msgctl (s, IPC_RMID, 0);
|
|
293 exit (0);
|
|
294 }
|
|
295 sleep (10);
|
|
296 }
|
|
297 }
|
|
298
|
|
299 while (1)
|
|
300 {
|
|
301 if ((fromlen = msgrcv (s, msgp, BUFSIZ - 1, 1, 0)) < 0)
|
|
302 {
|
|
303 perror ("msgrcv");
|
470
|
304 exit (1);
|
204
|
305 }
|
|
306 else
|
|
307 {
|
|
308 msgctl (s, IPC_STAT, &msg_st);
|
|
309 strncpy (string, msgp->mtext, fromlen);
|
|
310 string[fromlen] = 0; /* make sure */
|
|
311 /* Newline is part of string.. */
|
|
312 printf ("Client: %d %s", s, string);
|
|
313 fflush (stdout);
|
|
314 /* Now, wait for a wakeup */
|
|
315 fgets (msgp->mtext, BUFSIZ, stdin);
|
|
316 msgp->mtext[strlen (msgp->mtext)-1] = 0;
|
|
317 /* strcpy (msgp->mtext, "done");*/
|
|
318 msgp->mtype = msg_st.msg_lspid;
|
|
319 msgsnd (s, msgp, strlen (msgp->mtext)+1, 0);
|
|
320 }
|
|
321 }
|
|
322 }
|
|
323
|
|
324 #endif /* HAVE_SYSVIPC */
|
|
325
|
|
326 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */
|