(x-invocation-args): Add defvar.
[emacs.git] / lib-src / emacsserver.c
blob19de7e66eb86c2eefabc5514d1b480cda4da0b3e
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)
9 any later version.
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. */
27 #define NO_SHORTNAMES
28 #include "../src/config.h"
29 #undef read
30 #undef write
31 #undef open
32 #undef close
35 #if !defined(HAVE_SOCKETS) && !defined(HAVE_SYSVIPC)
36 #include <stdio.h>
38 main ()
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);
45 #else /* HAVE_SOCKETS or HAVE_SYSVIPC */
47 #if ! defined (HAVE_SYSVIPC)
48 /* BSD code is very different from SYSV IPC code */
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>
58 extern int errno;
60 main ()
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;
71 int geteuid ();
72 char *getenv ();
74 openfiles_size = 20;
75 openfiles = (FILE **) malloc (openfiles_size * sizeof (FILE *));
76 if (openfiles == 0)
77 abort ();
79 /*
80 * Open up an AF_UNIX socket in this person's home directory
83 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
85 perror ("socket");
86 exit (1);
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)
95 perror ("unlink");
96 exit (1);
98 #else
99 if ((homedir = getenv ("HOME")) == NULL)
101 fprintf (stderr,"No home directory\n");
102 exit (1);
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
110 if (bind (s, &server, strlen (server.sun_path) + 2) < 0)
112 perror ("bind");
113 exit (1);
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)
122 perror ("listen");
123 exit (1);
126 /* Disable sigpipes in case luser kills client... */
127 signal (SIGPIPE, SIG_IGN);
128 for (;;)
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 */
135 fromlen = sizeof (fromunix);
136 fromunix.sun_family = AF_UNIX;
137 infd = accept (s, &fromunix, &fromlen); /* open socket fd */
138 if (infd < 0)
140 if (errno == EMFILE || errno == ENFILE)
141 printf ("Too many clients.\n");
142 else
143 perror ("accept");
144 continue;
147 if (infd >= openfiles_size)
149 openfiles_size *= 2;
150 openfiles = (FILE **) realloc (openfiles,
151 openfiles_size * sizeof (FILE *));
152 if (openfiles == 0)
153 abort ();
156 infile = fdopen (infd, "r+"); /* open stream */
157 if (infile == NULL)
159 printf ("Too many clients.\n");
160 write (infd, "Too many clients.\n", 18);
161 close (infd); /* Prevent descriptor leak.. */
162 continue;
164 str = fgets (string, BUFSIZ, infile);
165 if (str == NULL)
167 perror ("fgets");
168 close (infd); /* Prevent descriptor leak.. */
169 continue;
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)
179 break;
180 printf ("%s", string);
182 fflush (stdout);
183 fflush (infile);
184 continue;
186 else if (rmask & 1) /* emacs sends codeword, fd, and string message */
188 /* Read command codeword and fd */
189 clearerr (stdin);
190 scanf ("%s %d%*c", code, &infd);
191 if (ferror (stdin) || feof (stdin))
193 fprintf (stderr, "server: error reading from standard input\n");
194 exit (1);
197 /* Transfer text from Emacs to the client, up to a newline. */
198 infile = openfiles[infd];
199 while (1)
201 if (fgets (string, BUFSIZ, stdin) == 0)
202 break;
203 fprintf (infile, "%s", string);
204 if (string[strlen (string) - 1] == '\n')
205 break;
207 fflush (infile);
209 /* If command is close, close connection to client. */
210 if (strncmp (code, "Close:", 6) == 0)
211 if (infd > 2)
213 fclose (infile);
214 close (infd);
216 continue;
221 #else /* This is the SYSV IPC section */
223 #include <sys/types.h>
224 #include <sys/signal.h>
225 #include <sys/ipc.h>
226 #include <sys/msg.h>
227 #include <setjmp.h>
229 jmp_buf msgenv;
231 SIGTYPE
232 msgcatch ()
234 longjmp (msgenv, 1);
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>
243 main ()
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;
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");
261 exit (1);
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)
270 perror ("msgget");
271 exit (1);
274 /* Fork so we can close connection even if parent dies */
275 p = fork ();
276 if (setjmp (msgenv))
278 msgctl (s, IPC_RMID, 0);
279 kill (p, SIGKILL);
280 exit (0);
282 signal (SIGTERM, msgcatch);
283 signal (SIGINT, msgcatch);
284 /* If parent goes away, remove message box and exit */
285 if (p == 0)
287 p = getppid ();
288 setpgrp (); /* Gnu kills process group on exit */
289 while (1)
291 if (kill (p, 0) < 0)
293 msgctl (s, IPC_RMID, 0);
294 exit (0);
296 sleep (10);
300 while (1)
302 if ((fromlen = msgrcv (s, msgp, BUFSIZ - 1, 1, 0)) < 0)
304 perror ("msgrcv");
305 exit (1);
307 else
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);
325 #endif /* HAVE_SYSVIPC */
327 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */