annotate lib-src/gnuserv.c @ 424:11054d720c21 r21-2-20

Import from CVS: tag r21-2-20
author cvs
date Mon, 13 Aug 2007 11:26:11 +0200
parents 697ef44129c6
children
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 /* -*-C-*-
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Server code for handling requests from clients and forwarding them
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 on to the GNU Emacs process.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 This file is part of GNU Emacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 Copying is permitted under those conditions described by the GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 General Public License.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 Copyright (C) 1989 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 Author: Andy Norman (ange@hplb.hpl.hp.com), based on 'etc/server.c'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 from the 18.52 GNU Emacs distribution.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 Please mail bugs and suggestions to the author at the above address.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
18 /* HISTORY
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
19 * 11-Nov-1990 bristor@simba
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 * Added EOT stuff.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 * This file incorporates new features added by Bob Weiner <weiner@mot.com>,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 * Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 * Please see the note at the end of the README file for details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 * (If gnuserv came bundled with your emacs, the README file is probably
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 * ../etc/gnuserv.README relative to the directory containing this file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 369
diff changeset
32 #if 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 369
diff changeset
33 static char rcsid [] = "!Header: gnuserv.c,v 2.1 95/02/16 11:58:27 arup alpha !";
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 369
diff changeset
34 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 369
diff changeset
36 #include "gnuserv.h"
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 #ifdef USE_LITOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 #ifdef linux
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 #include <bsd/sgtty.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 #include <sgtty.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 #endif
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 #ifdef AIX
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 #include <sys/select.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 #include <stdlib.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 #include <sys/types.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 #include <sys/stat.h>
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
54
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
55 #ifdef HAVE_UNISTD_H
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 #include <unistd.h>
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
57 #endif /* HAVE_UNISTD_H */
171
929b76928fce Import from CVS: tag r20-3b12
cvs
parents: 163
diff changeset
58
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
59 #ifdef HAVE_STRING_H
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 #include <string.h>
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
61 #endif /* HAVE_STRING_H */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 !defined(INTERNET_DOMAIN_SOCKETS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 main ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 fprintf (stderr,"Sorry, the Emacs server is only supported on systems that have\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 fprintf (stderr,"Unix Domain sockets, Internet Domain sockets or System V IPC\n");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 } /* main */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 #ifdef SYSV_IPC
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 int ipc_qid = 0; /* ipc message queue id */
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
76 int ipc_wpid = 0; /* watchdog task pid */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 ipc_exit -- clean up the queue id and queue, then kill the watchdog task
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 if it exists. exit with the given status.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 */
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
83 void
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
84 ipc_exit (int stat)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 {
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
86 msgctl (ipc_qid,IPC_RMID,0);
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
87
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
88 if (ipc_wpid != 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
89 kill (ipc_wpid, SIGKILL);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
91 exit (stat);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 } /* ipc_exit */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 ipc_handle_signal -- catch the signal given and clean up.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 */
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
98 void
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
99 ipc_handle_signal(int sig)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 {
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
101 ipc_exit (0);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 } /* ipc_handle_signal */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
105 /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 ipc_spawn_watchdog -- spawn a watchdog task to clean up the message queue should the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 server process die.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 */
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
109 void
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
110 ipc_spawn_watchdog (void)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 {
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
112 if ((ipc_wpid = fork ()) == 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
113 { /* child process */
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
114 int ppid = getppid (); /* parent's process id */
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
115
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
116 setpgrp(); /* gnu kills process group on exit */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
118 while (1)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
119 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
120 if (kill (ppid, 0) < 0) /* ppid is no longer valid, parent
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
121 may have died */
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
122 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
123 ipc_exit (0);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
124 } /* if */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
126 sleep(10); /* have another go later */
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
127 } /* while */
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
128 } /* if */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 } /* ipc_spawn_watchdog */
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 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 ipc_init -- initialize server, setting the global msqid that can be listened on.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 */
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
136 void
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
137 ipc_init (struct msgbuf **msgpp)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 key_t key; /* messge key */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 char buf[GSERV_BUFSZ]; /* pathname for key */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141
259
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents: 239
diff changeset
142 sprintf (buf,"%s/gsrv%d",tmpdir,(int)geteuid ());
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
143 creat (buf,0600);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
144 key = ftok (buf,1);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
146 if ((ipc_qid = msgget (key,0600|IPC_CREAT)) == -1)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
147 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
148 perror (progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
149 fprintf (stderr, "%s: unable to create msg queue\n", progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
150 ipc_exit (1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
151 } /* if */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
153 ipc_spawn_watchdog ();
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
155 signal (SIGTERM,ipc_handle_signal);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
156 signal (SIGINT,ipc_handle_signal);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
158 if ((*msgpp = (struct msgbuf *)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
159 malloc (sizeof **msgpp + GSERV_BUFSZ)) == NULL)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
160 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
161 fprintf (stderr,
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
162 "%s: unable to allocate space for message buffer\n", progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
163 ipc_exit(1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
164 } /* if */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 } /* ipc_init */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 handle_ipc_request -- accept a request from a client, pass the request on
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 to the GNU Emacs process, then wait for its reply and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 pass that on to the client.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 */
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
173 void
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
174 handle_ipc_request (struct msgbuf *msgp)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 struct msqid_ds msg_st; /* message status */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 char buf[GSERV_BUFSZ];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 int len; /* length of message / read */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 int s, result_len; /* tag fields on the response from emacs */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 int offset = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 int total = 1; /* # bytes that will actually be sent off */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
183 if ((len = msgrcv (ipc_qid, msgp, GSERV_BUFSZ - 1, 1, 0)) < 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
184 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
185 perror (progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
186 fprintf (stderr, "%s: unable to receive\n", progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
187 ipc_exit (1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
188 } /* if */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
190 msgctl (ipc_qid, IPC_STAT, &msg_st);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
191 strncpy (buf, msgp->mtext, len);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 buf[len] = '\0'; /* terminate */
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
193
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
194 printf ("%d %s", ipc_qid, buf);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
195 fflush (stdout);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 /* now for the response from gnu */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 msgp->mtext[0] = '\0';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 #if 0
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
201 if ((len = read(0,buf,GSERV_BUFSZ-1)) < 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
202 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
203 perror (progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
204 fprintf (stderr, "%s: unable to read\n", progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
205 ipc_exit (1);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 } /* if */
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
207
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
208 sscanf (buf, "%d:%[^\n]\n", &junk, msgp->mtext);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
209 #else
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 /* read in "n/m:" (n=client fd, m=message length) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
213 while (offset < (GSERV_BUFSZ-1) &&
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
214 ((len = read (0, buf + offset, 1)) > 0) &&
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
215 buf[offset] != ':')
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
216 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
217 offset += len;
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
218 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
220 if (len < 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
221 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
222 perror (progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
223 fprintf (stderr, "%s: unable to read\n", progname);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
227 /* parse the response from emacs, getting client fd & result length */
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
228 buf[offset] = '\0';
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
229 sscanf (buf, "%d/%d", &s, &result_len);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
230
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
231 while (result_len > 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
232 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
233 if ((len = read(0, buf, min2 (result_len, GSERV_BUFSZ - 1))) < 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
234 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
235 perror (progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
236 fprintf (stderr, "%s: unable to read\n", progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
237 exit (1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
238 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
240 /* Send this string off, but only if we have enough space */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
242 if (GSERV_BUFSZ > total)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
243 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
244 if (total + len <= GSERV_BUFSZ)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
245 buf[len] = 0;
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
246 else
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
247 buf[GSERV_BUFSZ - total] = 0;
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
248
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
249 send_string(s,buf);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
250 total += strlen(buf);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
251 }
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
252
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
253 result_len -= len;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 /* eat the newline */
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
257 while ((len = read (0,buf,1)) == 0)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 ;
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
259 if (len < 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
260 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
261 perror(progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
262 fprintf (stderr,"%s: unable to read\n", progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
263 exit (1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
264 }
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
265 if (buf[0] != '\n')
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
266 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
267 fprintf (stderr,"%s: garbage after result [%c]\n", progname, buf[0]);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
268 exit (1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
269 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 /* Send a response back to the client. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 msgp->mtype = msg_st.msg_lspid;
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
275 if (msgsnd (ipc_qid,msgp,strlen(msgp->mtext)+1,0) < 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
276 perror ("msgsend(gnuserv)");
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 } /* handle_ipc_request */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 #endif /* SYSV_IPC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 echo_request -- read request from a given socket descriptor, and send the information
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 to stdout (the gnu process).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 echo_request (int s)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 char buf[GSERV_BUFSZ];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 int len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 printf("%d ",s);
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
294
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 /* read until we get a newline or no characters */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 while ((len = recv(s,buf,GSERV_BUFSZ-1,0)) > 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 buf[len] = '\0';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 printf("%s",buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 if (buf[len-1] == EOT_CHR) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 fflush(stdout);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 break; /* end of message */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 } /* while */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 if (len < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 fprintf(stderr,"%s: unable to recv\n",progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 } /* if */
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
312
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 } /* echo_request */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 handle_response -- accept a response from stdin (the gnu process) and pass the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 information on to the relevant client.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 handle_response (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 char buf[GSERV_BUFSZ+1];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 int offset=0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 int s;
424
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
326 int len = 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 int result_len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 /* read in "n/m:" (n=client fd, m=message length) */
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
330 while (offset < GSERV_BUFSZ &&
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 ((len = read(0,buf+offset,1)) > 0) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 buf[offset] != ':') {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 offset += len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 if (len < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 fprintf(stderr,"%s: unable to read\n",progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 }
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
341
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 /* parse the response from emacs, getting client fd & result length */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 buf[offset] = '\0';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 sscanf(buf,"%d/%d", &s, &result_len);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 while (result_len > 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 if ((len = read(0,buf,min2(result_len,GSERV_BUFSZ))) < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 fprintf(stderr,"%s: unable to read\n",progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 buf[len] = '\0';
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 send_string(s,buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 result_len -= len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 /* eat the newline */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 while ((len = read(0,buf,1)) == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 ;
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
360 if (len < 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
361 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
362 perror(progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
363 fprintf(stderr,"%s: unable to read\n",progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
364 exit(1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
365 }
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
366 if (buf[0] != '\n')
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
367 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
368 fprintf(stderr,"%s: garbage after result\n",progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
369 exit(1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
370 }
108
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
371 /* send the newline */
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
372 buf[1] = '\0';
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
373 send_string(s,buf);
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
374 close(s);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 } /* handle_response */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 #ifdef INTERNET_DOMAIN_SOCKETS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 struct entry {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 u_long host_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 struct entry *next;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 struct entry *permitted_hosts[TABLE_SIZE];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 #ifdef AUTH_MAGIC_COOKIE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 # include <X11/X.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 # include <X11/Xauth.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 static Xauth *server_xauth = NULL;
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
393 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
395 static int
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 timed_read (int fd, char *buf, int max, int timeout, int one_line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 fd_set rmask;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 struct timeval tv; /* = {timeout, 0}; */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 char c = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 int nbytes = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 int r;
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
403
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 tv.tv_sec = timeout;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 tv.tv_usec = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 FD_ZERO(&rmask);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 FD_SET(fd, &rmask);
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
409
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
410 do
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
411 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
412 r = select(fd + 1, &rmask, NULL, NULL, &tv);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
414 if (r > 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
415 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
416 if (read (fd, &c, 1) == 1 )
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
417 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
418 *buf++ = c;
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
419 ++nbytes;
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
420 }
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
421 else
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
422 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
423 printf ("read error on socket\004\n");
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
424 return -1;
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
425 }
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
426 }
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
427 else if (r == 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
428 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
429 printf ("read timed out\004\n");
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
430 return -1;
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
431 }
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
432 else
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
433 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
434 printf ("error in select\004\n");
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
435 return -1;
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
436 }
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
437 } while ((nbytes < max) && !(one_line && (c == '\n')));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 --buf;
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
440 if (one_line && *buf == '\n')
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
441 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
442 *buf = 0;
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
443 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 return nbytes;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 }
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
447
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
448
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 permitted -- return whether a given host is allowed to connect to the server.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 permitted (u_long host_addr, int fd)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 int key;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 struct entry *entry;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
459 char auth_protocol[128];
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 char buf[1024];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 int auth_data_len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
463 if (fd > 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
464 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
465 /* we are checking permission on a real connection */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
467 /* Read auth protocol name */
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
468
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
469 if (timed_read(fd, auth_protocol, AUTH_NAMESZ, AUTH_TIMEOUT, 1) <= 0)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 return FALSE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
472 if (strcmp (auth_protocol, DEFAUTH_NAME) &&
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
473 strcmp (auth_protocol, MCOOKIE_NAME))
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
474 {
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
475 printf ("authentication protocol (%s) from client is invalid...\n",
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
476 auth_protocol);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
477 printf ("... Was the client an old version of gnuclient/gnudoit?\004\n");
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
478
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
479 return FALSE;
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
480 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
482 if (!strcmp(auth_protocol, MCOOKIE_NAME))
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
483 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
484
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
485 /*
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
486 * doing magic cookie auth
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
487 */
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
488
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
489 if (timed_read(fd, buf, 10, AUTH_TIMEOUT, 1) <= 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
490 return FALSE;
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
491
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
492 auth_data_len = atoi(buf);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
493
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
494 if (timed_read(fd, buf, auth_data_len, AUTH_TIMEOUT, 0) != auth_data_len)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
495 return FALSE;
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
496
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 #ifdef AUTH_MAGIC_COOKIE
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 369
diff changeset
498 if (server_xauth && server_xauth->data &&
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 369
diff changeset
499 !memcmp(buf, server_xauth->data, auth_data_len))
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 369
diff changeset
500 {
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 369
diff changeset
501 return TRUE;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 369
diff changeset
502 }
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
503 #else
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
504 printf ("client tried Xauth, but server is not compiled with Xauth\n");
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 #endif
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
506
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 * auth failed, but allow this to fall through to the GNU_SECURE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 * protocol....
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
512 printf ("Xauth authentication failed, trying GNU_SECURE auth...\004\n");
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
513
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
514 }
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
515
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
516 /* Other auth protocols go here, and should execute only if the
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
517 * auth_protocol name matches.
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
518 */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 /* Now, try the old GNU_SECURE stuff... */
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
524
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 /* First find the hash key */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 key = HASH(host_addr) % TABLE_SIZE;
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
527
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 /* Now check the chain for that hash key */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 for(entry=permitted_hosts[key]; entry != NULL; entry=entry->next)
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
530 if (host_addr == entry->host_addr)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 return(TRUE);
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
532
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 return(FALSE);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 } /* permitted */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
538 /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 add_host -- add the given host to the list of permitted hosts, provided it isn't
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 already there.
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
541 */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 add_host (u_long host_addr)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 int key;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 struct entry *new_entry;
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
547
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
548 if (!permitted(host_addr, -1))
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
549 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
550 if ((new_entry = (struct entry *) malloc(sizeof(struct entry))) == NULL) {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
551 fprintf(stderr,"%s: unable to malloc space for permitted host entry\n",
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
552 progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
553 exit(1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
554 } /* if */
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
555
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
556 new_entry->host_addr = host_addr;
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
557 key = HASH(host_addr) % TABLE_SIZE;
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
558 new_entry->next = permitted_hosts[key];
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
559 permitted_hosts[key] = new_entry;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 } /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 } /* add_host */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565 /*
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 371
diff changeset
566 setup_table -- initialize the table of hosts allowed to contact the server,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 by reading from the file specified by the GNU_SECURE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 environment variable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569 Put in the local machine, and, if a security file is specifed,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 add each host that is named in the file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 Return the number of hosts added.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 setup_table (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 FILE *host_file;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 char *file_name;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
578 char hostname[HOSTNAMSZ];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
579 u_int host_addr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 int i, hosts=0;
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
581
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582 /* Make sure every entry is null */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 for (i=0; i<TABLE_SIZE; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584 permitted_hosts[i] = NULL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 gethostname(hostname,HOSTNAMSZ);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
588 if ((host_addr = internet_addr(hostname)) == -1)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
589 {
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
590 fprintf(stderr,"%s: unable to find %s in /etc/hosts or from YP",
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
591 progname,hostname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
592 exit(1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
593 } /* if */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 #ifdef AUTH_MAGIC_COOKIE
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
596
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
597 server_xauth = XauGetAuthByAddr (FamilyInternet,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 sizeof(host_addr), (char *)&host_addr,
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
599 strlen(MCOOKIE_SCREEN), MCOOKIE_SCREEN,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
600 strlen(MCOOKIE_X_NAME), MCOOKIE_X_NAME);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
601 hosts++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
602
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
603 #endif /* AUTH_MAGIC_COOKIE */
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
604
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 #if 0 /* Don't even want to allow access from the local host by default */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
607 add_host(host_addr); /* add local host */
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
608 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610 if (((file_name = getenv("GNU_SECURE")) != NULL && /* security file */
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
611 (host_file = fopen(file_name,"r")) != NULL)) /* opened ok */
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
612 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
613 while ((fscanf(host_file,"%s",hostname) != EOF)) /* find a host */
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
614 if ((host_addr = internet_addr(hostname)) != -1)/* get its addr */
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
615 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
616 add_host(host_addr); /* add the addr */
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
617 hosts++;
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
618 }
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
619 fclose(host_file);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
620 } /* if */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 return hosts;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623 } /* setup_table */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
624
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
626 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 internet_init -- initialize server, returning an internet socket that can
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628 be listened on.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
629 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
630 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
631 internet_init (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 int ls; /* socket descriptor */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634 struct servent *sp; /* pointer to service information */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
635 struct sockaddr_in server; /* for local socket address */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636 char *ptr; /* ptr to return from getenv */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
638 if (setup_table() == 0)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639 return -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 /* clear out address structure */
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
642 memset((char *)&server,0,sizeof(struct sockaddr_in));
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
643
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 /* Set up address structure for the listen socket. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645 server.sin_family = AF_INET;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 server.sin_addr.s_addr = INADDR_ANY;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648 /* Find the information for the gnu server
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 * in order to get the needed port number.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
650 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 if ((ptr=getenv("GNU_PORT")) != NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652 server.sin_port = htons(atoi(ptr));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 else if ((sp = getservbyname ("gnuserv", "tcp")) == NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 server.sin_port = htons(DEFAULT_PORT+getuid());
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
655 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
656 server.sin_port = sp->s_port;
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
657
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 /* Create the listen socket. */
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
659 if ((ls = socket (AF_INET,SOCK_STREAM, 0)) == -1)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
660 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
661 perror(progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
662 fprintf(stderr,"%s: unable to create socket\n",progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
663 exit(1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
664 } /* if */
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
665
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666 /* Bind the listen address to the socket. */
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
667 if (bind(ls,(struct sockaddr *) &server,sizeof(struct sockaddr_in)) == -1)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
668 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
669 perror(progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
670 fprintf(stderr,"%s: unable to bind socket\n",progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
671 exit(1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
672 } /* if */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
674 /* Initiate the listen on the socket so remote users
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
675 * can connect.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 */
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
677 if (listen(ls,20) == -1)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
678 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
679 perror(progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
680 fprintf(stderr,"%s: unable to listen\n",progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
681 exit(1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
682 } /* if */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 return(ls);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 } /* internet_init */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
687
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
688
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690 handle_internet_request -- accept a request from a client and send the information
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691 to stdout (the gnu process).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
692 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 handle_internet_request (int ls)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
695 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696 int s;
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
697 size_t addrlen = sizeof(struct sockaddr_in);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 struct sockaddr_in peer; /* for peer socket address */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
700 memset((char *)&peer,0,sizeof(struct sockaddr_in));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
701
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
702 if ((s = accept(ls,(struct sockaddr *)&peer, (void *) &addrlen)) == -1)
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
703 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
704 perror(progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
705 fprintf(stderr,"%s: unable to accept\n",progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
706 exit(1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
707 } /* if */
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
708
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709 /* Check that access is allowed - if not return crud to the client */
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
710 if (!permitted(peer.sin_addr.s_addr, s))
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
711 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
712 send_string(s,"gnudoit: Connection refused\ngnudoit: unable to connect to remote");
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
713 close(s);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
715 printf("Refused connection from %s\004\n", inet_ntoa(peer.sin_addr));
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
716 return;
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
717 } /* if */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
719 echo_request(s);
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
720
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721 } /* handle_internet_request */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 #endif /* INTERNET_DOMAIN_SOCKETS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
725 #ifdef UNIX_DOMAIN_SOCKETS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
726 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
727 unix_init -- initialize server, returning an unix-domain socket that can
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728 be listened on.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730 static int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731 unix_init (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
732 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
733 int ls; /* socket descriptor */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734 struct sockaddr_un server; /* unix socket address */
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
735 int bindlen;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
737 if ((ls = socket(AF_UNIX,SOCK_STREAM, 0)) < 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
738 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
739 perror(progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
740 fprintf(stderr,"%s: unable to create socket\n",progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
741 exit(1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
742 } /* if */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744 /* Set up address structure for the listen socket. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 #ifdef HIDE_UNIX_SOCKET
259
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents: 239
diff changeset
746 sprintf(server.sun_path,"%s/gsrvdir%d",tmpdir,(int)geteuid());
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
747 if (mkdir(server.sun_path, 0700) < 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
748 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
749 /* assume it already exists, and try to set perms */
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
750 if (chmod(server.sun_path, 0700) < 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
751 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
752 perror(progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
753 fprintf(stderr,"%s: can't set permissions on %s\n",
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
754 progname, server.sun_path);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
755 exit(1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
756 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 strcat(server.sun_path,"/gsrv");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 unlink(server.sun_path); /* remove old file if it exists */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760 #else /* HIDE_UNIX_SOCKET */
259
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents: 239
diff changeset
761 sprintf(server.sun_path,"%s/gsrv%d",tmpdir,(int)geteuid());
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
762 unlink(server.sun_path); /* remove old file if it exists */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763 #endif /* HIDE_UNIX_SOCKET */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
764
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
765 server.sun_family = AF_UNIX;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766 #ifdef HAVE_SOCKADDR_SUN_LEN
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767 /* See W. R. Stevens "Advanced Programming in the Unix Environment"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768 p. 502 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769 bindlen = (sizeof (server.sun_len) + sizeof (server.sun_family)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
770 + strlen (server.sun_path) + 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
771 server.sun_len = bindlen;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773 bindlen = strlen (server.sun_path) + sizeof (server.sun_family);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774 #endif
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
775
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
776 if (bind(ls,(struct sockaddr *)&server,bindlen) < 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
777 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
778 perror(progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
779 fprintf(stderr,"%s: unable to bind socket\n",progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
780 exit(1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
781 } /* if */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
782
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783 chmod(server.sun_path,0700); /* only this user can send commands */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785 if (listen(ls,20) < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 perror(progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787 fprintf(stderr,"%s: unable to listen\n",progname);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789 } /* if */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791 /* #### there are also better ways of dealing with this when
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792 sigvec() is present. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
793 #if defined (HAVE_SIGPROCMASK)
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
794 {
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
795 sigset_t _mask;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796 sigemptyset (&_mask);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797 sigaddset (&_mask, SIGPIPE);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
798 sigprocmask (SIG_BLOCK, &_mask, NULL);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
799 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
800 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
801 signal(SIGPIPE,SIG_IGN); /* in case user kills client */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
802 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804 return(ls);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
806 } /* unix_init */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
807
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
808
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
809 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
810 handle_unix_request -- accept a request from a client and send the information
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
811 to stdout (the gnu process).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
812 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
813 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
814 handle_unix_request (int ls)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
815 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
816 int s;
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
817 size_t len = sizeof(struct sockaddr_un);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
818 struct sockaddr_un server; /* for unix socket address */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
819
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
820 server.sun_family = AF_UNIX;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
821
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
822 if ((s = accept(ls,(struct sockaddr *)&server, (void *)&len)) < 0)
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
823 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
824 perror(progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
825 fprintf(stderr,"%s: unable to accept\n",progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
826 } /* if */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
827
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
828 echo_request(s);
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
829
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
830 } /* handle_unix_request */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
831 #endif /* UNIX_DOMAIN_SOCKETS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
832
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
833
215
1f0dabaa0855 Import from CVS: tag r20-4b6
cvs
parents: 171
diff changeset
834 int
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 371
diff changeset
835 main (int argc, char *argv[])
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
836 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
837 int chan; /* temporary channel number */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
838 #ifdef SYSV_IPC
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
839 struct msgbuf *msgp; /* message buffer */
239
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 215
diff changeset
840 #else
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 215
diff changeset
841 int ils = -1; /* internet domain listen socket */
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 215
diff changeset
842 int uls = -1; /* unix domain listen socket */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
843 #endif /* SYSV_IPC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
844
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
845 progname = argv[0];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
846
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
847 for(chan=3; chan < _NFILE; close(chan++)) /* close unwanted channels */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
848 ;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
849
259
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents: 239
diff changeset
850 #ifdef USE_TMPDIR
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents: 239
diff changeset
851 tmpdir = getenv("TMPDIR");
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents: 239
diff changeset
852 #endif
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents: 239
diff changeset
853 if (!tmpdir)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents: 239
diff changeset
854 tmpdir = "/tmp";
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
855 #ifdef USE_LITOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
856 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
857 /* this is to allow ^D to pass to emacs */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
858 int d = LLITOUT;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
859 (void) ioctl(fileno(stdout), TIOCLBIS, &d);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
860 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
861 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
862
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
863 #ifdef SYSV_IPC
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
864 ipc_init(&msgp); /* get a msqid to listen on, and a message buffer */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
865 #endif /* SYSV_IPC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
866
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
867 #ifdef INTERNET_DOMAIN_SOCKETS
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 371
diff changeset
868 ils = internet_init(); /* get an internet domain socket to listen on */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
869 #endif /* INTERNET_DOMAIN_SOCKETS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
870
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
871 #ifdef UNIX_DOMAIN_SOCKETS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
872 uls = unix_init(); /* get a unix domain socket to listen on */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
873 #endif /* UNIX_DOMAIN_SOCKETS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
874
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
875 while (1) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
876 #ifdef SYSV_IPC
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
877 handle_ipc_request(msgp);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
878 #else /* NOT SYSV_IPC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
879 fd_set rmask;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
880 FD_ZERO(&rmask);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
881 FD_SET(fileno(stdin), &rmask);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
882 if (uls >= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
883 FD_SET(uls, &rmask);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
884 if (ils >= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
885 FD_SET(ils, &rmask);
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
886
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 410
diff changeset
887 if (select(max2(fileno(stdin),max2(uls,ils)) + 1, &rmask,
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
888 (fd_set *)NULL, (fd_set *)NULL, (struct timeval *)NULL) < 0)
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
889 {
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
890 perror(progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
891 fprintf(stderr,"%s: unable to select\n",progname);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
892 exit(1);
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 108
diff changeset
893 } /* if */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
894
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
895 #ifdef UNIX_DOMAIN_SOCKETS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
896 if (uls > 0 && FD_ISSET(uls, &rmask))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
897 handle_unix_request(uls);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
898 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
899
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
900 #ifdef INTERNET_DOMAIN_SOCKETS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
901 if (ils > 0 && FD_ISSET(ils, &rmask))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
902 handle_internet_request(ils);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
903 #endif /* INTERNET_DOMAIN_SOCKETS */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
904
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
905 if (FD_ISSET(fileno(stdin), &rmask)) /* from stdin (gnu process) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
906 handle_response();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
907 #endif /* NOT SYSV_IPC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
908 } /* while */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
909
215
1f0dabaa0855 Import from CVS: tag r20-4b6
cvs
parents: 171
diff changeset
910 return 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
911 } /* main */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
912
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
913 #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */