xref: /illumos-gate/usr/src/cmd/lp/cmd/lpsched/terminate.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0a44ef6dSjacobs  * Common Development and Distribution License (the "License").
6*0a44ef6dSjacobs  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
23*0a44ef6dSjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*0a44ef6dSjacobs /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0a44ef6dSjacobs /*	  All Rights Reserved  	*/
29*0a44ef6dSjacobs 
307c478bd9Sstevel@tonic-gate #include "lpsched.h"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * terminate() - STOP A CHILD PROCESS
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * Note:  If you're trying to debug lpsched, and worried about
367c478bd9Sstevel@tonic-gate  *        seeing lots of calls to terminate() in the debug output,
377c478bd9Sstevel@tonic-gate  *        don't be; it gets called once for each entry in the child
387c478bd9Sstevel@tonic-gate  *        process table, whether or not there's such a child.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate void
terminate(register EXEC * ep)427c478bd9Sstevel@tonic-gate terminate(register EXEC *ep)
437c478bd9Sstevel@tonic-gate {
447c478bd9Sstevel@tonic-gate 	int retries;		/* fix for sunsoft bugid 1108465	*/
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	if (ep->pid <= 0)
477c478bd9Sstevel@tonic-gate 		return;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	if (ep->flags & EXF_KILLED)
507c478bd9Sstevel@tonic-gate 		return;
517c478bd9Sstevel@tonic-gate 	ep->flags |= EXF_KILLED;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	/*
547c478bd9Sstevel@tonic-gate 	 * Theoretically, the following "if-then" is not needed,
557c478bd9Sstevel@tonic-gate 	 * but there's some bug in the code that occasionally
567c478bd9Sstevel@tonic-gate 	 * prevents us from hearing from a finished child.
577c478bd9Sstevel@tonic-gate 	 * (Kill -9 on the child would do that, of course, but
587c478bd9Sstevel@tonic-gate 	 * the problem has occurred in other cases.)
597c478bd9Sstevel@tonic-gate 	 */
607c478bd9Sstevel@tonic-gate 	if (kill(-ep->pid, SIGTERM) == -1 && errno == ESRCH) {
617c478bd9Sstevel@tonic-gate 		ep->pid = -99;
627c478bd9Sstevel@tonic-gate 		ep->status = SIGTERM;
637c478bd9Sstevel@tonic-gate 		ep->Errno = 0;
647c478bd9Sstevel@tonic-gate 		DoneChildren++;
657c478bd9Sstevel@tonic-gate 		return;
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	/*
697c478bd9Sstevel@tonic-gate 	 * Start fix for sunsoft bugid 1108465
707c478bd9Sstevel@tonic-gate 	 * the original code here was extremely optimistic, and
717c478bd9Sstevel@tonic-gate 	 * under certain circumstances, the pid's would still be
727c478bd9Sstevel@tonic-gate 	 * left around. here we get really serious about killing
737c478bd9Sstevel@tonic-gate 	 * the sucker.
747c478bd9Sstevel@tonic-gate 	 * we patiently wait for the pid to die. if it doesn't
757c478bd9Sstevel@tonic-gate 	 * do so in a reasonable amount of time, we get more forceful.
767c478bd9Sstevel@tonic-gate 	 * note that the original "ep->pid == -99" is a crude hack;
777c478bd9Sstevel@tonic-gate 	 * but that the convention is being followed. sigh.
787c478bd9Sstevel@tonic-gate 	 */
797c478bd9Sstevel@tonic-gate 	for (retries = 5; retries > 0; retries--) {
807c478bd9Sstevel@tonic-gate 		/* see if the process is still there		*/
817c478bd9Sstevel@tonic-gate 		if ((kill(-ep->pid, 0) == -1) && (errno == ESRCH)) {
827c478bd9Sstevel@tonic-gate 			ep->pid = -99;
837c478bd9Sstevel@tonic-gate 			ep->status = SIGTERM;
847c478bd9Sstevel@tonic-gate 			ep->Errno = 0;
857c478bd9Sstevel@tonic-gate 			DoneChildren++;
867c478bd9Sstevel@tonic-gate 			return;
877c478bd9Sstevel@tonic-gate 		} else if (errno == EINTR)
887c478bd9Sstevel@tonic-gate 			break;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 		sleep(2);
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	/* if it's still not dead, then get more forceful	*/
947c478bd9Sstevel@tonic-gate 	for (retries = 5; retries > 0; retries--) {
957c478bd9Sstevel@tonic-gate 		if ((kill(-ep->pid, SIGKILL) == -1) && (errno == ESRCH)) {
967c478bd9Sstevel@tonic-gate 			ep->pid = -99;
977c478bd9Sstevel@tonic-gate 			ep->status = SIGTERM;
987c478bd9Sstevel@tonic-gate 			ep->Errno = 0;
997c478bd9Sstevel@tonic-gate 			DoneChildren++;
1007c478bd9Sstevel@tonic-gate 			return;
1017c478bd9Sstevel@tonic-gate 		}
1027c478bd9Sstevel@tonic-gate 		sleep(3);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 	/* end of sunsoft bugfix 1108465	*/
1057c478bd9Sstevel@tonic-gate 	/*
1067c478bd9Sstevel@tonic-gate 	 * well hardkill didn't work so just flag this request as done
1077c478bd9Sstevel@tonic-gate 	 */
1087c478bd9Sstevel@tonic-gate 	ep->pid = -99;
1097c478bd9Sstevel@tonic-gate 	ep->status = SIGTERM;
1107c478bd9Sstevel@tonic-gate 	ep->Errno = 0;
1117c478bd9Sstevel@tonic-gate 	DoneChildren++;
1127c478bd9Sstevel@tonic-gate }
113