Modify tid and pid from int to pid_t
[platform/core/api/resource.git] / src / plugin / plugin.c
1 /* MIT License
2  *
3  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is furnished
10  * to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  * THE SOFTWARE. */
22
23 #include "plugin.h"
24 #include "cpu-boosting-type.h"
25 #include "cpu-boosting-private.h"
26
27 #include <errno.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
33 #include <sys/un.h>
34 #ifndef gettid
35 #include <sys/syscall.h>
36
37 #ifdef SYS_gettid
38 #define gettid() (pid_t) syscall(SYS_gettid)
39 #else
40 #error "SYS_gettid unavailable on this system"
41 #endif
42 #endif
43
44 #define SOCK_PATH "/run/.resourced.socket"
45
46 static int resource_create_and_connect_sock(void)
47 {
48         int sock;
49         socklen_t size;
50         struct sockaddr_un sockaddr;
51
52         sock = socket(AF_UNIX, SOCK_STREAM, 0);
53         if (sock < 0) {
54                 _E("[CPU-BOOSTING-PLUGIN] Failed to allocate a socket");
55                 return -1;
56         }
57
58         sockaddr.sun_family = AF_UNIX;
59         strncpy(sockaddr.sun_path, SOCK_PATH, strlen(SOCK_PATH) + 1);
60         size = sizeof(sockaddr);
61
62         if (connect(sock, (struct sockaddr *)&sockaddr, size) < 0) {
63                 _E("[CPU-BOOSTING-PLUGIN] Failed to connect to the resourced module");
64                 close(sock);
65                 return -1;
66         }
67
68         return sock;
69 }
70
71 static inline bool resource_pid_input_is_valid(resource_pid_t pid)
72 {
73         if (pid.pid < 0) {
74                 _E("[CPU-BOOSTING-PLUGIN] pid should be euqal or larger than 0");
75                 return false;
76         }
77
78         if (pid.pid == 0 && pid.tid != NULL && pid.tid_count <= 0) {
79                 _E("[CPU-BOOSTING-PLUGIN] tid count should be larger than 0");
80                 return false;
81         }
82
83         return true;
84 }
85
86 static inline bool resource_cpu_boosting_level_input_is_valid(cpu_boosting_level_e level)
87 {
88         if (level < CPU_BOOSTING_LEVEL_STRONG || level > CPU_BOOSTING_LEVEL_WEAK) {
89                 _E("[CPU-BOOSTING-PLUGIN] cpu boosting level should be located between %d and %d, but current level = %d", CPU_BOOSTING_LEVEL_STRONG, CPU_BOOSTING_LEVEL_WEAK, level);
90                 return false;
91         }
92
93         return true;
94 }
95
96 API int resource_set_cpu_boosting (resource_pid_t pid,
97                 cpu_boosting_level_e level, int timeout_msec)
98 {
99         pid_t tid;
100         int byte;
101         int ret = 0;
102         int sock;
103         cpu_boosting_input_t input;
104 //      cpu_boosting_output_t output;
105
106         if (!resource_pid_input_is_valid(pid))
107                 return -1;
108
109         if (!resource_cpu_boosting_level_input_is_valid(level))
110                 return -1;
111
112         if ((sock = resource_create_and_connect_sock()) < 0)
113                 return -1;
114
115         input.command = CPU_BOOSTING_COMMAND_SET;
116         input.pid = pid;
117         input.level = level;
118         input.timeout_msec = timeout_msec;
119
120         if (input.pid.pid > 0)
121                 input.body_size = 0;
122         else {
123                 if (input.pid.tid == NULL) {
124                         tid = gettid();
125                         input.pid.tid = &tid;
126                         input.pid.tid_count = 1;
127                 }
128
129                 input.body_size = input.pid.tid_count * sizeof(pid_t);
130         }
131
132         byte = send(sock, (const void *)&input, sizeof(input), 0);
133         if (byte != sizeof(input)) {
134                 _E("[CPU-BOOSTING-PLUGIN] error is based on %s", strerror(errno));
135                 _E("[CPU-BOOSTING-PLUGIN] client input size is %u, but sent size is %d",
136                                 (unsigned int)sizeof(input), byte);
137                 ret = -1;
138                 goto close_sock;
139         }
140
141         if (input.body_size > 0) {
142                 byte = send(sock, (const void *)input.pid.tid, input.body_size, 0);
143                 if (byte != input.body_size) {
144                         _E("[CPU-BOOSTING-PLUGIN] error is based on %s", strerror(errno));
145                         _E("[CPU-BOOSTING-PLUGIN] client input size is %d, but sent size is %d",
146                                         input.body_size, byte);
147                         ret = -1;
148                         goto close_sock;
149                 }
150         }
151
152 /*      byte = recv(sock, (void *)&output, sizeof(bool), 0);
153         if (byte != sizeof(bool)) {
154                 _E("[CPU-BOOSTING-PLUGIN] error is based on %s", strerror(errno));
155                 _E("[CPU-BOOSTING-PLUGIN] client output size is %u, but received size is %d",
156                                 (unsigned int)sizeof(bool), byte);
157                 goto close_sock;
158         }
159
160
161         if (!output.success)
162                 _E("[CPU-BOOSTING-PLUGIN] fail to communicate");
163         else {
164                 _D("[CPU-BOOSTING-PLUGIN] success to communicate");
165                 ret = 0;
166         }*/
167
168 close_sock:
169         close(sock);
170
171         return ret;
172 }
173
174 API int resource_clear_cpu_boosting (resource_pid_t pid)
175 {
176         _D("[CPU-BOOSTING-PLUGIN] %s called", __func__);
177
178         return 0;
179 }
180
181 API int resource_get_cpu_boosting_level (resource_pid_t pid,
182                 cpu_boosting_level_info_t *level)
183 {
184         _D("[CPU-BOOSTING-PLUGIN] %s called", __func__);
185
186         return 0;
187 }
188
189 API int resource_set_cpu_inheritance (pid_t source_tid, const char *dest_process, int timeout_msec)
190 {
191         _D("[CPU-BOOSTING-PLUGIN] %s called", __func__);
192
193         return 0;
194 }
195
196 API int resource_clear_cpu_inheritance (pid_t source_tid, const char *dest_process)
197 {
198         _D("[CPU-BOOSTING-PLUGIN] %s called", __func__);
199
200         return 0;
201 }
202
203 API int resource_register_cpu_inheritance_destination (const char *dest_process, resource_pid_t pid)
204 {
205         _D("[CPU-BOOSTING-PLUGIN] %s called", __func__);
206
207         return 0;
208 }
209
210 API int resource_unregister_cpu_inheritance_destination (const char *dest_process)
211 {
212         _D("[CPU-BOOSTING-PLUGIN] %s called", __func__);
213
214         return 0;
215 }
216
217 void __CONSTRUCTOR__ cpu_boosting_plugin_init(void)
218 {
219         _D("[CPU-BOOSTING-PLUGIN] CPU boosting plugin Module is loaded");
220 }
221
222 void __DESTRUCTOR__ cpu_boosting_plugin_exit(void)
223 {
224         _D("[CPU-BOOSTING-PLUGIN] CPU boosting plugin Module is unloaded");
225 }