home *** CD-ROM | disk | FTP | other *** search
- /*
- * uw IPC
- *
- * Copyright 1986 by John D. Bruner. All rights reserved. Permission to
- * copy this program is given provided that the copy is not sold and that
- * this copyright notice is included.
- */
-
- #include <sys/types.h>
- #include <sys/file.h>
- #include <sys/stat.h>
- #include <sys/socket.h>
- #include <sys/un.h>
- #include <sys/ioctl.h>
- #include <sys/uio.h>
- #include <netinet/in.h>
- #include <strings.h>
- #include <netdb.h>
- #include <errno.h>
- #include <stdio.h>
-
- #include "uw_param.h"
- #include "uw_err.h"
- #include "uw_opt.h"
- #include "uw_win.h"
- #include "uw_fd.h"
- #include "uw_pcl.h"
- #include "uw_ipc.h"
-
- #ifndef ntohs
- /* declaring these as one-element arrays or as NULL pointers is a HACK */
- extern unsigned long ntohl(), htonl();
- extern unsigned short ntohs(), htons();
- static struct netadj na_ntoh[1] = {
- (short (*)())ntohs, (long (*)())ntohl, ntohs, ntohl
- };
- static struct netadj na_hton[1] = {
- (short (*)())htons, (long (*)())htonl, htons, htonl
- };
- #else
- static struct netadj *na_ntoh = NULL;
- static struct netadj *na_hton = NULL;
- #endif
-
- static int have_udport;
- static char uipc_port[] = "/tmp/uwXXXXXX";
-
- static int inet_sd;
- static struct ipcmsg {
- int im_len;
- struct uwipc im_msg;
- } *inet_buf;
-
- extern int errno;
-
- ipc_init(use_uipc)
- {
- ipc_isinit();
- if (use_uipc)
- ipc_udinit();
- }
-
-
- /*
- * UNIX-domain
- */
-
- static
- ipc_udinit()
- {
- register int len;
- register char *cp;
- register fildes_t sd;
- auto struct sockaddr_un sa;
- auto char *env[2];
- extern char *mktemp();
-
- len = strlen(UIPC_ENV) + sizeof uipc_port + 1;
- if ((cp = malloc(len)) != NULL) {
- (void)sprintf(cp, "%s=%s", UIPC_ENV, mktemp(uipc_port));
- env[0] = cp;
- env[1] = (char *)0;
- env_set(env);
-
- sa.sun_family = AF_UNIX;
- (void)strncpy(sa.sun_path, uipc_port, sizeof sa.sun_path-1);
- sa.sun_path[sizeof sa.sun_path-1] = '\0';
- if ((sd = socket(AF_UNIX, SOCK_DGRAM, 0)) >= 0 &&
- bind(sd,&sa,sizeof sa.sun_family+strlen(sa.sun_path)) >= 0){
- have_udport = 1;
- (void)chmod(uipc_port, S_IREAD|S_IWRITE);
- (void)fcntl(sd, F_SETFL, FNDELAY);
- fdmap[sd].f_type = FDT_UDSOCK;
- FD_SET(sd, &selmask[0].sm_rd);
- }
- }
- }
-
- ipc_exit()
- {
- if (have_udport)
- (void)unlink(uipc_port);
- }
-
- ipc_udrecv(sd)
- register fildes_t sd;
- {
- register struct window *w;
- register int cnt;
- struct msghdr msg;
- auto int fd;
- struct iovec iov;
- struct stat st1, st2;
- union {
- struct uwipc uwip;
- char data[1024];
- } buf;
-
-
- /*
- * main() calls this routine when there is a message waiting on
- * the UNIX-domain socket. The message's access rights are
- * expected to contain the file descriptor for the "master" side
- * of a pseudo-tty. The message contains the name of the pty.
- * The sender is expected to start up a process on the slave side
- * of the pty. This allows the host end to create windows which
- * run something other than the shell.
- */
- fd = -1;
-
- iov.iov_base = (caddr_t)buf.data;
- iov.iov_len = sizeof buf - 1;
-
- msg.msg_name = (caddr_t)0;
- msg.msg_namelen = 0;
- msg.msg_iov = &iov;
- msg.msg_iovlen = 1;
- msg.msg_accrights = (caddr_t)&fd;
- msg.msg_accrightslen = sizeof fd;
-
- if ((cnt=recvmsg(sd, &msg, 0)) < 0 || cnt != buf.uwip.uwip_len)
- return;
- switch (buf.uwip.uwip_cmd) {
- case UWC_NEWT:
- if (msg.msg_accrightslen > 0 && fd >= 0) {
- /*
- * We can't trust the process which connected to us,
- * so we verify that it really passed us a pseudo-tty's
- * file descriptor by checking the device name and its
- * inode number. [Of course, if someone else wants to
- * hand us a terminal session running under their
- * uid....]
- */
- if (cnt == sizeof buf)
- cnt--;
- buf.data[cnt] = '\0';
- if (strncmp(buf.uwip.uwip_newt.uwnt_pty,
- "/dev/pty", sizeof "/dev/pty"-1) ||
- fstat(fd, &st1) < 0 ||
- stat(buf.uwip.uwip_newt.uwnt_pty, &st2) < 0 ||
- st1.st_dev != st2.st_dev ||
- st1.st_ino != st2.st_ino) {
- (void)close(fd);
- return;
- }
- /*
- * OK, we believe the sender. We allocate a window and
- * tell the Macintosh to create that window on its end.
- * If we have no fDCko1O
- buuuid...fif someone else wantsaL,
- nitcEn_pathtxp.uwT st2.st o*)0;
- I,iHT_pathluang
- We ase, iOcourse MacintosSto contain incl1hluangT N b║sg_accrighmàizeofallows the host end to creas = '\0'e buf - uwi;
-
- if (i*bufbind(sd,&sa