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