home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / question / 10609 < prev    next >
Encoding:
Internet Message Format  |  1992-09-01  |  3.6 KB

  1. Xref: sparky comp.unix.questions:10609 comp.sys.hp:9928
  2. Newsgroups: comp.unix.questions,comp.sys.hp
  3. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!gshp9000.gradsch.ohio-state.edu!brummel
  4. From: brummel@gshp9000.gradsch.ohio-state.edu (Karl Brummel)
  5. Subject: HP-UX and init(1M)
  6. Message-ID: <1992Sep1.155619.28849@magnus.acs.ohio-state.edu>
  7. Sender: news@magnus.acs.ohio-state.edu
  8. Nntp-Posting-Host: gshp9000.gradsch.ohio-state.edu
  9. Organization: The Ohio State University Graduate School
  10. Date: Tue, 1 Sep 1992 15:56:19 GMT
  11. Lines: 66
  12.  
  13. Howdy netters.
  14. I'm writing this program to hang out on a tty and wait for a file to arrive. 
  15. I would like to have it in /etc/inittab so that it will get respawned after
  16. each file.  This is happening on a HP 9000/832 running HP-UX 7.08.
  17.  
  18. My program waits for a file to arrive, then, fork()s and exec()s a shell
  19. script that handles the actual printing. The standard input of the shell
  20. script is connected to a pipe that my program spews data through.
  21.  
  22. This all works dandy from the command line.  Unfortunately, when it is run by
  23. init(1M), it flails hard.  Things work great until the fork and exec.  Then,
  24. /bin/sh closes the file descriptor for the terminal, and init(1M) *apparently*
  25. thinks its child has exited and rexecutes my program, which in turn gets a byte
  26. from the file, forks, and execs the shell script, which closes the terminal,
  27. which causes init to run my program...eventually leading to a message from
  28. init saying my process is respawning too rapidly.
  29.  
  30. How do I beat this?  When I change my program to run the spooler directly,
  31. it works as long as I leave out the call that closes the terminal.  As soon as
  32. I put it back, it flails.  This wouldn't be a problem if I could change the
  33. program without editing /etc/inittab, killing the process, recompiling the
  34. program, editing /etc/inittab, and then running /etc/telinit q each time I
  35. want to change some small detail of the way the file is handled.
  36.  
  37. I have tried setsid() after the fork() to detach the child process from the
  38. controlling terminal, but to no avail.  I need to send init(1M)
  39. the message that it should worry about what the children of the program it
  40. runs does with its file descriptors, or how to tell /bin/sh not to close
  41. files.  Any ideas?  Thanks.  Please send email to brumski+@osu.edu.
  42. Here's a chunk of the program...
  43.  
  44. char     buffer[INSIZE],reset[20];
  45. int    in,count,pipefd[2],writeflag,totalsize;
  46.  
  47. in=openterm("/dev/tty6p7"); /* Open the terminal, set up control flow
  48.                 and such. */
  49. count=read(in,buffer,1);     /* First read is blocking.  Wait for a file. */
  50. pipe(pipefd);    /* Create a pipe. */
  51. /* HERE'S WHERE THINGS GET UGLY. */
  52. if (fork()==0) { /* This is the child, to become the spooler. */
  53.     dup2(pipefd[0],0); /* Connect standard input to the pipe. */
  54.     close(pipefd[0]);  /* These are no longer needed. */
  55.     close(pipefd[1]);
  56.     close(in); /* HERE'S WHERE THINGS GET *REALLY* UGLY. */
  57.     execl("/usr/local/etc/pcprint.sh","pcprint.sh",(char *) 0);
  58.     /* Be sure that the child exits, so if execl() fails the child will
  59.         not continue. */
  60.     perror("execl");
  61.     exit(-1);
  62.     }
  63. close(pipefd[0]);
  64. totalsize=spew(in,pipefd[1],count,&writeflag); /* Get the rest of the file.*?
  65. /* If we have actually sent anything, append a reset string for the printer. */
  66. if(writeflag) {
  67.     getreset(reset); /* read the reset string for the printer. */
  68.     write(pipefd[1],reset,strlen(reset));
  69.     totalsize+=strlen(reset);
  70.     }
  71. account(writeflag,totalsize); /* Record the transaction. */
  72. close(pipefd[1]);    /* Close the pipe.  Else lp will never complete. */
  73. close(in);
  74. return(0);
  75. }
  76. -- 
  77.                       Karl Brummel -- brumski+@osu.edu
  78.             "Speed isn't everything...it's the ONLY thing."
  79.