1*f14fb602SLionel Sambuc /* $NetBSD: clock_settime.c,v 1.12 2011/10/15 23:00:02 christos 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: clock_settime.c,v 1.12 2011/10/15 23:00:02 christos Exp $");
372fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
382fe8fb19SBen Gras
392fe8fb19SBen Gras #include "namespace.h"
402fe8fb19SBen Gras
412fe8fb19SBen Gras #include <sys/types.h>
422fe8fb19SBen Gras #include <sys/ioctl.h>
432fe8fb19SBen Gras #include <sys/syscall.h>
442fe8fb19SBen Gras
452fe8fb19SBen Gras #include <errno.h>
462fe8fb19SBen Gras #include <fcntl.h>
472fe8fb19SBen Gras #include <paths.h>
482fe8fb19SBen Gras #include <string.h>
492fe8fb19SBen Gras #include <time.h>
502fe8fb19SBen Gras #include <unistd.h>
512fe8fb19SBen Gras
522fe8fb19SBen Gras #include <sys/clockctl.h>
532fe8fb19SBen Gras
542fe8fb19SBen Gras extern int __clockctl_fd;
552fe8fb19SBen Gras
562fe8fb19SBen Gras int ____clock_settime50(clockid_t, const struct timespec *);
572fe8fb19SBen Gras
582fe8fb19SBen Gras int
clock_settime(clockid_t clock_id,const struct timespec * tp)592fe8fb19SBen Gras clock_settime(clockid_t clock_id, const struct timespec *tp)
602fe8fb19SBen Gras {
612fe8fb19SBen Gras struct clockctl_clock_settime args;
622fe8fb19SBen Gras int rv;
632fe8fb19SBen Gras
642fe8fb19SBen Gras /*
652fe8fb19SBen Gras * always try the syscall first and attempt to switch to
662fe8fb19SBen Gras * clockctl if that fails.
672fe8fb19SBen Gras */
682fe8fb19SBen Gras if (__clockctl_fd == -1) {
692fe8fb19SBen Gras rv = ____clock_settime50(clock_id, tp);
702fe8fb19SBen Gras
712fe8fb19SBen Gras /*
722fe8fb19SBen Gras * return unless we failed with EPERM
732fe8fb19SBen Gras */
742fe8fb19SBen Gras if (rv != -1 || errno != EPERM)
752fe8fb19SBen Gras return rv;
762fe8fb19SBen Gras
772fe8fb19SBen Gras /*
782fe8fb19SBen Gras * If this fails, it means that we are not root
792fe8fb19SBen Gras * and we cannot open clockctl. This is a failure.
802fe8fb19SBen Gras */
81*f14fb602SLionel Sambuc __clockctl_fd = open(_PATH_CLOCKCTL, O_WRONLY | O_CLOEXEC, 0);
822fe8fb19SBen Gras if (__clockctl_fd == -1) {
832fe8fb19SBen Gras /* original error was EPERM - don't leak open errors */
842fe8fb19SBen Gras errno = EPERM;
852fe8fb19SBen Gras return -1;
862fe8fb19SBen Gras }
872fe8fb19SBen Gras }
882fe8fb19SBen Gras
892fe8fb19SBen Gras /*
902fe8fb19SBen Gras * If __clockctl_fd >=0, clockctl has already been open
912fe8fb19SBen Gras * and used, so we carry on using it.
922fe8fb19SBen Gras */
932fe8fb19SBen Gras args.clock_id = clock_id;
942fe8fb19SBen Gras args.tp = tp;
952fe8fb19SBen Gras return ioctl(__clockctl_fd, CLOCKCTL_CLOCK_SETTIME, &args);
962fe8fb19SBen Gras }
97