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 <liblmkd_utils.h>
26 #include <cutils/sockets.h>
27 
lmkd_connect()28 int lmkd_connect() {
29     return socket_local_client("lmkd",
30                                ANDROID_SOCKET_NAMESPACE_RESERVED,
31                                SOCK_SEQPACKET | SOCK_CLOEXEC);
32 }
33 
lmkd_register_proc(int sock,struct lmk_procprio * params)34 int lmkd_register_proc(int sock, struct lmk_procprio *params) {
35     LMKD_CTRL_PACKET packet;
36     size_t size;
37     int ret;
38 
39     size = lmkd_pack_set_procprio(packet, params);
40     ret = TEMP_FAILURE_RETRY(write(sock, packet, size));
41 
42     return (ret < 0) ? -1 : 0;
43 }
44 
lmkd_unregister_proc(int sock,struct lmk_procremove * params)45 int lmkd_unregister_proc(int sock, struct lmk_procremove *params) {
46     LMKD_CTRL_PACKET packet;
47     size_t size;
48     int ret;
49 
50     size = lmkd_pack_set_procremove(packet, params);
51     ret = TEMP_FAILURE_RETRY(write(sock, packet, size));
52 
53     return (ret < 0) ? -1 : 0;
54 }
55 
lmkd_update_props(int sock)56 enum update_props_result lmkd_update_props(int sock) {
57     LMKD_CTRL_PACKET packet;
58     int size;
59 
60     size = lmkd_pack_set_update_props(packet);
61     if (TEMP_FAILURE_RETRY(write(sock, packet, size)) < 0) {
62         return UPDATE_PROPS_SEND_ERR;
63     }
64 
65     size = TEMP_FAILURE_RETRY(read(sock, packet, CTRL_PACKET_MAX_SIZE));
66     if (size < 0) {
67         return UPDATE_PROPS_RECV_ERR;
68     }
69 
70     if (size != 2 * sizeof(int) || lmkd_pack_get_cmd(packet) != LMK_UPDATE_PROPS) {
71         return UPDATE_PROPS_FORMAT_ERR;
72     }
73 
74     struct lmk_update_props_reply params;
75     lmkd_pack_get_update_props_repl(packet, &params);
76 
77     return params.result == 0 ? UPDATE_PROPS_SUCCESS : UPDATE_PROPS_FAIL;
78 }
79 
create_memcg(uid_t uid,pid_t pid)80 int create_memcg(uid_t uid, pid_t pid) {
81     char buf[256];
82     int tasks_file;
83     int written;
84 
85     snprintf(buf, sizeof(buf), "/dev/memcg/apps/uid_%u", uid);
86     if (mkdir(buf, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0 &&
87         errno != EEXIST) {
88         return -1;
89     }
90 
91     snprintf(buf, sizeof(buf), "/dev/memcg/apps/uid_%u/pid_%u", uid, pid);
92     if (mkdir(buf, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0 &&
93         errno != EEXIST) {
94         return -1;
95     }
96 
97     snprintf(buf, sizeof(buf), "/dev/memcg/apps/uid_%u/pid_%u/tasks", uid, pid);
98     tasks_file = open(buf, O_WRONLY);
99     if (tasks_file < 0) {
100         return -2;
101     }
102     written = snprintf(buf, sizeof(buf), "%u", pid);
103     if (__predict_false(written >= (int)sizeof(buf))) {
104         written = sizeof(buf) - 1;
105     }
106     written = TEMP_FAILURE_RETRY(write(tasks_file, buf, written));
107     close(tasks_file);
108 
109     return (written < 0) ? -3 : 0;
110 }
111 
112