home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1621 / sock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  5.3 KB  |  332 lines

  1. /* Copyright 1990, Daniel J. Bernstein. All rights reserved. */
  2.  
  3. #include "config.h"
  4. #include <sys/types.h>
  5. #include <sys/time.h>
  6. #ifndef NO_UNIXSOCKS
  7. #include <sys/socket.h>
  8. #include <sys/un.h>
  9. #ifndef NO_FDPASSING
  10. #include <sys/uio.h>
  11. #endif
  12. #include <stdio.h>
  13. #include <strings.h>
  14. #endif
  15. #include "sock.h"
  16. #include "tty.h"
  17. #include "err.h"
  18.  
  19. static int bufwrite(fd,buf,num)
  20. int fd;
  21. char *buf;
  22. int num;
  23. {
  24.  int r;
  25.  
  26.  do
  27.   {
  28.    r = write(fd,buf,num);
  29.    if (r > 0)
  30.     {
  31.      buf += r;
  32.      num -= r;
  33.     }
  34.   }
  35.  while ((num > 0) && ((r != -1) || (errno == EINTR)));
  36.  return (r >= 0) ? 0 : -1;
  37. }
  38.  
  39. static int bufread(fd,buf,num)
  40. int fd;
  41. char *buf;
  42. int num;
  43. {
  44.  int r;
  45.  
  46.  do
  47.   {
  48.    r = read(fd,buf,num);
  49.    if (r > 0)
  50.     {
  51.      buf += r;
  52.      num -= r;
  53.     }
  54.   }
  55.  while ((num > 0) && ((r != -1) || (errno == EINTR)));
  56.  /* Note that we ignore EOF. */
  57.  return (r >= 0) ? 0 : -1;
  58. }
  59.  
  60. int pty_readsock(line,path)
  61. char *line;
  62. char *path;
  63. {
  64. #ifdef NO_UNIXSOCKS
  65.  return -1;
  66. #else
  67.  int s;
  68.  struct sockaddr_un sa;
  69.  
  70.  if ((s = socket(AF_UNIX,SOCK_STREAM,0)) == -1)
  71.    return -1;
  72.  sa.sun_family = AF_UNIX;
  73.  (void) sprintf(sa.sun_path,"re.%s",line + sizeof(DEVSTY) - 3);
  74.  (void) strcpy(path,sa.sun_path);
  75.  (void) unlink(sa.sun_path);
  76.  if (bind(s,(struct sockaddr *) &sa,strlen(sa.sun_path) + 2) == -1)
  77.    return -1;
  78.  if (listen(s,5) == -1)
  79.    return -1;
  80.  return s;
  81. #endif
  82. }
  83.  
  84. int pty_writesock(line)
  85. char *line;
  86. {
  87. #ifdef NO_UNIXSOCKS
  88.  return -1;
  89. #else
  90.  int s;
  91.  struct sockaddr_un sa;
  92.  
  93.  if ((s = socket(AF_UNIX,SOCK_STREAM,0)) == -1)
  94.    return -1;
  95.  sa.sun_family = AF_UNIX;
  96.  (void) sprintf(sa.sun_path,"re.%s",line + sizeof(DEVSTY) - 3);
  97.  if (connect(s,(struct sockaddr *) &sa,strlen(sa.sun_path) + 2) == -1)
  98.    return -1;
  99.  (void) unlink(sa.sun_path);
  100.  return s;
  101. #endif
  102. }
  103.  
  104. int pty_acceptsock(fd)
  105. int fd;
  106. {
  107. #ifdef NO_UNIXSOCKS
  108.  return -1;
  109. #else
  110.  struct sockaddr_un sa;
  111.  int salen = sizeof(sa);
  112.  
  113.  return accept(fd,(struct sockaddr *) &sa,&salen);
  114. #endif
  115. }
  116.  
  117. int pty_putgetonefd(fd,fp)
  118. int fd;
  119. int *fp;
  120. {
  121. #ifdef NO_FDPASSING
  122.  return -1;
  123. #else
  124.  struct msghdr msg[2];
  125.  int acc[5];
  126.  struct iovec i[2];
  127.  
  128.  msg[0].msg_name = 0;
  129.  msg[0].msg_namelen = 0;
  130.  msg[0].msg_iov = &i[0]; /* grrrr */
  131.  msg[0].msg_iovlen = 0;
  132.  msg[0].msg_accrights = (caddr_t) acc;
  133.  msg[0].msg_accrightslen = 5 * sizeof(int);
  134. #ifdef USLEEP
  135.  (void) usleep((unsigned) 100000);
  136. #else
  137.  (void) sleep(1); /* XXX: work around fd passing bug */
  138. #endif
  139.  if (recvmsg(fd,msg,0) == -1)
  140.    return -1;
  141.  if (msg[0].msg_accrightslen != sizeof(int))
  142.    return -1;
  143.  if (*fp != -1)
  144.    (void) close(*fp);
  145.  *fp = acc[0]; /* yay! we've passed a file descriptor! */
  146.  return 0;
  147. #endif
  148. }
  149.  
  150. int pty_putgetfd(fd,ch,fp)
  151. int fd;
  152. char ch;
  153. int *fp;
  154. {
  155. #ifdef NO_FDPASSING
  156.  return -1;
  157. #else
  158.  if (bufwrite(fd,&ch,1) < 0)
  159.    return -1;
  160.  if (pty_putgetonefd(fd,fp) < 0)
  161.    return -1;
  162.  if (bufwrite(fd,".",1) < 0)
  163.    return -1;
  164.  return 0;
  165. #endif
  166. }
  167.  
  168. int pty_putgetint(fd,ch,ip)
  169. int fd;
  170. char ch;
  171. int *ip;
  172. {
  173.  if (bufwrite(fd,&ch,1) < 0)
  174.    return -1;
  175.  if (bufread(fd,(char *) ip,sizeof(int)) < 0)
  176.    return -1;
  177.  if (bufwrite(fd,".",1) < 0)
  178.    return -1;
  179.  return 0;
  180. }
  181.  
  182. int pty_putgetstr(fd,ch,str)
  183. int fd;
  184. char ch;
  185. char str[TTYNAMELEN];
  186. {
  187.  if (bufwrite(fd,&ch,1) < 0)
  188.    return -1;
  189.  if (bufread(fd,str,TTYNAMELEN) < 0)
  190.    return -1;
  191.  if (bufwrite(fd,".",1) < 0)
  192.    return -1;
  193.  return 0;
  194. }
  195.  
  196. int pty_putgettty(fd,ch,tmo)
  197. int fd;
  198. char ch;
  199. struct ttymodes *tmo;
  200. {
  201.  if (bufwrite(fd,&ch,1) < 0)
  202.    return -1;
  203.  if (bufread(fd,(char *) tmo,sizeof(struct ttymodes)) < 0)
  204.    return -1;
  205.  if (bufwrite(fd,".",1) < 0)
  206.    return -1;
  207.  return 0;
  208. }
  209.  
  210. int pty_sendonefd(fd,fp)
  211. int fd;
  212. int *fp;
  213. {
  214. #ifdef NO_FDPASSING
  215.  return -1;
  216. #else
  217.  struct msghdr msg[2];
  218.  int acc[5]; /* or just 5? or just 1? who cares */
  219.  struct iovec i[2];
  220.  
  221.  msg[0].msg_name = 0;
  222.  msg[0].msg_namelen = 0;
  223.  msg[0].msg_iov = i; /* grrrr */
  224.  msg[0].msg_iovlen = 0;
  225.  msg[0].msg_accrights = (caddr_t) acc;
  226.  msg[0].msg_accrightslen = sizeof(int);
  227.  acc[0] = *fp;
  228.  if (sendmsg(fd,&msg[0],0) == -1)
  229.    return -1;
  230.  /* yay! we've passed a file descriptor! */
  231.  return 0;
  232. #endif
  233. }
  234.  
  235. int pty_sendfd(fd,ch,fp)
  236. int fd;
  237. char ch;
  238. int *fp;
  239. {
  240. #ifdef NO_FDPASSING
  241.  return -1;
  242. #else
  243.  if (bufwrite(fd,&ch,1) < 0)
  244.    return -1;
  245.  if (bufread(fd,&ch,1) < 0)
  246.    return -1;
  247.  if (ch == ' ')
  248.    return 1;
  249.  if (pty_sendonefd(fd,fp) < 0)
  250.    return -1;
  251.  if (bufread(fd,&ch,1) < 0)
  252.    return -1;
  253.  if (ch != '.')
  254.    return 1;
  255.  return 0;
  256. #endif
  257. }
  258.  
  259. int pty_sendint(fd,ch,ip)
  260. int fd;
  261. char ch;
  262. int *ip;
  263. {
  264.  if (bufwrite(fd,&ch,1) < 0)
  265.    return -1;
  266.  if (bufread(fd,&ch,1) < 0)
  267.    return -1;
  268.  if (ch == ' ')
  269.    return 1;
  270.  if (bufwrite(fd,(char *) ip,sizeof(int)) < 0)
  271.    return -1;
  272.  if (bufread(fd,&ch,1) < 0)
  273.    return -1;
  274.  if (ch != '.')
  275.    return 1;
  276.  return 0;
  277. }
  278.  
  279. int pty_sendstr(fd,ch,str)
  280. int fd;
  281. char ch;
  282. char str[TTYNAMELEN];
  283. {
  284.  if (bufwrite(fd,&ch,1) < 0)
  285.    return -1;
  286.  if (bufread(fd,&ch,1) < 0)
  287.    return -1;
  288.  if (ch == ' ')
  289.    return 1;
  290.  if (bufwrite(fd,str,TTYNAMELEN) < 0)
  291.    return -1;
  292.  if (bufread(fd,&ch,1) < 0)
  293.    return -1;
  294.  if (ch != '.')
  295.    return 1;
  296.  return 0;
  297. }
  298.  
  299. int pty_sendtty(fd,ch,tmo)
  300. int fd;
  301. char ch;
  302. struct ttymodes *tmo;
  303. {
  304.  if (bufwrite(fd,&ch,1) < 0)
  305.    return -1;
  306.  if (bufread(fd,&ch,1) < 0)
  307.    return -1;
  308.  if (ch == ' ')
  309.    return 1;
  310.  if (bufwrite(fd,(char *) tmo,sizeof(struct ttymodes)) < 0)
  311.    return -1;
  312.  if (bufread(fd,&ch,1) < 0)
  313.    return -1;
  314.  if (ch != '.')
  315.    return 1;
  316.  return 0;
  317. }
  318.  
  319. int pty_putch(fd,ch)
  320. int fd;
  321. char *ch;
  322. {
  323.  return bufwrite(fd,ch,1);
  324. }
  325.  
  326. int pty_getch(fd,ch)
  327. int fd;
  328. char *ch;
  329. {
  330.  return bufread(fd,ch,1);
  331. }
  332.