1 /*
2 * Copyright 2018 Google, Inc
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 #include <errno.h>
18 #include <fcntl.h>
19 #include <sys/cdefs.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <stdio.h>
23 #include <unistd.h>
24
25 #include <cutils/sockets.h>
26 #include <liblmkd_utils.h>
27 #include <processgroup/processgroup.h>
28
lmkd_connect()29 int lmkd_connect() {
30 return socket_local_client("lmkd",
31 ANDROID_SOCKET_NAMESPACE_RESERVED,
32 SOCK_SEQPACKET | SOCK_CLOEXEC);
33 }
34
lmkd_register_proc(int sock,struct lmk_procprio * params)35 int lmkd_register_proc(int sock, struct lmk_procprio *params) {
36 LMKD_CTRL_PACKET packet;
37 size_t size;
38 int ret;
39
40 size = lmkd_pack_set_procprio(packet, params);
41 ret = TEMP_FAILURE_RETRY(write(sock, packet, size));
42
43 return (ret < 0) ? -1 : 0;
44 }
45
lmkd_unregister_proc(int sock,struct lmk_procremove * params)46 int lmkd_unregister_proc(int sock, struct lmk_procremove *params) {
47 LMKD_CTRL_PACKET packet;
48 size_t size;
49 int ret;
50
51 size = lmkd_pack_set_procremove(packet, params);
52 ret = TEMP_FAILURE_RETRY(write(sock, packet, size));
53
54 return (ret < 0) ? -1 : 0;
55 }
56
lmkd_update_props(int sock)57 enum update_props_result lmkd_update_props(int sock) {
58 LMKD_CTRL_PACKET packet;
59 int size;
60
61 size = lmkd_pack_set_update_props(packet);
62 if (TEMP_FAILURE_RETRY(write(sock, packet, size)) < 0) {
63 return UPDATE_PROPS_SEND_ERR;
64 }
65
66 size = TEMP_FAILURE_RETRY(read(sock, packet, CTRL_PACKET_MAX_SIZE));
67 if (size < 0) {
68 return UPDATE_PROPS_RECV_ERR;
69 }
70
71 if (size != 2 * sizeof(int) || lmkd_pack_get_cmd(packet) != LMK_UPDATE_PROPS) {
72 return UPDATE_PROPS_FORMAT_ERR;
73 }
74
75 struct lmk_update_props_reply params;
76 lmkd_pack_get_update_props_repl(packet, ¶ms);
77
78 return params.result == 0 ? UPDATE_PROPS_SUCCESS : UPDATE_PROPS_FAIL;
79 }
80
create_memcg(uid_t uid,pid_t pid)81 int create_memcg(uid_t uid, pid_t pid) {
82 return createProcessGroup(uid, pid, true) == 0 ? 0 : -1;
83 }
84
85