Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | // 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 | |
| 10 | #include <base/string_number_conversions.h> |
| 11 | |
| 12 | namespace debugd { |
| 13 | |
| 14 | ProcessWithId::ProcessWithId() { } |
| 15 | |
| 16 | bool ProcessWithId::Init() { |
| 17 | return generate_id(); |
| 18 | } |
| 19 | |
| 20 | bool ProcessWithId::generate_id() { |
| 21 | char buf[16]; |
| 22 | FILE *urandom = fopen("/dev/urandom", "r"); |
| 23 | 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 | |
| 38 | }; // namespace debugd |