Home | History | Annotate | Line # | Download | only in sys
clock_settime.c revision 1.5
      1  1.5       cb /*	$NetBSD: clock_settime.c,v 1.5 2003/07/16 19:42:11 cb 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.1     manu #include "namespace.h"
     35  1.2  thorpej 
     36  1.2  thorpej #include <sys/types.h>
     37  1.2  thorpej #include <sys/ioctl.h>
     38  1.2  thorpej #include <sys/syscall.h>
     39  1.3     manu #include <sys/systm.h>
     40  1.2  thorpej 
     41  1.3     manu #include <errno.h>
     42  1.1     manu #include <fcntl.h>
     43  1.1     manu #include <paths.h>
     44  1.2  thorpej #include <string.h>
     45  1.2  thorpej #include <time.h>
     46  1.1     manu #include <unistd.h>
     47  1.1     manu 
     48  1.1     manu #include <sys/clockctl.h>
     49  1.1     manu 
     50  1.1     manu #ifdef __weak_alias
     51  1.1     manu __weak_alias(clock_settime,_clock_settime)
     52  1.1     manu #endif
     53  1.1     manu 
     54  1.3     manu extern int __clockctl_fd;
     55  1.3     manu 
     56  1.1     manu int
     57  1.1     manu clock_settime(clock_id, tp)
     58  1.1     manu 	clockid_t clock_id;
     59  1.1     manu 	const struct timespec *tp;
     60  1.1     manu {
     61  1.3     manu 	struct sys_clock_settime_args args;
     62  1.1     manu 	int error;
     63  1.1     manu 	quad_t q;
     64  1.1     manu 	int rv;
     65  1.1     manu 
     66  1.3     manu 	/*
     67  1.3     manu 	 * if __clockctl_fd == -1, then this is not our first time,
     68  1.3     manu 	 * and we know root is the calling user. We use the system call
     69  1.3     manu 	 */
     70  1.3     manu 	if (__clockctl_fd == -1) {
     71  1.3     manu try_syscall:
     72  1.3     manu 		q = __syscall((quad_t)SYS_clock_settime, clock_id, tp);
     73  1.3     manu 		if (/* LINTED constant */ sizeof (quad_t) == sizeof (register_t)
     74  1.3     manu 		    || /* LINTED constant */ BYTE_ORDER == LITTLE_ENDIAN)
     75  1.3     manu 			rv = (int)q;
     76  1.3     manu 		else
     77  1.3     manu 			rv = (int)((u_quad_t)q >> 32);
     78  1.3     manu 
     79  1.3     manu 		/*
     80  1.3     manu 		 * If credentials changed from root to an unprivilegied
     81  1.3     manu 		 * user, and we already had __clockctl_fd = -1, then we
     82  1.3     manu 		 * tried the system call as a non root user, it failed
     83  1.3     manu 		 * with EPERM, and we will try clockctl.
     84  1.3     manu 		 */
     85  1.3     manu 		if (rv != -1 || errno != EPERM)
     86  1.3     manu 			return rv;
     87  1.3     manu 		__clockctl_fd = -2;
     88  1.3     manu 	}
     89  1.3     manu 
     90  1.3     manu 	/*
     91  1.3     manu 	 * If __clockctl_fd = -2 then this is our first time here,
     92  1.3     manu 	 * or credentials have changed (the calling process dropped root
     93  1.3     manu 	 * root privilege). Check if root is the calling user. If it is,
     94  1.3     manu 	 * we try the system call, if it is not, we try clockctl.
     95  1.1     manu 	 */
     96  1.3     manu 	if (__clockctl_fd == -2) {
     97  1.3     manu 		/*
     98  1.3     manu 		 * Root always uses the syscall
     99  1.3     manu 		 */
    100  1.3     manu 		if (geteuid() == 0) {
    101  1.3     manu 			__clockctl_fd = -1;
    102  1.3     manu 			goto try_syscall;
    103  1.3     manu 		}
    104  1.3     manu 
    105  1.3     manu 		/*
    106  1.3     manu 		 * If this fails, it means that we are not root
    107  1.3     manu 		 * and we cannot open clockctl. This is a failure.
    108  1.3     manu 		 */
    109  1.3     manu 		__clockctl_fd = open(_PATH_CLOCKCTL, O_WRONLY, 0);
    110  1.3     manu 		if (__clockctl_fd == -1)
    111  1.3     manu 			return -1;
    112  1.5       cb 		(void) fcntl(__clockctl_fd, F_SETFD, FD_CLOEXEC);
    113  1.3     manu 	}
    114  1.1     manu 
    115  1.1     manu 	/*
    116  1.3     manu 	 * If __clockctl_fd >=0, clockctl has already been open
    117  1.3     manu 	 * and used, so we carry on using it.
    118  1.1     manu 	 */
    119  1.3     manu 	SCARG(&args, clock_id) = clock_id;
    120  1.3     manu 	SCARG(&args, tp) = tp;
    121  1.3     manu 	error = ioctl(__clockctl_fd, CLOCKCTL_CLOCK_SETTIME, &args);
    122  1.3     manu 	return error;
    123  1.1     manu 
    124  1.1     manu }
    125