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