xref: /dflybsd-src/lib/libc/gen/ovsbrk.c (revision 8618d94a0e2ff8303ad93c123a3fa598c26a116e)
1*8618d94aSMatthew Dillon /*
2*8618d94aSMatthew Dillon  * Copyright (c) 2019 The DragonFly Project.  All rights reserved.
3*8618d94aSMatthew Dillon  *
4*8618d94aSMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
5*8618d94aSMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
6*8618d94aSMatthew Dillon  *
7*8618d94aSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8*8618d94aSMatthew Dillon  * modification, are permitted provided that the following conditions
9*8618d94aSMatthew Dillon  * are met:
10*8618d94aSMatthew Dillon  *
11*8618d94aSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12*8618d94aSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13*8618d94aSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14*8618d94aSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
15*8618d94aSMatthew Dillon  *    the documentation and/or other materials provided with the
16*8618d94aSMatthew Dillon  *    distribution.
17*8618d94aSMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
18*8618d94aSMatthew Dillon  *    contributors may be used to endorse or promote products derived
19*8618d94aSMatthew Dillon  *    from this software without specific, prior written permission.
20*8618d94aSMatthew Dillon  *
21*8618d94aSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*8618d94aSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*8618d94aSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*8618d94aSMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25*8618d94aSMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*8618d94aSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*8618d94aSMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28*8618d94aSMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29*8618d94aSMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30*8618d94aSMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31*8618d94aSMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*8618d94aSMatthew Dillon  * SUCH DAMAGE.
33*8618d94aSMatthew Dillon  */
34*8618d94aSMatthew Dillon /*
35*8618d94aSMatthew Dillon  * As of 500503 the sbrk() system call works.  Prior to 500503 the sbrk()
36*8618d94aSMatthew Dillon  * system call returns EOPNOTSUPP and must be emulated by libc, which we
37*8618d94aSMatthew Dillon  * accomplish with some fake_sbrk() assembly that calls into the even older
38*8618d94aSMatthew Dillon  * 'break' system call.
39*8618d94aSMatthew Dillon  *
40*8618d94aSMatthew Dillon  * Note that in older kernels sbrk() does not ENOSYS or generate a signal.
41*8618d94aSMatthew Dillon  * It returns -1 and EOPNOTSUPP.
42*8618d94aSMatthew Dillon  *
43*8618d94aSMatthew Dillon  * libc generates the normal sbrk() system call where 'sbrk' is a weak
44*8618d94aSMatthew Dillon  * symbol.  We override it here.
45*8618d94aSMatthew Dillon  */
46*8618d94aSMatthew Dillon #include "namespace.h"
47*8618d94aSMatthew Dillon #include <sys/errno.h>
48*8618d94aSMatthew Dillon #include <time.h>
49*8618d94aSMatthew Dillon #include <unistd.h>
50*8618d94aSMatthew Dillon #include "un-namespace.h"
51*8618d94aSMatthew Dillon 
52*8618d94aSMatthew Dillon void *_sbrk(intptr_t incr);
53*8618d94aSMatthew Dillon void *fake_sbrk(intptr_t incr);
54*8618d94aSMatthew Dillon 
55*8618d94aSMatthew Dillon void *
sbrk(intptr_t incr)56*8618d94aSMatthew Dillon sbrk(intptr_t incr)
57*8618d94aSMatthew Dillon {
58*8618d94aSMatthew Dillon 	void *res;
59*8618d94aSMatthew Dillon 
60*8618d94aSMatthew Dillon 	res = _sbrk(incr);
61*8618d94aSMatthew Dillon 	if (res == (void *)-1 && errno == EOPNOTSUPP)
62*8618d94aSMatthew Dillon 		res = fake_sbrk(incr);
63*8618d94aSMatthew Dillon 	return res;
64*8618d94aSMatthew Dillon }
65