lin
2025-08-01 633231e833e21d5b8b1c00cb15aedb62b3b78e8f
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
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#include "run_command.h"
 
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
 
#include <string>
 
#include "base/logging.h"
 
#include "compat/string.h"
 
namespace quipper {
 
namespace {
 
bool CloseFdOnExec(int fd) {
  int fd_flags = fcntl(fd, F_GETFD);
  if (fd_flags == -1) {
    PLOG(ERROR) << "F_GETFD";
    return false;
  }
  if (fcntl(fd, F_SETFD, fd_flags | FD_CLOEXEC)) {
    PLOG(ERROR) << "F_SETFD FD_CLOEXEC";
    return false;
  }
  return true;
}
 
void ReadFromFd(int fd, std::vector<char>* output) {
  static const int kReadSize = 4096;
  ssize_t read_sz;
  size_t read_off = output->size();
  do {
    output->resize(read_off + kReadSize);
    do {
      read_sz = read(fd, output->data() + read_off, kReadSize);
    } while (read_sz < 0 && errno == EINTR);
    if (read_sz < 0) {
      PLOG(FATAL) << "read";
      break;
    }
    read_off += read_sz;
  } while (read_sz > 0);
  output->resize(read_off);
}
 
}  // namespace
 
int RunCommand(const std::vector<string>& command, std::vector<char>* output) {
  std::vector<char*> c_str_cmd;
  c_str_cmd.reserve(command.size() + 1);
  for (const auto& c : command) {
    // This cast is safe: POSIX states that exec shall not modify argv nor the
    // strings pointed to by argv.
    c_str_cmd.push_back(const_cast<char*>(c.c_str()));
  }
  c_str_cmd.push_back(nullptr);
 
  // Create pipe for stdout:
  int output_pipefd[2];
  if (output) {
    if (pipe(output_pipefd)) {
      PLOG(ERROR) << "pipe";
      return -1;
    }
  }
 
  // Pipe for the child to return errno if exec fails:
  int errno_pipefd[2];
  if (pipe(errno_pipefd)) {
    PLOG(ERROR) << "pipe for errno";
    return -1;
  }
  if (!CloseFdOnExec(errno_pipefd[1])) return -1;
 
  const pid_t child = fork();
  if (child == 0) {
    close(errno_pipefd[0]);
 
    if (output) {
      if (close(output_pipefd[0]) < 0) {
        PLOG(FATAL) << "close read end of pipe";
      }
    }
 
    int devnull_fd = open("/dev/null", O_WRONLY);
    if (devnull_fd < 0) {
      PLOG(FATAL) << "open /dev/null";
    }
 
    if (dup2(output ? output_pipefd[1] : devnull_fd, 1) < 0) {
      PLOG(FATAL) << "dup2 stdout";
    }
 
    if (dup2(devnull_fd, 2) < 0) {
      PLOG(FATAL) << "dup2 stderr";
    }
 
    if (close(devnull_fd) < 0) {
      PLOG(FATAL) << "close /dev/null";
    }
 
    execvp(c_str_cmd[0], c_str_cmd.data());
    int exec_errno = errno;
 
    // exec failed... Write errno to a pipe so parent can retrieve it.
    int ret;
    do {
      ret = write(errno_pipefd[1], &exec_errno, sizeof(exec_errno));
    } while (ret < 0 && errno == EINTR);
    close(errno_pipefd[1]);
 
    std::_Exit(EXIT_FAILURE);
  }
 
  if (close(errno_pipefd[1])) {
    PLOG(FATAL) << "close write end of errno pipe";
  }
  if (output) {
    if (close(output_pipefd[1]) < 0) {
      PLOG(FATAL) << "close write end of pipe";
    }
  }
 
  // Check for errno:
  int child_exec_errno;
  int read_errno_res;
  do {
    read_errno_res =
        read(errno_pipefd[0], &child_exec_errno, sizeof(child_exec_errno));
  } while (read_errno_res < 0 && errno == EINTR);
  if (read_errno_res < 0) {
    PLOG(FATAL) << "read errno";
  }
  if (close(errno_pipefd[0])) {
    PLOG(FATAL) << "close errno";
  }
 
  if (read_errno_res > 0) {
    // exec failed in the child.
    while (waitpid(child, nullptr, 0) < 0 && errno == EINTR) {
    }
    errno = child_exec_errno;
    return -1;
  }
 
  // Read stdout from pipe.
  if (output) {
    ReadFromFd(output_pipefd[0], output);
    if (close(output_pipefd[0])) {
      PLOG(FATAL) << "close output";
    }
  }
 
  // Wait for child.
  int exit_status;
  while (waitpid(child, &exit_status, 0) < 0 && errno == EINTR) {
  }
  errno = 0;
  if (WIFEXITED(exit_status)) return WEXITSTATUS(exit_status);
  return -1;
}
 
}  // namespace quipper