1*5c2921b0SApple OSS Distributions /*
2*5c2921b0SApple OSS Distributions * Copyright (c) 2013 Apple Inc. All rights reserved.
3*5c2921b0SApple OSS Distributions *
4*5c2921b0SApple OSS Distributions * @APPLE_LICENSE_HEADER_START@
5*5c2921b0SApple OSS Distributions *
6*5c2921b0SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*5c2921b0SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*5c2921b0SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*5c2921b0SApple OSS Distributions * compliance with the License. Please obtain a copy of the License at
10*5c2921b0SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this
11*5c2921b0SApple OSS Distributions * file.
12*5c2921b0SApple OSS Distributions *
13*5c2921b0SApple OSS Distributions * The Original Code and all software distributed under the License are
14*5c2921b0SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*5c2921b0SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*5c2921b0SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*5c2921b0SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*5c2921b0SApple OSS Distributions * Please see the License for the specific language governing rights and
19*5c2921b0SApple OSS Distributions * limitations under the License.
20*5c2921b0SApple OSS Distributions *
21*5c2921b0SApple OSS Distributions * @APPLE_LICENSE_HEADER_END@
22*5c2921b0SApple OSS Distributions */
23*5c2921b0SApple OSS Distributions
24*5c2921b0SApple OSS Distributions #include <sys/types.h>
25*5c2921b0SApple OSS Distributions #include <sys/time.h>
26*5c2921b0SApple OSS Distributions #include <sys/errno.h>
27*5c2921b0SApple OSS Distributions #include <strings.h>
28*5c2921b0SApple OSS Distributions #include <Availability.h>
29*5c2921b0SApple OSS Distributions
30*5c2921b0SApple OSS Distributions #include "gethostuuid_private.h"
31*5c2921b0SApple OSS Distributions
32*5c2921b0SApple OSS Distributions extern int __gethostuuid(uuid_t, const struct timespec *);
33*5c2921b0SApple OSS Distributions
34*5c2921b0SApple OSS Distributions static volatile int (*_gethostuuid_callback)(uuid_t) = (void *)0;
35*5c2921b0SApple OSS Distributions
36*5c2921b0SApple OSS Distributions int
gethostuuid(uuid_t uuid,const struct timespec * timeout)37*5c2921b0SApple OSS Distributions gethostuuid(uuid_t uuid, const struct timespec *timeout)
38*5c2921b0SApple OSS Distributions {
39*5c2921b0SApple OSS Distributions int result;
40*5c2921b0SApple OSS Distributions
41*5c2921b0SApple OSS Distributions result = __gethostuuid(uuid, timeout);
42*5c2921b0SApple OSS Distributions if ((result == -1) && (errno == EPERM)) {
43*5c2921b0SApple OSS Distributions if (_gethostuuid_callback) {
44*5c2921b0SApple OSS Distributions result = _gethostuuid_callback(uuid);
45*5c2921b0SApple OSS Distributions } else {
46*5c2921b0SApple OSS Distributions /* no fallback, return -1/EPERM */
47*5c2921b0SApple OSS Distributions memset(uuid, 0x00, sizeof(*uuid));
48*5c2921b0SApple OSS Distributions }
49*5c2921b0SApple OSS Distributions }
50*5c2921b0SApple OSS Distributions
51*5c2921b0SApple OSS Distributions return result;
52*5c2921b0SApple OSS Distributions }
53*5c2921b0SApple OSS Distributions
54*5c2921b0SApple OSS Distributions /* SPI to call gethostuuid syscall directly, without fallback, need an entitlement */
55*5c2921b0SApple OSS Distributions int
_getprivatesystemidentifier(uuid_t uuid,const struct timespec * timeout)56*5c2921b0SApple OSS Distributions _getprivatesystemidentifier(uuid_t uuid, const struct timespec *timeout)
57*5c2921b0SApple OSS Distributions {
58*5c2921b0SApple OSS Distributions return __gethostuuid(uuid, timeout);
59*5c2921b0SApple OSS Distributions }
60*5c2921b0SApple OSS Distributions
61*5c2921b0SApple OSS Distributions int
_register_gethostuuid_callback(int (* new_callback)(uuid_t))62*5c2921b0SApple OSS Distributions _register_gethostuuid_callback(int (*new_callback)(uuid_t))
63*5c2921b0SApple OSS Distributions {
64*5c2921b0SApple OSS Distributions if (__sync_bool_compare_and_swap((void **)&_gethostuuid_callback, (void *)0, (void *)new_callback)) {
65*5c2921b0SApple OSS Distributions return 0;
66*5c2921b0SApple OSS Distributions } else {
67*5c2921b0SApple OSS Distributions return EINVAL;
68*5c2921b0SApple OSS Distributions }
69*5c2921b0SApple OSS Distributions }
70