softwarecontainer  0.18.0-739e8d7 2017-05-04
jobabstract.cpp
1 /*
2  * Copyright (C) 2016-2017 Pelagicore AB
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * For further information see LICENSE
18  */
19 
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <fcntl.h>
23 #include "jobabstract.h"
24 
25 namespace softwarecontainer {
26 
27 JobAbstract::JobAbstract(ExecutablePtr &executable) :
28  m_executable(executable),
29  m_exitStatus(-1)
30 {
31 }
32 
33 JobAbstract::~JobAbstract()
34 {
35 }
36 
37 void JobAbstract::captureStdin()
38 {
39  pipe(m_stdin);
40 }
41 
42 void JobAbstract::setOutputFile(const std::string &path)
43 {
44  mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
45  m_stdout[1] = m_stderr[1] = ::open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, mode);
46  log_debug() << "stdout/stderr redirected to " << path << " fd:" << m_stdout[0];
47 }
48 
49 void JobAbstract::captureStdout()
50 {
51  pipe(m_stdout);
52 }
53 
54 void JobAbstract::captureStderr()
55 {
56  pipe(m_stderr);
57 }
58 
59 int JobAbstract::wait()
60 {
61  m_exitStatus = waitForProcessTermination(m_pid);
62  return m_exitStatus;
63 }
64 
65 int JobAbstract::stdout()
66 {
67  return m_stdout[0];
68 }
69 
70 int JobAbstract::stderr()
71 {
72  return m_stderr[0];
73 }
74 
75 int JobAbstract::stdin()
76 {
77  return m_stdin[1];
78 }
79 
80 pid_t JobAbstract::pid()
81 {
82  return m_pid;
83 }
84 
86 {
87  if (0 == m_exitStatus) {
88  return true;
89  }
90 
91  return false;
92 }
93 
95 {
96  return !isSuccess();
97 }
98 
104 {
105  // TODO : find a way to test whether the exec() which occurs in the container succeeded
106  return (m_pid != 0);
107 }
108 
109 void JobAbstract::setEnvironmentVariable(const std::string &key, const std::string &value)
110 {
111  m_env[key] = value;
112 }
113 
114 void JobAbstract::setEnvironmentVariables(const EnvironmentVariables &env)
115 {
116  m_env = env;
117 }
118 
119 } // namespace softwarecontainer
bool isRunning()
That method always returns true as soon as the start() method has been called, even if the command fa...
int waitForProcessTermination(pid_t pid)
waitForProcessTermination Waits for a process to terminate and then returns the status of the process...
Developers guide to adding a config item:
bool isSuccess()
Helper about return value of jobs.
Definition: jobabstract.cpp:85
bool isError()
Helper about return value of jobs.
Definition: jobabstract.cpp:94