Began implementing the VCommunication namespace.
[aesalon.git] / monitor / src / program / Launcher.cpp
blob30c1fdc22797dd877027eb2d5ace9f7f146106f5
1 /**
2 Aesalon, a tool to visualize a program's behaviour at run-time.
3 Copyright (C) 2010, Aesalon Development Team.
5 Aesalon is distributed under the terms of the GNU GPLv3. For more
6 licensing information, see the file LICENSE included with the distribution.
8 @file monitor/src/program/Launcher.cpp
12 #include <iostream>
14 #include <sys/wait.h>
16 #include <string.h>
17 #include <errno.h>
19 #include "program/Launcher.h"
21 #include "common/AssertionException.h"
22 #include "common/NYIException.h"
23 #include "Coordinator.h"
24 #include "common/StringTo.h"
25 #include "common/PathSanitizer.h"
26 #include "common/StreamAsString.h"
28 namespace Monitor {
29 namespace Program {
31 Launcher::Launcher(char **argv) : m_argv(argv) {
35 Launcher::~Launcher() {
39 pid_t Launcher::forkTarget() {
40 setupEnvironment();
42 m_targetPid = fork();
43 if(m_targetPid == -1) {
44 std::cout << "Could not fork . . ." << std::endl;
45 exit(1);
47 else if(m_targetPid == 0) {
48 execvp(m_argv[0], m_argv);
49 std::cout << m_argv[0] << ": " << strerror(errno) << std::endl;
50 exit(0);
53 return m_targetPid;
56 void Launcher::waitForChild() {
57 siginfo_t sinfo;
58 waitid(P_PID, m_targetPid, &sinfo, WEXITED);
59 if(sinfo.si_code == CLD_DUMPED || sinfo.si_code == CLD_KILLED)
60 Coordinator::instance()->setReturnValue(128 + sinfo.si_status);
61 else Coordinator::instance()->setReturnValue(sinfo.si_status);
64 void Launcher::setupEnvironment() {
65 /*setenv("AC___conductorFd", (Common::StreamAsString() << m_controllerFds[1]).operator std::string().c_str(), 1);*/
67 setenv("AesalonSHMName", (Common::StreamAsString() << "/Aesalon-" << getpid()).operator std::string().c_str(), 1);
69 std::vector<std::string> modules;
71 Coordinator::instance()->vault()->get("::modules", modules);
73 std::string preload;
75 if(getenv("LD_PRELOAD")) {
76 preload = getenv("LD_PRELOAD");
77 preload += ":";
80 for(std::vector<std::string>::iterator i = modules.begin(); i != modules.end(); ++i) {
81 std::string moduleRoot = Coordinator::instance()->vault()->get(*i + ":root");
82 std::string collectorPath = Coordinator::instance()->vault()->get(*i + ":collectorPath");
83 if(collectorPath.length()) {
84 if(preload.length()) {
85 preload += ":";
87 preload += moduleRoot + collectorPath;
91 setenv("LD_PRELOAD", preload.c_str(), 1);
94 } // namespace Program
95 } // namespace Monitor