blob: 284d79bb3faf2f5114fa35b07674db6a60a30040 [file] [log] [blame]
[email protected]5c41e6e12012-03-17 02:20:461// 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
avi246998d82015-12-22 02:39:047#include <stddef.h>
tfarina10a5c062015-09-04 18:47:578#include <stdint.h>
9
[email protected]5c41e6e12012-03-17 02:20:4610#include <limits>
11
12#include "base/atomic_sequence_num.h"
[email protected]5c41e6e12012-03-17 02:20:4613#include "base/rand_util.h"
[email protected]4aa794a12013-06-11 06:32:1814#include "base/strings/stringprintf.h"
avi246998d82015-12-22 02:39:0415#include "build/build_config.h"
[email protected]5c41e6e12012-03-17 02:20:4616
17namespace {
18
19// Global atomic used to guarantee channel IDs are unique.
tzikbc4270b2017-07-13 04:54:4920base::AtomicSequenceNumber g_last_id;
[email protected]5c41e6e12012-03-17 02:20:4621
22} // namespace
23
24namespace IPC {
25
26// static
Ken Rockot8b8c9062017-09-18 19:36:2127constexpr size_t Channel::kMaximumMessageSize;
28
29// static
[email protected]5c41e6e12012-03-17 02:20:4630std::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.
hidehiko763f8be2015-02-03 07:24:3438#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
[email protected]21f97752012-06-16 00:35:0143 int process_id = base::GetCurrentProcId();
hidehiko763f8be2015-02-03 07:24:3444#endif
[email protected]5c41e6e12012-03-17 02:20:4645 return base::StringPrintf("%d.%u.%d",
[email protected]fe5d4062012-04-23 21:18:1946 process_id,
[email protected]5c41e6e12012-03-17 02:20:4647 g_last_id.GetNext(),
tfarina10a5c062015-09-04 18:47:5748 base::RandInt(0, std::numeric_limits<int32_t>::max()));
[email protected]5c41e6e12012-03-17 02:20:4649}
50
51} // namespace IPC