xref: /illumos-gate/usr/src/uts/common/syscall/times.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 2004 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 <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/sysmacros.h>
36 #include <sys/systm.h>
37 #include <sys/tuneable.h>
38 #include <sys/errno.h>
39 #include <sys/proc.h>
40 #include <sys/time.h>
41 #include <sys/times.h>
42 #include <sys/debug.h>
43 #include <sys/msacct.h>
44 
45 /*
46  * Return system and user times.
47  */
48 
49 clock_t
50 times(struct tms *tp)
51 {
52 	proc_t *p = ttoproc(curthread);
53 	struct tms	p_time;
54 	clock_t ret_lbolt;
55 
56 	mutex_enter(&p->p_lock);
57 	p_time.tms_utime = (clock_t)NSEC_TO_TICK(
58 	    mstate_aggr_state(p, LMS_USER));
59 	p_time.tms_stime = (clock_t)NSEC_TO_TICK(
60 	    mstate_aggr_state(p, LMS_SYSTEM));
61 	p_time.tms_cutime = p->p_cutime;
62 	p_time.tms_cstime = p->p_cstime;
63 	mutex_exit(&p->p_lock);
64 
65 	if (copyout(&p_time, tp, sizeof (p_time)))
66 		return (set_errno(EFAULT));
67 
68 	ret_lbolt = lbolt;
69 
70 	return (ret_lbolt == -1 ? 0 : ret_lbolt);
71 }
72 
73 #ifdef _SYSCALL32_IMPL
74 
75 /*
76  * We deliberately -don't- return EOVERFLOW on type overflow,
77  * since the 32-bit kernel simply wraps 'em around.
78  */
79 clock32_t
80 times32(struct tms32 *tp)
81 {
82 	proc_t	*p = ttoproc(curthread);
83 	struct tms32	p_time;
84 	clock32_t	ret_lbolt;
85 
86 	mutex_enter(&p->p_lock);
87 	p_time.tms_utime = (clock32_t)NSEC_TO_TICK(
88 	    mstate_aggr_state(p, LMS_USER));
89 	p_time.tms_stime = (clock32_t)NSEC_TO_TICK(
90 	    mstate_aggr_state(p, LMS_SYSTEM));
91 	p_time.tms_cutime = (clock32_t)p->p_cutime;
92 	p_time.tms_cstime = (clock32_t)p->p_cstime;
93 	mutex_exit(&p->p_lock);
94 
95 	if (copyout(&p_time, tp, sizeof (p_time)))
96 		return (set_errno(EFAULT));
97 
98 	ret_lbolt = (clock32_t)lbolt;
99 
100 	return (ret_lbolt == (clock32_t)-1 ? 0 : ret_lbolt);
101 }
102 
103 #endif	/* _SYSCALL32_IMPL */
104