Elly Jones | a44d22d | 2012-01-05 18:05:56 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 5 | #include "debugd/src/process_with_id.h" |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 6 | |
| 7 | #include <fcntl.h> |
| 8 | #include <unistd.h> |
| 9 | |
Ben Chan | 9953a59 | 2014-02-05 23:32:00 -0800 | [diff] [blame] | 10 | #include <base/strings/string_number_conversions.h> |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 11 | |
| 12 | namespace debugd { |
| 13 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 14 | bool ProcessWithId::Init() { |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 15 | return SandboxedProcess::Init() && GenerateId(); |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 16 | } |
| 17 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 18 | bool ProcessWithId::GenerateId() { |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 19 | char buf[16]; |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 20 | FILE* urandom = fopen("/dev/urandom", "r"); |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 21 | 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 Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 36 | } // namespace debugd |