blob: daf950f970fbfcbe116a96bd44a14e4345f84b49 [file] [log] [blame]
Elly Jonesa44d22d2012-01-05 18:05:56 -05001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonese7cb5b32011-12-01 14:18:32 -05002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "process_with_id.h"
6
7#include <fcntl.h>
8#include <unistd.h>
9
Ben Chan9953a592014-02-05 23:32:00 -080010#include <base/strings/string_number_conversions.h>
Elly Jonese7cb5b32011-12-01 14:18:32 -050011
12namespace debugd {
13
Ben Chana0011d82014-05-13 00:19:29 -070014ProcessWithId::ProcessWithId() {}
Elly Jonese7cb5b32011-12-01 14:18:32 -050015
16bool ProcessWithId::Init() {
Ben Chana0011d82014-05-13 00:19:29 -070017 return SandboxedProcess::Init() && GenerateId();
Elly Jonese7cb5b32011-12-01 14:18:32 -050018}
19
Ben Chana0011d82014-05-13 00:19:29 -070020bool ProcessWithId::GenerateId() {
Elly Jonese7cb5b32011-12-01 14:18:32 -050021 char buf[16];
Ben Chana0011d82014-05-13 00:19:29 -070022 FILE* urandom = fopen("/dev/urandom", "r");
Elly Jonese7cb5b32011-12-01 14:18:32 -050023 if (!urandom) {
24 PLOG(ERROR) << "Can't open /dev/urandom";
25 return false;
26 }
27 if (fread(&buf, sizeof(buf), 1, urandom) != 1) {
28 PLOG(ERROR) << "Can't read";
29 fclose(urandom);
30 return false;
31 }
32
33 id_ = base::HexEncode(buf, sizeof(buf));
34 fclose(urandom);
35 return true;
36}
37
Ben Chana0011d82014-05-13 00:19:29 -070038} // namespace debugd