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

  1. From: maart@cs.vu.nl (Maarten Litmaath)
  2. Newsgroups: comp.unix.wizards,alt.sources
  3. Subject: Re: Trying to kill a daemon at logout time
  4. Message-ID: <6399@star.cs.vu.nl>
  5. Date: 26 Apr 90 18:22:31 GMT
  6.  
  7. In article <EMV.90Apr25091503@picasso.math.lsa.umich.edu>,
  8.     emv@math.lsa.umich.edu (Edward Vielmetti) writes:
  9. )In article <3175@astroatc.UUCP>
  10. )    chrisc@astroatc.UUCP (Chris Czerwinski) writes:
  11. )
  12. )
  13. )   I have a program that generates a daemon when I log in. That is easy
  14. )   enough. My problem is that I want it to disappear when I logout. 
  15. )
  16. )If your program generates a daemon, then you should have the daemon write
  17. )the pid it's using to a file (ala syslogd or named).  Then you can simply
  18. )do
  19. )    kill -TERM `cat ~chrisc/mydaemon.pid`
  20. )without any risk of whomping the wrong job or having to resort to any
  21. )odd shell invocations.
  22.  
  23. If you don't like temp files, you can use the following program, which
  24. essentially is the opposite of nohup(1).  Note the close-on-exec trick.
  25.  
  26. : This is a shar archive.  Extract with sh, not csh.
  27. : This archive ends with exit, so do not worry about trailing junk.
  28. : --------------------------- cut here --------------------------
  29. PATH=/bin:/usr/bin:/usr/ucb
  30. echo Extracting 'hup.c'
  31. sed 's/^X//' > 'hup.c' << '+ END-OF-FILE ''hup.c'
  32. X/*
  33. X * hup.c
  34. X * Run a background job in the process group of its parent (the shell),
  35. X * so that it will receive a SIGHUP on logout.
  36. X * Usage: hup command
  37. X * Note: `hup' will put `command' in the background itself, so there's no
  38. X * need for a trailing `&'.
  39. X * Compile with `-DNO_FCNTL' if your UNIX variant doesn't have fcntl(2).
  40. X * If your shell is csh, you might want to end your `.logout' file with an
  41. X * `exec' command (e.g. `exec true') to get `hup' to work on an `rlogin'.  :-(
  42. X * Author: Maarten Litmaath @ VU Informatika Amsterdam (maart@cs.vu.nl)
  43. X */
  44. X#include    <stdio.h>
  45. X#include    <signal.h>
  46. X#ifdef    NO_FCNTL
  47. X#include    <sys/ioctl.h>
  48. X#else
  49. X#include    <fcntl.h>
  50. X#endif    /* NO_FCNTL */
  51. X
  52. Xchar    Id[] = "@(#)hup 1.0 90/03/09 Maarten Litmaath";
  53. X
  54. Xint    Keyboard_signals[] = {
  55. X    SIGINT,
  56. X    SIGQUIT,
  57. X#ifdef    SIGTSTP
  58. X    SIGTSTP,
  59. X#endif    /* SIGTSTP */
  60. X    0
  61. X};
  62. X
  63. Xmain(argc, argv)
  64. Xint    argc;
  65. Xchar    **argv;
  66. X{
  67. X    int    pgrp, sig, i, pp[2];
  68. X    char    buf[1];
  69. X
  70. X    if (argc == 1) {
  71. X        fprintf(stderr, "Usage: %s command\n", argv[0]);
  72. X        exit(1);
  73. X    }
  74. X
  75. X    pgrp = getpgrp(getppid());
  76. X
  77. X    if (pipe(pp) < 0) {
  78. X        perror("pipe");
  79. X        exit(1);
  80. X    }
  81. X
  82. X    switch (fork()) {
  83. X    case -1:
  84. X        perror("fork");
  85. X        exit(1);
  86. X    case 0:
  87. X        for (i = 0; sig = Keyboard_signals[i]; i++)
  88. X            signal(sig, SIG_IGN);
  89. X
  90. X        if (setpgrp(0, pgrp) < 0) {
  91. X            perror("setpgrp");
  92. X            exit(1);
  93. X        }
  94. X
  95. X        close(pp[0]);
  96. X
  97. X        /* set close-on-exec flag */
  98. X#ifdef    NO_FCNTL
  99. X        ioctl(pp[1], FIOCLEX, (int *) 0);
  100. X#else
  101. X        fcntl(pp[1], F_SETFD, 1);
  102. X#endif    /* NO_FCNTL */
  103. X
  104. X        execvp(argv[1], &argv[1]);
  105. X        perror(argv[1]);
  106. X
  107. X        /* send a message to indicate failure */
  108. X        write(pp[1], buf, 1);
  109. X        exit(1);
  110. X    }
  111. X    close(pp[1]);
  112. X    exit(read(pp[0], buf, 1));
  113. X}
  114. + END-OF-FILE hup.c
  115. chmod 'u=rw,g=r,o=r' 'hup.c'
  116. set `wc -c 'hup.c'`
  117. count=$1
  118. case $count in
  119. 1627)    :;;
  120. *)    echo 'Bad character count in ''hup.c' >&2
  121.         echo 'Count should be 1627' >&2
  122. esac
  123. exit 0
  124. --
  125.  1) Alias must go as well.            |Maarten Litmaath @ VU Amsterdam:
  126.  2) Sleep(3) should be sleep(2) again.|maart@cs.vu.nl, uunet!cs.vu.nl!maart
  127.