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