added files
[bcm963xx.git] / userapps / opensource / net-snmp / agent / snmp_perl.c
1 #include <EXTERN.h>
2 #include "perl.h"
3
4 #include <net-snmp/net-snmp-config.h>
5 #include <net-snmp/net-snmp-includes.h>
6 #include <net-snmp/agent/net-snmp-agent-includes.h>
7
8 static PerlInterpreter *my_perl;
9
10 void            boot_DynaLoader(CV * cv);
11
12 void
13 xs_init(void)
14 {
15     char            myfile[] = __FILE__;
16     char            modulename[] = "DynaLoader::boot_DynaLoader";
17     /*
18      * DynaLoader is a special case 
19      */
20     newXS(modulename, boot_DynaLoader, myfile);
21 }
22
23 void
24 maybe_source_perl_startup(void)
25 {
26     const char     *embedargs[] = { "", "" };
27     const char     *perl_init_file = netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,
28                                                            NETSNMP_DS_AGENT_PERL_INIT_FILE);
29     char            init_file[SNMP_MAXBUF];
30
31     static int      have_done_init = 0;
32
33     if (have_done_init)
34         return;
35     have_done_init = 1;
36
37     if (!perl_init_file) {
38         snprintf(init_file, sizeof(init_file) - 1,
39                  "%s/%s", SNMPSHAREPATH, "snmp_perl.pl");
40         perl_init_file = init_file;
41     }
42     embedargs[1] = perl_init_file;
43
44     DEBUGMSGTL(("perl", "initializing perl (%s)\n", embedargs[1]));
45     my_perl = perl_alloc();
46     if (!my_perl)
47         goto bail_out;
48
49     perl_construct(my_perl);
50     if (perl_parse(my_perl, xs_init, 2, (char **) embedargs, NULL))
51         goto bail_out;
52
53     if (perl_run(my_perl))
54         goto bail_out;
55
56     DEBUGMSGTL(("perl", "done initializing perl\n"));
57
58     return;
59
60   bail_out:
61     snmp_log(LOG_ERR, "embedded perl support failed to initalize\n");
62     netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, 
63                            NETSNMP_DS_AGENT_DISABLE_PERL, 1);
64     return;
65 }
66
67 void
68 do_something_perlish(char *something)
69 {
70     if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
71                                NETSNMP_DS_AGENT_DISABLE_PERL)) {
72         return;
73     }
74     maybe_source_perl_startup();
75     if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
76                                NETSNMP_DS_AGENT_DISABLE_PERL)) {
77         return;
78     }
79     DEBUGMSGTL(("perl", "calling perl\n"));
80 #ifdef HAVE_EVAL_PV
81     /* newer perl */
82     eval_pv(something, TRUE);
83 #else
84 #ifdef HAVE_PERL_EVAL_PV
85     /* older perl */
86     perl_eval_pv(something, TRUE);
87 #endif /* HAVE_PERL_EVAL_PV */
88 #endif /* HAVE_EVAL_PV */
89     DEBUGMSGTL(("perl", "finished calling perl\n"));
90 }
91
92 void
93 perl_config_handler(const char *token, char *line)
94 {
95     do_something_perlish(line);
96 }
97
98 void
99 init_perl(void)
100 {
101     const char     *appid = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, 
102                                                   NETSNMP_DS_LIB_APPTYPE);
103     const char     *defaultid = "snmpd";
104
105     if (!appid) {
106         appid = defaultid;
107     }
108
109     /*
110      * register config handlers 
111      */
112     snmpd_register_config_handler("perl", perl_config_handler, NULL,
113                                   "PERLCODE");
114
115     /*
116      * define the perlInitFile token to point to an init file 
117      */
118     netsnmp_ds_register_premib(ASN_OCTET_STR, appid, "perlInitFile",
119                                NETSNMP_DS_APPLICATION_ID, 
120                                NETSNMP_DS_AGENT_PERL_INIT_FILE);
121
122     /*
123      * define the perlInitFile token to point to an init file 
124      */
125     netsnmp_ds_register_premib(ASN_BOOLEAN, appid, "disablePerl",
126                                NETSNMP_DS_APPLICATION_ID,
127                                NETSNMP_DS_AGENT_DISABLE_PERL);
128 }
129
130 void
131 shutdown_perl(void)
132 {
133     if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, 
134                                NETSNMP_DS_AGENT_DISABLE_PERL)) {
135         return;
136     }
137     DEBUGMSGTL(("perl", "shutting down perl\n"));
138     perl_destruct(my_perl);
139     perl_free(my_perl);
140     DEBUGMSGTL(("perl", "finished shutting down perl\n"));
141 }