e6c702528507f554d22ab819744ce96cc2a21e09
[openafs.git] / src / cf / ucontext.m4
1 dnl Test getcontext() and makecontext() to ensure that we are able to
2 dnl copy the current user context, modify it with our own private stack
3 dnl and return to the original user context.
4 dnl
5 AC_DEFUN([OPENAFS_WORKING_UCONTEXT],[
6   AC_MSG_CHECKING([if user context manipulation is complete])
7   AC_TRY_RUN([
8 #include <stdio.h>
9 #include <stdlib.h>
10 #ifdef HAVE_UCONTEXT_H
11 #include <ucontext.h>
12 #endif
13
14 #define STACK_SIZE 16384
15
16 static ucontext_t main_context, thread_context;
17 static char *alt_stack;
18
19 static void
20 thread(void)
21 {
22         unsigned long stack_ptr;
23         unsigned long offset;
24
25         offset = (unsigned long) &stack_ptr - (unsigned long) alt_stack;
26         if (offset > STACK_SIZE)
27                 exit(EXIT_FAILURE);
28         swapcontext(&thread_context, &main_context);
29         /* should never get here */
30         exit(EXIT_FAILURE);
31 }
32
33 int
34 main(int argc, char **argv)
35 {
36         if (getcontext(&thread_context) == -1)
37                 exit(EXIT_FAILURE);
38         alt_stack = malloc(STACK_SIZE);
39         if (!alt_stack)
40                 exit(EXIT_FAILURE);
41         thread_context.uc_stack.ss_sp = alt_stack;
42         thread_context.uc_stack.ss_size = STACK_SIZE;
43         makecontext(&thread_context, thread, 0);
44
45         if (swapcontext(&main_context, &thread_context) == -1)
46                 exit(EXIT_FAILURE);
47
48         free(alt_stack);
49         exit(EXIT_SUCCESS);
50 }],AC_MSG_RESULT([yes])
51    AC_DEFINE([HAVE_WORKING_SWAPCONTEXT],[1],[user context manipulation is complete]),
52    AC_MSG_RESULT([no]))])