mini_epn.c 3.6 KB

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