annotate lib-src/emacsserver.c @ 114:8619ce7e4c50 r20-1b9

Import from CVS: tag r20-1b9
author cvs
date Mon, 13 Aug 2007 09:21:54 +0200
parents 131b0175ea99
children 9f59509498e1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* Communication subprocess for GNU Emacs acting as server.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Copyright (C) 1986, 1987, 1993 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 This file is part of GNU Emacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 GNU Emacs is free software; you can redistribute it and/or modify
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 GNU Emacs is distributed in the hope that it will be useful,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 GNU General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 along with GNU Emacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 the Free the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 /* Synched up with: FSF 19.28. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 /* The GNU Emacs edit server process is run as a subprocess of Emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 under control of the file lisp/server.el.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 This program accepts communication from client (program emacsclient.c)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 and passes their commands (consisting of keyboard characters)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 up to the Emacs which then executes them. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 #define NO_SHORTNAMES
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 #include <../src/config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 #undef read
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 #undef write
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 #undef open
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 #undef close
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 #undef signal
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 #if __STDC__ || defined(STDC_HEADERS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 # include <stdlib.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 # include <unistd.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 # include <fcntl.h> /* for creat() */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 # include <string.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 extern int errno;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 #if !defined(HAVE_SOCKETS) && !defined(HAVE_SYSVIPC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 main ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 fprintf (stderr, "Sorry, the Emacs server is supported only on systems\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 fprintf (stderr, "with Berkeley sockets or System V IPC.\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 #else /* HAVE_SOCKETS or HAVE_SYSVIPC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 #if ! defined (HAVE_SYSVIPC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 /* BSD code is very different from SYSV IPC code */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 #include <sys/types.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 #include <sys/file.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 #include <sys/signal.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 #include <sys/stat.h> /* Needed for chmod, at least on Linux */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 #include <sys/un.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 #include <errno.h>
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents: 42
diff changeset
69 #include "../src/sysproc.h" /* Needed for select */
114
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 70
diff changeset
70 #ifndef SOCK_STREAM
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 70
diff changeset
71 /* this is normally included by src/sysproc.h. might be safe to omit
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 70
diff changeset
72 * it entirely. lousy ultrix's sys/socket.h chokes if it's included
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 70
diff changeset
73 * twice, so we can't include unconditionally. */
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 70
diff changeset
74 #include <sys/socket.h>
8619ce7e4c50 Import from CVS: tag r20-1b9
cvs
parents: 70
diff changeset
75 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 main ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 char system_name[256];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 int s, infd, fromlen;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 struct sockaddr_un server, fromunix;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 char *str, string[BUFSIZ], code[BUFSIZ];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 FILE *infile;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 FILE **openfiles;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 int openfiles_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 openfiles_size = 20;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 openfiles = (FILE **) malloc (openfiles_size * sizeof (FILE *));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 if (openfiles == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 * Open up an AF_UNIX socket in this person's home directory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 perror ("socket");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 memset (&server, 0, sizeof (server));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 server.sun_family = AF_UNIX;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 #ifndef SERVER_HOME_DIR
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 gethostname (system_name, sizeof (system_name));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 sprintf (server.sun_path, "/tmp/esrv%d-%s", (int) geteuid (), system_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 if (unlink (server.sun_path) == -1 && errno != ENOENT)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 perror ("unlink");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 char *homedir;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 if ((homedir = getenv ("HOME")) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 fprintf (stderr,"No home directory\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 sprintf (server.sun_path, "/tmp/esrv%d", geteuid ());
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 #if 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 strcpy (server.sun_path, homedir);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 strcat (server.sun_path, "/.emacs_server");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 /* Delete anyone else's old server. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 unlink (server.sun_path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 #endif /* SERVER_HOME_DIR */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 int bindlen;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 #ifdef HAVE_SOCKADDR_SUN_LEN
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 /* See W. R. Stevens "Advanced Programming in the Unix Environment"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 p. 502 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 bindlen = (sizeof (server.sun_len) + sizeof (server.sun_family)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 + strlen (server.sun_path) + 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 server.sun_len = bindlen;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 bindlen = strlen (server.sun_path) + sizeof (server.sun_family);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 if (bind (s, (struct sockaddr *) &server, bindlen) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 perror ("bind");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 /* Only this user can send commands to this Emacs. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 chmod (server.sun_path, 0600);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 * Now, just wait for everything to come in..
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 if (listen (s, 5) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 perror ("listen");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 /* Disable sigpipes in case luser kills client... */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 signal (SIGPIPE, SIG_IGN);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 for (;;)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 int rmask = (1 << s) + 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 if (select (s + 1, (fd_set *)&rmask, 0, 0, 0) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 perror ("select");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 if (rmask & (1 << s)) /* client sends list of filenames */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 fromlen = sizeof (fromunix);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 fromunix.sun_family = AF_UNIX;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 infd = accept (s, (struct sockaddr *) &fromunix,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 #ifdef HAVE_POSIX_ACCEPT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 /* Posix dictates that this arg should be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 a size_t *, but all known existing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 implementations want an int *. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 (size_t *) &fromlen
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 &fromlen
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 if (infd < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 if (errno == EMFILE || errno == ENFILE)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 printf ("Too many clients.\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 perror ("accept");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 if (infd >= openfiles_size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 openfiles_size *= 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 openfiles = (FILE **) realloc (openfiles,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 openfiles_size * sizeof (FILE *));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 if (openfiles == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 infile = (FILE *) fdopen (infd, "r+"); /* open stream */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 if (infile == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 printf ("Too many clients.\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 write (infd, "Too many clients.\n", 18);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 close (infd); /* Prevent descriptor leak.. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 str = fgets (string, BUFSIZ, infile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 if (str == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 perror ("fgets");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 close (infd); /* Prevent descriptor leak.. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 openfiles[infd] = infile;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 printf ("Client: %d %s", infd, string);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 /* If what we read did not end in a newline,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 it means there is more. Keep reading from the socket
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 and outputting to Emacs, until we get the newline. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 while (string[strlen (string) - 1] != '\n')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 if (fgets (string, BUFSIZ, infile) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 printf ("%s", string);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 fflush (stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 fflush (infile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 else if (rmask & 1) /* emacs sends codeword, fd, and string message */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 /* Read command codeword and fd */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 clearerr (stdin);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 scanf ("%s %d%*c", code, &infd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 if (ferror (stdin) || feof (stdin))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 fprintf (stderr, "server: error reading from standard input\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 /* Transfer text from Emacs to the client, up to a newline. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 infile = openfiles[infd];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 while (1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 if (fgets (string, BUFSIZ, stdin) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 fprintf (infile, "%s", string);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 if (string[strlen (string) - 1] == '\n')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 fflush (infile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 /* If command is close, close connection to client. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 if (strncmp (code, "Close:", 6) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 if (infd > 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 fclose (infile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 close (infd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 #else /* This is the SYSV IPC section */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 #include <sys/types.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 #include <sys/signal.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 #include <sys/ipc.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 #include <sys/msg.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 #include <setjmp.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 jmp_buf msgenv;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 static SIGTYPE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 msgcatch (int arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 longjmp (msgenv, 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 /* "THIS has to be fixed. Remember, stderr may not exist...-rlk."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 Incorrect. This program runs as an inferior of Emacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 Its stderr always exists--rms. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 #if !__STDC__ && !defined(STDC_HEADERS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 #ifndef convex
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 char *getenv ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 main ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 int s, fromlen, ioproc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 #if 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 int infd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 #endif /* 0 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 key_t key;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 struct msgbuf * msgp =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 (struct msgbuf *) malloc (sizeof *msgp + BUFSIZ);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 struct msqid_ds msg_st;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 int p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 char *homedir;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 char string[BUFSIZ];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 #if 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 FILE *infile;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 #endif /* 0 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 * Create a message queue using ~/.emacs_server as the path for ftok
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 if ((homedir = getenv ("HOME")) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 fprintf (stderr,"No home directory\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 strcpy (string, homedir);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 strcat (string, "/.emacs_server");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 creat (string, 0600);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 key = ftok (string, 1); /* unlikely to be anyone else using it */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 s = msgget (key, 0600 | IPC_CREAT);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 if (s == -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 perror ("msgget");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 /* Fork so we can close connection even if parent dies */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 p = fork ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 if (setjmp (msgenv))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 msgctl (s, IPC_RMID, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 kill (p, SIGKILL);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 exit (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 signal (SIGTERM, msgcatch);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 signal (SIGINT, msgcatch);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 if (p > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 /* This is executed in the original process that did the fork above. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 /* Get pid of Emacs itself. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 p = getppid ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 setpgrp (); /* Gnu kills process group on exit */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 while (1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 /* Is Emacs still alive? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 if (kill (p, 0) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 msgctl (s, IPC_RMID, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 exit (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 sleep (10);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 /* This is executed in the child made by forking above.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 Call it c1. Make another process, ioproc. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 ioproc = fork ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 if (ioproc == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 /* In process ioproc, wait for text from Emacs,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 and send it to the process c1.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 This way, c1 only has to wait for one source of input. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 while (fgets (msgp->mtext, BUFSIZ, stdin))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 msgp->mtype = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 /* In the process c1,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 listen for messages from clients and pass them to Emacs. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 while (1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 if ((fromlen = msgrcv (s, msgp, BUFSIZ - 1, 1, 0)) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 perror ("msgrcv");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 msgctl (s, IPC_STAT, &msg_st);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 /* Distinguish whether the message came from a client, or from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 ioproc. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 if (msg_st.msg_lspid == ioproc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 char code[BUFSIZ];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 int inproc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 /* Message from ioproc: tell a client we are done. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 msgp->mtext[strlen (msgp->mtext)-1] = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 sscanf (msgp->mtext, "%s %d", code, &inproc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 msgp->mtype = inproc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 /* This is a request from a client: copy to stdout
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 so that Emacs will get it. Include msg_lspid
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 so server.el can tell us where to send the reply. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 strncpy (string, msgp->mtext, fromlen);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 string[fromlen] = 0; /* make sure */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 /* Newline is part of string.. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 printf ("Client: %d %s", (int) msg_st.msg_lspid, string);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 fflush (stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 /* Now, wait for a wakeup */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 #endif /* HAVE_SYSVIPC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */