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