home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / sys / hp / 10392 < prev    next >
Encoding:
Text File  |  1992-09-14  |  13.4 KB  |  605 lines

  1. Newsgroups: comp.sys.hp
  2. Path: sparky!uunet!utcsri!helios.physics.utoronto.ca!alchemy.chem.utoronto.ca!news
  3. From: system@alchemy.chem.utoronto.ca (System Admin (Mike Peterson))
  4. Subject: BSD to HP-UX porting tricks summary (LONG)
  5. Message-ID: <1992Sep14.190917.3551@alchemy.chem.utoronto.ca>
  6. Sender: news@alchemy.chem.utoronto.ca (USENET news)
  7. Organization: University of Toronto Chemistry Department
  8. Date: Mon, 14 Sep 1992 19:09:17 GMT
  9. Lines: 594
  10.  
  11.     HP TRICKS (home-grown and from news postings)
  12.     =============================================
  13.  
  14. Things to watch out for:
  15.  
  16. TIOCNOTTY   - this is not available on HP-UX (use setsid()); trying
  17.           to open '/dev/tty' will also fail with status -1.
  18. setpgrp     - this doesn't get rid of the controlling terminal
  19.           if you use -lBSD (use setsid()).
  20. BSD signals - you almost certainly want them if your program came from
  21.           a BSD system (e.g. Sun); this breaks 'setpgrp' among other
  22.           things (see 'man bsdproc' and notes below).
  23.  
  24.  
  25. getwd:
  26. ------
  27.  
  28. /* Replace 'SIZEOFARG-1' with the declared size of "arg", minus 1 */
  29.  
  30. #ifdef __hpux
  31. #include <unistd.h>
  32. #define getwd(arg)    getcwd(arg, (size_t) SIZEOFARG-1)
  33. #else
  34. char    *getwd();
  35. #endif
  36.  
  37.  
  38. getrusage:
  39. ----------
  40.  
  41. From: scot@pawnee.ugrad.ee.ufl.edu (Scott Miller)
  42. Newsgroups: comp.sys.hp
  43. Subject: Re: Where is getrusage()? (Summary)
  44. Organization: UF EE Department
  45.  
  46. getrusage() is in the syscall includes
  47.  
  48. Here is the code fragment I used:
  49.  
  50. #ifdef hpux
  51. #include <sys/syscall.h>
  52. #define getrusage(a, b)  syscall(SYS_GETRUSAGE, a, b)
  53. #endif /* hpux */
  54.  
  55.  
  56. srandom, random:
  57. ----------------
  58.  
  59. From: mjo@snclib.snc.edu (Mike O'Connor)
  60. Newsgroups: comp.sys.hp
  61. Subject: Re: random and srandom on HP9000/720 with HPUX-8.07
  62.  
  63. #define srandom srand
  64. #define random rand
  65.  
  66.  
  67. getdtablesize:
  68. --------------
  69.  
  70. /*
  71.  * getdtablesize ()
  72.  *
  73.  * Returns the maximum number of file descriptors allowed.
  74.  */
  75.  
  76. #include <unistd.h>
  77.  
  78.     int
  79. getdtablesize ()
  80. {
  81.     return(sysconf(_SC_OPEN_MAX));
  82. }
  83.  
  84.  
  85. usleep:
  86. -------
  87.  
  88. /*
  89.  *  NAME:
  90.  *      usleep     -- This is the precision timer for Test Set
  91.  *                    Automation. It uses the select(2) system
  92.  *                    call to delay for the desired number of
  93.  *                    micro-seconds. This call returns ZERO
  94.  *                    (which is usually ignored) on successful
  95.  *                    completion, -1 otherwise. 
  96.  *
  97.  *  ALGORITHM:
  98.  *      1) We range check the passed in microseconds and log a
  99.  *         warning message if appropriate. We then return without
  100.  *         delay, flagging an error. 
  101.  *      2) Load the Seconds and micro-seconds portion of the
  102.  *         interval timer structure.
  103.  *      3) Call select(2) with no file descriptors set, just the
  104.  *         timer, this results in either delaying the proper
  105.  *         ammount of time or being interupted early by a signal.
  106.  *
  107.  *  HISTORY:
  108.  *      Added when the need for a subsecond timer was evident.
  109.  *
  110.  *  AUTHOR:
  111.  *      Michael J. Dyer                   Telephone:   AT&T 414.647.4044
  112.  *      General Electric Medical Systems        GE DialComm  8 *767.4044
  113.  *      P.O. Box 414  Mail Stop 12-27         Sect'y   AT&T 414.647.4584
  114.  *      Milwaukee, Wisconsin  USA 53201                      8 *767.4584
  115.  *      internet:  mike@sherlock.med.ge.com     GEMS WIZARD e-mail: DYER
  116.  */
  117.  
  118. #include <unistd.h>
  119. #include <stdlib.h>
  120. #include <stdio.h>
  121. #include <errno.h>
  122. #include <time.h>
  123. #include <sys/time.h>
  124. #include <sys/param.h>
  125. #include <sys/types.h>
  126.  
  127. int     usleep( unsigned long int microSeconds )
  128. {
  129.         unsigned int            Seconds, uSec;
  130.         int                     nfds, readfds, writefds, exceptfds;
  131.         struct  timeval         Timer;
  132.  
  133.         nfds = readfds = writefds = exceptfds = 0;
  134.  
  135.         if( (microSeconds == (unsigned long) 0) 
  136.                 || microSeconds > (unsigned long) 4000000 )
  137.         {
  138.                 errno = ERANGE;         /* value out of range */
  139.                 perror( "usleep time out of range ( 0 -> 4000000 ) " );
  140.                 return -1;
  141.         }
  142.  
  143.         Seconds = microSeconds / (unsigned long) 1000000;
  144.         uSec    = microSeconds % (unsigned long) 1000000;
  145.  
  146.         Timer.tv_sec            = Seconds;
  147.         Timer.tv_usec           = uSec;
  148.  
  149.         if( select( nfds, &readfds, &writefds, &exceptfds, &Timer ) < 0 )
  150.         {
  151.                 perror( "usleep (select) failed" );
  152.                 return -1;
  153.         }
  154.  
  155.         return 0;
  156. }
  157.  
  158.  
  159. flock:
  160. ------
  161.  
  162. /*
  163.  * flock (fd, operation)
  164.  *
  165.  * This routine performs some file locking like the BSD 'flock'
  166.  * on the object described by the int file descriptor 'fd',
  167.  * which must already be open.
  168.  *
  169.  * The operations that are available are:
  170.  *
  171.  * LOCK_SH  -  get a shared lock.
  172.  * LOCK_EX  -  get an exclusive lock.
  173.  * LOCK_NB  -  don't block (must be ORed with LOCK_SH or LOCK_EX).
  174.  * LOCK_UN  -  release a lock.
  175.  *
  176.  * Return value: 0 if lock successful, -1 if failed.
  177.  *
  178.  * Note that whether the locks are enforced or advisory is
  179.  * controlled by the presence or absence of the SETGID bit on
  180.  * the executable.
  181.  *
  182.  * Note that there is no difference between shared and exclusive
  183.  * locks, since the 'lockf' system call in SYSV doesn't make any
  184.  * distinction.
  185.  *
  186.  * The file "<sys/file.h>" contains the definitions of the available
  187.  * operations, which must be added manually (see below for the values).
  188.  */
  189.  
  190. #include <unistd.h>
  191. #include <sys/file.h>
  192. #include <errno.h>
  193.  
  194. extern int errno;
  195.  
  196.     int
  197. flock (int fd, int operation)
  198. {
  199.     int i;
  200.  
  201.     switch (operation) {
  202.  
  203.     /* LOCK_SH - get a shared lock */
  204.     case LOCK_SH:
  205.     /* LOCK_EX - get an exclusive lock */
  206.     case LOCK_EX:
  207.         i = lockf (fd, F_LOCK, 0);
  208.         break;
  209.  
  210.     /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
  211.     case LOCK_SH|LOCK_NB:
  212.     /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
  213.     case LOCK_EX|LOCK_NB:
  214.         i = lockf (fd, F_TLOCK, 0);
  215.         if (i == -1)
  216.             if ((errno == EAGAIN) || (errno == EACCES))
  217.                 errno = EWOULDBLOCK;
  218.         break;
  219.  
  220.     /* LOCK_UN - unlock */
  221.     case LOCK_UN:
  222.         i = lockf (fd, F_ULOCK, 0);
  223.         break;
  224.  
  225.     /* Default - can't decipher operation */
  226.     default:
  227.         i = -1;
  228.         errno = EINVAL;
  229.         break;
  230.     }
  231.  
  232.     return (i);
  233. }
  234.  
  235. /*
  236.  * An alternative version was posted by James Gritton
  237.  * (gritton@byu.edu) in comp.sys.hp.
  238.  * As far as I can tell, it works the same as the above
  239.  * except for the "errno" values returned (and it defaults
  240.  * an invalid operation to "unlock").
  241.  * The definitions of LOCK_xx should be put into <sys/file.h> and/or
  242.  * <fcntl.h>.
  243.  * Note: this was typed in, so it may not work as given.
  244.  */
  245.  
  246. /*
  247.  *#include <fcntl.h>
  248.  *#define LOCK_SH 1
  249.  *#define LOCK_EX 2
  250.  *#define LOCK_NB 4
  251.  *#define LOCK_UN 8
  252.  *
  253.  *    int
  254.  *flock (int fd, int operation)
  255.  *{
  256.  *    struct flock fl;
  257.  *
  258.  *    switch (operation & ~LOCK_NB) {
  259.  *    case LOCK_SH:
  260.  *        fl.l_type = F_RDLCK;
  261.  *        break;
  262.  *    case LOCK_EX:
  263.  *        fl.l_type = F_WRLCK;
  264.  *        break;
  265.  *    default:
  266.  *        fl.l_type = F_UNLCK;
  267.  *        break;
  268.  *    }
  269.  *
  270.  *    fl.l_whence = SEEK_SET;
  271.  *    fl.l_start = fl.l_len = 0L;
  272.  *
  273.  *    return fcntl (fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &fl);
  274.  *}
  275.  */
  276.  
  277.  
  278. getclktck:
  279. ----------
  280.  
  281. /*
  282.  * getclktck ()
  283.  *
  284.  * Returns the value of CLK_TCK (timer resolution).
  285.  */
  286.  
  287. #include <unistd.h>
  288.  
  289.     int
  290. getclktck ()
  291. {
  292.     return(sysconf(_SC_CLK_TCK));
  293. }
  294.  
  295.  
  296. getloadavg (works on a wide variety of systems):
  297. ------------------------------------------------
  298.  
  299. /*
  300.  * getloadavg (ave, n)
  301.  *
  302.  * This routine returns 'n' double precision floats containing
  303.  * the load averages in 'ave'; at most 3 values will be returned.
  304.  *
  305.  * Return value: 0 if successful, -1 if failed (and all load
  306.  * averages are returned as 0).
  307.  */
  308.  
  309. #include <sys/types.h>
  310. #include <stdio.h>
  311. #include <nlist.h>
  312. #include <errno.h>
  313.  
  314. extern int errno;
  315.  
  316. #define STRSIZ    512            /* Sprintf buffer size */
  317. static char errstr[STRSIZ];        /* Global sprintf buffer */
  318.  
  319. int ugetloads(float *a);
  320. static void mperror(char *s);
  321. static char *syserr();
  322.  
  323. #define merror(a1)        fprintf(stderr,"%s",a1)
  324. #define merror1(fmt,a1)        { sprintf(errstr,fmt,a1); merror(errstr); }
  325.  
  326. struct    nlist nl[] = {
  327. #ifdef stardent
  328. # define unixpath "/unix"
  329.     { "avenrun" },
  330. #else
  331. #ifdef __hpux
  332. # define unixpath "/hp-ux"
  333. #ifdef __hppa       /* series 700 & 800 */
  334.     { "avenrun" },
  335. #else               /* series 300 & 400 */
  336.     { "_avenrun" },
  337. #endif
  338. #else
  339. # define unixpath "/vmunix"
  340.     { "_avenrun" },
  341. #endif
  342. #endif
  343.     { 0 },
  344. };
  345.  
  346. #ifndef RISCos
  347.     int
  348. getloadavg (double *a, int na)
  349. {
  350.     int i, nreturn;
  351.     static int kmem = -1;
  352. #if defined(vax) || defined(__hpux)
  353.     double avenrun[3];
  354. #else
  355.     long avenrun[3];
  356. #endif
  357. #ifdef NOKMEM
  358.     float aves[3];
  359. #endif /* NOKMEM */
  360.  
  361.     nreturn = na;
  362.     if ( nreturn < 0 )
  363.         nreturn = 0;
  364.     if ( nreturn > 3 )
  365.         nreturn = 3;
  366.  
  367. #ifdef NOKMEM
  368. /* Use 'uptime' output for BSD-like systems with no /dev/kmem */
  369.  
  370.     i = ugetloads(aves);
  371.     if( i == -1 ){
  372.         merror("ugetloads failed\n");
  373.         goto failed;
  374.     }
  375.     for (i = 0; i < nreturn; i++)
  376.         a[i] = aves[i];
  377.  
  378. #else /*NOKMEM*/
  379.  
  380.     if(kmem == -1) {
  381. #ifdef sgi
  382. # include <sys/sysmp.h>
  383.     nl[0].n_value = sysmp(MP_KERNADDR, MPKA_AVENRUN) & 0x7fffffff;
  384. #else
  385.         nlist(unixpath, nl);
  386.         if (nl[0].n_type==0) {
  387.             merror1("%s: No namelist\n", unixpath);
  388.             goto failed;
  389.         }
  390. #ifdef stardent
  391.         nl[0].n_value &= 0x7fffffff;
  392. #endif
  393. #endif
  394.         if((kmem = open("/dev/kmem", 0)) == -1) {
  395.             mperror("Can't open(/dev/kmem)");
  396.             goto failed;
  397.         }
  398.     }
  399.     if( lseek(kmem, (off_t)nl[0].n_value, 0) == -1 ){
  400.         mperror("Can't lseek in kmem");
  401.         goto failed;
  402.     }
  403.     if( read(kmem, (char *)avenrun, sizeof(avenrun)) != sizeof(avenrun) ){
  404.         mperror("Can't read kmem");
  405.         goto failed;
  406.     }
  407.     for (i = 0; i < nreturn; i++)
  408. #if defined(sun) || defined(sequent)
  409.         a[i] = avenrun[i] / FSCALE;
  410. #else 
  411. #ifdef sgi
  412.         a[i] = avenrun[i] / 1024;
  413. #else
  414. #if defined(BSD4_2) || defined(__hpux)
  415.         a[i] = avenrun[i];
  416. #else 
  417. #ifdef stardent
  418.         a[i] = (double)avenrun[i] / (1<<16);
  419. #else
  420.         a[i] = avenrun[i] / 1024;
  421. #endif /*stardent*/
  422. #endif /*BSD4_2*/
  423. #endif /*sgi*/
  424. #endif /*sun*/
  425. #endif /*NOKMEM*/
  426.     return(0); 
  427. failed:;
  428.     for (i = 0; i < nreturn; i++)
  429.         a[i] = 0;
  430.     return(-1);
  431. }
  432. #else /*RISCos*/
  433. #include <sys/fixpoint.h>
  434.     static
  435. getloadavg (double *a, int na)
  436. {
  437.     int i, nreturn;
  438.     static int kmem = -1;
  439.     fix avenrun[3];
  440.  
  441.     nreturn = na;
  442.     if ( nreturn < 0 )
  443.         nreturn = 0;
  444.     if ( nreturn > 3 )
  445.         nreturn = 3;
  446.  
  447.     if(kmem == -1) {
  448.         nlist("/unix", nl);
  449.         if (nl[0].n_type==0) {
  450.             merror("/unix: No namelist\n");
  451.             goto failed;
  452.         }
  453.         if((kmem = open("/dev/kmem", 0)) == -1) {
  454.             mperror("Can't open(/dev/kmem)");
  455.             goto failed;
  456.         }
  457.     }
  458.     if( lseek(kmem, (off_t)nl[0].n_value, 0) == -1 ){
  459.         mperror("Can't lseek in kmem");
  460.         goto failed;
  461.     }
  462.     if( read(kmem, (char *)avenrun, sizeof(avenrun)) != sizeof(avenrun) ){
  463.         mperror("Can't read kmem");
  464.         goto failed;
  465.     }
  466.     for (i = 0; i < nreturn; i++)
  467.             a[i] = (int) FIX_TO_INT(avenrun[i]) + .5;
  468.     return(0);
  469. failed:;
  470.     for (i = 0; i < nreturn; i++)
  471.         a[i] = 0;
  472.     return(-1);
  473. }
  474. #endif /* RISCOS */
  475.  
  476. /* ugetloads(ls)
  477.  * float ld[3];
  478.  *
  479.  * Puts the 1, 5, and 15 minute load averages in the float
  480.  * array passed to it.  This program calls upon uptime(1)
  481.  * which could have different ways of printing ie. with bsd4.2
  482.  * "   9:34pm  up 11 hrs,  3 users,  load average: 0.25, 0.22, 0.24  "
  483.  *                                notice the commas -- ^ --- ^.
  484.  * while bsd4.1 does not print commas.  The BSD41 define will 
  485.  * take care of this if that is your system, it defaults to
  486.  * the 4.2 version.
  487.  *
  488.  * Author:
  489.  *  John Bien
  490.  *  {ihnp4 | ucbvax | decvax}!trwrb!jsb
  491.  *
  492.  * This routine taken from comp.sources.unix: Volume 4, Issue 78
  493.  */
  494.  
  495. FILE *popen();
  496.  
  497. ugetloads(ld)
  498. float ld[3];
  499. {
  500.     FILE *stream;
  501.     int i;
  502.  
  503.     if((stream = popen("uptime","r")) == NULL)
  504.     return(-1);
  505.  
  506. #ifdef BSD41
  507.     i = fscanf(stream,"%*[^l] load average: %f %f %f", &ld[0],&ld[1],&ld[2]);
  508. #else
  509.     i = fscanf(stream,"%*[^l] load average: %f, %f, %f", &ld[0],&ld[1],&ld[2]);
  510. #endif /* BSD41 */
  511.     pclose(stream);
  512.     return i == 3 ? 0 : -1;
  513. }
  514.  
  515. /* Routine to print messages to stderr, appending the system error message */
  516.  
  517.     static void
  518. mperror(char *s)
  519. {
  520.     char *p;
  521.     char str[STRSIZ];    /* must have own internal buffer */
  522.  
  523.     if( (p=index(s,'\n')) != NULL )
  524.         *p = '\0';
  525.     sprintf(str,"%s: %s\n", s, syserr());
  526.     if( p )
  527.         *p = '\n';
  528.     merror(str);
  529. }
  530.  
  531. /* Routine to get the last system error message */
  532.  
  533. extern int sys_nerr;
  534. extern char *sys_errlist[];
  535.  
  536.     static char *
  537. syserr()
  538. {
  539.     static char buf[80];
  540.  
  541.     if (errno >= 0 && errno < sys_nerr)
  542.         return(sys_errlist[errno]);
  543.     sprintf(buf,"Unknown error %d", errno);
  544.     return(buf);
  545. }
  546.  
  547.  
  548. Dissociate from controlling terminal:
  549. -------------------------------------
  550.  
  551. From: jhd@irfu.se (Jan D.)
  552. Organization: Swedish Institute of Space Physics, Uppsala, Sweden
  553.  
  554. The code above should look like this on HP-UX (POSIX ?):
  555.  
  556.     #include <unistd.h>    /* For _SC_OPEN_MAX */
  557.  
  558.     long    tblsiz = sysconf(_SC_OPEN_MAX);
  559.  
  560.      if (fork())
  561.          exit(0);
  562.  
  563.     setsid();    /* Disassociate from controlling terminal */
  564.      for (c = 0; c < tblsiz; c++)
  565.          (void) close(c);
  566.      (void) open("/", O_RDONLY);
  567.      (void) dup2(0, 1);
  568.      (void) dup2(0, 2);
  569.  
  570.  
  571. Here's the deal regarding '-lBSD':
  572.  
  573. setpgrp() (in libc) is equivalent to setsid().
  574. setpgrp(pid, pgrp) (in -lBSD) is equivalent to POSIX setpgid(pid, pgrp).
  575. setpgrp2(pid, pgrp) is also equivalent to POSIX setpgid(pid, pgrp).
  576.  
  577. If you don't link with -lBSD you can replace setsid() in with setpgrp()
  578. if you wan't. They both will get rid of the controlling terminal.
  579.  
  580. setpgrp(pid, pgrp) (-lBSD style), setpgid(pid, pgrp)
  581. and setpgrp2(pid, pgrp) will NOT remove the controlling terminal.
  582.  
  583. Thus: The only way (I know of) in HP-UX to remove the controlling terminal
  584. is with setsid() or setpgrp() in libc. The only way in POSIX to get
  585. rid of the controlling terminal is with setsid().
  586.  
  587.  
  588. setlinebuf:
  589. -----------
  590.  
  591. /*
  592.  * setlinebuf (FILE *fp)
  593.  *
  594.  * Routine to set line buffering on "fp".
  595.  */
  596.  
  597. #include <stdio.h>
  598.  
  599.     int
  600. setlinebuf (FILE *fp)
  601. {
  602.     (void) setvbuf (fp, NULL, _IOLBF, 0);
  603.     return(0);
  604. }
  605.