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