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}
int m_exitCode
Exit code after the program has terminated.
Result wait()
Wait for the program to terminate.
const String m_path
Absolute path to the program to run.
Result exec()
Runs the external program.
Result terminate() const
Terminate the program.
ApplicationLauncher(const char *path, const char **argv)
Constructor.
const int getExitCode() const
Retrieve exit code of the program.
const ProcessID getPid() const
Retrieve Process Identifier of the program.
const char ** m_argv
Array with pointers to program arguments.
ProcessID m_pid
PID of the DatastoreServer.
C int forkexec(const char *path, const char *argv[])
Create a new process and execute program.
Definition forkexec.cpp:27
#define ESRCH
No such process.
Definition errno.h:268
C char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
Definition strerror.cpp:20
C int errno
The lvalue errno is used by many functions to return error values.
C void exit(int status)
Terminate a process.
Definition exit.cpp:21
#define SIGTERM
Termination request.
Definition signal.h:54
ProcessID pid_t
Used for process IDs and process group IDs.
Definition types.h:32
#define WEXITSTATUS(st)
Returns the exit status of the child process.
Definition wait.h:39
C int kill(pid_t pid, int sig)
Send a signal to a process or a group of processes.
Definition kill.cpp:23
C pid_t waitpid(pid_t pid, int *stat_loc, int options)
Wait for a child process to stop or terminate.
Definition waitpid.cpp:23
#define NULL
NULL means zero.
Definition Macros.h:39
u32 ProcessID
Process Identification Number.
Definition Types.h:140
#define ERROR(msg)
Output an error message.
Definition Log.h:61
#define DEBUG(msg)
Output a debug message to standard output.
Definition Log.h:89
The <sys/stat.h> header shall define the stat structure.
Definition stat.h:177