xref: /titanic_44/usr/src/cmd/lp/cmd/lpsched/terminate.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 2000-2003 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include "lpsched.h"
34 
35 /*
36  * terminate() - STOP A CHILD PROCESS
37  *
38  * Note:  If you're trying to debug lpsched, and worried about
39  *        seeing lots of calls to terminate() in the debug output,
40  *        don't be; it gets called once for each entry in the child
41  *        process table, whether or not there's such a child.
42  */
43 
44 void
45 terminate(register EXEC *ep)
46 {
47 	int retries;		/* fix for sunsoft bugid 1108465	*/
48 
49 	if (ep->pid <= 0)
50 		return;
51 
52 #if	defined(DEBUG)
53 	if (debug & DB_EXEC)
54 		execlog("KILL: pid %d%s%s\n", ep->pid,
55 			((ep->flags & EXF_KILLED)? ", second time" : ""),
56 			(kill(ep->pid, 0) == -1? ", but child is GONE!" : ""));
57 #endif
58 
59 	if (ep->flags & EXF_KILLED)
60 		return;
61 	ep->flags |= EXF_KILLED;
62 
63 	/*
64 	 * Theoretically, the following "if-then" is not needed,
65 	 * but there's some bug in the code that occasionally
66 	 * prevents us from hearing from a finished child.
67 	 * (Kill -9 on the child would do that, of course, but
68 	 * the problem has occurred in other cases.)
69 	 */
70 	if (kill(-ep->pid, SIGTERM) == -1 && errno == ESRCH) {
71 		ep->pid = -99;
72 		ep->status = SIGTERM;
73 		ep->Errno = 0;
74 		DoneChildren++;
75 		return;
76 	}
77 
78 	/*
79 	 * Start fix for sunsoft bugid 1108465
80 	 * the original code here was extremely optimistic, and
81 	 * under certain circumstances, the pid's would still be
82 	 * left around. here we get really serious about killing
83 	 * the sucker.
84 	 * we patiently wait for the pid to die. if it doesn't
85 	 * do so in a reasonable amount of time, we get more forceful.
86 	 * note that the original "ep->pid == -99" is a crude hack;
87 	 * but that the convention is being followed. sigh.
88 	 */
89 	for (retries = 5; retries > 0; retries--) {
90 		/* see if the process is still there		*/
91 		if ((kill(-ep->pid, 0) == -1) && (errno == ESRCH)) {
92 			ep->pid = -99;
93 			ep->status = SIGTERM;
94 			ep->Errno = 0;
95 			DoneChildren++;
96 			return;
97 		} else if (errno == EINTR)
98 			break;
99 
100 		sleep(2);
101 	}
102 
103 	/* if it's still not dead, then get more forceful	*/
104 	for (retries = 5; retries > 0; retries--) {
105 		if ((kill(-ep->pid, SIGKILL) == -1) && (errno == ESRCH)) {
106 			ep->pid = -99;
107 			ep->status = SIGTERM;
108 			ep->Errno = 0;
109 			DoneChildren++;
110 			return;
111 		}
112 		sleep(3);
113 	}
114 	/* end of sunsoft bugfix 1108465	*/
115 	/*
116 	 * well hardkill didn't work so just flag this request as done
117 	 */
118 	ep->pid = -99;
119 	ep->status = SIGTERM;
120 	ep->Errno = 0;
121 	DoneChildren++;
122 }
123