blob: ac09c5ab1ef1d00b31c4f3a2e7b0a260673dbfbe [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
7#include <limits>
8
9#include "base/atomic_sequence_num.h"
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090010#include "base/rand_util.h"
avi@chromium.orge7eaf392013-06-11 15:32:18 +090011#include "base/strings/stringprintf.h"
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090012
13namespace {
14
15// Global atomic used to guarantee channel IDs are unique.
16base::StaticAtomicSequenceNumber g_last_id;
17
18} // namespace
19
20namespace IPC {
21
22// static
23std::string Channel::GenerateUniqueRandomChannelID() {
24 // Note: the string must start with the current process id, this is how
25 // some child processes determine the pid of the parent.
26 //
27 // This is composed of a unique incremental identifier, the process ID of
28 // the creator, an identifier for the child instance, and a strong random
29 // component. The strong random component prevents other processes from
30 // hijacking or squatting on predictable channel names.
hidehikof43014e2015-02-03 16:24:34 +090031#if defined(OS_NACL_NONSFI)
32 // The seccomp sandbox disallows use of getpid(), so we provide a
33 // dummy PID.
34 int process_id = -1;
35#else
bbudge@chromium.orgb562dcc2012-06-16 09:35:01 +090036 int process_id = base::GetCurrentProcId();
hidehikof43014e2015-02-03 16:24:34 +090037#endif
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090038 return base::StringPrintf("%d.%u.%d",
bbudge@chromium.orgaa4d16a2012-04-24 06:18:19 +090039 process_id,
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090040 g_last_id.GetNext(),
41 base::RandInt(0, std::numeric_limits<int32>::max()));
jschuh@chromium.orgf75a4d12012-03-17 11:20:46 +090042}
43
44} // namespace IPC