xref: /aosp_15_r20/external/libchrome/ipc/ipc_channel.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_channel.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
8*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include <limits>
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include "base/atomic_sequence_num.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/rand_util.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/strings/stringprintf.h"
15*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
16*635a8641SAndroid Build Coastguard Worker 
17*635a8641SAndroid Build Coastguard Worker namespace {
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker // Global atomic used to guarantee channel IDs are unique.
20*635a8641SAndroid Build Coastguard Worker base::AtomicSequenceNumber g_last_id;
21*635a8641SAndroid Build Coastguard Worker 
22*635a8641SAndroid Build Coastguard Worker }  // namespace
23*635a8641SAndroid Build Coastguard Worker 
24*635a8641SAndroid Build Coastguard Worker namespace IPC {
25*635a8641SAndroid Build Coastguard Worker 
26*635a8641SAndroid Build Coastguard Worker // static
27*635a8641SAndroid Build Coastguard Worker constexpr size_t Channel::kMaximumMessageSize;
28*635a8641SAndroid Build Coastguard Worker 
29*635a8641SAndroid Build Coastguard Worker // static
GenerateUniqueRandomChannelID()30*635a8641SAndroid Build Coastguard Worker std::string Channel::GenerateUniqueRandomChannelID() {
31*635a8641SAndroid Build Coastguard Worker   // Note: the string must start with the current process id, this is how
32*635a8641SAndroid Build Coastguard Worker   // some child processes determine the pid of the parent.
33*635a8641SAndroid Build Coastguard Worker   //
34*635a8641SAndroid Build Coastguard Worker   // This is composed of a unique incremental identifier, the process ID of
35*635a8641SAndroid Build Coastguard Worker   // the creator, an identifier for the child instance, and a strong random
36*635a8641SAndroid Build Coastguard Worker   // component. The strong random component prevents other processes from
37*635a8641SAndroid Build Coastguard Worker   // hijacking or squatting on predictable channel names.
38*635a8641SAndroid Build Coastguard Worker #if defined(OS_NACL_NONSFI)
39*635a8641SAndroid Build Coastguard Worker   // The seccomp sandbox disallows use of getpid(), so we provide a
40*635a8641SAndroid Build Coastguard Worker   // dummy PID.
41*635a8641SAndroid Build Coastguard Worker   int process_id = -1;
42*635a8641SAndroid Build Coastguard Worker #else
43*635a8641SAndroid Build Coastguard Worker   int process_id = base::GetCurrentProcId();
44*635a8641SAndroid Build Coastguard Worker #endif
45*635a8641SAndroid Build Coastguard Worker   return base::StringPrintf("%d.%u.%d",
46*635a8641SAndroid Build Coastguard Worker       process_id,
47*635a8641SAndroid Build Coastguard Worker       g_last_id.GetNext(),
48*635a8641SAndroid Build Coastguard Worker       base::RandInt(0, std::numeric_limits<int32_t>::max()));
49*635a8641SAndroid Build Coastguard Worker }
50*635a8641SAndroid Build Coastguard Worker 
51*635a8641SAndroid Build Coastguard Worker }  // namespace IPC
52