blob: bd9b80e3e88f9569f6adf04daa46832c0f9707e4 [file] [log] [blame]
Suren Baghdasaryana92de712018-03-07 12:27:50 -08001/*
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#ifndef _LMKD_H_
18#define _LMKD_H_
19
20#include <arpa/inet.h>
21#include <sys/cdefs.h>
22#include <sys/types.h>
23
24__BEGIN_DECLS
25
26/*
27 * Supported LMKD commands
28 */
29enum lmk_cmd {
30 LMK_TARGET = 0, /* Associate minfree with oom_adj_score */
31 LMK_PROCPRIO, /* Register a process and set its oom_adj_score */
32 LMK_PROCREMOVE, /* Unregister a process */
Suren Baghdasaryane3b60472018-10-10 14:17:17 -070033 LMK_PROCPURGE, /* Purge all registered processes */
Suren Baghdasaryand4a29902018-10-12 11:07:40 -070034 LMK_GETKILLCNT, /* Get number of kills */
Suren Baghdasaryana92de712018-03-07 12:27:50 -080035};
36
37/*
38 * Max number of targets in LMK_TARGET command.
39 */
40#define MAX_TARGETS 6
41
42/*
43 * Max packet length in bytes.
44 * Longest packet is LMK_TARGET followed by MAX_TARGETS
45 * of minfree and oom_adj_score values
46 */
47#define CTRL_PACKET_MAX_SIZE (sizeof(int) * (MAX_TARGETS * 2 + 1))
48
49/* LMKD packet - first int is lmk_cmd followed by payload */
50typedef int LMKD_CTRL_PACKET[CTRL_PACKET_MAX_SIZE / sizeof(int)];
51
52/* Get LMKD packet command */
Pirama Arumuga Nainar425e2382019-02-06 15:16:35 -080053static inline enum lmk_cmd lmkd_pack_get_cmd(LMKD_CTRL_PACKET pack) {
Suren Baghdasaryana92de712018-03-07 12:27:50 -080054 return (enum lmk_cmd)ntohl(pack[0]);
55}
56
57/* LMK_TARGET packet payload */
58struct lmk_target {
59 int minfree;
60 int oom_adj_score;
61};
62
63/*
64 * For LMK_TARGET packet get target_idx-th payload.
65 * Warning: no checks performed, caller should ensure valid parameters.
66 */
Pirama Arumuga Nainar425e2382019-02-06 15:16:35 -080067static inline void lmkd_pack_get_target(LMKD_CTRL_PACKET packet, int target_idx,
68 struct lmk_target* target) {
Suren Baghdasaryana92de712018-03-07 12:27:50 -080069 target->minfree = ntohl(packet[target_idx * 2 + 1]);
70 target->oom_adj_score = ntohl(packet[target_idx * 2 + 2]);
71}
72
73/*
74 * Prepare LMK_TARGET packet and return packet size in bytes.
75 * Warning: no checks performed, caller should ensure valid parameters.
76 */
Pirama Arumuga Nainar425e2382019-02-06 15:16:35 -080077static inline size_t lmkd_pack_set_target(LMKD_CTRL_PACKET packet, struct lmk_target* targets,
78 size_t target_cnt) {
Suren Baghdasaryana92de712018-03-07 12:27:50 -080079 int idx = 0;
80 packet[idx++] = htonl(LMK_TARGET);
81 while (target_cnt) {
82 packet[idx++] = htonl(targets->minfree);
83 packet[idx++] = htonl(targets->oom_adj_score);
84 targets++;
85 target_cnt--;
86 }
87 return idx * sizeof(int);
88}
89
Suren Baghdasaryane353d862019-10-22 17:12:01 -070090/* Process types for lmk_procprio.ptype */
91enum proc_type {
92 PROC_TYPE_FIRST,
93 PROC_TYPE_APP = PROC_TYPE_FIRST,
94 PROC_TYPE_SERVICE,
95 PROC_TYPE_COUNT,
96};
97
Suren Baghdasaryana92de712018-03-07 12:27:50 -080098/* LMK_PROCPRIO packet payload */
99struct lmk_procprio {
100 pid_t pid;
101 uid_t uid;
102 int oomadj;
Suren Baghdasaryane353d862019-10-22 17:12:01 -0700103 enum proc_type ptype;
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800104};
105
106/*
107 * For LMK_PROCPRIO packet get its payload.
108 * Warning: no checks performed, caller should ensure valid parameters.
109 */
Suren Baghdasaryane353d862019-10-22 17:12:01 -0700110static inline void lmkd_pack_get_procprio(LMKD_CTRL_PACKET packet, int field_count,
111 struct lmk_procprio* params) {
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800112 params->pid = (pid_t)ntohl(packet[1]);
113 params->uid = (uid_t)ntohl(packet[2]);
114 params->oomadj = ntohl(packet[3]);
Suren Baghdasaryane353d862019-10-22 17:12:01 -0700115 /* if field is missing assume PROC_TYPE_APP for backward compatibility */
116 params->ptype = field_count > 3 ? (enum proc_type)ntohl(packet[4]) : PROC_TYPE_APP;
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800117}
118
119/*
120 * Prepare LMK_PROCPRIO packet and return packet size in bytes.
121 * Warning: no checks performed, caller should ensure valid parameters.
122 */
Pirama Arumuga Nainar425e2382019-02-06 15:16:35 -0800123static inline size_t lmkd_pack_set_procprio(LMKD_CTRL_PACKET packet, struct lmk_procprio* params) {
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800124 packet[0] = htonl(LMK_PROCPRIO);
125 packet[1] = htonl(params->pid);
126 packet[2] = htonl(params->uid);
127 packet[3] = htonl(params->oomadj);
Suren Baghdasaryane353d862019-10-22 17:12:01 -0700128 packet[4] = htonl((int)params->ptype);
129 return 5 * sizeof(int);
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800130}
131
132/* LMK_PROCREMOVE packet payload */
133struct lmk_procremove {
134 pid_t pid;
135};
136
137/*
138 * For LMK_PROCREMOVE packet get its payload.
139 * Warning: no checks performed, caller should ensure valid parameters.
140 */
Pirama Arumuga Nainar425e2382019-02-06 15:16:35 -0800141static inline void lmkd_pack_get_procremove(LMKD_CTRL_PACKET packet,
142 struct lmk_procremove* params) {
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800143 params->pid = (pid_t)ntohl(packet[1]);
144}
145
146/*
147 * Prepare LMK_PROCREMOVE packet and return packet size in bytes.
148 * Warning: no checks performed, caller should ensure valid parameters.
149 */
Pirama Arumuga Nainar425e2382019-02-06 15:16:35 -0800150static inline size_t lmkd_pack_set_procremove(LMKD_CTRL_PACKET packet,
Suren Baghdasaryan4c6d3d72019-10-18 11:22:27 -0700151 struct lmk_procremove* params) {
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800152 packet[0] = htonl(LMK_PROCREMOVE);
153 packet[1] = htonl(params->pid);
154 return 2 * sizeof(int);
155}
156
Suren Baghdasaryane3b60472018-10-10 14:17:17 -0700157/*
158 * Prepare LMK_PROCPURGE packet and return packet size in bytes.
159 * Warning: no checks performed, caller should ensure valid parameters.
160 */
Pirama Arumuga Nainar425e2382019-02-06 15:16:35 -0800161static inline size_t lmkd_pack_set_procpurge(LMKD_CTRL_PACKET packet) {
Suren Baghdasaryane3b60472018-10-10 14:17:17 -0700162 packet[0] = htonl(LMK_PROCPURGE);
163 return sizeof(int);
164}
165
Suren Baghdasaryand4a29902018-10-12 11:07:40 -0700166/* LMK_GETKILLCNT packet payload */
167struct lmk_getkillcnt {
168 int min_oomadj;
169 int max_oomadj;
170};
171
172/*
173 * For LMK_GETKILLCNT packet get its payload.
174 * Warning: no checks performed, caller should ensure valid parameters.
175 */
Pirama Arumuga Nainar425e2382019-02-06 15:16:35 -0800176static inline void lmkd_pack_get_getkillcnt(LMKD_CTRL_PACKET packet,
177 struct lmk_getkillcnt* params) {
Suren Baghdasaryand4a29902018-10-12 11:07:40 -0700178 params->min_oomadj = ntohl(packet[1]);
179 params->max_oomadj = ntohl(packet[2]);
180}
181
182/*
183 * Prepare LMK_GETKILLCNT packet and return packet size in bytes.
184 * Warning: no checks performed, caller should ensure valid parameters.
185 */
Pirama Arumuga Nainar425e2382019-02-06 15:16:35 -0800186static inline size_t lmkd_pack_set_getkillcnt(LMKD_CTRL_PACKET packet,
187 struct lmk_getkillcnt* params) {
Suren Baghdasaryand4a29902018-10-12 11:07:40 -0700188 packet[0] = htonl(LMK_GETKILLCNT);
189 packet[1] = htonl(params->min_oomadj);
190 packet[2] = htonl(params->max_oomadj);
191 return 3 * sizeof(int);
192}
193
194/*
195 * Prepare LMK_GETKILLCNT reply packet and return packet size in bytes.
196 * Warning: no checks performed, caller should ensure valid parameters.
197 */
Pirama Arumuga Nainar425e2382019-02-06 15:16:35 -0800198static inline size_t lmkd_pack_set_getkillcnt_repl(LMKD_CTRL_PACKET packet, int kill_cnt) {
Suren Baghdasaryand4a29902018-10-12 11:07:40 -0700199 packet[0] = htonl(LMK_GETKILLCNT);
200 packet[1] = htonl(kill_cnt);
201 return 2 * sizeof(int);
202}
203
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800204__END_DECLS
205
206#endif /* _LMKD_H_ */