xref: /illumos-gate/usr/src/cmd/lp/cmd/lpsched/terminate.c (revision 2a8bcb4e)
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 #include "lpsched.h"
31 
32 /*
33  * terminate() - STOP A CHILD PROCESS
34  *
35  * Note:  If you're trying to debug lpsched, and worried about
36  *        seeing lots of calls to terminate() in the debug output,
37  *        don't be; it gets called once for each entry in the child
38  *        process table, whether or not there's such a child.
39  */
40 
41 void
terminate(register EXEC * ep)42 terminate(register EXEC *ep)
43 {
44 	int retries;		/* fix for sunsoft bugid 1108465	*/
45 
46 	if (ep->pid <= 0)
47 		return;
48 
49 	if (ep->flags & EXF_KILLED)
50 		return;
51 	ep->flags |= EXF_KILLED;
52 
53 	/*
54 	 * Theoretically, the following "if-then" is not needed,
55 	 * but there's some bug in the code that occasionally
56 	 * prevents us from hearing from a finished child.
57 	 * (Kill -9 on the child would do that, of course, but
58 	 * the problem has occurred in other cases.)
59 	 */
60 	if (kill(-ep->pid, SIGTERM) == -1 && errno == ESRCH) {
61 		ep->pid = -99;
62 		ep->status = SIGTERM;
63 		ep->Errno = 0;
64 		DoneChildren++;
65 		return;
66 	}
67 
68 	/*
69 	 * Start fix for sunsoft bugid 1108465
70 	 * the original code here was extremely optimistic, and
71 	 * under certain circumstances, the pid's would still be
72 	 * left around. here we get really serious about killing
73 	 * the sucker.
74 	 * we patiently wait for the pid to die. if it doesn't
75 	 * do so in a reasonable amount of time, we get more forceful.
76 	 * note that the original "ep->pid == -99" is a crude hack;
77 	 * but that the convention is being followed. sigh.
78 	 */
79 	for (retries = 5; retries > 0; retries--) {
80 		/* see if the process is still there		*/
81 		if ((kill(-ep->pid, 0) == -1) && (errno == ESRCH)) {
82 			ep->pid = -99;
83 			ep->status = SIGTERM;
84 			ep->Errno = 0;
85 			DoneChildren++;
86 			return;
87 		} else if (errno == EINTR)
88 			break;
89 
90 		sleep(2);
91 	}
92 
93 	/* if it's still not dead, then get more forceful	*/
94 	for (retries = 5; retries > 0; retries--) {
95 		if ((kill(-ep->pid, SIGKILL) == -1) && (errno == ESRCH)) {
96 			ep->pid = -99;
97 			ep->status = SIGTERM;
98 			ep->Errno = 0;
99 			DoneChildren++;
100 			return;
101 		}
102 		sleep(3);
103 	}
104 	/* end of sunsoft bugfix 1108465	*/
105 	/*
106 	 * well hardkill didn't work so just flag this request as done
107 	 */
108 	ep->pid = -99;
109 	ep->status = SIGTERM;
110 	ep->Errno = 0;
111 	DoneChildren++;
112 }
113