4
0

mini_epn.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. *
  3. * MINI_EPN.C - Mini Embedded Perl Nagios
  4. * Contributed by Stanley Hopcroft
  5. * Modified by Douglas Warner
  6. *
  7. * This is a sample mini embedded Perl interpreter (hacked out checks.c and
  8. * perlembed) for use in testing Perl plugins.
  9. *
  10. * It can be compiled with the following command (see 'man perlembed' for
  11. * more info):
  12. *
  13. * gcc -omini_epn mini_epn.c `perl -MExtUtils::Embed -e ccopts -e ldopts`
  14. *
  15. * NOTES: The compiled binary needs to be in the same directory as the p1.pl
  16. * file supplied with Nagios (or vice versa)
  17. * When using mini_epn to test perl scripts, you must place positional
  18. * arguments immediately after the file/script and before any arguments
  19. * processed by Getopt
  20. *
  21. */
  22. #include <EXTERN.h>
  23. #include <perl.h>
  24. #include <fcntl.h>
  25. #include <string.h>
  26. /* include PERL xs_init code for module and C library support */
  27. #if defined(__cplusplus)
  28. #define is_cplusplus
  29. #endif
  30. #ifdef is_cplusplus
  31. extern "C" {
  32. #endif
  33. #define NO_XSLOCKS
  34. #include <XSUB.h>
  35. #ifdef is_cplusplus
  36. }
  37. # ifndef EXTERN_C
  38. # define EXTERN_C extern "C"
  39. # endif
  40. #else
  41. # ifndef EXTERN_C
  42. # define EXTERN_C extern
  43. # endif
  44. #endif
  45. EXTERN_C void xs_init _((void));
  46. EXTERN_C void boot_DynaLoader _((CV* cv));
  47. EXTERN_C void xs_init(void)
  48. {
  49. char *file = __FILE__;
  50. dXSUB_SYS;
  51. /* DynaLoader is a special case */
  52. newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
  53. }
  54. static PerlInterpreter *perl = NULL;
  55. int main(int argc, char **argv, char **env)
  56. {
  57. char *embedding[] = { "", "p1.pl" };
  58. char plugin_output[1024];
  59. char buffer[512];
  60. char tmpfname[32];
  61. char fname[32];
  62. char *args[] = {"","0", "", "", NULL };
  63. FILE *fp;
  64. const int command_line_size = 160;
  65. char command_line[command_line_size];
  66. char *ap ;
  67. int exitstatus;
  68. int pclose_result;
  69. #ifdef THREADEDPERL
  70. dTHX;
  71. #endif
  72. dSP;
  73. if ((perl=perl_alloc())==NULL) {
  74. snprintf(buffer,sizeof(buffer),"Error: Could not allocate memory for embedded Perl interpreter!\n");
  75. buffer[sizeof(buffer)-1]='\x0';
  76. printf("%s\n", buffer);
  77. exit(1);
  78. }
  79. perl_construct(perl);
  80. exitstatus=perl_parse(perl,xs_init,2,embedding,NULL);
  81. if (!exitstatus) {
  82. exitstatus=perl_run(perl);
  83. while(printf("Enter file name: ") && fgets(command_line, command_line_size, stdin)) {
  84. /* call the subroutine, passing it the filename as an argument */
  85. command_line[strlen(command_line) -1] = '\0';
  86. strncpy(fname,command_line,strcspn(command_line," "));
  87. fname[strcspn(command_line," ")] = '\x0';
  88. args[0] = fname ;
  89. args[3] = command_line + strlen(fname) + 1 ;
  90. /* generate a temporary filename to which stdout can be redirected. */
  91. sprintf(tmpfname,"/tmp/embedded%d",getpid());
  92. args[2] = tmpfname;
  93. /* call our perl interpreter to compile and optionally cache the command */
  94. perl_call_argv("Embed::Persistent::eval_file", G_DISCARD | G_EVAL, args);
  95. perl_call_argv("Embed::Persistent::run_package", G_DISCARD | G_EVAL, args);
  96. /* check return status */
  97. if(SvTRUE(ERRSV)){
  98. pclose_result=-2;
  99. printf("embedded perl ran %s with error %s\n",fname,SvPV(ERRSV,PL_na));
  100. }
  101. /* read back stdout from script */
  102. fp=fopen(tmpfname, "r");
  103. /* default return string in case nothing was returned */
  104. strcpy(plugin_output,"(No output!)");
  105. fgets(plugin_output,sizeof(plugin_output)-1,fp);
  106. plugin_output[sizeof(plugin_output)-1]='\x0';
  107. fclose(fp);
  108. unlink(tmpfname);
  109. printf("embedded perl plugin output was %d,%s\n",pclose_result, plugin_output);
  110. }
  111. }
  112. PL_perl_destruct_level = 0;
  113. perl_destruct(perl);
  114. perl_free(perl);
  115. exit(exitstatus);
  116. }