blob: 284d79bb3faf2f5114fa35b07674db6a60a30040 [file] [log] [blame]
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +09001// 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
avi42ebda42015-12-22 11:39:04 +09007#include <stddef.h>
tfarina1cbfa082015-09-05 03:47:57 +09008#include <stdint.h>
9
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090010#include <limits>
11
12#include "base/atomic_sequence_num.h"
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090013#include "base/rand_util.h"
avi@chromium.orge7eaf392013-06-11 15:32:18 +090014#include "base/strings/stringprintf.h"
avi42ebda42015-12-22 11:39:04 +090015#include "build/build_config.h"
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090016
17namespace {
18
19// Global atomic used to guarantee channel IDs are unique.
tzik9b477852017-07-13 13:54:49 +090020base::AtomicSequenceNumber g_last_id;
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090021
22} // namespace
23
24namespace IPC {
25
26// static
Ken Rockot610b17b2017-09-19 04:36:21 +090027constexpr size_t Channel::kMaximumMessageSize;
28
29// static
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090030std::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.
hidehikof43014e2015-02-03 16:24:34 +090038#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.orgb562dcc2012-06-16 09:35:01 +090043 int process_id = base::GetCurrentProcId();
hidehikof43014e2015-02-03 16:24:34 +090044#endif
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090045 return base::StringPrintf("%d.%u.%d",
bbudge@chromium.orgaa4d16a2012-04-24 06:18:19 +090046 process_id,
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090047 g_last_id.GetNext(),
tfarina1cbfa082015-09-05 03:47:57 +090048 base::RandInt(0, std::numeric_limits<int32_t>::max()));
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090049}
50
51} // namespace IPC