xref: /freebsd/lib/libc/gen/trivial-getcontextx.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1*4b12fb61SEd Maste /*
2*4b12fb61SEd Maste  * Copyright (c) 2011 Konstantin Belousov <kib@FreeBSD.org>
3*4b12fb61SEd Maste  * All rights reserved.
4*4b12fb61SEd Maste  *
5*4b12fb61SEd Maste  * Redistribution and use in source and binary forms, with or without
6*4b12fb61SEd Maste  * modification, are permitted provided that the following conditions
7*4b12fb61SEd Maste  * are met:
8*4b12fb61SEd Maste  *
9*4b12fb61SEd Maste  * 1. Redistributions of source code must retain the above copyright
10*4b12fb61SEd Maste  *    notice, this list of conditions and the following disclaimer.
11*4b12fb61SEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
12*4b12fb61SEd Maste  *    notice, this list of conditions and the following disclaimer in the
13*4b12fb61SEd Maste  *    documentation and/or other materials provided with the distribution.
14*4b12fb61SEd Maste  *
15*4b12fb61SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*4b12fb61SEd Maste  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*4b12fb61SEd Maste  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*4b12fb61SEd Maste  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*4b12fb61SEd Maste  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*4b12fb61SEd Maste  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*4b12fb61SEd Maste  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*4b12fb61SEd Maste  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*4b12fb61SEd Maste  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*4b12fb61SEd Maste  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*4b12fb61SEd Maste  */
26*4b12fb61SEd Maste 
27*4b12fb61SEd Maste #include <sys/types.h>
28*4b12fb61SEd Maste #include <sys/ucontext.h>
29*4b12fb61SEd Maste #include <errno.h>
30*4b12fb61SEd Maste #include <stdlib.h>
31*4b12fb61SEd Maste 
32*4b12fb61SEd Maste int
__getcontextx_size(void)33*4b12fb61SEd Maste __getcontextx_size(void)
34*4b12fb61SEd Maste {
35*4b12fb61SEd Maste 
36*4b12fb61SEd Maste 	return (sizeof(ucontext_t));
37*4b12fb61SEd Maste }
38*4b12fb61SEd Maste 
39*4b12fb61SEd Maste int
__fillcontextx2(char * ctx)40*4b12fb61SEd Maste __fillcontextx2(char *ctx)
41*4b12fb61SEd Maste {
42*4b12fb61SEd Maste 
43*4b12fb61SEd Maste 	return (0);
44*4b12fb61SEd Maste }
45*4b12fb61SEd Maste 
46*4b12fb61SEd Maste int
__fillcontextx(char * ctx)47*4b12fb61SEd Maste __fillcontextx(char *ctx)
48*4b12fb61SEd Maste {
49*4b12fb61SEd Maste 	ucontext_t *ucp;
50*4b12fb61SEd Maste 
51*4b12fb61SEd Maste 	ucp = (ucontext_t *)ctx;
52*4b12fb61SEd Maste 	return (getcontext(ucp));
53*4b12fb61SEd Maste }
54*4b12fb61SEd Maste 
55*4b12fb61SEd Maste __weak_reference(__getcontextx, getcontextx);
56*4b12fb61SEd Maste 
57*4b12fb61SEd Maste ucontext_t *
__getcontextx(void)58*4b12fb61SEd Maste __getcontextx(void)
59*4b12fb61SEd Maste {
60*4b12fb61SEd Maste 	char *ctx;
61*4b12fb61SEd Maste 	int error;
62*4b12fb61SEd Maste 
63*4b12fb61SEd Maste 	ctx = malloc(__getcontextx_size());
64*4b12fb61SEd Maste 	if (ctx == NULL)
65*4b12fb61SEd Maste 		return (NULL);
66*4b12fb61SEd Maste 	if (__fillcontextx(ctx) == -1) {
67*4b12fb61SEd Maste 		error = errno;
68*4b12fb61SEd Maste 		free(ctx);
69*4b12fb61SEd Maste 		errno = error;
70*4b12fb61SEd Maste 		return (NULL);
71*4b12fb61SEd Maste 	}
72*4b12fb61SEd Maste 	return ((ucontext_t *)ctx);
73*4b12fb61SEd Maste }
74