FreeNOS
Init.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 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 <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <sys/wait.h>
23 #include "Init.h"
24 
25 Init::Init(int argc, char **argv)
26  : POSIXApplication(argc, argv)
27 {
28  parser().setDescription("Initialize system processes");
29  parser().registerFlag('s', "script", "Set shell startup script");
30 }
31 
33 {
34 }
35 
37 {
38  const char *script = arguments().get("script") ?
39  arguments().get("script") : "/etc/init.sh";
40  const char *av[] = { "/bin/sh", script, ZERO };
41  int pid, status;
42 
43  NOTICE("Starting init script: " << script);
44 
45  // Execute the run commands file
46  if ((pid = runProgram("/bin/sh", av)) == -1)
47  {
48  ERROR("failed to execute /bin/sh: " <<
49  strerror(errno));
50  return IOError;
51  }
52 
53  // Wait for the command to finish before terminating
54  waitpid(pid, &status, 0);
55  return Success;
56 }
ArgumentContainer::get
const char * get(const char *name) const
Get argument by name.
Definition: ArgumentContainer.cpp:49
errno
C int errno
The lvalue errno is used by many functions to return error values.
NOTICE
#define NOTICE(msg)
Output a notice message.
Definition: Log.h:75
string.h
POSIXApplication
POSIX-compatible application.
Definition: POSIXApplication.h:35
Application::Success
@ Success
Definition: Application.h:55
Application::arguments
const ArgumentContainer & arguments() const
Get program arguments.
Definition: Application.cpp:112
ArgumentParser::setDescription
void setDescription(const String &desc)
Set program description.
Definition: ArgumentParser.cpp:95
wait.h
Init::~Init
virtual ~Init()
Destructor.
Definition: Init.cpp:32
Application::IOError
@ IOError
Definition: Application.h:57
POSIXApplication::runProgram
int runProgram(const char *path, const char **argv)
Runs an external program.
Definition: POSIXApplication.cpp:39
Init.h
strerror
char * strerror(int errnum)
The strerror function maps the number in errnum to a message string.
Definition: strerror.cpp:20
Application::Result
Result
Result codes.
Definition: Application.h:53
ArgumentParser::registerFlag
Result registerFlag(char arg, const char *name, const char *description)
Register a flag Argument.
Definition: ArgumentParser.cpp:100
Init::Init
Init(int argc, char **argv)
Constructor.
Definition: Init.cpp:25
Init::exec
virtual Result exec()
Execute the application.
Definition: Init.cpp:36
unistd.h
Application::parser
ArgumentParser & parser()
Get program arguments parser.
Definition: Application.cpp:102
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
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
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
errno.h