]> git.saurik.com Git - apple/libc.git/blame - gen/OSSystemInfo.c
Libc-262.2.12.tar.gz
[apple/libc.git] / gen / OSSystemInfo.c
CommitLineData
734aad71
A
1/*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26#include <sys/types.h>
27#include <sys/sysctl.h>
28#include <stdbool.h>
29
30static int osi_oid[2] = {-1, 0};
31
32bool
33OSSystemInfo(int selector, unsigned long long *resultp)
34{
35 int oid[3];
36 size_t size;
37
38 /*
39 * Check cached OID, look it up if we haven't already.
40 *
41 * NB. Whilst this isn't strictly thread safe, since the
42 * result as written by any thread will be the same
43 * there is no actual risk of corruption.
44 */
45 if (osi_oid[0] == -1) {
46 size = 2;
47 if (sysctlnametomib("hw.systeminfo", &osi_oid, &size) ||
48 (size != 2))
49 return(false);
50 }
51
52 /* build OID */
53 oid[0] = osi_oid[0];
54 oid[1] = osi_oid[1];
55 oid[2] = selector;
56
57 /* make the call */
58 size = sizeof(*resultp);
59 if (sysctl(oid, 3, resultp, &size, NULL, 0) ||
60 (size != sizeof(*resultp)))
61 return(false);
62
63 return(true);
64}
65