2 * Copyright (C) 2013-2015 Kay Sievers
3 * Copyright (C) 2013-2015 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
4 * Copyright (C) 2013-2015 Daniel Mack <daniel@zonque.org>
5 * Copyright (C) 2013-2015 David Herrmann <dh.herrmann@gmail.com>
6 * Copyright (C) 2013-2015 Linux Foundation
7 * Copyright (C) 2014-2015 Djalal Harouni
9 * kdbus is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU Lesser General Public License as published by the
11 * Free Software Foundation; either version 2.1 of the License, or (at
12 * your option) any later version.
15 #ifndef __KDBUS_CONNECTION_H
16 #define __KDBUS_CONNECTION_H
18 #include <linux/atomic.h>
19 #include <linux/kref.h>
20 #include <linux/lockdep.h>
21 #include <linux/path.h>
29 #define KDBUS_HELLO_SPECIAL_CONN (KDBUS_HELLO_ACTIVATOR | \
30 KDBUS_HELLO_POLICY_HOLDER | \
33 struct kdbus_name_entry;
38 * struct kdbus_conn - connection to a bus
39 * @kref: Reference count
40 * @active: Active references to the connection
42 * @flags: KDBUS_HELLO_* flags
43 * @attach_flags_send: KDBUS_ATTACH_* flags for sending
44 * @attach_flags_recv: KDBUS_ATTACH_* flags for receiving
45 * @description: Human-readable connection description, used for
46 * debugging. This field is only set when the
47 * connection is created.
48 * @ep: The endpoint this connection belongs to
49 * @lock: Connection data lock
50 * @hentry: Entry in ID <-> connection map
51 * @ep_entry: Entry in endpoint
52 * @monitor_entry: Entry in monitor, if the connection is a monitor
53 * @reply_list: List of connections this connection should
55 * @work: Delayed work to handle timeouts
57 * @match_db: Subscription filter to broadcast messages
58 * @meta_proc: Process metadata of connection creator, or NULL
59 * @meta_fake: Faked metadata, or NULL
60 * @pool: The user's buffer to receive messages
61 * @user: Owner of the connection
62 * @cred: The credentials of the connection at creation time
63 * @pid: Pid at creation time
64 * @root_path: Root path at creation time
65 * @request_count: Number of pending requests issued by this
66 * connection that are waiting for replies from
68 * @lost_count: Number of lost broadcast messages
69 * @wait: Wake up this endpoint
70 * @queue: The message queue associated with this connection
71 * @quota: Array of per-user quota indexed by user->id
72 * @n_quota: Number of elements in quota array
73 * @names_list: List of well-known names
74 * @name_count: Number of owned well-known names
75 * @privileged: Whether this connection is privileged on the domain
76 * @owner: Owned by the same user as the bus owner
81 #ifdef CONFIG_DEBUG_LOCK_ALLOC
82 struct lockdep_map dep_map;
86 atomic64_t attach_flags_send;
87 atomic64_t attach_flags_recv;
88 const char *description;
91 struct hlist_node hentry;
92 struct list_head ep_entry;
93 struct list_head monitor_entry;
94 struct list_head reply_list;
95 struct delayed_work work;
96 struct kdbus_match_db *match_db;
97 struct kdbus_meta_proc *meta_proc;
98 struct kdbus_meta_fake *meta_fake;
99 struct kdbus_pool *pool;
100 struct kdbus_user *user;
101 const struct cred *cred;
103 struct path root_path;
104 atomic_t request_count;
106 wait_queue_head_t wait;
107 struct kdbus_queue queue;
109 struct kdbus_quota *quota;
110 unsigned int n_quota;
112 /* protected by registry->rwlock */
113 struct list_head names_list;
114 unsigned int name_count;
120 struct kdbus_conn *kdbus_conn_ref(struct kdbus_conn *conn);
121 struct kdbus_conn *kdbus_conn_unref(struct kdbus_conn *conn);
122 bool kdbus_conn_active(const struct kdbus_conn *conn);
123 int kdbus_conn_acquire(struct kdbus_conn *conn);
124 void kdbus_conn_release(struct kdbus_conn *conn);
125 int kdbus_conn_disconnect(struct kdbus_conn *conn, bool ensure_queue_empty);
126 bool kdbus_conn_has_name(struct kdbus_conn *conn, const char *name);
127 int kdbus_conn_quota_inc(struct kdbus_conn *c, struct kdbus_user *u,
128 size_t memory, size_t fds);
129 void kdbus_conn_quota_dec(struct kdbus_conn *c, struct kdbus_user *u,
130 size_t memory, size_t fds);
131 void kdbus_conn_lost_message(struct kdbus_conn *c);
132 int kdbus_conn_entry_insert(struct kdbus_conn *conn_src,
133 struct kdbus_conn *conn_dst,
134 struct kdbus_staging *staging,
135 struct kdbus_reply *reply,
136 const struct kdbus_name_entry *name);
137 void kdbus_conn_move_messages(struct kdbus_conn *conn_dst,
138 struct kdbus_conn *conn_src,
142 bool kdbus_conn_policy_own_name(struct kdbus_conn *conn,
143 const struct cred *conn_creds,
145 bool kdbus_conn_policy_talk(struct kdbus_conn *conn,
146 const struct cred *conn_creds,
147 struct kdbus_conn *to);
148 bool kdbus_conn_policy_see_name_unlocked(struct kdbus_conn *conn,
149 const struct cred *curr_creds,
151 bool kdbus_conn_policy_see_notification(struct kdbus_conn *conn,
152 const struct cred *curr_creds,
153 const struct kdbus_msg *msg);
155 /* command dispatcher */
156 struct kdbus_conn *kdbus_cmd_hello(struct kdbus_ep *ep, struct file *file,
158 int kdbus_cmd_byebye_unlocked(struct kdbus_conn *conn, void __user *argp);
159 int kdbus_cmd_conn_info(struct kdbus_conn *conn, void __user *argp);
160 int kdbus_cmd_update(struct kdbus_conn *conn, void __user *argp);
161 int kdbus_cmd_send(struct kdbus_conn *conn, struct file *f, void __user *argp);
162 int kdbus_cmd_recv(struct kdbus_conn *conn, void __user *argp);
163 int kdbus_cmd_free(struct kdbus_conn *conn, void __user *argp);
166 * kdbus_conn_is_ordinary() - Check if connection is ordinary
167 * @conn: The connection to check
169 * Return: Non-zero if the connection is an ordinary connection
171 static inline int kdbus_conn_is_ordinary(const struct kdbus_conn *conn)
173 return !(conn->flags & KDBUS_HELLO_SPECIAL_CONN);
177 * kdbus_conn_is_activator() - Check if connection is an activator
178 * @conn: The connection to check
180 * Return: Non-zero if the connection is an activator
182 static inline int kdbus_conn_is_activator(const struct kdbus_conn *conn)
184 return conn->flags & KDBUS_HELLO_ACTIVATOR;
188 * kdbus_conn_is_policy_holder() - Check if connection is a policy holder
189 * @conn: The connection to check
191 * Return: Non-zero if the connection is a policy holder
193 static inline int kdbus_conn_is_policy_holder(const struct kdbus_conn *conn)
195 return conn->flags & KDBUS_HELLO_POLICY_HOLDER;
199 * kdbus_conn_is_monitor() - Check if connection is a monitor
200 * @conn: The connection to check
202 * Return: Non-zero if the connection is a monitor
204 static inline int kdbus_conn_is_monitor(const struct kdbus_conn *conn)
206 return conn->flags & KDBUS_HELLO_MONITOR;
210 * kdbus_conn_lock2() - Lock two connections
211 * @a: connection A to lock or NULL
212 * @b: connection B to lock or NULL
214 * Lock two connections at once. As we need to have a stable locking order, we
215 * always lock the connection with lower memory address first.
217 static inline void kdbus_conn_lock2(struct kdbus_conn *a, struct kdbus_conn *b)
221 mutex_lock(&a->lock);
223 mutex_lock_nested(&b->lock, !!a);
226 mutex_lock(&b->lock);
228 mutex_lock_nested(&a->lock, !!b);
233 * kdbus_conn_unlock2() - Unlock two connections
234 * @a: connection A to unlock or NULL
235 * @b: connection B to unlock or NULL
237 * Unlock two connections at once. See kdbus_conn_lock2().
239 static inline void kdbus_conn_unlock2(struct kdbus_conn *a,
240 struct kdbus_conn *b)
243 mutex_unlock(&a->lock);
245 mutex_unlock(&b->lock);
249 * kdbus_conn_assert_active() - lockdep assert on active lock
250 * @conn: connection that shall be active
252 * This verifies via lockdep that the caller holds an active reference to the
255 static inline void kdbus_conn_assert_active(struct kdbus_conn *conn)
257 lockdep_assert_held(conn);