annotate lib-src/emacsserver.c @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children 7e54bd776075
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/socket.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 #include <sys/signal.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 #include <sys/stat.h> /* Needed for chmod, at least on Linux */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 #include <sys/un.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 #include <errno.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 #include "../src/sysproc.h" /* Needed for select */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 main ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 char system_name[256];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 int s, infd, fromlen;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 struct sockaddr_un server, fromunix;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 char *str, string[BUFSIZ], code[BUFSIZ];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 FILE *infile;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 FILE **openfiles;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 int openfiles_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 openfiles_size = 20;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 openfiles = (FILE **) malloc (openfiles_size * sizeof (FILE *));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 if (openfiles == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 * Open up an AF_UNIX socket in this person's home directory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 perror ("socket");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 memset (&server, 0, sizeof (server));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 server.sun_family = AF_UNIX;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 #ifndef SERVER_HOME_DIR
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 gethostname (system_name, sizeof (system_name));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 sprintf (server.sun_path, "/tmp/esrv%d-%s", (int) geteuid (), system_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 if (unlink (server.sun_path) == -1 && errno != ENOENT)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 perror ("unlink");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 char *homedir;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 if ((homedir = getenv ("HOME")) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 fprintf (stderr,"No home directory\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 sprintf (server.sun_path, "/tmp/esrv%d", geteuid ());
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 #if 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 strcpy (server.sun_path, homedir);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 strcat (server.sun_path, "/.emacs_server");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 /* Delete anyone else's old server. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 unlink (server.sun_path);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 #endif /* SERVER_HOME_DIR */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 int bindlen;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 #ifdef HAVE_SOCKADDR_SUN_LEN
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 /* See W. R. Stevens "Advanced Programming in the Unix Environment"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 p. 502 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 bindlen = (sizeof (server.sun_len) + sizeof (server.sun_family)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 + strlen (server.sun_path) + 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 server.sun_len = bindlen;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 bindlen = strlen (server.sun_path) + sizeof (server.sun_family);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 if (bind (s, (struct sockaddr *) &server, bindlen) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 perror ("bind");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 /* Only this user can send commands to this Emacs. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 chmod (server.sun_path, 0600);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 * Now, just wait for everything to come in..
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 if (listen (s, 5) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 perror ("listen");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 /* Disable sigpipes in case luser kills client... */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 signal (SIGPIPE, SIG_IGN);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 for (;;)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 int rmask = (1 << s) + 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 if (select (s + 1, (fd_set *)&rmask, 0, 0, 0) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 perror ("select");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 if (rmask & (1 << s)) /* client sends list of filenames */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 fromlen = sizeof (fromunix);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 fromunix.sun_family = AF_UNIX;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 infd = accept (s, (struct sockaddr *) &fromunix,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 #ifdef HAVE_POSIX_ACCEPT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 /* Posix dictates that this arg should be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 a size_t *, but all known existing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 implementations want an int *. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (size_t *) &fromlen
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 &fromlen
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 );
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 if (infd < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 if (errno == EMFILE || errno == ENFILE)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 printf ("Too many clients.\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 perror ("accept");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 if (infd >= openfiles_size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 openfiles_size *= 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 openfiles = (FILE **) realloc (openfiles,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 openfiles_size * sizeof (FILE *));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 if (openfiles == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 abort ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 infile = (FILE *) fdopen (infd, "r+"); /* open stream */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 if (infile == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 printf ("Too many clients.\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 write (infd, "Too many clients.\n", 18);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 close (infd); /* Prevent descriptor leak.. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 str = fgets (string, BUFSIZ, infile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 if (str == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 perror ("fgets");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 close (infd); /* Prevent descriptor leak.. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 openfiles[infd] = infile;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 printf ("Client: %d %s", infd, string);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 /* If what we read did not end in a newline,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 it means there is more. Keep reading from the socket
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 and outputting to Emacs, until we get the newline. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 while (string[strlen (string) - 1] != '\n')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 if (fgets (string, BUFSIZ, infile) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 printf ("%s", string);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 fflush (stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 fflush (infile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 else if (rmask & 1) /* emacs sends codeword, fd, and string message */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 /* Read command codeword and fd */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 clearerr (stdin);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 scanf ("%s %d%*c", code, &infd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 if (ferror (stdin) || feof (stdin))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 fprintf (stderr, "server: error reading from standard input\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 /* Transfer text from Emacs to the client, up to a newline. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 infile = openfiles[infd];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 while (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 if (fgets (string, BUFSIZ, stdin) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 fprintf (infile, "%s", string);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 if (string[strlen (string) - 1] == '\n')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 fflush (infile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 /* If command is close, close connection to client. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 if (strncmp (code, "Close:", 6) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 if (infd > 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 fclose (infile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 close (infd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 #else /* This is the SYSV IPC section */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 #include <sys/types.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 #include <sys/signal.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 #include <sys/ipc.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 #include <sys/msg.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 #include <setjmp.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 jmp_buf msgenv;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 static SIGTYPE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 msgcatch (int arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 longjmp (msgenv, 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 /* "THIS has to be fixed. Remember, stderr may not exist...-rlk."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 Incorrect. This program runs as an inferior of Emacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 Its stderr always exists--rms. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 #if !__STDC__ && !defined(STDC_HEADERS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 #ifndef convex
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 char *getenv ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 main ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 int s, fromlen, ioproc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 #if 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 int infd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 #endif /* 0 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 key_t key;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 struct msgbuf * msgp =
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 (struct msgbuf *) malloc (sizeof *msgp + BUFSIZ);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 struct msqid_ds msg_st;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 int p;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 char *homedir;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 char string[BUFSIZ];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 #if 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 FILE *infile;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 #endif /* 0 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 * Create a message queue using ~/.emacs_server as the path for ftok
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 if ((homedir = getenv ("HOME")) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 fprintf (stderr,"No home directory\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 strcpy (string, homedir);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 strcat (string, "/.emacs_server");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 creat (string, 0600);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 key = ftok (string, 1); /* unlikely to be anyone else using it */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 s = msgget (key, 0600 | IPC_CREAT);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 if (s == -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 perror ("msgget");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 /* Fork so we can close connection even if parent dies */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 p = fork ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 if (setjmp (msgenv))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 msgctl (s, IPC_RMID, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 kill (p, SIGKILL);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 exit (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 signal (SIGTERM, msgcatch);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 signal (SIGINT, msgcatch);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 if (p > 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 /* This is executed in the original process that did the fork above. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 /* Get pid of Emacs itself. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 p = getppid ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 setpgrp (); /* Gnu kills process group on exit */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 while (1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 /* Is Emacs still alive? */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 if (kill (p, 0) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 msgctl (s, IPC_RMID, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 exit (0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 sleep (10);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 /* This is executed in the child made by forking above.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 Call it c1. Make another process, ioproc. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 ioproc = fork ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 if (ioproc == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 /* In process ioproc, wait for text from Emacs,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 and send it to the process c1.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 This way, c1 only has to wait for one source of input. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 while (fgets (msgp->mtext, BUFSIZ, stdin))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 msgp->mtype = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 /* In the process c1,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 listen for messages from clients and pass them to Emacs. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 while (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 if ((fromlen = msgrcv (s, msgp, BUFSIZ - 1, 1, 0)) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 perror ("msgrcv");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 exit (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 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 msgctl (s, IPC_STAT, &msg_st);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 /* Distinguish whether the message came from a client, or from
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 ioproc. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 if (msg_st.msg_lspid == ioproc)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 char code[BUFSIZ];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 int inproc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 /* Message from ioproc: tell a client we are done. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 msgp->mtext[strlen (msgp->mtext)-1] = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 sscanf (msgp->mtext, "%s %d", code, &inproc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 msgp->mtype = inproc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 continue;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 /* This is a request from a client: copy to stdout
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 so that Emacs will get it. Include msg_lspid
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 so server.el can tell us where to send the reply. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 strncpy (string, msgp->mtext, fromlen);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 string[fromlen] = 0; /* make sure */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 /* Newline is part of string.. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 printf ("Client: %d %s", (int) msg_st.msg_lspid, string);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 fflush (stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 /* Now, wait for a wakeup */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 #endif /* HAVE_SYSVIPC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */