jschuh@chromium.org | f75a4d1 | 2012-03-17 11:20:46 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium 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 "ipc/ipc_channel.h" |
| 6 | |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 7 | #include <stddef.h> |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
jschuh@chromium.org | f75a4d1 | 2012-03-17 11:20:46 +0900 | [diff] [blame] | 10 | #include <limits> |
| 11 | |
| 12 | #include "base/atomic_sequence_num.h" |
jschuh@chromium.org | f75a4d1 | 2012-03-17 11:20:46 +0900 | [diff] [blame] | 13 | #include "base/rand_util.h" |
avi@chromium.org | e7eaf39 | 2013-06-11 15:32:18 +0900 | [diff] [blame] | 14 | #include "base/strings/stringprintf.h" |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 15 | #include "build/build_config.h" |
jschuh@chromium.org | f75a4d1 | 2012-03-17 11:20:46 +0900 | [diff] [blame] | 16 | |
| 17 | namespace { |
| 18 | |
| 19 | // Global atomic used to guarantee channel IDs are unique. |
tzik | 9b47785 | 2017-07-13 13:54:49 +0900 | [diff] [blame] | 20 | base::AtomicSequenceNumber g_last_id; |
jschuh@chromium.org | f75a4d1 | 2012-03-17 11:20:46 +0900 | [diff] [blame] | 21 | |
| 22 | } // namespace |
| 23 | |
| 24 | namespace IPC { |
| 25 | |
| 26 | // static |
Ken Rockot | 610b17b | 2017-09-19 04:36:21 +0900 | [diff] [blame^] | 27 | constexpr size_t Channel::kMaximumMessageSize; |
| 28 | |
| 29 | // static |
jschuh@chromium.org | f75a4d1 | 2012-03-17 11:20:46 +0900 | [diff] [blame] | 30 | std::string Channel::GenerateUniqueRandomChannelID() { |
| 31 | // Note: the string must start with the current process id, this is how |
| 32 | // some child processes determine the pid of the parent. |
| 33 | // |
| 34 | // This is composed of a unique incremental identifier, the process ID of |
| 35 | // the creator, an identifier for the child instance, and a strong random |
| 36 | // component. The strong random component prevents other processes from |
| 37 | // hijacking or squatting on predictable channel names. |
hidehiko | f43014e | 2015-02-03 16:24:34 +0900 | [diff] [blame] | 38 | #if defined(OS_NACL_NONSFI) |
| 39 | // The seccomp sandbox disallows use of getpid(), so we provide a |
| 40 | // dummy PID. |
| 41 | int process_id = -1; |
| 42 | #else |
bbudge@chromium.org | b562dcc | 2012-06-16 09:35:01 +0900 | [diff] [blame] | 43 | int process_id = base::GetCurrentProcId(); |
hidehiko | f43014e | 2015-02-03 16:24:34 +0900 | [diff] [blame] | 44 | #endif |
jschuh@chromium.org | f75a4d1 | 2012-03-17 11:20:46 +0900 | [diff] [blame] | 45 | return base::StringPrintf("%d.%u.%d", |
bbudge@chromium.org | aa4d16a | 2012-04-24 06:18:19 +0900 | [diff] [blame] | 46 | process_id, |
jschuh@chromium.org | f75a4d1 | 2012-03-17 11:20:46 +0900 | [diff] [blame] | 47 | g_last_id.GetNext(), |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 48 | base::RandInt(0, std::numeric_limits<int32_t>::max())); |
jschuh@chromium.org | f75a4d1 | 2012-03-17 11:20:46 +0900 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | } // namespace IPC |