• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef INCLUDE_PERFETTO_EXT_IPC_CLIENT_INFO_H_
18 #define INCLUDE_PERFETTO_EXT_IPC_CLIENT_INFO_H_
19 
20 #include "perfetto/base/logging.h"
21 #include "perfetto/ext/base/sys_types.h"
22 #include "perfetto/ext/base/utils.h"
23 #include "perfetto/ext/ipc/basic_types.h"
24 
25 namespace perfetto {
26 namespace ipc {
27 
28 // Passed to Service(s) to identify remote clients.
29 class ClientInfo {
30  public:
31   ClientInfo() = default;
ClientInfo(ClientID client_id,uid_t uid,pid_t pid,base::MachineID machine_id)32   ClientInfo(ClientID client_id,
33              uid_t uid,
34              pid_t pid,
35              base::MachineID machine_id)
36       : client_id_(client_id), uid_(uid), pid_(pid), machine_id_(machine_id) {}
37 
38   bool operator==(const ClientInfo& other) const {
39     return std::tie(client_id_, uid_, pid_, machine_id_) ==
40            std::tie(other.client_id_, other.uid_, other.pid_,
41                     other.machine_id_);
42   }
43   bool operator!=(const ClientInfo& other) const { return !(*this == other); }
44 
45   // For map<> and other sorted containers.
46   bool operator<(const ClientInfo& other) const {
47     PERFETTO_DCHECK(client_id_ != other.client_id_ || *this == other);
48     return client_id_ < other.client_id_;
49   }
50 
is_valid()51   bool is_valid() const { return client_id_ != 0; }
52 
53   // A monotonic counter.
client_id()54   ClientID client_id() const { return client_id_; }
55 
56   // Posix User ID. Comes from the kernel, can be trusted.
uid()57   uid_t uid() const { return uid_; }
58 
59   // Posix process ID. Comes from the kernel and can be trusted.
pid()60   int32_t pid() const { return pid_; }
61 
62   // An integral ID that identifies the machine the client is on.
machine_id()63   base::MachineID machine_id() const { return machine_id_; }
64 
65  private:
66   ClientID client_id_ = 0;
67   // The following fields are emitted to trace packets and should be kept in
68   // sync with perfetto::ClientIdentity.
69   uid_t uid_ = kInvalidUid;
70   pid_t pid_ = base::kInvalidPid;
71   base::MachineID machine_id_ = base::kDefaultMachineID;
72 };
73 
74 }  // namespace ipc
75 }  // namespace perfetto
76 
77 #endif  // INCLUDE_PERFETTO_EXT_IPC_CLIENT_INFO_H_
78