aboutsummaryrefslogtreecommitdiffstats
path: root/chromeos/ui/x_server_runner.cc
blob: 57c981793c3671b4900344a75f33b93622e31b8b (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2014 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 "chromeos/ui/x_server_runner.h"

#include <arpa/inet.h>
#include <grp.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/signalfd.h>
#include <sys/wait.h>
#include <unistd.h>
#include <vector>

#include <base/bind.h>
#include <base/command_line.h>
#include <base/file_util.h>
#include <base/files/file.h>
#include <base/logging.h>
#include <base/macros.h>
#include <base/process/launch.h>
#include <base/rand_util.h>
#include <base/strings/string_number_conversions.h>
#include <base/strings/stringprintf.h>

#include "chromeos/bootstat/bootstat.h"
#include "chromeos/ui/util.h"

namespace chromeos {
namespace ui {

namespace {

// Path to the X server binary.
const char kXServerCommand[] = "/usr/bin/X";

// Writes |data| to |file|, returning true on success.
bool WriteString(base::File* file, const std::string& data) {
  return file->WriteAtCurrentPos(data.data(), data.size()) ==
      static_cast<int>(data.size());
}

// Writes |value| to |file| in big-endian order, returning true on success.
bool WriteUint16(base::File* file, uint16 value) {
  value = htons(value);
  return file->WriteAtCurrentPos(
      reinterpret_cast<const char*>(&value), sizeof(value)) ==
      static_cast<int>(sizeof(value));
}

// Creates a new X authority file at |path|, returning true on success.
bool CreateXauthFile(const base::FilePath& path, uid_t uid, gid_t gid) {
  base::File file(path,
      base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
  if (!file.IsValid()) {
    PLOG(ERROR) << "Couldn't open " << path.value();
    return false;
  }
  if (!util::SetPermissions(path, uid, gid, 0600))
    return false;

  const int kCookieSize = 16;
  // TODO(derat): base/rand_util.h says not to use RandBytesAsString() for
  // security-related purposes, but crypto::RandBytes() (which we don't package)
  // just wraps RandBytes(). The base implementation uses /dev/urandom, which is
  // fine for our purposes (see e.g. http://www.2uo.de/myths-about-urandom/),
  // but to make this code self-documenting, this call should be changed to
  // crypto::RandBytes() if/when that gets packaged for Chrome OS.
  const std::string kCookie(base::RandBytesAsString(kCookieSize));
  const uint16 kFamily = 0x100;
  const std::string kAddress = "localhost";
  const std::string kNumber = "0";
  const std::string kName = "MIT-MAGIC-COOKIE-1";

  if (!WriteUint16(&file, kFamily) ||
      !WriteUint16(&file, kAddress.size()) ||
      !WriteString(&file, kAddress) ||
      !WriteUint16(&file, kNumber.size()) ||
      !WriteString(&file, kNumber) ||
      !WriteUint16(&file, kName.size()) ||
      !WriteString(&file, kName) ||
      !WriteUint16(&file, kCookie.size()) ||
      !WriteString(&file, kCookie)) {
    PLOG(ERROR) << "Couldn't write to " << path.value();
    return false;
  }

  return true;
}

// Runs the X server, replacing the current process.
void ExecServer(int vt,
                int max_vt,
                const base::FilePath& xauth_file,
                const base::FilePath& log_file) {
  std::vector<std::string> args;
  args.push_back(kXServerCommand);
  args.push_back("-nohwaccess");
  args.push_back("-noreset");
  args.push_back("-maxvt");
  args.push_back(base::IntToString(max_vt));
  args.push_back("-nolisten");
  args.push_back("tcp");
  args.push_back(base::StringPrintf("vt%d", vt));
  args.push_back("-auth");
  args.push_back(xauth_file.value());
  args.push_back("-logfile");
  args.push_back(log_file.value());

  const size_t kMaxArgs = 32;
  char* argv[kMaxArgs + 1];
  CHECK_LE(args.size(), kMaxArgs);
  for (size_t i = 0; i < args.size(); ++i)
    argv[i] = const_cast<char*>(args[i].c_str());
  argv[args.size()] = NULL;

  // This call doesn't return on success.
  PCHECK(execv(argv[0], argv) == 0) << "execv() failed";
}

// Helper for ExecAndWaitForServer() that reads signals sent from |server_pid|
// via signalfd-created |fd|. Returns true if the server started successfully.
bool WaitForSignalFromServer(pid_t server_pid, int fd) {
  LOG(INFO) << "X server started with PID " << server_pid;
  while (true) {
    struct signalfd_siginfo siginfo;
    int bytes_read = HANDLE_EINTR(read(fd, &siginfo, sizeof(siginfo)));
    PCHECK(bytes_read >= 0);
    if (bytes_read != sizeof(siginfo)) {
      LOG(ERROR) << "Read " << bytes_read << " byte(s); expected "
                 << sizeof(siginfo);
      return false;
    }

    switch (siginfo.ssi_signo) {
      case SIGUSR1:
        LOG(INFO) << "X server is ready for connections";
        return true;
      case SIGCHLD: {
        int status = 0;
        int result = waitpid(server_pid, &status, WNOHANG);
        if (result != 0) {
          PCHECK(result == server_pid) << "waitpid() returned " << result;
          if (WIFEXITED(status)) {
            LOG(ERROR) << "X server exited with " << WEXITSTATUS(status)
                       << " before sending SIGUSR1";
            return false;
          } else if (WIFSIGNALED(status)) {
            LOG(ERROR) << "X server was terminated with signal "
                       << WTERMSIG(status) << " before sending SIGUSR1";
            return false;
          }
        }
        // In the event of a non-exit SIGCHLD, ignore it and loop to
        // read the next signal.
        LOG(INFO) << "Ignoring non-exit SIGCHLD";
        continue;
      }
      default:
        CHECK(false) << "Unexpected signal " << siginfo.ssi_signo;
    }
  }
  return false;
}

// Drops privileges, forks-and-execs the X server, waits for it to emit SIGUSR1
// to indicate that it's ready for connections, and returns true on success.
bool ExecAndWaitForServer(const std::string& user,
                          uid_t uid,
                          gid_t gid,
                          const base::Closure& closure) {
  // Avoid some syscalls when not running as root in tests.
  if (getuid() == 0) {
    if (setpriority(PRIO_PROCESS, 0, -20) != 0)
      PLOG(WARNING) << "setpriority() failed";

    PCHECK(initgroups(user.c_str(), gid) == 0);
    PCHECK(setgid(gid) == 0);
    PCHECK(setuid(uid) == 0);
  }

  sigset_t mask;
  PCHECK(sigemptyset(&mask) == 0);
  PCHECK(sigaddset(&mask, SIGUSR1) == 0);
  PCHECK(sigaddset(&mask, SIGCHLD) == 0);
  const int fd = signalfd(-1, &mask, 0);
  PCHECK(fd != -1) << "signalfd() failed";
  PCHECK(sigprocmask(SIG_BLOCK, &mask, NULL) == 0);

  bool success = false;
  switch (pid_t pid = fork()) {
    case -1:
      PLOG(ERROR) << "fork() failed";
      break;
    case 0:
      // Forked process: exec the X server.
      base::CloseSuperfluousFds(base::InjectiveMultimap());
      PCHECK(sigprocmask(SIG_UNBLOCK, &mask, NULL) == 0);

      // Set SIGUSR1's disposition to SIG_IGN before exec-ing so that X will
      // emit SIGUSR1 once it's ready to accept connections.
      PCHECK(signal(SIGUSR1, SIG_IGN) != SIG_ERR);

      closure.Run();

      // We should never reach this point, but crash just in case to avoid
      // double-closing the FD.
      LOG(FATAL) << "Server closure returned unexpectedly";
      break;
    default:
      // Original process: wait for the forked process to become ready or exit.
      success = WaitForSignalFromServer(pid, fd);
      break;
  }

  close(fd);
  return success;
}

}  // namespace

const char XServerRunner::kSocketDir[] = "/tmp/.X11-unix";
const char XServerRunner::kIceDir[] = "/tmp/.ICE-unix";
const char XServerRunner::kLogFile[] = "/var/log/xorg/Xorg.0.log";
const char XServerRunner::kXkbDir[] = "/var/lib/xkb";

XServerRunner::XServerRunner() : child_pid_(0) {}

XServerRunner::~XServerRunner() {}

bool XServerRunner::StartServer(const std::string& user,
                                int vt,
                                bool allow_vt_switching,
                                const base::FilePath& xauth_file) {
  uid_t uid = 0;
  gid_t gid = 0;
  if (!util::GetUserInfo(user, &uid, &gid))
    return false;

  if (!CreateXauthFile(xauth_file, uid, gid))
    return false;

  if (!util::EnsureDirectoryExists(GetPath(kSocketDir), 0, 0, 01777) ||
      !util::EnsureDirectoryExists(GetPath(kIceDir), 0, 0, 01777))
    return false;

  const base::FilePath log_file(GetPath(kLogFile));
  if (!util::EnsureDirectoryExists(log_file.DirName(), uid, gid, 0755) ||
      !util::EnsureDirectoryExists(GetPath(kXkbDir), uid, gid, 0755))
    return false;

  // Create a relative symlink from one directory above |log_file| to the file
  // itself (e.g. /var/log/Xorg.0.log -> xorg/Xorg.0.log).
  base::CreateSymbolicLink(
      log_file.DirName().BaseName().Append(log_file.BaseName()),
      log_file.DirName().DirName().Append(log_file.BaseName()));

  // Disable all the Ctrl-Alt-Fn shortcuts for switching between virtual
  // terminals if requested. Otherwise, disable only Fn (n>=3) keys.
  int max_vt = allow_vt_switching ? 2 : 0;

  switch (child_pid_ = fork()) {
    case -1:
      PLOG(ERROR) << "fork() failed";
      return false;
    case 0: {
      base::Closure closure = !callback_for_testing_.is_null() ?
          callback_for_testing_ :
          base::Bind(&ExecServer, vt, max_vt, xauth_file, log_file);
      // The child process waits for the server to start and exits with 0.
      exit(ExecAndWaitForServer(user, uid, gid, closure) ? 0 : 1);
    }
    default:
      LOG(INFO) << "Child process " << child_pid_
                << " starting X server in background";
  }
  return true;
}

bool XServerRunner::WaitForServer() {
  CHECK_GT(child_pid_, 0);
  int status = 0;
  if (waitpid(child_pid_, &status, 0) != child_pid_) {
    PLOG(ERROR) << "waitpid() on " << child_pid_ << " failed";
    return false;
  }
  if (!WIFEXITED(status)) {
    LOG(ERROR) << "Child process " << child_pid_ << " didn't exit normally";
    return false;
  }
  if (WEXITSTATUS(status) != 0) {
    LOG(ERROR) << "Child process " << child_pid_ << " exited with "
               << WEXITSTATUS(status);
    return false;
  }

  if (getuid() == 0) {
    // TODO(derat): Move session_manager's UpstartSignalEmitter into libchromeos
    // and use it here.
    util::Run("initctl", "emit", "x-started", NULL);
    bootstat_log("x-started");
  }

  return true;
}

base::FilePath XServerRunner::GetPath(const std::string& path) const {
  return util::GetReparentedPath(path, base_path_for_testing_);
}

}  // namespace ui
}  // namespace chromeos