home *** CD-ROM | disk | FTP | other *** search
/ Boot Disc 8 / boot-disc-1997-04.iso / PDA_Soft / Psion / comms / p3nfs / nfsd / mp_seria.c < prev    next >
C/C++ Source or Header  |  1996-03-30  |  6KB  |  265 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <unistd.h>    /* for usleep() */
  4. #include <string.h>    /* for bzero() */
  5. #include <termios.h>
  6. #if defined(linux) || defined(_IBMR2)
  7. # include <sys/ioctl.h>    /* for ioctl() */
  8. #endif
  9. #include <sys/errno.h>
  10. #ifdef sun
  11. # include <sys/ttold.h>    /* sun has TIOCEXCL there */
  12. #endif
  13. #if defined (__svr4__)
  14. #include <stdlib.h>
  15. #endif
  16.  
  17. #include "nfs_prot.h"
  18. #include "mp.h"
  19. #include "config.h"
  20.  
  21. extern int debug, dowakeup, errno;
  22.  
  23. #ifndef hpux
  24. # define mflag int
  25. # define getline(fd, z) if(ioctl(fd,TIOCMGET,(caddr_t)&z)<0){perror("TCGET");return 0;}
  26. # define PSION_ALIVE(z) (z & (TIOCM_DSR | TIOCM_CD | TIOCM_CTS))
  27. # define dtr(fd, set) { int d = TIOCM_DTR;\
  28.         if(ioctl(fd, set ? TIOCMBIS : TIOCMBIC, (caddr_t)&d) < 0) {\
  29.         perror("TIOCMBIC/S"); return 0; } }
  30. #endif /* !hpux */
  31.  
  32. /*
  33. #if !defined(CRTSCTS) && defined(_IBMR2)
  34. #define CRTSCTS 0x80000000
  35. #endif
  36. */
  37.  
  38. #ifdef __sgi
  39. #define CRTSCTS CNEW_RTSCTS
  40. #endif
  41.  
  42. #ifdef hpux
  43. # include <sys/termiox.h>
  44. # include <sys/modem.h>
  45.  
  46. # define getline(fd, z) if(ioctl(fd,MCGETA,&z)<0) {perror("MCGETA");return 0;}
  47. # define PSION_ALIVE(z) (z & (MDSR | MDCD | MCTS))
  48. # define dtr(fd, set) { if (set) z |= MDTR; else z &= ~MDTR;\
  49.                   if(ioctl(fd, MCSETA, &z) < 0) {\
  50.             perror("MCSETA"); return 0; } }
  51. #endif
  52.  
  53. #ifndef O_NOCTTY
  54. # define O_NOCTTY 0
  55. #endif
  56.  
  57.  
  58. /* returns 1 if OK. */
  59. int 
  60. fd_is_still_alive(fd, wake)
  61. int fd, wake;
  62. {
  63.   int w;
  64.   mflag z;
  65.  
  66.   if(debug > 2) { printf("fd_is_alive: check lines "); fflush(stdout);}
  67.   getline(fd, z)
  68.  
  69.   if(dowakeup)
  70.     {
  71.       if(debug > 2) printf("fd_is_alive: %s dtr\n", wake ? "set" : "clear");
  72.       dtr(fd, wake);
  73.  
  74.       if(! PSION_ALIVE(z) && wake)
  75.     {
  76.       if(debug > 1) printf("Trying to wake psion\n");
  77.       /* wake up psion by raising DTR */
  78.       dtr(fd, 1);
  79.  
  80.       w = 10;
  81.       do
  82.         {
  83.           if(w < 10)
  84.         usleep(100000);
  85.           getline(fd, z)
  86.         }
  87.       while(! PSION_ALIVE(z) && --w);
  88.       
  89.       if(debug > 1)
  90.         printf("Is %sawake (%d tries left)\n",PSION_ALIVE(z)?"":"not ",w);
  91.     }
  92.     }
  93.  
  94.   if(debug > 2) printf("fd_is_alive: %d\n", PSION_ALIVE(z));
  95.   return PSION_ALIVE(z);
  96. }
  97.  
  98. int
  99. init_serial(dev, speed)
  100.   char *dev;
  101.   int speed;
  102. {
  103.   int fd, baud;
  104.   struct termios ti;
  105. #ifdef hpux
  106.   struct termiox tx;
  107. #endif
  108.   static struct baud { int speed, baud; } btable[] =
  109.     {
  110.       { 9600,    B9600},
  111. #ifdef B19200
  112.       { 19200,    B19200},
  113. #else
  114. # ifdef EXTA
  115.       { 19200,    EXTA},
  116. # endif
  117. #endif
  118. #ifdef B38400
  119.       { 38400,    B38400},
  120. #else
  121. # ifdef EXTB
  122.       { 38400,    EXTB},
  123. # endif
  124. #endif
  125.       { 4800,    B4800},
  126.       { 2400,    B2400},
  127.       { 1200,    B1200},
  128.       { 300,     B300},
  129.       { 75,    B75},
  130.       { 50,    B50},
  131.       {0,0}
  132.     }, *bptr;
  133.   
  134.   if(speed)
  135.     {
  136.       for (bptr = btable; bptr->speed; bptr++)
  137.     if (bptr->speed == speed)
  138.       break;
  139.       if (!bptr->baud)
  140.     {
  141.       fprintf(stderr, "Cannot match selected speed %d\n", speed);
  142.       exit(1);
  143.     }
  144.       baud = bptr->baud;
  145.     }
  146.   else
  147.     {
  148.       baud = 0;
  149.     }
  150.  
  151.   if(background)
  152.     {
  153.       int uid, euid;
  154.  
  155.       if(debug) printf("using %s...\n", dev);
  156.       euid = geteuid();
  157.       uid = getuid();
  158.       if (seteuid(uid))
  159.     {
  160.       perror("seteuid"); exit(1);
  161.     }
  162.       if((fd = open(dev, O_RDWR | O_NDELAY | O_NOCTTY , 0)) < 0)
  163.     {
  164.       perror(dev); exit(1);
  165.     }
  166.       if (seteuid(euid))
  167.     {
  168.       perror("seteuid back"); exit(1);
  169.     }
  170.       if(debug) printf("open done\n");
  171. #ifdef TIOCEXCL
  172.       ioctl(fd, TIOCEXCL, (char *) 0);    /* additional open() calls shall fail */
  173. #else
  174.       fprintf(stderr, "WARNING: opened %s non-exclusive!\n", dev);
  175. #endif
  176.     }
  177.   else
  178.     {
  179.       char *f;
  180.  
  181.       fd = dup(0);
  182.  
  183.       if(tcgetattr(fd, &ti) < 0)
  184.         {
  185.       perror("tcgetattr serial");
  186.       exit(1);
  187.     }
  188.       baud = cfgetospeed(&ti);
  189.       close(0);                /* Let's close 0,1,2 */
  190.       f = "/dev/null";
  191.       if(open(f, O_RDONLY, 0) != 0)
  192.         {
  193.       perror(f); exit(0);
  194.     }
  195.       close(1);
  196.       f = "/tmp/p3nfsd.out";
  197.       if(open(f, O_WRONLY|O_CREAT|O_APPEND, 0666) != 1)
  198.         {
  199.       perror(f); exit(0);
  200.     }
  201.       fprintf(stderr, "p3nfsd: output written to %s\n", f);
  202.       close(2);
  203.       dup(1);
  204.     }
  205.  
  206.   bzero((char *)&ti, sizeof(struct termios));
  207. #if defined(hpux) || defined(_IBMR2)
  208.   ti.c_cflag = CS8 | HUPCL | CLOCAL | CREAD;
  209. #endif
  210. #if defined(sun) || defined(linux) || defined(__sgi)
  211.   ti.c_cflag = CS8 | HUPCL | CLOCAL | CRTSCTS | CREAD;
  212.   ti.c_iflag = IGNBRK | IGNPAR;
  213.   ti.c_cc[VMIN] = 1;
  214.   ti.c_cc[VTIME] = 0;
  215. #endif
  216.   cfsetispeed(&ti, baud);
  217.   cfsetospeed(&ti, baud);
  218.  
  219.   if(tcsetattr(fd, TCSADRAIN, &ti) < 0)
  220.     perror("tcsetattr TCSANOW");
  221.  
  222. #ifdef hpux
  223.   bzero(&tx, sizeof(struct termiox));
  224.   tx.x_hflag = RTSXOFF | CTSXON;
  225.   if (ioctl(fd, TCSETXW, &tx) < 0)
  226.     perror("TCSETXW");
  227. #endif
  228.  
  229. #if defined(_IBMR2)
  230.   ioctl(fd, TXDELCD, "dtr");
  231.   ioctl(fd, TXDELCD, "xon");
  232.   ioctl(fd, TXADDCD, "rts");  /* That's how AIX does CRTSCTS */
  233. #endif
  234.  
  235.   return fd;
  236. }
  237.  
  238. #if 0
  239. #ifdef sun
  240. /*
  241.  * Before exiting we should close the serial line file descriptor
  242.  * as we are probably on a "wicked sun", we have to reset the CRTSCTS
  243.  * flag first, otherwise we will hang in close(2).
  244.  * (suns that are not "wicked" are "broken" , i.e.: they do not have
  245.  * patch 100513-04. There are also suns that are "wicked" and "broken".
  246.  * These run Solaris >= 2.0 !! * YESSSSSS!!! gec
  247.  */
  248.  
  249. void
  250. ser_exit(status,fd)
  251. int status,fd;
  252. {
  253.   struct termios ti;
  254.   if(ioctl(fd, TCGETS, (caddr_t)&ti) < 0) {
  255.     perror("TCGETSW");
  256.   }
  257.   ti.c_cflag &= ~CRTSCTS;
  258.   if(ioctl(fd, TCSETS, (caddr_t)&ti) < 0) {
  259.     perror("TCSETSW");
  260.   }
  261.   (void) close(fd);
  262. }
  263. #endif /* sun */
  264. #endif
  265.