bf17d5f4c12361e44b9311f0cc3950d71399007d
[platform/core/system/libdbuspolicy.git] / src / kdbus.h
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
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 #pragma once
18
19 #include <pwd.h>
20 #include <grp.h>
21 #include <array>
22 #include "internal/bus_names_array.hpp"
23 #include <boost/utility/string_ref.hpp>
24
25 class KdbusConnectionInfo;
26
27 class KdbusConnection {
28         int fd;
29         uint8_t *pool;
30
31         int open_bus(const char *path);
32         int hello(uint64_t hello_flags,
33                           uint64_t attach_flags_send,
34                           uint64_t attach_flags_recv);
35 public:
36         typedef enum { POLICY_CONN_INFO_ALL, POLICY_CONN_INFO_NAME } conn_info_type;
37
38         void shared_init_fd(int fd_to_set) { fd = fd_to_set; }
39         void shared_init_pool(void *pool_to_set) { pool = static_cast<decltype(pool)>(pool_to_set); }
40         int connect(const char *path,
41                                 uint64_t hello_flags,
42                                 uint64_t attach_flags_send,
43                                 uint64_t attach_flags_recv);
44         int get_conn_info(const char *bus_name,
45                                           conn_info_type conn_info_type,
46                                           KdbusConnectionInfo *info);
47         int free_by_offset(uint64_t offset);
48 };
49
50 class KdbusConnectionInfo {
51         static const uid_t UID_INVALID = -1;
52         static const gid_t GID_INVALID = -1;
53
54         char const *_label{nullptr};
55
56         KdbusBusNames k_names;
57         uint64_t free_offset{0};
58         uid_t uid_n{UID_INVALID};
59         gid_t gid_n{GID_INVALID};
60         bool set{false};
61
62         KdbusConnection &my_connection;
63
64 public:
65         KdbusConnectionInfo(KdbusConnection &c) : my_connection{c} {}
66         ~KdbusConnectionInfo() { release_info(); }
67
68         void release_info();
69         void setNewData(uint64_t offset);
70
71         void addName(const char *name) { k_names.push_back(name); }
72
73         void setCreds(uid_t uid, gid_t gid) { uid_n = uid; gid_n = gid; }
74
75         void setLabel(const char *label) { _label = label; }
76
77         int get(const char *destination, KdbusConnection::conn_info_type conn_info_type) {
78                 return my_connection.get_conn_info(destination, conn_info_type, this);
79         }
80         const KdbusBusNames &names() const { return k_names; }
81         KdbusBusNames &names() { return k_names; }
82         uid_t uid() const { return uid_n; }
83         gid_t gid() const { return gid_n; }
84         const char *label() const { return _label; }
85 };