6 #include "include/on_exit.h"
7 #include "include/ceph_assert.h"
11 # define MAP_ANONYMOUS MAP_ANON
13 // cppcheck-suppress preprocessorErrorDirective
14 # error "Don't know how to create anonymous mmap"
18 static int func_scope_val
;
20 static void add(void *incp
)
22 func_scope_val
+= *((int*)incp
);
25 static void func_scope(void)
29 int *inc_1
= (int*)malloc(sizeof(*inc_1
));
31 mgr
.add_callback(add
, inc_1
);
33 int *inc_2
= (int*)malloc(sizeof(*inc_2
));
35 mgr
.add_callback(add
, inc_2
);
38 // shared between processes
39 static int *shared_val
;
41 #define MAIN_SCOPE_VAL 0x1111111
42 static OnExitManager main_scope_mgr
;
43 static void main_scope_cb(void *val
)
45 *shared_val
= *((int*)val
);
48 #define EXIT_FUNC_VAL 0x22222222
49 static OnExitManager exit_func_mgr
;
50 static void exit_func_cb(void *val
)
52 *shared_val
= *((int*)val
);
55 static void call_exit()
60 int main(int argc
, char **argv
)
62 // test basic function scope behavior
63 ceph_assert(func_scope_val
== 0);
65 ceph_assert(func_scope_val
== 8);
67 // shared mem for exit tests
68 shared_val
= (int*)mmap(NULL
, sizeof(int),
69 PROT_READ
|PROT_WRITE
, MAP_SHARED
|MAP_ANONYMOUS
, -1, 0);
70 ceph_assert(shared_val
!= MAP_FAILED
);
72 // test normal exit returning from main
75 ceph_assert(pid
>= 0);
78 int ret
= waitpid(pid
, &status
, 0);
79 ceph_assert(ret
== pid
); // should be our child
80 ceph_assert(status
== 0);
81 ceph_assert(*shared_val
== MAIN_SCOPE_VAL
);
83 // child adds a callback to the static scope callback manager and then
84 // exits by returning from main. The parent checks the value after the
85 // child exits via the memory map.
86 ceph_assert(*shared_val
== 0);
87 int *new_val
= (int*)malloc(sizeof(*new_val
));
88 *new_val
= MAIN_SCOPE_VAL
;
89 main_scope_mgr
.add_callback(main_scope_cb
, new_val
);
93 // test exit via exit()
96 ceph_assert(pid
>= 0);
99 int ret
= waitpid(pid
, &status
, 0);
100 ceph_assert(ret
== pid
); // should be our child
101 ceph_assert(WEXITSTATUS(status
) == 3);
102 ceph_assert(*shared_val
== EXIT_FUNC_VAL
);
104 // child adds a callback to the static scope callback manager and then
106 ceph_assert(*shared_val
== 0);
107 int *new_val
= (int*)malloc(sizeof(*new_val
));
108 *new_val
= EXIT_FUNC_VAL
;
109 exit_func_mgr
.add_callback(exit_func_cb
, new_val
);