home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1011 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  2.3 KB

  1. From: maart@cs.vu.nl (Maarten Litmaath)
  2. Newsgroups: comp.unix.questions,alt.sources
  3. Subject: hup(1) (was: signal problems on BSD)
  4. Message-ID: <5960@star.cs.vu.nl>
  5. Date: 12 Mar 90 14:59:35 GMT
  6.  
  7. This utility seems to do the job on SunOS 4.0.3c and 4.3BSD.  However,
  8. started by csh under rlogind the command somehow doesn't receive a SIGHUP
  9. on logout!  :-(  Work-around: end your .logout with `exec true'.
  10. Furthermore note the close-on-exec trick.  Can you figure it out?
  11. --------------------cut here--------------------
  12. /*
  13.  * hup.c
  14.  * Run a background job in the process group of its parent (the shell),
  15.  * so that it will receive a SIGHUP on logout.
  16.  * Usage: hup command
  17.  * Note: `hup' will put `command' in the background itself, so there's no
  18.  * need for a trailing `&'.
  19.  * Compile with `-DNO_FCNTL' if your UNIX variant doesn't have fcntl(2).
  20.  * If your shell is csh, you might want to end your `.logout' file with an
  21.  * `exec' command (e.g. `exec true') to get `hup' to work on an `rlogin'.  :-(
  22.  * Author: Maarten Litmaath @ VU Informatika Amsterdam (maart@cs.vu.nl)
  23.  */
  24. #include    <stdio.h>
  25. #include    <signal.h>
  26. #ifdef    NO_FCNTL
  27. #include    <sys/ioctl.h>
  28. #else
  29. #include    <fcntl.h>
  30. #endif    /* NO_FCNTL */
  31.  
  32. char    Id[] = "@(#)hup 1.0 90/03/09 Maarten Litmaath";
  33.  
  34. int    Keyboard_signals[] = {
  35.     SIGINT,
  36.     SIGQUIT,
  37. #ifdef    SIGTSTP
  38.     SIGTSTP,
  39. #endif    /* SIGTSTP */
  40.     0
  41. };
  42.  
  43. main(argc, argv)
  44. int    argc;
  45. char    **argv;
  46. {
  47.     int    pgrp, sig, i, pp[2];
  48.     char    buf[1];
  49.  
  50.     if (argc == 1) {
  51.         fprintf(stderr, "Usage: %s command\n", argv[0]);
  52.         exit(1);
  53.     }
  54.  
  55.     pgrp = getpgrp(getppid());
  56.  
  57.     if (pipe(pp) < 0) {
  58.         perror("pipe");
  59.         exit(1);
  60.     }
  61.  
  62.     switch (fork()) {
  63.     case -1:
  64.         perror("fork");
  65.         exit(1);
  66.     case 0:
  67.         for (i = 0; sig = Keyboard_signals[i]; i++)
  68.             signal(sig, SIG_IGN);
  69.  
  70.         if (setpgrp(0, pgrp) < 0) {
  71.             perror("setpgrp");
  72.             exit(1);
  73.         }
  74.  
  75.         close(pp[0]);
  76.  
  77.         /* set close-on-exec flag */
  78. #ifdef    NO_FCNTL
  79.         ioctl(pp[1], FIOCLEX, (int *) 0);
  80. #else
  81.         fcntl(pp[1], F_SETFD, 1);
  82. #endif    /* NO_FCNTL */
  83.  
  84.         execvp(argv[1], &argv[1]);
  85.         perror(argv[1]);
  86.  
  87.         /* send a message to indicate failure */
  88.         write(pp[1], buf, 1);
  89.         exit(1);
  90.     }
  91.     close(pp[1]);
  92.     exit(read(pp[0], buf, 1));
  93. }
  94. --------------------cut here--------------------
  95. --
  96.  1) Will 4.5BSD have wait5()?         |Maarten Litmaath @ VU Amsterdam:
  97.  2) Sleep(3) should be sleep(2) again.|maart@cs.vu.nl, uunet!mcsun!botter!maart
  98.