Home | History | Annotate | Line # | Download | only in common
linux32_time.c revision 1.2
      1 /*	$NetBSD: linux32_time.c,v 1.2 2006/02/24 06:39:47 manu 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.2 2006/02/24 06:39:47 manu 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/kernel.h>
     44 #include <sys/fcntl.h>
     45 #include <sys/select.h>
     46 #include <sys/sa.h>
     47 #include <sys/proc.h>
     48 #include <sys/ucred.h>
     49 #include <sys/swap.h>
     50 
     51 #include <machine/types.h>
     52 
     53 #include <sys/syscallargs.h>
     54 
     55 #include <compat/netbsd32/netbsd32.h>
     56 #include <compat/netbsd32/netbsd32_conv.h>
     57 #include <compat/netbsd32/netbsd32_syscallargs.h>
     58 
     59 #include <compat/linux/common/linux_types.h>
     60 #include <compat/linux/common/linux_signal.h>
     61 #include <compat/linux/common/linux_machdep.h>
     62 #include <compat/linux/common/linux_misc.h>
     63 #include <compat/linux/common/linux_oldolduname.h>
     64 #include <compat/linux/linux_syscallargs.h>
     65 
     66 #include <compat/linux32/common/linux32_types.h>
     67 #include <compat/linux32/common/linux32_signal.h>
     68 #include <compat/linux32/common/linux32_machdep.h>
     69 #include <compat/linux32/common/linux32_sysctl.h>
     70 #include <compat/linux32/common/linux32_socketcall.h>
     71 #include <compat/linux32/linux32_syscallargs.h>
     72 
     73 extern struct timezone linux_sys_tz;
     74 int
     75 linux32_sys_gettimeofday(l, v, retval)
     76 	struct lwp *l;
     77 	void *v;
     78 	register_t *retval;
     79 {
     80 	struct linux32_sys_gettimeofday_args /* {
     81 		syscallarg(netbsd32_timevalp_t) tp;
     82 		syscallarg(netbsd32_timezonep_t) tzp;
     83 	} */ *uap = v;
     84 	struct timeval tv;
     85 	struct netbsd32_timeval tv32;
     86 	int error;
     87 
     88 	if (NETBSD32PTR64(SCARG(uap, tp)) != NULL) {
     89 		microtime(&tv);
     90 		netbsd32_from_timeval(&tv, &tv32);
     91 		if ((error = copyout(&tv32,
     92 		    (caddr_t)NETBSD32PTR64(SCARG(uap, tp)),
     93 		    sizeof(tv32))) != 0)
     94 			return error;
     95 	}
     96 
     97 	/* timezone size does not change */
     98 	if (NETBSD32PTR64(SCARG(uap, tzp)) != NULL) {
     99 		if ((error = copyout(&linux_sys_tz,
    100 		    (caddr_t)NETBSD32PTR64(SCARG(uap, tzp)),
    101 		    sizeof(linux_sys_tz))) != 0)
    102 			return error;
    103 	}
    104 
    105 	return 0;
    106 }
    107 
    108 int
    109 linux32_sys_settimeofday(l, v, retval)
    110 	struct lwp *l;
    111 	void *v;
    112 	register_t *retval;
    113 {
    114 	struct linux32_sys_settimeofday_args /* {
    115 		syscallarg(netbsd32_timevalp_t) tp;
    116 		syscallarg(netbsd32_timezonep_t) tzp;
    117 	} */ *uap = v;
    118 	struct linux_sys_settimeofday_args ua;
    119 
    120 	NETBSD32TOP_UAP(tp, struct timeval);
    121 	NETBSD32TOP_UAP(tzp, struct timezone);
    122 
    123 	return linux_sys_settimeofday(l, &ua, retval);
    124 }
    125 
    126 int
    127 linux32_sys_time(l, v, retval)
    128 	struct lwp *l;
    129 	void *v;
    130 	register_t *retval;
    131 {
    132 	struct linux32_sys_time_args /* {
    133 		syscallcarg(linux32_timep_t) t;
    134 	} */ *uap = v;
    135         struct timeval atv;
    136         linux32_time_t tt;
    137         int error;
    138 
    139         microtime(&atv);
    140 
    141         tt = (linux32_time_t)atv.tv_sec;
    142 
    143         if (SCARG(uap, t) && (error = copyout(&tt,
    144 	    NETBSD32PTR64(SCARG(uap, t)), sizeof(tt))))
    145                 return error;
    146 
    147         retval[0] = tt;
    148 
    149         return 0;
    150 }
    151 
    152 
    153 int
    154 linux32_sys_times(l, v, retval)
    155 	struct lwp *l;
    156 	void *v;
    157 	register_t *retval;
    158 {
    159 	struct linux32_sys_times_args /* {
    160 		syscallarg(linux32_tmsp_t) tms;
    161 	} */ *uap = v;
    162 	struct linux32_tms ltms32;
    163 	struct linux_tms ltms;
    164 	struct linux_tms *ltmsp;
    165 	struct linux_sys_times_args ua;
    166 	caddr_t sg = stackgap_init(l->l_proc, 0);
    167 	int error;
    168 
    169 	ltmsp = stackgap_alloc(l->l_proc, &sg, sizeof(*ltmsp));
    170 	SCARG(&ua, tms) = (struct times *)ltmsp;
    171 
    172 	if ((error = linux_sys_times(l, &ua, retval)) != 0)
    173 		return error;
    174 
    175 	if ((error = copyin(ltmsp, &ltms, sizeof(ltms))) != 0)
    176 		return error;
    177 
    178 	ltms32.ltms32_utime = (linux32_clock_t)ltms.ltms_utime;
    179 	ltms32.ltms32_stime = (linux32_clock_t)ltms.ltms_stime;
    180 	ltms32.ltms32_cutime = (linux32_clock_t)ltms.ltms_cutime;
    181 	ltms32.ltms32_cstime = (linux32_clock_t)ltms.ltms_cstime;
    182 
    183 	if ((error = copyout(&ltms32,
    184 	    NETBSD32PTR64(SCARG(uap, tms)), sizeof(ltms32))) != 0)
    185 		return error;
    186 
    187 	return 0;
    188 }
    189 
    190 int
    191 linux32_sys_stime(l, v, retval)
    192 	struct lwp *l;
    193 	void *v;
    194 	register_t *retval;
    195 {
    196 	struct linux32_sys_stime_args /* {
    197 		syscallarg(linux32_timep_t) t;
    198 	} */ *uap = v;
    199 	struct proc *p = l->l_proc;
    200 	struct timespec ts;
    201 	linux32_time_t tt32;
    202 	int error;
    203 
    204 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    205 		return error;
    206 
    207 	if ((error = copyin(&tt32,
    208 	    NETBSD32PTR64(SCARG(uap, t)),
    209 	    sizeof tt32)) != 0)
    210 
    211 	ts.tv_sec = (long)tt32;
    212 	ts.tv_nsec = 0;
    213 
    214 	return settime(p, &ts);
    215 }
    216 
    217 int
    218 linux32_sys_utime(l, v, retval)
    219 	struct lwp *l;
    220 	void *v;
    221 	register_t *retval;
    222 {
    223 	struct linux32_sys_utime_args /* {
    224 		syscallarg(const netbsd32_charp) path;
    225 		syscallarg(linux32_utimbufp_t) times;
    226 	} */ *uap = v;
    227 	struct proc *p = l->l_proc;
    228         caddr_t sg = stackgap_init(p, 0);
    229         struct sys_utimes_args ua;
    230         struct timeval tv[2], *tvp;
    231         struct linux32_utimbuf lut;
    232         int error;
    233 
    234 	NETBSD32TOP_UAP(path, const char);
    235         CHECK_ALT_EXIST(l, &sg, SCARG(&ua, path));
    236 
    237 
    238         if (NETBSD32PTR64(SCARG(uap, times)) != NULL) {
    239                 if ((error = copyin(NETBSD32PTR64(SCARG(uap, times)),
    240 		    &lut, sizeof lut)))
    241                         return error;
    242 
    243                 tv[0].tv_sec = (long)lut.l_actime;
    244                 tv[0].tv_usec = 0;
    245                 tv[1].tv_sec = (long)lut.l_modtime;
    246 		tv[1].tv_usec = 0;
    247 
    248 	        tvp = (struct timeval *) stackgap_alloc(p, &sg, sizeof(tv));
    249 
    250                 if ((error = copyout(tv, tvp, sizeof(tv))))
    251                         return error;
    252                 SCARG(&ua, tptr) = tvp;
    253         } else {
    254                SCARG(&ua, tptr) = NULL;
    255 	}
    256 
    257         return sys_utimes(l, &ua, retval);
    258 }
    259