blob: de57e5cd2c30b65f69a766393a47fcfff795a7ed [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
Alex Vakulenko262be3f2014-07-30 15:25:50 -07005#include "debugd/src/process_with_id.h"
Elly Jonese7cb5b32011-12-01 14:18:32 -05006
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
Elly Jonese7cb5b32011-12-01 14:18:32 -050014bool ProcessWithId::Init() {
Ben Chana0011d82014-05-13 00:19:29 -070015 return SandboxedProcess::Init() && GenerateId();
Elly Jonese7cb5b32011-12-01 14:18:32 -050016}
17
Ben Chana0011d82014-05-13 00:19:29 -070018bool ProcessWithId::GenerateId() {
Elly Jonese7cb5b32011-12-01 14:18:32 -050019 char buf[16];
Ben Chana0011d82014-05-13 00:19:29 -070020 FILE* urandom = fopen("/dev/urandom", "r");
Elly Jonese7cb5b32011-12-01 14:18:32 -050021 if (!urandom) {
22 PLOG(ERROR) << "Can't open /dev/urandom";
23 return false;
24 }
25 if (fread(&buf, sizeof(buf), 1, urandom) != 1) {
26 PLOG(ERROR) << "Can't read";
27 fclose(urandom);
28 return false;
29 }
30
31 id_ = base::HexEncode(buf, sizeof(buf));
32 fclose(urandom);
33 return true;
34}
35
Ben Chana0011d82014-05-13 00:19:29 -070036} // namespace debugd