mini_epn.c 3.5 KB

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