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