Home | History | Annotate | Line # | Download | only in common
linux32_time.c revision 1.6
      1 /*	$NetBSD: linux32_time.c,v 1.6 2006/11/14 13:34:29 elad Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Emmanuel Dreyfus
     17  * 4. The name of the author may not be used to endorse or promote
     18  *    products derived from this software without specific prior written
     19  *    permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
     22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 
     36 __KERNEL_RCSID(0, "$NetBSD: linux32_time.c,v 1.6 2006/11/14 13:34:29 elad Exp $");
     37 
     38 #include <sys/types.h>
     39 #include <sys/param.h>
     40 #include <sys/fstypes.h>
     41 #include <sys/signal.h>
     42 #include <sys/dirent.h>
     43 #include <sys/kauth.h>
     44 #include <sys/kernel.h>
     45 #include <sys/fcntl.h>
     46 #include <sys/select.h>
     47 #include <sys/sa.h>
     48 #include <sys/proc.h>
     49 #include <sys/ucred.h>
     50 #include <sys/swap.h>
     51 
     52 #include <machine/types.h>
     53 
     54 #include <sys/syscallargs.h>
     55 
     56 #include <compat/netbsd32/netbsd32.h>
     57 #include <compat/netbsd32/netbsd32_conv.h>
     58 #include <compat/netbsd32/netbsd32_syscallargs.h>
     59 
     60 #include <compat/linux/common/linux_types.h>
     61 #include <compat/linux/common/linux_signal.h>
     62 #include <compat/linux/common/linux_machdep.h>
     63 #include <compat/linux/common/linux_misc.h>
     64 #include <compat/linux/common/linux_oldolduname.h>
     65 #include <compat/linux/linux_syscallargs.h>
     66 
     67 #include <compat/linux32/common/linux32_types.h>
     68 #include <compat/linux32/common/linux32_signal.h>
     69 #include <compat/linux32/common/linux32_machdep.h>
     70 #include <compat/linux32/common/linux32_sysctl.h>
     71 #include <compat/linux32/common/linux32_socketcall.h>
     72 #include <compat/linux32/linux32_syscallargs.h>
     73 
     74 extern struct timezone linux_sys_tz;
     75 int
     76 linux32_sys_gettimeofday(l, v, retval)
     77 	struct lwp *l;
     78 	void *v;
     79 	register_t *retval;
     80 {
     81 	struct linux32_sys_gettimeofday_args /* {
     82 		syscallarg(netbsd32_timevalp_t) tp;
     83 		syscallarg(netbsd32_timezonep_t) tzp;
     84 	} */ *uap = v;
     85 	struct timeval tv;
     86 	struct netbsd32_timeval tv32;
     87 	int error;
     88 
     89 	if (NETBSD32PTR64(SCARG(uap, tp)) != NULL) {
     90 		microtime(&tv);
     91 		netbsd32_from_timeval(&tv, &tv32);
     92 		if ((error = copyout(&tv32,
     93 		    (caddr_t)NETBSD32PTR64(SCARG(uap, tp)),
     94 		    sizeof(tv32))) != 0)
     95 			return error;
     96 	}
     97 
     98 	/* timezone size does not change */
     99 	if (NETBSD32PTR64(SCARG(uap, tzp)) != NULL) {
    100 		if ((error = copyout(&linux_sys_tz,
    101 		    (caddr_t)NETBSD32PTR64(SCARG(uap, tzp)),
    102 		    sizeof(linux_sys_tz))) != 0)
    103 			return error;
    104 	}
    105 
    106 	return 0;
    107 }
    108 
    109 int
    110 linux32_sys_settimeofday(l, v, retval)
    111 	struct lwp *l;
    112 	void *v;
    113 	register_t *retval;
    114 {
    115 	struct linux32_sys_settimeofday_args /* {
    116 		syscallarg(netbsd32_timevalp_t) tp;
    117 		syscallarg(netbsd32_timezonep_t) tzp;
    118 	} */ *uap = v;
    119 	struct linux_sys_settimeofday_args ua;
    120 
    121 	NETBSD32TOP_UAP(tp, struct timeval);
    122 	NETBSD32TOP_UAP(tzp, struct timezone);
    123 
    124 	return linux_sys_settimeofday(l, &ua, retval);
    125 }
    126 
    127 int
    128 linux32_sys_time(l, v, retval)
    129 	struct lwp *l;
    130 	void *v;
    131 	register_t *retval;
    132 {
    133 	struct linux32_sys_time_args /* {
    134 		syscallcarg(linux32_timep_t) t;
    135 	} */ *uap = v;
    136         struct timeval atv;
    137         linux32_time_t tt;
    138         int error;
    139 
    140         microtime(&atv);
    141 
    142         tt = (linux32_time_t)atv.tv_sec;
    143 
    144         if (SCARG(uap, t) && (error = copyout(&tt,
    145 	    NETBSD32PTR64(SCARG(uap, t)), sizeof(tt))))
    146                 return error;
    147 
    148         retval[0] = tt;
    149 
    150         return 0;
    151 }
    152 
    153 
    154 int
    155 linux32_sys_times(l, v, retval)
    156 	struct lwp *l;
    157 	void *v;
    158 	register_t *retval;
    159 {
    160 	struct linux32_sys_times_args /* {
    161 		syscallarg(linux32_tmsp_t) tms;
    162 	} */ *uap = v;
    163 	struct linux32_tms ltms32;
    164 	struct linux_tms ltms;
    165 	struct linux_tms *ltmsp;
    166 	struct linux_sys_times_args ua;
    167 	caddr_t sg = stackgap_init(l->l_proc, 0);
    168 	int error;
    169 
    170 	ltmsp = stackgap_alloc(l->l_proc, &sg, sizeof(*ltmsp));
    171 	SCARG(&ua, tms) = (struct times *)ltmsp;
    172 
    173 	if ((error = linux_sys_times(l, &ua, retval)) != 0)
    174 		return error;
    175 
    176 	if ((error = copyin(ltmsp, &ltms, sizeof(ltms))) != 0)
    177 		return error;
    178 
    179 	ltms32.ltms32_utime = (linux32_clock_t)ltms.ltms_utime;
    180 	ltms32.ltms32_stime = (linux32_clock_t)ltms.ltms_stime;
    181 	ltms32.ltms32_cutime = (linux32_clock_t)ltms.ltms_cutime;
    182 	ltms32.ltms32_cstime = (linux32_clock_t)ltms.ltms_cstime;
    183 
    184 	if ((error = copyout(&ltms32,
    185 	    NETBSD32PTR64(SCARG(uap, tms)), sizeof(ltms32))) != 0)
    186 		return error;
    187 
    188 	return 0;
    189 }
    190 
    191 int
    192 linux32_sys_stime(l, v, retval)
    193 	struct lwp *l;
    194 	void *v;
    195 	register_t *retval;
    196 {
    197 	struct linux32_sys_stime_args /* {
    198 		syscallarg(linux32_timep_t) t;
    199 	} */ *uap = v;
    200 	struct timespec ts;
    201 	linux32_time_t tt32;
    202 	int error;
    203 
    204 	if ((error = kauth_authorize_system(l->l_cred,
    205 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, NULL, NULL,
    206 	    NULL)) != 0)
    207 		return error;
    208 
    209 	if ((error = copyin(&tt32,
    210 	    NETBSD32PTR64(SCARG(uap, t)),
    211 	    sizeof tt32)) != 0)
    212 
    213 	ts.tv_sec = (long)tt32;
    214 	ts.tv_nsec = 0;
    215 
    216 	return settime(l->l_proc, &ts);
    217 }
    218 
    219 int
    220 linux32_sys_utime(l, v, retval)
    221 	struct lwp *l;
    222 	void *v;
    223 	register_t *retval;
    224 {
    225 	struct linux32_sys_utime_args /* {
    226 		syscallarg(const netbsd32_charp) path;
    227 		syscallarg(linux32_utimbufp_t) times;
    228 	} */ *uap = v;
    229 	struct proc *p = l->l_proc;
    230         caddr_t sg = stackgap_init(p, 0);
    231         struct sys_utimes_args ua;
    232         struct timeval tv[2], *tvp;
    233         struct linux32_utimbuf lut;
    234         int error;
    235 
    236 	NETBSD32TOP_UAP(path, const char);
    237         CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
    238 
    239 
    240         if (NETBSD32PTR64(SCARG(uap, times)) != NULL) {
    241                 if ((error = copyin(NETBSD32PTR64(SCARG(uap, times)),
    242 		    &lut, sizeof lut)))
    243                         return error;
    244 
    245                 tv[0].tv_sec = (long)lut.l_actime;
    246                 tv[0].tv_usec = 0;
    247                 tv[1].tv_sec = (long)lut.l_modtime;
    248 		tv[1].tv_usec = 0;
    249 
    250 	        tvp = (struct timeval *) stackgap_alloc(p, &sg, sizeof(tv));
    251 
    252                 if ((error = copyout(tv, tvp, sizeof(tv))))
    253                         return error;
    254                 SCARG(&ua, tptr) = tvp;
    255         } else {
    256                SCARG(&ua, tptr) = NULL;
    257 	}
    258 
    259         return sys_utimes(l, &ua, retval);
    260 }
    261