blob: e8f51da775ccefa99a59d6f070d20af0496c0b36 [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 Baghdasaryana92de712018-03-07 12:27:50 -080034};
35
36/*
37 * Max number of targets in LMK_TARGET command.
38 */
39#define MAX_TARGETS 6
40
41/*
42 * Max packet length in bytes.
43 * Longest packet is LMK_TARGET followed by MAX_TARGETS
44 * of minfree and oom_adj_score values
45 */
46#define CTRL_PACKET_MAX_SIZE (sizeof(int) * (MAX_TARGETS * 2 + 1))
47
48/* LMKD packet - first int is lmk_cmd followed by payload */
49typedef int LMKD_CTRL_PACKET[CTRL_PACKET_MAX_SIZE / sizeof(int)];
50
51/* Get LMKD packet command */
52inline enum lmk_cmd lmkd_pack_get_cmd(LMKD_CTRL_PACKET pack) {
53 return (enum lmk_cmd)ntohl(pack[0]);
54}
55
56/* LMK_TARGET packet payload */
57struct lmk_target {
58 int minfree;
59 int oom_adj_score;
60};
61
62/*
63 * For LMK_TARGET packet get target_idx-th payload.
64 * Warning: no checks performed, caller should ensure valid parameters.
65 */
66inline void lmkd_pack_get_target(LMKD_CTRL_PACKET packet,
67 int target_idx, struct lmk_target *target) {
68 target->minfree = ntohl(packet[target_idx * 2 + 1]);
69 target->oom_adj_score = ntohl(packet[target_idx * 2 + 2]);
70}
71
72/*
73 * Prepare LMK_TARGET packet and return packet size in bytes.
74 * Warning: no checks performed, caller should ensure valid parameters.
75 */
76inline size_t lmkd_pack_set_target(LMKD_CTRL_PACKET packet,
77 struct lmk_target *targets,
78 size_t target_cnt) {
79 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
90/* LMK_PROCPRIO packet payload */
91struct lmk_procprio {
92 pid_t pid;
93 uid_t uid;
94 int oomadj;
95};
96
97/*
98 * For LMK_PROCPRIO packet get its payload.
99 * Warning: no checks performed, caller should ensure valid parameters.
100 */
101inline void lmkd_pack_get_procprio(LMKD_CTRL_PACKET packet,
102 struct lmk_procprio *params) {
103 params->pid = (pid_t)ntohl(packet[1]);
104 params->uid = (uid_t)ntohl(packet[2]);
105 params->oomadj = ntohl(packet[3]);
106}
107
108/*
109 * Prepare LMK_PROCPRIO packet and return packet size in bytes.
110 * Warning: no checks performed, caller should ensure valid parameters.
111 */
112inline size_t lmkd_pack_set_procprio(LMKD_CTRL_PACKET packet,
113 struct lmk_procprio *params) {
114 packet[0] = htonl(LMK_PROCPRIO);
115 packet[1] = htonl(params->pid);
116 packet[2] = htonl(params->uid);
117 packet[3] = htonl(params->oomadj);
118 return 4 * sizeof(int);
119}
120
121/* LMK_PROCREMOVE packet payload */
122struct lmk_procremove {
123 pid_t pid;
124};
125
126/*
127 * For LMK_PROCREMOVE packet get its payload.
128 * Warning: no checks performed, caller should ensure valid parameters.
129 */
130inline void lmkd_pack_get_procremove(LMKD_CTRL_PACKET packet,
131 struct lmk_procremove *params) {
132 params->pid = (pid_t)ntohl(packet[1]);
133}
134
135/*
136 * Prepare LMK_PROCREMOVE packet and return packet size in bytes.
137 * Warning: no checks performed, caller should ensure valid parameters.
138 */
139inline size_t lmkd_pack_set_procremove(LMKD_CTRL_PACKET packet,
140 struct lmk_procprio *params) {
141 packet[0] = htonl(LMK_PROCREMOVE);
142 packet[1] = htonl(params->pid);
143 return 2 * sizeof(int);
144}
145
Suren Baghdasaryane3b60472018-10-10 14:17:17 -0700146/*
147 * Prepare LMK_PROCPURGE packet and return packet size in bytes.
148 * Warning: no checks performed, caller should ensure valid parameters.
149 */
150inline size_t lmkd_pack_set_procpurge(LMKD_CTRL_PACKET packet) {
151 packet[0] = htonl(LMK_PROCPURGE);
152 return sizeof(int);
153}
154
Suren Baghdasaryana92de712018-03-07 12:27:50 -0800155__END_DECLS
156
157#endif /* _LMKD_H_ */