__getosreldate.c revision 205997
1258945Sroberto/*-
2280849Scy * Copyright (c) 2007 Peter Wemm
3258945Sroberto * All rights reserved.
4258945Sroberto *
5258945Sroberto * Redistribution and use in source and binary forms, with or without
6258945Sroberto * modification, are permitted provided that the following conditions
7258945Sroberto * are met:
8258945Sroberto * 1. Redistributions of source code must retain the above copyright
9258945Sroberto *    notice, this list of conditions and the following disclaimer.
10258945Sroberto * 2. Redistributions in binary form must reproduce the above copyright
11258945Sroberto *    notice, this list of conditions and the following disclaimer in the
12258945Sroberto *    documentation and/or other materials provided with the distribution.
13258945Sroberto *
14258945Sroberto * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15258945Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16258945Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17258945Sroberto * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18258945Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19258945Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20258945Sroberto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21258945Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22258945Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23258945Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24258945Sroberto * SUCH DAMAGE.
25258945Sroberto */
26258945Sroberto
27258945Sroberto#include <sys/cdefs.h>
28258945Sroberto__FBSDID("$FreeBSD: head/lib/libc/gen/__getosreldate.c 205997 2010-03-31 18:37:00Z delphij $");
29258945Sroberto
30258945Sroberto#include <sys/param.h>
31258945Sroberto#include <sys/sysctl.h>
32258945Sroberto
33258945Srobertoint __getosreldate(void);
34258945Sroberto
35258945Sroberto/*
36258945Sroberto * This is private to libc.  It is intended for wrapping syscall stubs in order
37258945Sroberto * to avoid having to put SIGSYS signal handlers in place to test for presence
38258945Sroberto * of new syscalls.  This caches the result in order to be as quick as possible.
39258945Sroberto *
40258945Sroberto * Use getosreldate(3) for public use as it respects the $OSVERSION environment
41258945Sroberto * variable.
42258945Sroberto */
43258945Sroberto
44258945Srobertoint
45258945Sroberto__getosreldate(void)
46258945Sroberto{
47258945Sroberto	static int osreldate;
48258945Sroberto	size_t len;
49258945Sroberto	int oid[2];
50258945Sroberto	int error, osrel;
51258945Sroberto
52258945Sroberto	if (osreldate != 0)
53258945Sroberto		return (osreldate);
54258945Sroberto
55258945Sroberto	oid[0] = CTL_KERN;
56258945Sroberto	oid[1] = KERN_OSRELDATE;
57258945Sroberto	osrel = 0;
58258945Sroberto	len = sizeof(osrel);
59258945Sroberto	error = sysctl(oid, 2, &osrel, &len, NULL, 0);
60258945Sroberto	if (error == 0 && osrel > 0 && len == sizeof(osrel))
61258945Sroberto		osreldate = osrel;
62258945Sroberto	return (osreldate);
63258945Sroberto}
64258945Sroberto