run_simple_2.cc 2.75 KB
Newer Older
1
/*
2
** Copyright 2012-2014 Merethis
3
**
4
** This file is part of Centreon Perl Connector.
5
**
6
** Centreon Perl Connector is free software: you can redistribute it
7
8
9
10
** and/or modify it under the terms of the GNU Affero General Public
** License as published by the Free Software Foundation, either version
** 3 of the License, or (at your option) any later version.
**
11
** Centreon Perl Connector is distributed in the hope that it will be
12
13
14
15
16
** useful, but WITHOUT ANY WARRANTY; without even the implied warranty
** of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public
17
** License along with Centreon Perl Connector. If not, see
18
19
20
** <http://www.gnu.org/licenses/>.
*/

21
22
#include <cstdio>
#include <cstring>
23
24
25
26
#include <iostream>
#include <string>
#include <sys/wait.h>
#include "com/centreon/connector/perl/embedded_perl.hh"
27
#include "com/centreon/connector/perl/pipe_handle.hh"
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "com/centreon/io/file_stream.hh"
#include "com/centreon/logging/engine.hh"

using namespace com::centreon;
using namespace com::centreon::connector::perl;

/**
 *  Try executing a very simple Perl script.
 *
 *  @param[in] argc Argument count.
 *  @param[in] argv Argument values.
 *  @param[in] env  Process environment.
 *
 *  @return 0 on success.
 */
int main(int argc, char* argv[], char* env[]) {
  // Initialization.
  logging::engine::load();
46
  pipe_handle::load();
47
48
49
50
51
52
  embedded_perl::load(&argc, &argv, &env);

  // Return value.
  int retval(EXIT_FAILURE);

  // Write simple Perl script.
53
  std::string script_path(com::centreon::io::file_stream::temp_path());
54
  try {
55
    com::centreon::io::file_stream fs;
56
    fs.open(script_path.c_str(), "w");
57
58
59
60
61
    char const* data(
      "my $x;\n"
      "my $y = 40;\n"
      "$x = 2;\n"
      "exit $x + $y;\n");
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    unsigned int size(strlen(data));
    unsigned int rb(1);
    do {
      rb = fs.write(data, size);
      size -= rb;
      data += rb;
    } while ((rb > 0) && (size > 0));

    // Compile and execute script.
    int fds[3];
    pid_t child(embedded_perl::instance().run(script_path, fds));

    // Wait for child termination.
    int status;
    if (waitpid(child, &status, 0) == child)
      retval = !(WIFEXITED(status) && (WEXITSTATUS(status) == 42));
Dorian Guillois's avatar
Dorian Guillois committed
78
79
80
    if (retval)
      std::cerr << "execute script failed: exit_code=" << retval
                << ", status=" << WEXITSTATUS(status) << std::endl;
81
82
83

    // Remove temporary file.
    remove(script_path.c_str());
84
85
86
87
88
89
90
  }
  catch (std::exception const& e) {
    std::cerr << e.what() << std::endl;
  }
  catch (...) {
    std::cerr << "unknown error" << std::endl;
  }
91
92
93

  // Unload.
  embedded_perl::unload();
94
  pipe_handle::unload();
95
96
  logging::engine::unload();

97
98
  return (retval);
}