Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 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 | |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 25 | #include <cutils/sockets.h> |
Bart Van Assche | 911f106 | 2022-03-21 10:47:24 -0700 | [diff] [blame] | 26 | #include <liblmkd_utils.h> |
| 27 | #include <processgroup/processgroup.h> |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 28 | |
| 29 | int lmkd_connect() { |
| 30 | return socket_local_client("lmkd", |
| 31 | ANDROID_SOCKET_NAMESPACE_RESERVED, |
Suren Baghdasaryan | f2cbefd | 2019-10-21 17:59:22 -0700 | [diff] [blame] | 32 | SOCK_SEQPACKET | SOCK_CLOEXEC); |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 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 | |
Suren Baghdasaryan | 7d3c5c6 | 2019-10-18 11:22:27 -0700 | [diff] [blame] | 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 | |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 57 | enum update_props_result lmkd_update_props(int sock) { |
| 58 | LMKD_CTRL_PACKET packet; |
Suren Baghdasaryan | ed715a3 | 2020-05-11 16:31:57 -0700 | [diff] [blame] | 59 | int size; |
Suren Baghdasaryan | 1d0ebea | 2020-04-28 15:52:29 -0700 | [diff] [blame] | 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 | |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 81 | int create_memcg(uid_t uid, pid_t pid) { |
Bart Van Assche | 911f106 | 2022-03-21 10:47:24 -0700 | [diff] [blame] | 82 | return createProcessGroup(uid, pid, true) == 0 ? 0 : -1; |
Suren Baghdasaryan | f7932e5 | 2018-01-24 16:51:41 -0800 | [diff] [blame] | 83 | } |
| 84 | |