YggdrasilWM  0.1.1
A tiny window manager coded in C++
Spawn Class Reference

Implement the mecanism to spawn a program. More...

#include <Spawn.hpp>

Inheritance diagram for Spawn:
Inheritance graph
Collaboration diagram for Spawn:
Collaboration graph

Public Member Functions

 Spawn ()
 
 ~Spawn () override=default
 
void execute (const std::string &args) override
 Spawn a program using the command arguments Double fork to avoid zombie processes Parse the command arguments to get the program name and its arguments use of execvp to execute the command so it will search the bin using PATH throw an exception if the command fails. More...
 

Detailed Description

Implement the mecanism to spawn a program.

Constructor & Destructor Documentation

◆ Spawn()

Spawn::Spawn ( )
default

◆ ~Spawn()

Spawn::~Spawn ( )
overridedefault

Member Function Documentation

◆ execute()

void Spawn::execute ( const std::string &  args)
overridevirtual

Spawn a program using the command arguments Double fork to avoid zombie processes Parse the command arguments to get the program name and its arguments use of execvp to execute the command so it will search the bin using PATH throw an exception if the command fails.

Parameters
argsthe first word separated by a space is the binary name, the rest is the arguments

Implements CommandBase.

37  {
38  std::istringstream iss(args);
39  std::string command;
40  iss >> command;
41  std::vector<const char*> argv;
42  argv.push_back(command.c_str());
43  std::string arg;
44  while (iss >> arg) {
45  argv.push_back(arg.c_str());
46  }
47  argv.push_back(nullptr);
48  pid_t pid = fork();
49  if (pid < 0) {
50  throw std::runtime_error("Fork failed: " + std::string(strerror(errno)));
51  } else if (pid == 0) {
52  pid_t pid_inner = fork();
53  if (pid_inner < 0) {
54  exit(EXIT_FAILURE);
55  } else if (pid_inner == 0) {
56  execvp(command.c_str(), const_cast<char* const*>(argv.data()));
57  Logger::GetInstance()->Log("Failed to execute command \""
58  + command
59  + "\": "
60  + strerror(errno),L_ERROR);
61  exit(EXIT_FAILURE);
62  }
63  exit(EXIT_SUCCESS);
64  } else {
65  waitpid(pid, nullptr, 0);
66  }
67  Logger::GetInstance()->Log("Succefully launched " + args,L_INFO);
68 }
@ L_ERROR
Definition: Logger.hpp:51
@ L_INFO
Definition: Logger.hpp:49
virtual void Log(const std::string &message, LogLevel level) const
Log a message This method logs a message to the log file or stream. The message is only logged if the...
Definition: Logger.cpp:73
static Logger * GetInstance()
Get the Logger object.
Definition: Logger.cpp:41

References Logger::GetInstance(), L_ERROR, L_INFO, and Logger::Log().

Here is the call graph for this function:

The documentation for this class was generated from the following files: