summaryrefslogtreecommitdiffstats
path: root/src/util/pty/cleanup.c
blob: 0e9104ef94bfb117291b59cd331f32ae189dfde9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * pty_cleanup: Kill processes associated with pty.
 *
 * (C)Copyright 1995, 1996 by the Massachusetts Institute of Technology.
 *
 * 
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that copyright notice and this permission
 * notice appear in supporting documentation, and that the name of
 * M.I.T. not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission.  M.I.T. makes no representations about the suitability
 * of this software for any purpose.  It is provided "as is" without
 * express or implied warranty.
 * 
 */

#include <com_err.h>
#include "libpty.h"
#include "pty-int.h"
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif

long pty_cleanup (slave, pid, update_utmp)
    char *slave;
    int pid; /* May be zero for unknown.*/
    int update_utmp;
{
    int retval, fd;
    
    if (update_utmp)
	pty_update_utmp(PTY_DEAD_PROCESS,0,  "", slave, (char *)0, PTY_UTMP_USERNAME_VALID);
    
    (void)chmod(slave, 0666);
    (void)chown(slave, 0, 0);
#ifdef HAVE_REVOKE
    revoke(slave);
    /*
     * Revoke isn't guaranteed to send a SIGHUP to the processes it
     * dissociates from the terminal.  The best solution without a Posix
     * mechanism for forcing a hangup is to killpg() the process
     * group of the pty.  This will at least kill the shell and
     * hopefully, the child processes.  This is not always the case, however.
     * If the shell puts each job in a process group and doesn't pass
     * along SIGHUP, all processes may not die.
     */
    if ( pid > 0 ) {
#ifdef HAVE_KILLPG
	killpg(pid, SIGHUP);
#else
	kill( -(pid), SIGHUP );
#endif /*HAVE_KILLPG*/
    }
#else /* HAVE_REVOKE*/
#ifdef VHANG_LAST
    {
      int status;
#ifdef POSIX_SIGNALS
      sigset_t old, new;
      sigemptyset(&new);
      sigaddset(&new, SIGCHLD);
      sigprocmask ( SIG_BLOCK, &new, &old);
#else /*POSIX_SIGNALS*/
      int mask = sigblock(sigmask(SIGCHLD));
#endif /*POSIX_SIGNALS*/
      switch (retval = fork()) {
      case -1:
#ifdef POSIX_SIGNALS
	sigprocmask(SIG_SETMASK, &old, 0);
#else /*POSIX_SIGNALS*/
	sigsetmask(mask);
#endif /*POSIX_SIGNALS*/
	return errno;
      case 0:
	ptyint_void_association();
	if ( retval = ( pty_open_ctty( slave, &fd ))) 
	  exit(retval);
	ptyint_vhangup();
	exit(0);
	break;
      default:
#ifdef HAVE_WAITPID
	waitpid(retval, &status, 0);
#else /*HAVE_WAITPID*/
	wait(&status);
#endif
#ifdef POSIX_SIGNALS
	sigprocmask(SIG_SETMASK, &old, 0);
#else /*POSIX_SIGNALS*/
	sigsetmask(mask);
#endif /*POSIX_SIGNALS*/

	break;
      }
    }
#endif /*VHANG_LAST*/
#endif /* HAVE_REVOKE*/
#ifndef HAVE_STREAMS
    slave[strlen("/dev/")] = 'p';
    (void)chmod(slave, 0666);
    (void)chown(slave, 0, 0);
#endif
    return 0;
}