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