blob: fb7ed45a53fd2bc1a110ce3841375febd7d02e4f [file] [log] [blame]
Elly Jonesa44d22d2012-01-05 18:05:56 -05001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jones00e439d2011-12-19 16:59:10 -05002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Vakulenko262be3f2014-07-30 15:25:50 -07005#include "debugd/src/tracepath_tool.h"
Elly Jones00e439d2011-12-19 16:59:10 -05006
Alex Vakulenko262be3f2014-07-30 15:25:50 -07007#include "debugd/src/process_with_id.h"
Elly Jones00e439d2011-12-19 16:59:10 -05008
9namespace debugd {
10
Ben Chanaf125862017-02-08 23:11:18 -080011namespace {
12
Ben Chana0011d82014-05-13 00:19:29 -070013const char kTracepath[] = "/usr/sbin/tracepath";
Mike Frysinger9bab10a2015-05-18 03:35:32 -040014const char kTracepath6[] = "/usr/sbin/tracepath6";
Elly Jones00e439d2011-12-19 16:59:10 -050015
Ben Chanaf125862017-02-08 23:11:18 -080016} // namespace
17
Tom Hughesd6c2d392020-08-24 18:12:11 -070018std::string TracePathTool::Start(const base::ScopedFD& outfd,
19 const std::string& destination,
20 const brillo::VariantDictionary& options) {
Ben Chan0d840a72018-09-27 00:24:48 -070021 ProcessWithId* p =
22 CreateProcess(true /* sandboxed */, false /* access_root_mount_ns */);
Elly Jones00e439d2011-12-19 16:59:10 -050023 if (!p)
24 return "";
Mike Frysinger9bab10a2015-05-18 03:35:32 -040025
Eric Carusocc7106c2017-04-27 14:22:42 -070026 if (brillo::GetVariantValueOrDefault<bool>(options, "v6"))
Mike Frysinger9bab10a2015-05-18 03:35:32 -040027 p->AddArg(kTracepath6);
28 else
29 p->AddArg(kTracepath);
30
Eric Carusocc7106c2017-04-27 14:22:42 -070031 if (options.count("numeric") == 1)
Elly Jones00e439d2011-12-19 16:59:10 -050032 p->AddArg("-n");
Eric Carusocc7106c2017-04-27 14:22:42 -070033
Elly Jones00e439d2011-12-19 16:59:10 -050034 p->AddArg(destination);
Eric Caruso0b241882018-04-04 13:43:46 -070035 p->BindFd(outfd.get(), STDOUT_FILENO);
36 p->BindFd(outfd.get(), STDERR_FILENO);
Elly Jones00e439d2011-12-19 16:59:10 -050037 LOG(INFO) << "tracepath: running process id: " << p->id();
38 p->Start();
39 return p->id();
40}
41
Ben Chana0011d82014-05-13 00:19:29 -070042} // namespace debugd