Leosac  0.8.0
Open Source Access Control
unixshellscript.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
26 #include "unixshellscript.hpp"
27 
28 extern "C" {
29 #include <stdio.h>
30 }
31 
32 #include <sstream>
33 
35 #include "tools/log.hpp"
36 #include "unixfs.hpp"
37 #include "unixsyscall.hpp"
38 
39 using namespace Leosac::Tools;
40 
41 UnixShellScript::UnixShellScript(const std::string &script)
42  : _script(script)
43 {
44 }
45 
46 int UnixShellScript::run(const std::string &args)
47 {
48  FILE *stream;
49  std::ostringstream oss;
50  char buffer[BufferSize];
51  std::string line = _script + ' ' + args;
52  int ret;
53 
54  INFO("CmdLine: " << line);
55  if (!(stream = popen(line.c_str(), "r")))
56  throw(ScriptException(UnixSyscall::getErrorString("popen", errno) +
57  " command: '" + line + '\''));
58  while (fgets(buffer, BufferSize, stream))
59  oss << buffer;
60  _output = oss.str();
61  if ((ret = pclose(stream)) == -1 && errno)
62  throw(ScriptException(UnixSyscall::getErrorString("pclose", errno)));
63  return (ret);
64 }
65 
66 const std::string &UnixShellScript::getOutput() const
67 {
68  return (_output);
69 }
unixshellscript.hpp
UnixShellScript class declaration.
unixsyscall.hpp
unix syscall helper functions
INFO
@ INFO
Definition: log.hpp:34
Leosac::Tools::UnixShellScript::_script
const std::string _script
Definition: unixshellscript.hpp:71
Leosac::Tools::UnixSyscall::getErrorString
static std::string getErrorString(const std::string &func, int errNo)
Definition: unixsyscall.cpp:32
Leosac::Tools::UnixShellScript::run
int run(const std::string &args=std::string())
Definition: unixshellscript.cpp:46
unixfs.hpp
unix filesystem helper functions
Leosac::Tools::UnixShellScript::UnixShellScript
UnixShellScript(const std::string &script)
Definition: unixshellscript.cpp:41
Leosac::Tools::UnixShellScript::getOutput
const std::string & getOutput() const
Definition: unixshellscript.cpp:66
Leosac::Tools::UnixShellScript::BufferSize
static const std::size_t BufferSize
Definition: unixshellscript.hpp:38
Leosac::Tools::UnixShellScript::_output
std::string _output
Definition: unixshellscript.hpp:72
log.hpp
scriptexception.hpp
Exception class for Script related errors.
Leosac::Tools
Definition: DatabaseLogSink.hpp:27
ScriptException
Definition: scriptexception.hpp:33