summaryrefslogtreecommitdiffstats
path: root/src/Common/Backends/SystemBackendPosix.cpp
blob: ca8b440dc033193f12eba14638f33a6510553044 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * SystemBackendPosix.cpp
 *
 * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "SystemBackendPosix.h"

#include <cstdlib>
#include <cstring>

#include <fcntl.h>
#include <signal.h>
#include <spawn.h>
#include <sys/wait.h>
#include <unistd.h>

#include <sigc++/bind.h>

namespace Mad {
namespace Common {
namespace Backends {

SystemBackendPosix SystemBackendPosix::backend;

std::map<pid_t, sigc::slot<void, int> > SystemBackendPosix::processes;
std::map<pid_t, std::pair<sigc::slot<void, int, const std::string&>, int> > SystemBackendPosix::processesWithOutput;


void SystemBackendPosix::setChildHandler() {
  struct sigaction action;

  action.sa_handler = childHandler;
  sigemptyset(&action.sa_mask);
  action.sa_flags = 0;

  sigaction(SIGCHLD, &action, 0);
}

SystemBackendPosix::~SystemBackendPosix() {
  struct sigaction action;

  action.sa_handler = SIG_DFL;
  sigemptyset(&action.sa_mask);
  action.sa_flags = 0;

  sigaction(SIGCHLD, &action, 0);
}


void SystemBackendPosix::fsInfoCallback(int, const std::string &output, const sigc::slot<void, const std::vector<FSInfo>& > &callback) {
  // TODO Process df output

  callback(std::vector<FSInfo>());
}

bool SystemBackendPosix::fsInfo(const sigc::slot<void, const std::vector<FSInfo>& > &callback) {
  std::vector<std::string> argv;

  argv.push_back("/bin/df");
  argv.push_back("-P");
  argv.push_back("-k");

  return execWithOutput(sigc::bind(sigc::mem_fun(this, &SystemBackendPosix::fsInfoCallback), callback), "/bin/df", argv);
}


void SystemBackendPosix::childHandler(int) {
  int status;
  pid_t pid;

  while((pid = waitpid(-1, &status, WNOHANG)) > 0) {
    std::map<pid_t, sigc::slot<void, int> >::iterator it = processes.find(pid);

    if(it != processes.end()) {
      it->second(status);
      processes.erase(it);
    }
    else {
      std::map<pid_t, std::pair<sigc::slot<void, int, const std::string&>, int> >::iterator it2 = processesWithOutput.find(pid);

      if(it2 != processesWithOutput.end()) {
        char buffer[1024];
        ssize_t n;
        std::string output;

        while((n = read(it2->second.second, buffer, sizeof(buffer))) > 0)
          output += std::string(buffer, n);

        close(it2->second.second);

        it2->second.first(status, output);
        processesWithOutput.erase(it2);
      }
    }
  }

  setChildHandler();
}

std::pair<char**, char**> SystemBackendPosix::makeArgs(const std::string &filename, const std::vector<std::string> &argv, const std::vector<std::string> &env) {
  char **argvp, **envp;

  if(argv.empty()) {
    argvp = new char*[2];

    argvp[0] = strdup(filename.c_str());
    argvp[1] = 0;
  }
  else {
    argvp = new char*[argv.size() + 1];

    for(size_t s = 0; s < argv.size(); ++s) {
      argvp[s] = strdup(argv[s].c_str());
    }

    argvp[argv.size()] = 0;
  }

  if(env.empty()) {
    envp = environ;
  }
  else {
    envp = new char*[env.size() + 1];

    for(size_t s = 0; s < env.size(); ++s) {
      envp[0] = strdup(env[s].c_str());
    }

    envp[env.size()] = 0;
  }

  return std::make_pair(argvp, envp);
}

void SystemBackendPosix::destroyArgs(std::pair<char**, char**> args) {
  for(char **p = args.first; *p != 0; ++p)
    std::free(*p);

  delete [] args.first;

  if(args.second != environ) {
    for(char **p = args.second; *p != 0; ++p)
      std::free(*p);

    delete [] args.second;
  }
}

bool SystemBackendPosix::exec(const sigc::slot<void, int> &resultHandler, const std::string &filename, const std::vector<std::string> &argv, const std::vector<std::string> &env) {
  pid_t pid;
  std::pair<char**, char**> args = makeArgs(filename, argv, env);

  sigset_t set, oldset;
  sigemptyset(&set);
  sigaddset(&set, SIGCHLD);
  sigprocmask(SIG_BLOCK, &set, &oldset);

  bool ret = (posix_spawnp(&pid, filename.c_str(), 0, 0, args.first, args.second) == 0);

  if(ret)
    processes.insert(std::make_pair(pid, resultHandler));

  sigprocmask(SIG_SETMASK, &oldset, 0);

  destroyArgs(args);

  return ret;
}

bool SystemBackendPosix::execWithOutput(const sigc::slot<void, int, const std::string&> &resultHandler, const std::string &filename, const std::vector<std::string> &argv, const std::vector<std::string> &env) {
  pid_t pid;
  std::pair<char**, char**> args = makeArgs(filename, argv, env);

  int saveStdout = dup(STDOUT_FILENO);
  int pipeHandles[2];

  pipe(pipeHandles); // TODO Better pipe handling

  fcntl(pipeHandles[0], F_SETFD, FD_CLOEXEC);

  sigset_t set, oldset;
  sigemptyset(&set);
  sigaddset(&set, SIGCHLD);
  sigprocmask(SIG_BLOCK, &set, &oldset);

  dup2(pipeHandles[1], STDOUT_FILENO); // set the new pipe as stdout
  close(pipeHandles[1]);

  bool ret = (posix_spawnp(&pid, filename.c_str(), 0, 0, args.first, args.second) == 0);

  if(ret)
    processesWithOutput.insert(std::make_pair(pid, std::make_pair(resultHandler, pipeHandles[0])));

  dup2(saveStdout, STDOUT_FILENO); // restore old stdout
  close(saveStdout);

  sigprocmask(SIG_SETMASK, &oldset, 0);

  destroyArgs(args);

  return ret;
}

}
}
}