adjtime.c revision 1.2.2.3 1 1.2.2.3 nathanw /* $NetBSD: adjtime.c,v 1.2.2.3 2002/01/28 20:51:26 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.3 nathanw #include <errno.h>
36 1.2.2.2 nathanw #include <fcntl.h>
37 1.2.2.2 nathanw #include <paths.h>
38 1.2.2.2 nathanw #include <unistd.h>
39 1.2.2.2 nathanw #include <time.h>
40 1.2.2.2 nathanw #include <string.h>
41 1.2.2.2 nathanw #include <sys/types.h>
42 1.2.2.2 nathanw #include <sys/ioctl.h>
43 1.2.2.2 nathanw #include <sys/syscall.h>
44 1.2.2.3 nathanw #include <sys/systm.h>
45 1.2.2.3 nathanw
46 1.2.2.2 nathanw #include <sys/clockctl.h>
47 1.2.2.2 nathanw
48 1.2.2.2 nathanw #ifdef __weak_alias
49 1.2.2.2 nathanw __weak_alias(adjtime,_adjtime)
50 1.2.2.2 nathanw #endif
51 1.2.2.2 nathanw
52 1.2.2.3 nathanw extern int __clockctl_fd;
53 1.2.2.3 nathanw
54 1.2.2.2 nathanw int
55 1.2.2.2 nathanw adjtime(delta, olddelta)
56 1.2.2.2 nathanw const struct timeval *delta;
57 1.2.2.2 nathanw struct timeval *olddelta;
58 1.2.2.2 nathanw {
59 1.2.2.3 nathanw struct sys_adjtime_args args;
60 1.2.2.2 nathanw int error;
61 1.2.2.2 nathanw quad_t q;
62 1.2.2.2 nathanw int rv;
63 1.2.2.2 nathanw
64 1.2.2.3 nathanw /*
65 1.2.2.3 nathanw * if __clockctl_fd == -1, then this is not our first time,
66 1.2.2.3 nathanw * and we know root is the calling user. We use the system call
67 1.2.2.3 nathanw */
68 1.2.2.3 nathanw if (__clockctl_fd == -1) {
69 1.2.2.3 nathanw try_syscall:
70 1.2.2.3 nathanw q = __syscall((quad_t)SYS_adjtime, delta, olddelta);
71 1.2.2.3 nathanw if (/* LINTED constant */ sizeof (quad_t) == sizeof (register_t)
72 1.2.2.3 nathanw || /* LINTED constant */ BYTE_ORDER == LITTLE_ENDIAN)
73 1.2.2.3 nathanw rv = (int)q;
74 1.2.2.3 nathanw else
75 1.2.2.3 nathanw rv = (int)((u_quad_t)q >> 32);
76 1.2.2.3 nathanw
77 1.2.2.3 nathanw /*
78 1.2.2.3 nathanw * If credentials changed from root to an unprivilegied
79 1.2.2.3 nathanw * user, and we already had __clockctl_fd = -1, then we
80 1.2.2.3 nathanw * tried the system call as a non root user, it failed
81 1.2.2.3 nathanw * with EPERM, and we will try clockctl.
82 1.2.2.3 nathanw */
83 1.2.2.3 nathanw if (rv != -1 || errno != EPERM)
84 1.2.2.3 nathanw return rv;
85 1.2.2.3 nathanw __clockctl_fd = -2;
86 1.2.2.3 nathanw }
87 1.2.2.3 nathanw
88 1.2.2.3 nathanw /*
89 1.2.2.3 nathanw * If __clockctl_fd = -2 then this is our first time here,
90 1.2.2.3 nathanw * or credentials have changed (the calling process dropped root
91 1.2.2.3 nathanw * root privilege). Check if root is the calling user. If it is,
92 1.2.2.3 nathanw * we try the system call, if it is not, we try clockctl.
93 1.2.2.2 nathanw */
94 1.2.2.3 nathanw if (__clockctl_fd == -2) {
95 1.2.2.3 nathanw /*
96 1.2.2.3 nathanw * Root always uses the syscall
97 1.2.2.3 nathanw */
98 1.2.2.3 nathanw if (geteuid() == 0) {
99 1.2.2.3 nathanw __clockctl_fd = -1;
100 1.2.2.3 nathanw goto try_syscall;
101 1.2.2.3 nathanw }
102 1.2.2.3 nathanw
103 1.2.2.3 nathanw /*
104 1.2.2.3 nathanw * If this fails, it means that we are not root
105 1.2.2.3 nathanw * and we cannot open clockctl. This is a failure.
106 1.2.2.3 nathanw */
107 1.2.2.3 nathanw __clockctl_fd = open(_PATH_CLOCKCTL, O_WRONLY, 0);
108 1.2.2.3 nathanw if (__clockctl_fd == -1)
109 1.2.2.3 nathanw return -1;
110 1.2.2.3 nathanw }
111 1.2.2.2 nathanw
112 1.2.2.2 nathanw /*
113 1.2.2.3 nathanw * If __clockctl_fd >=0, clockctl has already been open
114 1.2.2.3 nathanw * and used, so we carry on using it.
115 1.2.2.2 nathanw */
116 1.2.2.3 nathanw SCARG(&args, delta) = delta;
117 1.2.2.3 nathanw SCARG(&args, olddelta) = olddelta;
118 1.2.2.3 nathanw error = ioctl(__clockctl_fd, CLOCKCTL_ADJTIME, &args);
119 1.2.2.3 nathanw return error;
120 1.2.2.2 nathanw
121 1.2.2.2 nathanw }
122