Home | History | Annotate | Line # | Download | only in sys
settimeofday.c revision 1.2
      1 /*	$NetBSD: settimeofday.c,v 1.2 2001/09/17 23:52:14 thorpej 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 "namespace.h"
     35 
     36 #include <sys/types.h>
     37 #include <sys/ioctl.h>
     38 #include <sys/syscall.h>
     39 #include <sys/clockctl.h>
     40 
     41 #include <fcntl.h>
     42 #include <paths.h>
     43 #include <string.h>
     44 #include <time.h>
     45 #include <unistd.h>
     46 
     47 #ifdef __weak_alias
     48 __weak_alias(settimeofday,_settimeofday)
     49 #endif
     50 
     51 int
     52 settimeofday(tv, tzp)
     53 	const struct timeval *tv;
     54 	const struct timezone *tzp;
     55 {
     56 	struct clockctl_settimeofday_args args;
     57 	int error;
     58 	int fd;
     59 	quad_t q;
     60 	int rv;
     61 
     62 	/*
     63 	 * Root always uses the settimeofday syscall
     64 	 */
     65 	if (geteuid() == 0)
     66 		goto try_syscall;
     67 
     68 	/*
     69 	 * Try to use /dev/clockctl, and revert to
     70 	 * settimeofday syscall if it fails.
     71 	 */
     72 	fd = open(_PATH_CLOCKCTL, O_WRONLY, 0);
     73 	if (fd == -1)
     74 		goto try_syscall;
     75 
     76 	(void)memcpy(&args.tv, tv, sizeof(tv));
     77 	(void)memcpy(&args.tzp, tzp, sizeof(tzp));
     78 	error = ioctl(fd, CLOCKCTL_SETTIMEOFDAY, &args);
     79 	(void)close(fd);
     80 	if (!error)
     81 		return 0;
     82 
     83 try_syscall:
     84 	q = __syscall((quad_t)SYS_settimeofday, tv, tzp);
     85 	if (/* LINTED constant */ sizeof (quad_t) == sizeof (register_t) ||
     86 		 /* LINTED constant */ BYTE_ORDER == LITTLE_ENDIAN)
     87 		rv = (int)q;
     88 	else
     89 		rv = (int)((u_quad_t)q >> 32);
     90 	return rv;
     91 }
     92