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
25Init::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: " <<
50 return IOError;
51 }
52
53 // Wait for the command to finish before terminating
54 waitpid(pid, &status, 0);
55 return Success;
56}
Result
Result codes.
Definition Application.h:54
const ArgumentContainer & arguments() const
Get program arguments.
ArgumentParser & parser()
Get program arguments parser.
const char * get(const char *name) const
Get argument by name.
void setDescription(const String &desc)
Set program description.
Result registerFlag(char arg, const char *name, const char *description)
Register a flag Argument.
virtual Result exec()
Execute the application.
Definition Init.cpp:36
virtual ~Init()
Destructor.
Definition Init.cpp:32
Init(int argc, char **argv)
Constructor.
Definition Init.cpp:25
POSIX-compatible application.
int runProgram(const char *path, const char **argv)
Runs an external program.
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 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 ERROR(msg)
Output an error message.
Definition Log.h:61
#define NOTICE(msg)
Output a notice message.
Definition Log.h:75
#define ZERO
Zero value.
Definition Macros.h:43