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