blob: d2a5be1102af2426edf71390529c7a41ad47a1cf [file] [log] [blame]
Steven Morelandf183fdd2020-10-27 00:12:12 +00001/*
2 * Copyright (C) 2020 The Android Open Source Project
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 "Utils.h"
18
19#include <string.h>
20
Yifan Hongb675ffe2021-08-05 16:37:17 -070021using android::base::ErrnoError;
22using android::base::Result;
23
Steven Morelandf183fdd2020-10-27 00:12:12 +000024namespace android {
25
26void zeroMemory(uint8_t* data, size_t size) {
27 memset(data, 0, size);
28}
29
Yifan Hongb675ffe2021-08-05 16:37:17 -070030Result<void> setNonBlocking(android::base::borrowed_fd fd) {
31 int flags = TEMP_FAILURE_RETRY(fcntl(fd.get(), F_GETFL));
32 if (flags == -1) {
33 return ErrnoError() << "Could not get flags for fd";
34 }
35 if (int ret = TEMP_FAILURE_RETRY(fcntl(fd.get(), F_SETFL, flags | O_NONBLOCK)); ret == -1) {
36 return ErrnoError() << "Could not set non-blocking flag for fd";
37 }
38 return {};
39}
40
41} // namespace android