xref: /titanic_44/usr/src/cmd/lp/cmd/lpsched/terminate.c (revision 49b225e1cfa7bbf7738d4df0a03f18e3283426eb)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
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 (ep->flags & EXF_KILLED)
53 		return;
54 	ep->flags |= EXF_KILLED;
55 
56 	/*
57 	 * Theoretically, the following "if-then" is not needed,
58 	 * but there's some bug in the code that occasionally
59 	 * prevents us from hearing from a finished child.
60 	 * (Kill -9 on the child would do that, of course, but
61 	 * the problem has occurred in other cases.)
62 	 */
63 	if (kill(-ep->pid, SIGTERM) == -1 && errno == ESRCH) {
64 		ep->pid = -99;
65 		ep->status = SIGTERM;
66 		ep->Errno = 0;
67 		DoneChildren++;
68 		return;
69 	}
70 
71 	/*
72 	 * Start fix for sunsoft bugid 1108465
73 	 * the original code here was extremely optimistic, and
74 	 * under certain circumstances, the pid's would still be
75 	 * left around. here we get really serious about killing
76 	 * the sucker.
77 	 * we patiently wait for the pid to die. if it doesn't
78 	 * do so in a reasonable amount of time, we get more forceful.
79 	 * note that the original "ep->pid == -99" is a crude hack;
80 	 * but that the convention is being followed. sigh.
81 	 */
82 	for (retries = 5; retries > 0; retries--) {
83 		/* see if the process is still there		*/
84 		if ((kill(-ep->pid, 0) == -1) && (errno == ESRCH)) {
85 			ep->pid = -99;
86 			ep->status = SIGTERM;
87 			ep->Errno = 0;
88 			DoneChildren++;
89 			return;
90 		} else if (errno == EINTR)
91 			break;
92 
93 		sleep(2);
94 	}
95 
96 	/* if it's still not dead, then get more forceful	*/
97 	for (retries = 5; retries > 0; retries--) {
98 		if ((kill(-ep->pid, SIGKILL) == -1) && (errno == ESRCH)) {
99 			ep->pid = -99;
100 			ep->status = SIGTERM;
101 			ep->Errno = 0;
102 			DoneChildren++;
103 			return;
104 		}
105 		sleep(3);
106 	}
107 	/* end of sunsoft bugfix 1108465	*/
108 	/*
109 	 * well hardkill didn't work so just flag this request as done
110 	 */
111 	ep->pid = -99;
112 	ep->status = SIGTERM;
113 	ep->Errno = 0;
114 	DoneChildren++;
115 }
116