blob: e72d159922b3f8ba8e0ae188f8f788e4239f30a4 [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 */
53inline enum lmk_cmd lmkd_pack_get_cmd(LMKD_CTRL_PACKET pack) {
54 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 */
67inline void lmkd_pack_get_target(LMKD_CTRL_PACKET packet,
68 int target_idx, struct lmk_target *target) {
69 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 */
77inline size_t lmkd_pack_set_target(LMKD_CTRL_PACKET packet,
78 struct lmk_target *targets,
79 size_t target_cnt) {
80 int idx = 0;
81 packet[idx++] = htonl(LMK_TARGET);
82 while (target_cnt) {
83 packet[idx++] = htonl(targets->minfree);
84 packet[idx++] = htonl(targets->oom_adj_score);
85 targets++;
86 target_cnt--;
87 }
88 return idx * sizeof(int);
89}
90
91/* LMK_PROCPRIO packet payload */
92struct lmk_procprio {
93 pid_t pid;
94 uid_t uid;
95 int oomadj;
96};
97
98/*
99 * For LMK_PROCPRIO packet get its payload.
100 * Warning: no checks performed, caller should ensure valid parameters.
101 */
102inline void lmkd_pack_get_procprio(LMKD_CTRL_PACKET packet,
103 struct lmk_procprio *params) {
104 params->pid = (pid_t)ntohl(packet[1]);
105 params->uid = (uid_t)ntohl(packet[2]);
106 params->oomadj = ntohl(packet[3]);
107}
108
109/*
110 * Prepare LMK_PROCPRIO packet and return packet size in bytes.
111 * Warning: no checks performed, caller should ensure valid parameters.
112 */
113inline size_t lmkd_pack_set_procprio(LMKD_CTRL_PACKET packet,
114 struct lmk_procprio *params) {
115 packet[0] = htonl(LMK_PROCPRIO);
116 packet[1] = htonl(params->pid);
117 packet[2] = htonl(params->uid);
118 packet[3] = htonl(params->oomadj);
119 return 4 * sizeof(int);
120}
121
122/* LMK_PROCREMOVE packet payload */
123struct lmk_procremove {
124 pid_t pid;
125};
126
127/*
128 * For LMK_PROCREMOVE packet get its payload.
129 * Warning: no checks performed, caller should ensure valid parameters.
130 */
131inline void lmkd_pack_get_procremove(LMKD_CTRL_PACKET packet,
132 struct lmk_procremove *params) {
133 params->pid = (pid_t)ntohl(packet[1]);
134}
135
136/*
137 * Prepare LMK_PROCREMOVE packet and return packet size in bytes.
138 * Warning: no checks performed, caller should ensure valid parameters.
139 */
140inline size_t lmkd_pack_set_procremove(LMKD_CTRL_PACKET packet,
141 struct lmk_procprio *params) {
142 packet[0] = htonl(LMK_PROCREMOVE);
143 packet[1] = htonl(params->pid);
144 return 2 * sizeof(int);
145}
146
Suren Baghdasaryane3b60472018-10-10 14:17:17 -0700147/*
148 * Prepare LMK_PROCPURGE packet and return packet size in bytes.
149 * Warning: no checks performed, caller should ensure valid parameters.
150 */
151inline size_t lmkd_pack_set_procpurge(LMKD_CTRL_PACKET packet) {
152 packet[0] = htonl(LMK_PROCPURGE);
153 return sizeof(int);
154}
155
Suren Baghdasaryand4a29902018-10-12 11:07:40 -0700156/* LMK_GETKILLCNT packet payload */
157struct lmk_getkillcnt {
158 int min_oomadj;
159 int max_oomadj;
160};
161
162/*
163 * For LMK_GETKILLCNT packet get its payload.
164 * Warning: no checks performed, caller should ensure valid parameters.
165 */
166inline void lmkd_pack_get_getkillcnt(LMKD_CTRL_PACKET packet,
167 struct lmk_getkillcnt *params) {
168 params->min_oomadj = ntohl(packet[1]);
169 params->max_oomadj = ntohl(packet[2]);
170}
171
172/*
173 * Prepare LMK_GETKILLCNT packet and return packet size in bytes.
174 * Warning: no checks performed, caller should ensure valid parameters.
175 */
176inline size_t lmkd_pack_set_getkillcnt(LMKD_CTRL_PACKET packet,
177 struct lmk_getkillcnt *params) {
178 packet[0] = htonl(LMK_GETKILLCNT);
179 packet[1] = htonl(params->min_oomadj);
180 packet[2] = htonl(params->max_oomadj);
181 return 3 * sizeof(int);
182}
183
184/*
185 * Prepare LMK_GETKILLCNT reply packet and return packet size in bytes.
186 * Warning: no checks performed, caller should ensure valid parameters.
187 */
188inline size_t lmkd_pack_set_getkillcnt_repl(LMKD_CTRL_PACKET packet, int kill_cnt) {
189 packet[0] = htonl(LMK_GETKILLCNT);
190 packet[1] = htonl(kill_cnt);
191 return 2 * sizeof(int);
192}
193
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800194__END_DECLS
195
196#endif /* _LMKD_H_ */