settimeofday.c revision 1.1 1 1.1 manu /* $NetBSD: settimeofday.c,v 1.1 2001/09/16 07:41:58 manu Exp $ */
2 1.1 manu
3 1.1 manu /*
4 1.1 manu * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 manu * All rights reserved.
6 1.1 manu *
7 1.1 manu * This code is derived from software contributed to The NetBSD Foundation
8 1.1 manu * by Emmanuel Dreyfus.
9 1.1 manu *
10 1.1 manu * Redistribution and use in source and binary forms, with or without
11 1.1 manu * modification, are permitted provided that the following conditions
12 1.1 manu * are met:
13 1.1 manu * 1. Redistributions of source code must retain the above copyright
14 1.1 manu * notice, this list of conditions and the following disclaimer.
15 1.1 manu * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 manu * notice, this list of conditions and the following disclaimer in the
17 1.1 manu * documentation and/or other materials provided with the distribution.
18 1.1 manu * 3. The name of the author may not be used to endorse or promote products
19 1.1 manu * derived from this software without specific prior written permission.
20 1.1 manu *
21 1.1 manu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 manu * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 manu * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 manu * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 manu * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 1.1 manu * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.1 manu * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 1.1 manu * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 1.1 manu * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 manu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 manu * SUCH DAMAGE.
32 1.1 manu */
33 1.1 manu
34 1.1 manu #include "namespace.h"
35 1.1 manu #include <fcntl.h>
36 1.1 manu #include <paths.h>
37 1.1 manu #include <unistd.h>
38 1.1 manu #include <time.h>
39 1.1 manu #include <sys/types.h>
40 1.1 manu #include <sys/ioctl.h>
41 1.1 manu #include <sys/syscall.h>
42 1.1 manu #include <sys/clockctl.h>
43 1.1 manu
44 1.1 manu #ifdef __weak_alias
45 1.1 manu __weak_alias(settimeofday,_settimeofday)
46 1.1 manu #endif
47 1.1 manu
48 1.1 manu int
49 1.1 manu settimeofday(tv, tzp)
50 1.1 manu const struct timeval *tv;
51 1.1 manu const struct timezone *tzp;
52 1.1 manu {
53 1.1 manu struct clockctl_settimeofday_args args;
54 1.1 manu int error;
55 1.1 manu int fd;
56 1.1 manu quad_t q;
57 1.1 manu int rv;
58 1.1 manu
59 1.1 manu /*
60 1.1 manu * Root always uses the settimeofday syscall
61 1.1 manu */
62 1.1 manu if (geteuid() == 0)
63 1.1 manu goto try_syscall;
64 1.1 manu
65 1.1 manu /*
66 1.1 manu * Try to use /dev/clockctl, and revert to
67 1.1 manu * settimeofday syscall if it fails.
68 1.1 manu */
69 1.1 manu fd = open(_PATH_CLOCKCTL, O_WRONLY, 0);
70 1.1 manu if (fd == -1)
71 1.1 manu goto try_syscall;
72 1.1 manu
73 1.1 manu (void)memcpy(&args.tv, tv, sizeof(tv));
74 1.1 manu (void)memcpy(&args.tzp, tzp, sizeof(tzp));
75 1.1 manu error = ioctl(fd, CLOCKCTL_SETTIMEOFDAY, &args);
76 1.1 manu (void)close(fd);
77 1.1 manu if (!error)
78 1.1 manu return 0;
79 1.1 manu
80 1.1 manu try_syscall:
81 1.1 manu q = __syscall((quad_t)SYS_settimeofday, tv, tzp);
82 1.1 manu if (/* LINTED constant */ sizeof (quad_t) == sizeof (register_t) ||
83 1.1 manu /* LINTED constant */ BYTE_ORDER == LITTLE_ENDIAN)
84 1.1 manu rv = (int)q;
85 1.1 manu else
86 1.1 manu rv = (int)((u_quad_t)q >> 32);
87 1.1 manu return rv;
88 1.1 manu }
89