FreeNOS
ApplicationLauncher.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <Log.h>
19 #include <stdlib.h>
20 #include <sys/stat.h>
21 #include <sys/wait.h>
22 #include <unistd.h>
23 #include <signal.h>
24 #include <errno.h>
25 #include <string.h>
26 #include "ApplicationLauncher.h"
27 
29  const char **argv)
30  : m_path(path)
31  , m_argv(argv)
32  , m_pid(0)
33  , m_exitCode(0)
34 {
35 }
36 
38 {
39  return m_pid;
40 }
41 
43 {
44  return WEXITSTATUS(m_exitCode);
45 }
46 
48 {
49  int pid;
50 
51 #ifdef __HOST__
52  struct stat buf;
53 
54  if (stat(*m_path, &buf) != 0)
55  {
56  DEBUG("stat failed for " << *m_path << ": " << strerror(errno));
57  return NotFound;
58  }
59 
60  pid = fork();
61  switch (pid)
62  {
63  case 0:
64  execve(*m_path, (char * const *)m_argv, (char * const *)NULL);
65  ERROR("execve failed for " << *m_path << ": " << strerror(errno));
66  exit(1);
67  break;
68 
69  case -1:
70  ERROR("fork failed for " << *m_path << ": " << strerror(errno));
71  break;
72 
73  default:
74  break;
75  }
76 #else
77  pid = forkexec(*m_path, m_argv);
78  if (pid == -1)
79  {
80  DEBUG("forkexec failed for " << *m_path << ": " << strerror(errno));
81  return IOError;
82  }
83 #endif
84 
85  m_pid = pid;
86  return Success;
87 }
88 
90 {
91  if (m_pid == 0)
92  {
93  return InvalidArgument;
94  }
95 
96  if (::kill(m_pid, SIGTERM) == 0)
97  {
98  return Success;
99  }
100  else
101  {
102  return IOError;
103  }
104 }
105 
107 {
108  if (m_pid == 0)
109  {
110  return InvalidArgument;
111  }
112 
113  if (waitpid(m_pid, &m_exitCode, 0) == (pid_t) m_pid)
114  {
115  return Success;
116  }
117  else if (errno == ESRCH)
118  {
119  return NotFound;
120  }
121  else
122  {
123  return IOError;
124  }
125 }
ApplicationLauncher::getPid
const ProcessID getPid() const
Retrieve Process Identifier of the program.
Definition: ApplicationLauncher.cpp:37
ApplicationLauncher::terminate
Result terminate() const
Terminate the program.
Definition: ApplicationLauncher.cpp:89
stat
The <sys/stat.h> header shall define the stat structure.
Definition: stat.h:176
ApplicationLauncher::ApplicationLauncher
ApplicationLauncher(const char *path, const char **argv)
Constructor.
Definition: ApplicationLauncher.cpp:28
ApplicationLauncher::m_path
const String m_path
Absolute path to the program to run.
Definition: ApplicationLauncher.h:98
errno
C int errno
The lvalue errno is used by many functions to return error values.
ApplicationLauncher::m_argv
const char ** m_argv
Array with pointers to program arguments.
Definition: ApplicationLauncher.h:101
string.h
WEXITSTATUS
#define WEXITSTATUS(st)
Returns the exit status of the child process.
Definition: wait.h:39
ProcessID
u32 ProcessID
Process Identification Number.
Definition: Types.h:140
ApplicationLauncher::IOError
@ IOError
Definition: ApplicationLauncher.h:47
ApplicationLauncher::wait
Result wait()
Wait for the program to terminate.
Definition: ApplicationLauncher.cpp:106
ApplicationLauncher::Result
Result
Result code.
Definition: ApplicationLauncher.h:42
wait.h
Log.h
ApplicationLauncher::NotFound
@ NotFound
Definition: ApplicationLauncher.h:46
DEBUG
#define DEBUG(msg)
Output a debug message to standard output.
Definition: Log.h:89
SIGTERM
#define SIGTERM
Termination request.
Definition: signal.h:54
strerror
char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
Definition: strerror.cpp:20
NULL
#define NULL
NULL means zero.
Definition: Macros.h:39
ApplicationLauncher::exec
Result exec()
Runs the external program.
Definition: ApplicationLauncher.cpp:47
signal.h
ApplicationLauncher::m_pid
ProcessID m_pid
PID of the DatastoreServer.
Definition: ApplicationLauncher.h:104
forkexec
int forkexec(const char *path, const char *argv[])
Create a new process and execute program.
Definition: forkexec.cpp:27
ApplicationLauncher::getExitCode
const int getExitCode() const
Retrieve exit code of the program.
Definition: ApplicationLauncher.cpp:42
ESRCH
#define ESRCH
No such process.
Definition: errno.h:268
stat
int stat(const char *path, struct stat *buf)
Get file status.
Definition: stat.cpp:25
pid_t
ProcessID pid_t
Used for process IDs and process group IDs.
Definition: types.h:32
stat.h
unistd.h
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
ApplicationLauncher::InvalidArgument
@ InvalidArgument
Definition: ApplicationLauncher.h:45
ApplicationLauncher::Success
@ Success
Definition: ApplicationLauncher.h:44
kill
C int kill(pid_t pid, int sig)
Send a signal to a process or a group of processes.
Definition: kill.cpp:23
stdlib.h
waitpid
pid_t waitpid(pid_t pid, int *stat_loc, int options)
Wait for a child process to stop or terminate.
Definition: waitpid.cpp:23
ApplicationLauncher.h
ApplicationLauncher::m_exitCode
int m_exitCode
Exit code after the program has terminated.
Definition: ApplicationLauncher.h:107
exit
C void exit(int status)
Terminate a process.
Definition: exit.cpp:21
errno.h