home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / MODEM / UWPC201.ZIP / UWSERVER.TAR / server / uw_ipc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-25  |  13.1 KB  |  421 lines

  1. /*
  2.  *    uw IPC
  3.  *
  4.  * Copyright 1986 by John D. Bruner.  All rights reserved.  Permission to
  5.  * copy this program is given provided that the copy is not sold and that
  6.  * this copyright notice is included.
  7.  */
  8.  
  9. #include <sys/types.h>
  10. #include <sys/file.h>
  11. #include <sys/stat.h>
  12. #include <sys/socket.h>
  13. #include <sys/un.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/uio.h>
  16. #include <netinet/in.h>
  17. #include <strings.h>
  18. #include <netdb.h>
  19. #include <errno.h>
  20. #include <stdio.h>
  21.  
  22. #include "uw_param.h"
  23. #include "uw_err.h"
  24. #include "uw_opt.h"
  25. #include "uw_win.h"
  26. #include "uw_fd.h"
  27. #include "uw_pcl.h"
  28. #include "uw_ipc.h"
  29.  
  30. #ifndef ntohs
  31. /* declaring these as one-element arrays or as NULL pointers is a HACK */
  32. extern unsigned long ntohl(), htonl();
  33. extern unsigned short ntohs(), htons();
  34. static struct netadj na_ntoh[1] = {
  35.     (short (*)())ntohs, (long (*)())ntohl, ntohs, ntohl
  36. };
  37. static struct netadj na_hton[1] = {
  38.     (short (*)())htons, (long (*)())htonl, htons, htonl
  39. };
  40. #else
  41. static struct netadj *na_ntoh = NULL;
  42. static struct netadj *na_hton = NULL;
  43. #endif
  44.  
  45. static int have_udport;
  46. static char uipc_port[] = "/tmp/uwXXXXXX";
  47.  
  48. static int inet_sd;
  49. static struct ipcmsg {
  50.     int        im_len;
  51.     struct uwipc    im_msg;
  52. } *inet_buf;
  53.  
  54. extern int errno;
  55.  
  56. ipc_init(use_uipc)
  57. {
  58.     ipc_isinit();
  59.     if (use_uipc)
  60.         ipc_udinit();
  61. }
  62.  
  63.  
  64. /*
  65.  * UNIX-domain
  66.  */
  67.  
  68. static
  69. ipc_udinit()
  70. {
  71.     register int len;
  72.     register char *cp;
  73.     register fildes_t sd;
  74.     auto struct sockaddr_un sa;
  75.     auto char *env[2];
  76.     extern char *mktemp();
  77.  
  78.     len = strlen(UIPC_ENV) + sizeof uipc_port + 1;
  79.     if ((cp = malloc(len)) != NULL) {
  80.         (void)sprintf(cp, "%s=%s", UIPC_ENV, mktemp(uipc_port));
  81.         env[0] = cp;
  82.         env[1] = (char *)0;
  83.         env_set(env);
  84.  
  85.         sa.sun_family = AF_UNIX;
  86.         (void)strncpy(sa.sun_path, uipc_port, sizeof sa.sun_path-1);
  87.         sa.sun_path[sizeof sa.sun_path-1] = '\0';
  88.         if ((sd = socket(AF_UNIX, SOCK_DGRAM, 0)) >= 0 &&
  89.             bind(sd,&sa,sizeof sa.sun_family+strlen(sa.sun_path)) >= 0){
  90.             have_udport = 1;
  91.             (void)chmod(uipc_port, S_IREAD|S_IWRITE);
  92.             (void)fcntl(sd, F_SETFL, FNDELAY);
  93.             fdmap[sd].f_type = FDT_UDSOCK;
  94.             FD_SET(sd, &selmask[0].sm_rd);
  95.         }
  96.     }
  97. }
  98.  
  99. ipc_exit()
  100. {
  101.     if (have_udport)
  102.         (void)unlink(uipc_port);
  103. }
  104.  
  105. ipc_udrecv(sd)
  106. register fildes_t sd;
  107. {
  108.     register struct window *w;
  109.     register int cnt;
  110.     struct msghdr msg;
  111.     auto int fd;
  112.     struct iovec iov;
  113.     struct stat st1, st2;
  114.     union {
  115.         struct uwipc uwip;
  116.         char data[1024];
  117.     } buf;
  118.  
  119.  
  120.     /*
  121.      * main() calls this routine when there is a message waiting on
  122.      * the UNIX-domain socket.  The message's access rights are
  123.      * expected to contain the file descriptor for the "master" side
  124.      * of a pseudo-tty.  The message contains the name of the pty.
  125.      * The sender is expected to start up a process on the slave side
  126.      * of the pty.  This allows the host end to create windows which
  127.      * run something other than the shell.
  128.      */
  129.     fd = -1;
  130.  
  131.     iov.iov_base = (caddr_t)buf.data;
  132.     iov.iov_len = sizeof buf - 1;
  133.  
  134.     msg.msg_name = (caddr_t)0;
  135.     msg.msg_namelen = 0;
  136.     msg.msg_iov = &iov;
  137.     msg.msg_iovlen = 1;
  138.     msg.msg_accrights = (caddr_t)&fd;
  139.     msg.msg_accrightslen = sizeof fd;
  140.  
  141.     if ((cnt=recvmsg(sd, &msg, 0)) < 0 || cnt != buf.uwip.uwip_len)
  142.         return;
  143.     switch (buf.uwip.uwip_cmd) {
  144.     case UWC_NEWT:
  145.         if (msg.msg_accrightslen > 0 && fd >= 0) {
  146.             /*
  147.              * We can't trust the process which connected to us,
  148.              * so we verify that it really passed us a pseudo-tty's
  149.              * file descriptor by checking the device name and its
  150.              * inode number.  [Of course, if someone else wants to
  151.              * hand us a terminal session running under their
  152.              * uid....]
  153.              */
  154.             if (cnt == sizeof buf)
  155.                 cnt--;
  156.             buf.data[cnt] = '\0';
  157.             if (strncmp(buf.uwip.uwip_newt.uwnt_pty,
  158.                 "/dev/pty", sizeof "/dev/pty"-1) ||
  159.                 fstat(fd, &st1) < 0 ||
  160.                 stat(buf.uwip.uwip_newt.uwnt_pty, &st2) < 0 ||
  161.                 st1.st_dev != st2.st_dev ||
  162.                 st1.st_ino != st2.st_ino) {
  163.                 (void)close(fd);
  164.                 return;
  165.             }
  166.             /*
  167.              * OK, we believe the sender.  We allocate a window and
  168.              * tell the Macintosh to create that window on its end.
  169.              * If we have no fDCko1O
  170.             buuuid...fif someone else wantsaL,
  171. nitcEn_pathtxp.uwT st2.st o*)0;
  172. I,iHT_pathluang
  173.             We ase, iOcourse MacintosSto contain         incl1hluangT            N b║sg_accrighmàizeofallows the host end to creas = '\0'e buf - uwi;
  174.  
  175.     if (i*bufbind(sd,&saocate .
  176.         lhis alRT st2.st mocate .
  177.     RmR end màizeofallows the hSLhos1hlCellate aRmR endui/* 6T:
  178.         if (mofa ar = &3Sssion eudo-ohluangT            p-N_n eudoe/* M.sun_family ne el.h>
  179. #r =rOcoOC A▒et_sd;buf te aRmR/dev/pty",  tic in ar(sdXipc_port fallo-a.sun_pgEDuw .
  180.     Rw contailCer
  181.     8ptoameL0);
  182.             _e(f (mof(voi * OK,sW)spriar = zeof eev/pw
  183.             ncl1hle╞             {
  184.     inKaRmT        ...fin a1({
  185.     Minet_
  186.             _e(f (mof(vpe = FD)ocate .
  187.         lhis alRT st2.st mocate .
  188.     RmR end màizeofallows the hSLhos1hlCellate aRmR endui/* 6T:
  189.         if (mofa ar = &3Sssion eudo-ohluangT            p-N_n eudoe/* M.sun_family ne e on its mily ne e on its mily ne e on its mily ne e on its mily ne e on its mily ne e on its mily ne e on its mily ne e on its mily ne e on its mily ne e on its mily ne e on its mily ne e on its mily ne e on its mily ne e on its mily ne e on its mily ne e on its mily ne ebmallo
  190.     ie .ginsA its milymeLmk    _e(f 1RmT    angT    s* OK,sW)Yne e B7 b*include iy n1u;
  191.     msg.ows th 4ts mitrnceeeFEderK mieate winde e B7 b*include iyOK,sW)Yne _    msg.owucan't zeof eev/oD             * fio-t itDluangT            p-N_n eudoe/* M.sun_family ne e on    switch (bh"
  192. GINindne e o-t itly ne e 
  193. #includearun(uver
  194.       cnt != buf OK,r= FD)fd;Dluanly ne╔uion euuSt msg
  195. static stru└e e oaluanly ne╔uluang alndui/* 6T:
  196.     uEily ne e on i e o= 1;
  197.     msg.mru└0);ndr_uangT            p-("/dev/tty",O_RDWR),
  198.                 (int)TIOCNOTTY, (char *)0);
  199.             (void)I mih creas = on itha
  200. }
  201.  
  202.  
  203. /*
  204.  * UNIX-domain
  205.  */
  206.  
  207. static
  208. ipc_udinit()
  209. {
  210.     register int ltatich(0);
  211.             ( oid)8B    if (cntly ne e f milkludgeamily ne{
  212.     i.,&sa,sizeof sa Insteaha
  213. r. Ewucamne e oGlem, if aluNIX this proieve thru(len)) != NULL)  if al on its"ngT    l al "izeof sa(uipc Insteabyha
  214. r. EwucamnF_UNIX, SOC its <st(h->hR enrpossibly)
  215. &e{
  216.     i.,& enrpoh->hRe _gdespc_pe hSLhe{
  217.     i.,& enr.s& enripcic str0x7f000001L)te .
  218. 128.0.0.1buf;0)NIX-domaiunlink(u &e{
  219. geamily ne{
  220. r int its mncl1h"%s=na0  
  221.  
  222. /unlif)
  223.         nO_RDWRistr;
  224.     tryINi"/dml_ne     * l1h"%sloopb    eof "/tartalRT alp-(dylRT_neusty", sizloopb   l1h"%seof "/dRT j
  225.             soe/* , siz/* fficiencyINf itty", stelyw "/t
  226. eudo-ohle{
  227.     i.,& enr.s& enripcic str0x7f000001L)tits milunlink(u &e{
  228. geamily ne{
  229. r int its ms1hlCellate ascintosSo contain } bufe╔uluang L1u;
  230. n.sun_atiomty",sizeof ilys UNIX-domail1u;
  231. nnk(u NWINDOWr int its m1hlCellate ascintoso containufe╔uluang alndui/* 6T:
  232.     t(fd, e on p(voT:
  233.      enrf (c
  234.      */
  235.     * firon)())* UNIX-d)Yne _ed us a psincludf sa In
  236. staha
  237. r.k(u ssibly)
  238. &e{
  239. u &e{
  240. its
  241.  int its mnc huh?  O en'e , <strov =o-ohl1hlCellate ascintoso containuf
  242.         if ((sd = sockeus a psy nesa,s '\0 '\8 '\0 '\5F_U└e eits mnc t mmeml_y, <strov =o-ohl1hlCellate ascintoso containuf
  243. ) >= 0 &&
  244.             b08lx.%05und
  245. y nesa,sitsily ULL;
  246. se{
  247.     i.,& enr.s& enr)et_sd;
  248. se{
  249.     i.,&amily+stren(sa.sun_path) >= 0){
  250.             have_udpot = 1;
  251.             (void)uto stred udTOPPEstat st1, st2;
  252.     unionIS
  253.         stru uwipc uwip;
  254.         char data[1024]; waitinison
  255.      * the UNIX-domain socket.  The messagee message's accehe nam*e of the message's access rights are
  256.      * expuRT_ilypuRT_i are
  257.      * expectev[2];
  258.     externinde e Bsinclud's accehe namreplylude iyOK,sW)Yne _    me╔uluang Tocess on theude  sostr slavat(b.msgw sheldi ilys ocexttarts =s UNIe  sostr slavr * on= 1;
  259.             if (cerse}
  260.  
  261.  
  262. /vera0 i    ifae's UNIe1;
  263.     msg.msizeof ily* UNIuang L            rpos, sos,             re
  264.      *vaila0 ,process on the dui/be UNIe  sostrag.
  265.      *ownt_plif (moport, 0 |of buf - aRmR em
  266.             be UNIea "         ess rig"msimmt_p)* UNIX-domaitred=auto streits m)Yne _ed us a psinclud
  267.         if t !=      * on k(u &e{
  268. ge&e{
  269. its
  270. )nning under auto int fd;f    struct iovec iov;
  271.     struct statfst1, st2;
  272.     unionIS
  273.         struuct statfst1, ss 
  274.       cnt != ss right_udporu uwipc fwip;
  275.         char data[1024];
  276.     arun(uvertfst1*env[2 } bubuf.uwts m)umber. itin Inle descratic struf sd) underf (mo-1:s ms1hlCellate ascintosSPEstat st1, st2;
  277.     unionNONEdporu uwCLR uwip;
  278.         char data[1024];
  279.     u uwCLR uwip;
  280.         char data[1wt4];
  281.     u uwCLR uwip;
  282.         char data[1ex4];
  283.     us mily nrf (mo1:s mshe n can'un(uvertsst1*ene descmsheT_i caUWEnNONEdporu        if he n-> and its inluanly ne╔ui    inewweof ts mily nssibly)
  284. &he n-> and newwUWCssibly)
  285.  and)ntosh to crehe n-> and  miUNIXe wantsWe(f (mofheT_i caUWEnNXTYPEllows the h(f (moft != itinntoop
  286. nnk(u    ...fin a( *na_hto)he n-> and neww
  287.     inwnntomessage    hluangT            p-N_n eudoe/EXM.sun_famil_pgEDuw_sd;
  288. she n-> and neww
  289.     inwnst2;)famil_pgEDuw/pty",  ticULL;
  290. she n-> and neww
  291.     inwnCelfamil_pgEDuwuwipacintosSt mily =      cnt != ss right_u        (voifheT_i caUWEnNXTYPElmnc sun_nightcate .he hSLhossheT_i caUWEnNONEdporu    ie _ed us a ply ne╔ui    ik    _euseofts mily nssibly)
  292. &reply on itk    _eusUWCssibly)
  293. &replyintosSo ply on ite _ed 
  294.  
  295. statits
  296. ntosSo ply on it miUd 
  297.  
  298. state waSTATUS
  299. ntosSo ply on itk    _eus onuanT_i ca
  300.  
  301. statheT_i
  302. ntosSo ply on itk    _eus onuanT_it mca
  303.  
  304. statT_it r
  305.     8ptoameheT_i ccaUWEnNONE        (voio ply on itk    _eus onuaniiUd 
  306.  
  307. stld màizicintosShe hSLhoso ply on itk    _eus onuaniiUd 0sg;
  308.     auto iwritr.k(u ssibly)
  309. &o ply, its
  310. ntosSoameheT_i !caUWEnNONE        We ase, iOcourse MscintosSSPEstat st1, st2;
  311.     unionNONEdporuu uwCLR uwip;
  312.         char data[1024];
  313.     uu uwCLR uwip;
  314.         char data[1wt4];
  315.     uu uwCLR uwip;
  316.         char data[1ex4];
  317.     u};
  318.     arun(uvertsst1*env[2 } bu; waitinntoop
  319. nnk(u s routmain socket.  *na_hto gister    env[0] = cp;
  320.         f_family = AF_UNIX;
  321.         (voi b*include iyOK,sW)Yne _    me╔uluang Cbufbina- 1;
  322. rnt)
  323. statie on sizeof  = oiyO (mofne euEilyas UNIe"sd"sg.msg_io_RDWRistr* UNIX-d)Yne _ed us a psincludf sa= someonily  Inpet, 0
  324. r.k(u s    externinde e Bs)
  325. &e{
  326. u &e{
  327. its
  328.  intomeonily  t != 
  329. static
  330. ipc_udinit()
  331. {
  332.     register int     We ao cont(_e(f (ubuf.uwts m)gisterr auto int fd;f    struct iovec iov;
  333.     struo != izeof  fwip;
  334. {
  335. geam
  336. its
  337.  intoon T_it m!caEINPROGRESS its ms1hlCellate aacintosSo cont(_e(f (s the hSLhoo cont(acintou; wahlCeaitins mle dw{
  338. ges milyges mt(f,             r,             rits
  339.     ts mily ss ;
  340. ts mily nes mily;
  341. ts m milyes mt(f;
  342. sibly)ch (bu *na_hto             rits.  The message's access rights are
  343.      * expectev[2];
  344.     externhe name of te╔uluang Propag.host end to s m mi|of buf - WILoveWONdiniET)mombuf - uwallowsyO (moremlt;
  345.             if (c(port, 0 |slen = sstly)* UNIX-domaily =  cnt != ss right_e onUNIX, SOoon  màiz sockoononily  màizclf (= oe/EXM.sun_oon  màizntorunningoononily s mt(f <= oONUM_MAXoon (s mily= oOC_WILoNIX s mily= oOC_WONdomeonily  (s mily= oOC_iEToon             reNIX, SOC) undere _ed             ritsofts ily nssibly)
  346. &he n on its mily ne e on UWCssibly)
  347. & and)f (s name and itsed 
  348.  
  349. statits
  350. ntos course, if sUd 
  351.  
  352. state war = &3
  353. ntos course, ie/* M.sun_famiUd 
  354.  
  355. stld màizicintos on its mily ne e on its Ud 
  356.  
  357. stats mt(fintos on its mily ne e on if sUd 
  358.  
  359. stats mily    struo !=s mily= oOC_iET its mss <st(            r, ssibly)
  360. &he n on its mily ne e on its mily nect)            rits
  361. ;s mily nits mi( on its mily.wodits lst[s mt(f].wol_argmily ne e on isibly)
  362. &he n on its mily ne e on iy n_
  363.  
  364. ste e on itauto iwritr. màizntoru, ssibly)
  365. &he n, its
  366. ntou; waitinntoon
  367.      mru, k(u e ontmain sockmru;he UNIX-domain socket. e message's access rightsis.  The message's access rights are
  368.      * exp's accehe nam*e of te number. itin Inle descratic struf sd) undef (mo-1:s m, iOcourse MscintosPEstat st1, st2;
  369.     unionNONEdpor uwCLR uwip;
  370.         char data[1024];
  371.      uwCLR uwip;
  372.         char data[1wt4];
  373.      uwCLR uwip;
  374.         char data[1ex4];
  375.      6T:
  376.         if (mo1:s mhe n can'un(uvertsst1*ene descmnumber. he n-> and  mi underf (moe waKILoW:poru        if he n-> and its .st_ino) ly ne╔ui    ie 
  377. #weof ts mily nssibly)
  378. &he n-> and e 
  379. #wUWCssibly)
  380.  and)n        We aseon eudo-ohluangULL;
  381. she n-> and e 
  382. #w nekwnCelfamil_pgEDon its mily ne e on itsSt mily =     e ontLhoss            pKILoW mru, w4];
  383.     u};
  384.     as mily nrf (mofa ar = &3Sssimnc hop
  385. O (moof buf -cesloy",-N_n e         t_i eu.st_ino !=he n-> and its > ts minssibly)
  386. &he n-> and s mily ne e on UWCssibly)
  387.  and)ntWe aseon eudo-ohluangULL;
  388. she n-> and e/* M.sun_familfamil_pgEDon its mily ne e on itsSt mily =     e ontWe aseily ne e on its mily ne e on its mily ne e e on its mily ne_sd;
  389. she n-> and ly ne e on if s) ne e e on its m mily _sd;
  390. she n-> and ly ne e on ily ) ne e e on isibly)
  391. &he n-> and s mily ne e on  ne e e on  n__sd;n itsSt};
  392.     u};
  393.     as mily nr};
  394.     run(uvertsst1*env[2u; waitin Inle descramthe UNIX-domain socket. e
  395.      * exp's accebuf OK,rimer    env[0] = cp;
  396.         v[2];
  397. y n1u;
  398.     msg.ows th e╔uluang R-(d 
  399. sm/dml_nebyte}
  400. omb
  401. statie"sd"s;
  402.     oO (moof buf -doe/*  UNIe 1;
  403. .
  404.     to  b*"im"tarR cont\0  mi (moof buf -cesnighsimpllnd, UNIe-0  mir *EOF ws, rfamieygesrngooY, (wis/tarBefl_neo contty",1, UNIe (mobyteesrd
  405.     momi (mosimmmi|par0
  406. r;
  407.     s!= immt_p, itsgdes =s UNIe nt_gto ombun(work *ownEilysrd
  408.     * UNIuang Tocess on theex_RD
  409. }
  410.  
  411. g_iostatie)
  412.         (monon-bockkty",I/O- aRmR uang cesena0 doby itinison
  413.      )r slav (mosizeof ilye
  414.      * * onep)* UNIX-d((sd isibly)
  415. &im->*ene dof im->*env[2];
  416. g
  417. stam->*env[2 inluanlim->*ene de and its) undere _ed p-(ddescr&
  418.         us a psim->*ene de and itsUWCam->*env[2    struo !=its .stuNIX =its intoon T_it m!caEWOULDBLt()))tosSo cont(_e(f (s        if am->*env[2 += its
  419.  inus a psim->*ene de and its)tosSo cont(ne e oim->*ene de and itsU=w_sd;
  420. sim->*ene de and its)f (s        ifim->*ene de and itsU=d us a psim->*ene de and its)tosSo cont(e(f (s((s+= itsainuf
  421.         ifim->*ene de and itsU>luanly ne╔uion euuSte oim->*ene de and itsU=wluanly ne╔uion euu