1*f14fb602SLionel Sambuc /* $NetBSD: ntp_adjtime.c,v 1.13 2012/03/20 16:26:12 matt Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 2001 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * This code is derived from software contributed to The NetBSD Foundation
82fe8fb19SBen Gras * by Emmanuel Dreyfus.
92fe8fb19SBen Gras *
102fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras * are met:
132fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras * 3. The name of the author may not be used to endorse or promote products
192fe8fb19SBen Gras * derived from this software without specific prior written permission.
202fe8fb19SBen Gras *
212fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
222fe8fb19SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
232fe8fb19SBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
242fe8fb19SBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
252fe8fb19SBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
262fe8fb19SBen Gras * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
272fe8fb19SBen Gras * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
282fe8fb19SBen Gras * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
292fe8fb19SBen Gras * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
302fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
312fe8fb19SBen Gras * SUCH DAMAGE.
322fe8fb19SBen Gras */
332fe8fb19SBen Gras
342fe8fb19SBen Gras #include <sys/cdefs.h>
352fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
36*f14fb602SLionel Sambuc __RCSID("$NetBSD: ntp_adjtime.c,v 1.13 2012/03/20 16:26:12 matt Exp $");
372fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
382fe8fb19SBen Gras
392fe8fb19SBen Gras #include "namespace.h"
402fe8fb19SBen Gras #include <sys/types.h>
412fe8fb19SBen Gras #include <sys/time.h>
422fe8fb19SBen Gras #include <sys/timex.h>
432fe8fb19SBen Gras #include <sys/ioctl.h>
442fe8fb19SBen Gras #include <sys/syscall.h>
452fe8fb19SBen Gras
462fe8fb19SBen Gras #include <sys/clockctl.h>
472fe8fb19SBen Gras
482fe8fb19SBen Gras #include <errno.h>
492fe8fb19SBen Gras #include <fcntl.h>
502fe8fb19SBen Gras #include <paths.h>
512fe8fb19SBen Gras #include <string.h>
522fe8fb19SBen Gras #include <unistd.h>
532fe8fb19SBen Gras
542fe8fb19SBen Gras #ifdef __weak_alias
552fe8fb19SBen Gras __weak_alias(ntp_adjtime,_ntp_adjtime)
562fe8fb19SBen Gras #endif
572fe8fb19SBen Gras
582fe8fb19SBen Gras extern int __clockctl_fd;
592fe8fb19SBen Gras
602fe8fb19SBen Gras int __ntp_adjtime(struct timex *);
612fe8fb19SBen Gras
622fe8fb19SBen Gras int
ntp_adjtime(struct timex * tp)63*f14fb602SLionel Sambuc ntp_adjtime(struct timex *tp)
642fe8fb19SBen Gras {
652fe8fb19SBen Gras struct clockctl_ntp_adjtime args;
662fe8fb19SBen Gras int error;
672fe8fb19SBen Gras int rv;
682fe8fb19SBen Gras
692fe8fb19SBen Gras /*
702fe8fb19SBen Gras * we always attempt to use the syscall unless we had to
712fe8fb19SBen Gras * use the clockctl device before
722fe8fb19SBen Gras *
732fe8fb19SBen Gras * ntp_adjtime() is callable for mortals if tp->modes == 0 !
742fe8fb19SBen Gras */
752fe8fb19SBen Gras if (__clockctl_fd == -1) {
762fe8fb19SBen Gras rv = __ntp_adjtime(tp);
772fe8fb19SBen Gras
782fe8fb19SBen Gras /*
792fe8fb19SBen Gras * if we fail with EPERM we try the clockctl device
802fe8fb19SBen Gras */
812fe8fb19SBen Gras if (rv != -1 || errno != EPERM)
822fe8fb19SBen Gras return rv;
832fe8fb19SBen Gras
842fe8fb19SBen Gras /*
852fe8fb19SBen Gras * If this fails, it means that we are not root
862fe8fb19SBen Gras * and we cannot open clockctl. This is a true
872fe8fb19SBen Gras * failure.
882fe8fb19SBen Gras */
89*f14fb602SLionel Sambuc __clockctl_fd = open(_PATH_CLOCKCTL, O_WRONLY | O_CLOEXEC, 0);
902fe8fb19SBen Gras if (__clockctl_fd == -1) {
912fe8fb19SBen Gras /* original error was EPERM - don't leak open errors */
922fe8fb19SBen Gras errno = EPERM;
932fe8fb19SBen Gras return -1;
942fe8fb19SBen Gras }
952fe8fb19SBen Gras }
962fe8fb19SBen Gras
972fe8fb19SBen Gras /*
982fe8fb19SBen Gras * If __clockctl_fd >=0, clockctl has already been open
992fe8fb19SBen Gras * and used, so we carry on using it.
1002fe8fb19SBen Gras */
1012fe8fb19SBen Gras args.tp = tp;
1022fe8fb19SBen Gras error = ioctl(__clockctl_fd, CLOCKCTL_NTP_ADJTIME, &args);
1032fe8fb19SBen Gras
1042fe8fb19SBen Gras /*
1052fe8fb19SBen Gras * There is no way to get retval set through ioctl(), hence we
1062fe8fb19SBen Gras * have to handle it here, using args.retval
1072fe8fb19SBen Gras */
1082fe8fb19SBen Gras if (error == 0) {
1092fe8fb19SBen Gras rv = (int)args.retval;
1102fe8fb19SBen Gras return rv;
1112fe8fb19SBen Gras }
1122fe8fb19SBen Gras
1132fe8fb19SBen Gras return error;
1142fe8fb19SBen Gras }
115