Home | History | Annotate | Line # | Download | only in sys
ntp_adjtime.c revision 1.9
      1  1.9   hubertf /*	$NetBSD: ntp_adjtime.c,v 1.9 2007/01/17 23:24:22 hubertf Exp $ */
      2  1.1      manu 
      3  1.1      manu /*
      4  1.1      manu  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  1.1      manu  * All rights reserved.
      6  1.1      manu  *
      7  1.1      manu  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1      manu  * by Emmanuel Dreyfus.
      9  1.1      manu  *
     10  1.1      manu  * Redistribution and use in source and binary forms, with or without
     11  1.1      manu  * modification, are permitted provided that the following conditions
     12  1.1      manu  * are met:
     13  1.1      manu  * 1. Redistributions of source code must retain the above copyright
     14  1.1      manu  *    notice, this list of conditions and the following disclaimer.
     15  1.1      manu  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1      manu  *    notice, this list of conditions and the following disclaimer in the
     17  1.1      manu  *    documentation and/or other materials provided with the distribution.
     18  1.1      manu  * 3. The name of the author may not be used to endorse or promote products
     19  1.1      manu  *    derived from this software without specific prior written permission.
     20  1.1      manu  *
     21  1.1      manu  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.1      manu  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.1      manu  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.1      manu  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.1      manu  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     26  1.1      manu  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  1.1      manu  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     28  1.1      manu  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     29  1.1      manu  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      manu  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      manu  * SUCH DAMAGE.
     32  1.1      manu  */
     33  1.1      manu 
     34  1.6     lukem #include <sys/cdefs.h>
     35  1.6     lukem #if defined(LIBC_SCCS) && !defined(lint)
     36  1.9   hubertf __RCSID("$NetBSD: ntp_adjtime.c,v 1.9 2007/01/17 23:24:22 hubertf Exp $");
     37  1.6     lukem #endif /* LIBC_SCCS and not lint */
     38  1.6     lukem 
     39  1.1      manu #include "namespace.h"
     40  1.2   thorpej #include <sys/types.h>
     41  1.2   thorpej #include <sys/time.h>
     42  1.1      manu #include <sys/timex.h>
     43  1.1      manu #include <sys/ioctl.h>
     44  1.1      manu #include <sys/syscall.h>
     45  1.3      manu 
     46  1.1      manu #include <sys/clockctl.h>
     47  1.2   thorpej 
     48  1.9   hubertf #include <errno.h>
     49  1.2   thorpej #include <fcntl.h>
     50  1.2   thorpej #include <paths.h>
     51  1.2   thorpej #include <string.h>
     52  1.2   thorpej #include <unistd.h>
     53  1.9   hubertf 
     54  1.1      manu #ifdef __weak_alias
     55  1.1      manu __weak_alias(ntp_adjtime,_ntp_adjtime)
     56  1.9   hubertf #endif
     57  1.1      manu 
     58  1.3      manu extern int __clockctl_fd;
     59  1.3      manu 
     60  1.1      manu int
     61  1.1      manu ntp_adjtime(tp)
     62  1.1      manu 	struct timex *tp;
     63  1.1      manu {
     64  1.7  christos 	struct clockctl_ntp_adjtime args;
     65  1.1      manu 	int error;
     66  1.1      manu 	quad_t q;
     67  1.1      manu 	int rv;
     68  1.1      manu 
     69  1.3      manu 	/*
     70  1.8    kardel 	 * we always attempt to use the syscall unless we had to
     71  1.8    kardel 	 * use the clockctl device before
     72  1.8    kardel 	 *
     73  1.8    kardel 	 * ntp_adjtime() is callable for mortals if tp->modes == 0 !
     74  1.3      manu 	 */
     75  1.3      manu 	if (__clockctl_fd == -1) {
     76  1.3      manu 		q = __syscall((quad_t)SYS_ntp_adjtime, tp);
     77  1.3      manu 		if (/* LINTED constant */ sizeof (quad_t) == sizeof (register_t)
     78  1.3      manu 		    || /* LINTED constant */ BYTE_ORDER == LITTLE_ENDIAN)
     79  1.3      manu 			rv = (int)q;
     80  1.3      manu 		else
     81  1.9   hubertf 			rv = (int)((u_quad_t)q >> 32);
     82  1.3      manu 
     83  1.3      manu 		/*
     84  1.8    kardel 		 * if we fail with EPERM we try the clockctl device
     85  1.3      manu 		 */
     86  1.3      manu 		if (rv != -1 || errno != EPERM)
     87  1.3      manu 			return rv;
     88  1.3      manu 
     89  1.3      manu 		/*
     90  1.3      manu 		 * If this fails, it means that we are not root
     91  1.9   hubertf 		 * and we cannot open clockctl. This is a true
     92  1.8    kardel 		 * failure.
     93  1.3      manu 		 */
     94  1.3      manu 		__clockctl_fd = open(_PATH_CLOCKCTL, O_WRONLY, 0);
     95  1.8    kardel 		if (__clockctl_fd == -1) {
     96  1.8    kardel 			/* original error was EPERM - don't leak open errors */
     97  1.8    kardel 			errno = EPERM;
     98  1.3      manu 			return -1;
     99  1.8    kardel 		}
    100  1.8    kardel 
    101  1.5        cb 		(void) fcntl(__clockctl_fd, F_SETFD, FD_CLOEXEC);
    102  1.3      manu 	}
    103  1.3      manu 
    104  1.9   hubertf 	/*
    105  1.3      manu 	 * If __clockctl_fd >=0, clockctl has already been open
    106  1.3      manu 	 * and used, so we carry on using it.
    107  1.1      manu 	 */
    108  1.7  christos 	args.tp = tp;
    109  1.3      manu 	error = ioctl(__clockctl_fd, CLOCKCTL_NTP_ADJTIME, &args);
    110  1.1      manu 
    111  1.3      manu 	/*
    112  1.3      manu 	 * There is no way to get retval set through ioctl(), hence we
    113  1.3      manu 	 * have to handle it here, using args.retval
    114  1.1      manu 	 */
    115  1.3      manu 	if (error == 0) {
    116  1.3      manu 		rv = (int)args.retval;
    117  1.3      manu 		return rv;
    118  1.1      manu 	}
    119  1.8    kardel 
    120  1.3      manu 	return error;
    121  1.1      manu }
    122