xref: /illumos-gate/usr/src/lib/libproc/common/pr_exit.c (revision dd72704bd9e794056c558153663c739e2012d721)
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 /*
23  * Copyright 1997-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <sys/lwp.h>
31 #include "libproc.h"
32 
33 /*
34  * exit() system call -- executed by subject process.
35  */
36 int
37 pr_exit(struct ps_prochandle *Pr, int status)
38 {
39 	sysret_t rval;			/* return value from exit() */
40 	argdes_t argd[1];		/* arg descriptors for exit() */
41 	argdes_t *adp;
42 	int error;
43 
44 	if (Pr == NULL) {		/* no subject process */
45 		exit(status);
46 		return (0);		/* not reached */
47 	}
48 
49 	adp = &argd[0];		/* status argument */
50 	adp->arg_value = status;
51 	adp->arg_object = NULL;
52 	adp->arg_type = AT_BYVAL;
53 	adp->arg_inout = AI_INPUT;
54 	adp->arg_size = 0;
55 
56 	error = Psyscall(Pr, &rval, SYS_exit, 1, &argd[0]);
57 	/* actually -- never returns.  Expect ENOENT */
58 
59 	if (error < 0) {
60 		if (errno == ENOENT)	/* expected case */
61 			error = ENOENT;
62 		else
63 			error = ENOSYS;
64 	}
65 
66 	if (error == 0)		/* can't happen? */
67 		return (rval.sys_rval1);
68 
69 	if (error == ENOENT)	/* expected case */
70 		return (0);
71 
72 	errno = error;
73 	return (-1);
74 }
75 
76 /*
77  * lwp_exit() system call -- executed by subject lwp.
78  */
79 int
80 pr_lwp_exit(struct ps_prochandle *Pr)
81 {
82 	sysret_t rval;			/* return value from lwp_exit() */
83 	int error;
84 
85 	if (Pr == NULL) {		/* no subject process */
86 		(void) syscall(SYS_lwp_exit);
87 		return (0);		/* not reached */
88 	}
89 
90 	error = Psyscall(Pr, &rval, SYS_lwp_exit, 0, NULL);
91 	/* actually -- never returns.  Expect ENOENT */
92 
93 	if (error < 0) {
94 		if (errno == ENOENT)	/* expected case */
95 			error = ENOENT;
96 		else
97 			error = ENOSYS;
98 	}
99 
100 	if (error == 0)		/* can't happen? */
101 		return (rval.sys_rval1);
102 
103 	if (error == ENOENT)	/* expected case */
104 		return (0);
105 
106 	errno = error;
107 	return (-1);
108 }
109