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