1 /* $NetBSD: settimeofday.c,v 1.13 2009/01/11 02:46:30 christos Exp $ */
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 __RCSID("$NetBSD: settimeofday.c,v 1.13 2009/01/11 02:46:30 christos Exp $");
37 #endif /* LIBC_SCCS and not lint */
39 #include "namespace.h"
41 #include <sys/types.h>
42 #include <sys/ioctl.h>
43 #include <sys/syscall.h>
45 #include <sys/clockctl.h>
54 int __clockctl_fd
= -1;
56 int ____settimeofday50(const struct timeval
*, const void *);
59 settimeofday(const struct timeval
*tv
, const void *tzp
)
61 struct clockctl_settimeofday args
;
65 * try syscal first and attempt to switch to clockctl
66 * if that fails with EPERM
68 if (__clockctl_fd
== -1) {
69 rv
= ____settimeofday50(tv
, tzp
);
72 * switch to clockctl if we fail with EPERM, this
73 * may be cause by an attempt to set the time backwards
74 * but we should leave the access permission checking
75 * entirely to the kernel
77 if (rv
!= -1 || errno
!= EPERM
)
80 __clockctl_fd
= open(_PATH_CLOCKCTL
, O_WRONLY
, 0);
81 if (__clockctl_fd
== -1) {
82 /* original error was EPERM - don't leak open errors */
87 (void) fcntl(__clockctl_fd
, F_SETFD
, FD_CLOEXEC
);
91 * If __clockctl_fd >=0, clockctl has already been open
92 * and used, so we carry on using it.
96 return ioctl(__clockctl_fd
, CLOCKCTL_SETTIMEOFDAY
, &args
);