summaryrefslogtreecommitdiff
path: root/test/testdso.c
blob: d07148a3534f2c97b8b2a8b5c95a19a4d0d5c1e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "apr_general.h"
#include "apr_pools.h"
#include "apr_errno.h"
#include "apr_dso.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#if APR_HAVE_UNISTD_H
#include <unistd.h>
#endif

#define LIB_NAME "mod_test.so"

static void closeapr(void)
{
    apr_terminate();
}

int main (int argc, char ** argv)
{
    apr_dso_handle_t *h = NULL;
    apr_dso_handle_sym_t func1 = NULL;
    apr_dso_handle_sym_t func2 = NULL;
    apr_status_t status;
    apr_pool_t *cont;
    void (*function)(void);
    void (*function1)(int);
    int *retval;
    char filename[256];   

    getcwd(filename, 256);
    strcat(filename, "/");
    strcat(filename, LIB_NAME);

    apr_initialize();
    atexit(closeapr);
        
    if (apr_create_pool(&cont, NULL) != APR_SUCCESS) {
        fprintf(stderr, "Couldn't allocate context.");
        exit(-1);
    }

    fprintf(stdout,"Trying to load DSO now.....................");
    fflush(stdout);
    if ((status = apr_dso_load(&h, filename, cont)) != APR_SUCCESS){
        char my_error[256];
        apr_strerror(status, my_error, sizeof(my_error));
        fprintf(stderr, "%s!\n", my_error);
        exit (-1);
    }
    fprintf(stdout,"OK\n");

    fprintf(stdout,"Trying to get the DSO's attention..........");
    fflush(stdout);
    if ((status = apr_dso_sym(&func1, h, "print_hello")) != APR_SUCCESS) { 
        char my_error[256];
        apr_dso_error(h, my_error, sizeof(my_error));
        fprintf(stderr, "%s\n", my_error);
        exit (-1);
    }        
    fprintf(stdout,"OK\n");
    
    function = (void *)func1;
    (*function)();

    fprintf(stdout,"Saying farewell 5 times....................");
    fflush(stdout);
    if (apr_dso_sym(&func2, h, "print_goodbye") != APR_SUCCESS) {
        fprintf(stderr, "Failed!\n");
        exit (-1);
    }        
    fprintf(stdout,"OK\n");

    function1 = (void *)(int)func2;
    (*function1)(5);

    fprintf(stdout,"Checking how many times I said goodbye..");
    fflush(stdout);
    if (apr_dso_sym(&func1, h, "goodbyes") != APR_SUCCESS) {
        fprintf(stderr, "Failed!\n");
        exit (-1);
    }
    retval = (int *)func1;
    fprintf(stdout,"%d..", (*retval));
    fflush(stdout);
    if ((*retval) == 5){
        fprintf(stderr,"OK\n");
    } else {
        fprintf(stderr,"Failed!\n");
    }
       
    fprintf(stdout,"Trying to unload DSO now...................");
    if (apr_dso_unload(h) != APR_SUCCESS) {
        fprintf(stderr, "Failed!\n");
        exit (-1);
    }
    fprintf(stdout,"OK\n");

    fprintf(stdout,"Checking it's been unloaded................");
    fflush(stdout);
    if (apr_dso_sym(&func1, h, "print_hello") == APR_SUCCESS) {
        fprintf(stderr, "Failed!\n");
        exit (-1);
    }        
    fprintf(stdout,"OK\n");
    
    return 0;
}