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 7 #include <stddef.h> 8 #include <stdint.h> 9 10 #include <limits> 11 12 #include "base/atomic_sequence_num.h" 13 #include "base/rand_util.h" 14 #include "base/strings/stringprintf.h" 15 #include "build/build_config.h" 16 17 namespace { 18 19 // Global atomic used to guarantee channel IDs are unique. 20 base::AtomicSequenceNumber g_last_id; 21 22 } // namespace 23 24 namespace IPC { 25 26 // static 27 constexpr size_t Channel::kMaximumMessageSize; 28 29 // static GenerateUniqueRandomChannelID()30std::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. 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 43 int process_id = base::GetCurrentProcId(); 44 #endif 45 return base::StringPrintf("%d.%u.%d", 46 process_id, 47 g_last_id.GetNext(), 48 base::RandInt(0, std::numeric_limits<int32_t>::max())); 49 } 50 51 } // namespace IPC 52