File: application.h

package info (click to toggle)
wsclean 2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,196 kB
  • sloc: cpp: 34,504; ansic: 234; python: 174; makefile: 10
file content (42 lines) | stat: -rw-r--r-- 975 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef APPLICATION_H
#define APPLICATION_H

#include "wsclean/logger.h"

#include <sys/types.h>
#include <sys/wait.h>

#include <unistd.h>

class Application {
public:
	static void Run(const std::string& commandLine)
	{
		Logger::Info << "Running: " << commandLine << '\n';
		int pid = vfork();
		switch (pid) {
			case -1: // Error
				throw std::runtime_error("Could not vfork() new process for executing command line application");
			case 0: // Child
				execl("/bin/sh", "sh", "-c", commandLine.c_str(), NULL);
				_exit(127);
		}
		// Wait for process to terminate
		int pStatus;
		do {
			int pidReturn;
			do {
				pidReturn = waitpid(pid, &pStatus, 0);
			} while (pidReturn == -1 && errno == EINTR);
		} while(!WIFEXITED(pStatus) && !WIFSIGNALED(pStatus));
		if(WIFEXITED(pStatus))
		{
			// all good
			// const int exitStatus = WEXITSTATUS(pStatus);
		} else {
			throw std::runtime_error("Running command line application returned an error");
		}
	}
};

#endif