summaryrefslogtreecommitdiff
path: root/lib/forkexec.cc
blob: 67a38427d2832b4e70e402fb35d809289d8b88c3 (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
// nullmailer -- a simple relay-only MTA
// Copyright (C) 2016  Bruce Guenter <bruce@untroubled.org>
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// You can contact me at <bruce@untroubled.org>.  There is also a mailing list
// available to discuss this package.  To subscribe, send an email to
// <nullmailer-subscribe@lists.untroubled.org>.

#include "config.h"
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "cli++/cli++.h"
#include "fdbuf/fdbuf.h"
#include "mystring/mystring.h"
#include "defines.h"
#include "errcodes.h"
#include "forkexec.h"

#define ERR(MSG) do{ ferr << cli_program << ": " << MSG << ": " << strerror(errno) << endl; } while(0)
#define FAIL(MSG) do{ ERR(MSG); return false; }while(0)

static int fdnull = -1;

fork_exec::fork_exec(const char* p)
  : pid(-1), name(p)
{
}

fork_exec::~fork_exec()
{
  wait();
}

bool fork_exec::operator!() const
{
  return pid < 0;
}

bool fork_exec::start(const char* args[], int redirn, int redirs[])
{
  autoclose_pipe pipes[redirn];
  for (int i = 0; i < redirn; i++) {
    if (redirs[i] == REDIRECT_PIPE_TO || redirs[i] == REDIRECT_PIPE_FROM)
      if (!pipes[i].open())
        FAIL("Could not create pipe");
    if (redirs[i] == REDIRECT_NULL)
      if (fdnull < 0)
        if ((fdnull = open("/dev/null", O_RDWR)) < 0)
          FAIL("Could not open \"/dev/null\"");
  }
  if ((pid = fork()) < 0)
    FAIL("Could not fork");
  if (pid == 0) {
    // Child process, exec program
    for (int i = 0; i < redirn; i++) {
      int r = redirs[i];
      if (r == REDIRECT_NULL)
        dup2(fdnull, i);
      else if (r == REDIRECT_PIPE_FROM) {
        dup2(pipes[i][1], i);
        pipes[i].close();
      }
      else if (r == REDIRECT_PIPE_TO) {
        dup2(pipes[i][0], i);
        pipes[i].close();
      }
      else if (r > 0) {
        dup2(r, i);
        if (r >= redirn)
          close(r);
      }
    }
    execv(args[0], (char**)args);
    ERR("Could not exec " << name);
    _exit(ERR_EXEC_FAILED);
  }
  for (int i = 0; i < redirn; i++) {
    if (redirs[i] == REDIRECT_PIPE_TO)
      redirs[i] = pipes[i].extract(1);
    else if (redirs[i] == REDIRECT_PIPE_FROM)
      redirs[i] = pipes[i].extract(0);
  }
  return true;
}

bool fork_exec::start(const char* program, int redirn, int redirs[])
{
  const char* args[2] = { program, NULL };
  return start(args, redirn, redirs);
}

int fork_exec::wait_status()
{
  if (pid > 0) {
    int status;
    if (waitpid(pid, &status, 0) == pid) {
      pid = -1;
      return status;
    }
  }
  return -1;
}

bool fork_exec::wait()
{
  if (pid > 0) {
    int status = wait_status();
    if (status < 0)
      FAIL("Error catching the return value from " << name);
    if (WIFEXITED(status)) {
      status = WEXITSTATUS(status);
      if (status) {
        ferr << cli_program << ": " << name << " failed: " << status << endl;
        return false;
      }
    }
    else
      FAIL(name << " crashed or was killed");
  }
  return true;
}

mystring program_path(const char* basedir, const char* program, const char* envvar)
{
  if (envvar) {
    const char* env;
    if ((env = getenv(envvar)) != NULL)
      return env;
  }
  mystring path = basedir;
  path += '/';
  path += program;
  return path;
}
    
static const char* nqpath()
{
  static mystring cache;
  if (!cache)
    cache = program_path(SBIN_DIR, "nullmailer-queue", "NULLMAILER_QUEUE");
  return cache.c_str();
}

queue_pipe::queue_pipe()
  : fork_exec("nullmailer-queue")
{
}

int queue_pipe::start()
{
  int redirs[] = { REDIRECT_PIPE_TO, REDIRECT_NULL, REDIRECT_NULL };
  if (!fork_exec::start(nqpath(), 3, redirs))
    return -1;
  return redirs[0];
}