cpp android 论坛,os.cpp - Android社区 - https://www.androidos.net.cn/

/*

* Copyright (C) 2016 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

#include #include #include #include #include "android-base/logging.h"

#include "wifilogd/local_utils.h"

#include "wifilogd/os.h"

namespace android {

namespace wifilogd {

using local_utils::GetMaxVal;

namespace {

constexpr auto kMaxNanoSeconds = 1000 * 1000 * 1000 - 1;

}

constexpr int Os::kInvalidFd;

Os::Os() : raw_os_(new RawOs()) {}

Os::Os(std::unique_ptrraw_os) : raw_os_(std::move(raw_os)) {}

Os::~Os() {}

std::tupleOs::GetControlSocket(

const std::string& socket_name) {

int sock_fd = raw_os_->GetControlSocket(socket_name.c_str());

if (sock_fd < 0) {

return {kInvalidFd, errno};

} else {

return {sock_fd, 0};

}

}

Os::Timestamp Os::GetTimestamp(clockid_t clock_id) const {

struct timespec now_timespec;

int failed = raw_os_->ClockGettime(clock_id, &now_timespec);

if (failed) {

LOG(FATAL) << "Unexpected error: " << std::strerror(errno);

}

CHECK(now_timespec.tv_nsec <= kMaxNanoSeconds);

Timestamp now_timestamp;

now_timestamp.secs = SAFELY_CLAMP(

now_timespec.tv_sec, uint32_t, 0,

// The upper-bound comes from the source-type on 32-bit platforms,

// and the dest-type on 64-bit platforms. Using min(), we can figure out

// which type to use for the upper bound, without resorting to macros.

std::min(static_cast(GetMaxVal(now_timestamp.secs)),

static_cast(GetMaxVal(now_timespec.tv_sec))));

now_timestamp.nsecs =

SAFELY_CLAMP(now_timespec.tv_nsec, uint32_t, 0, kMaxNanoSeconds);

return now_timestamp;

}

void Os::Nanosleep(uint32_t sleep_time_nsec) {

struct timespec sleep_timespec = {

0, // tv_sec

SAFELY_CLAMP(sleep_time_nsec, decltype(timespec::tv_nsec), 0, kMaxNanos)};

int failed = 0;

do {

struct timespec remaining_timespec;

failed = raw_os_->Nanosleep(&sleep_timespec, &remaining_timespec);

sleep_timespec = remaining_timespec;

} while (failed && errno == EINTR && sleep_timespec.tv_nsec > 0);

if (failed && errno != EINTR) {

// The only other documented errors for the underlying nanosleep() call are

// EFAULT and EINVAL. But we always pass valid pointers, and the values in

// |sleep_timespec| are always valid.

LOG(FATAL) << "Unexpected error: " << std::strerror(errno);

}

}

std::tupleOs::ReceiveDatagram(int fd, void* buf,

size_t buflen) {

// recv() takes a size_t, but returns an ssize_t. That means that the largest

// successful read that recv() can report is the maximal ssize_t. Passing a

// larger |buflen| risks mistakenly reporting a truncated read.

CHECK(buflen <= GetMaxVal());

const ssize_t res = raw_os_->Recv(fd, buf, buflen, MSG_TRUNC);

if (res < 0) {

return {0, errno};

}

// Due to the MSG_TRUNC flag, |res| may reasonably be larger than

// |buflen|. In such cases, |res| indicates the full size of the datagram,

// before being truncated to fit our buffer. Hence, we omit the

// buffer-overflow CHECK that exists in Write().

return {res, 0};

}

std::tupleOs::Write(int fd, const void* buf,

size_t buflen) {

// write() takes a size_t, but returns an ssize_t. That means that the

// largest successful write that write() can report is the maximal ssize_t.

// Passing a larger |buflen| risks mistakenly reporting a truncated write.

CHECK(buflen <= GetMaxVal());

const ssize_t res = raw_os_->Write(fd, buf, buflen);

if (res < 0) {

return {0, errno};

}

CHECK(res <=

SAFELY_CLAMP(buflen, ssize_t, 0,

GetMaxVal())); // Abort on buffer overflow.

// Note that |res| may be less than buflen. However, a) a short write is

// not an error, and b) |errno| may be stale, as |errno| is only guaranteed to

// be set if an error occurred. Hence, we return Errno of 0 unconditionally.

// See http://yarchive.net/comp/linux/write_error_return.html

return {res, 0};

}

} // namespace wifilogd

} // namespace android

C++程序

|

137行

|

4.64 KB

/*

* Copyright (C) 2016 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

#include

#include

#include

#include

#include "android-base/logging.h"

#include "wifilogd/local_utils.h"

#include "wifilogd/os.h"

namespace android {

namespace wifilogd {

using local_utils::GetMaxVal;

namespace {

constexpr auto kMaxNanoSeconds = 1000 * 1000 * 1000 - 1;

}

constexpr int Os::kInvalidFd;

Os::Os() : raw_os_(new RawOs()) {}

Os::Os(std::unique_ptr raw_os) : raw_os_(std::move(raw_os)) {}

Os::~Os() {}

std::tuple Os::GetControlSocket(

const std::string& socket_name) {

int sock_fd = raw_os_->GetControlSocket(socket_name.c_str());

if (sock_fd < 0) {

return {kInvalidFd, errno};

} else {

return {sock_fd, 0};

}

}

Os::Timestamp Os::GetTimestamp(clockid_t clock_id) const {

struct timespec now_timespec;

int failed = raw_os_->ClockGettime(clock_id, &now_timespec);

if (failed) {

LOG(FATAL) << "Unexpected error: " << std::strerror(errno);

}

CHECK(now_timespec.tv_nsec <= kMaxNanoSeconds);

Timestamp now_timestamp;

now_timestamp.secs = SAFELY_CLAMP(

now_timespec.tv_sec, uint32_t, 0,

// The upper-bound comes from the source-type on 32-bit platforms,

// and the dest-type on 64-bit platforms. Using min(), we can figure out

// which type to use for the upper bound, without resorting to macros.

std::min(static_cast(GetMaxVal(now_timestamp.secs)),

static_cast(GetMaxVal(now_timespec.tv_sec))));

now_timestamp.nsecs =

SAFELY_CLAMP(now_timespec.tv_nsec, uint32_t, 0, kMaxNanoSeconds);

return now_timestamp;

}

void Os::Nanosleep(uint32_t sleep_time_nsec) {

struct timespec sleep_timespec = {

0, // tv_sec

SAFELY_CLAMP(sleep_time_nsec, decltype(timespec::tv_nsec), 0, kMaxNanos)};

int failed = 0;

do {

struct timespec remaining_timespec;

failed = raw_os_->Nanosleep(&sleep_timespec, &remaining_timespec);

sleep_timespec = remaining_timespec;

} while (failed && errno == EINTR && sleep_timespec.tv_nsec > 0);

if (failed && errno != EINTR) {

// The only other documented errors for the underlying nanosleep() call are

// EFAULT and EINVAL. But we always pass valid pointers, and the values in

// |sleep_timespec| are always valid.

LOG(FATAL) << "Unexpected error: " << std::strerror(errno);

}

}

std::tuple Os::ReceiveDatagram(int fd, void* buf,

size_t buflen) {

// recv() takes a size_t, but returns an ssize_t. That means that the largest

// successful read that recv() can report is the maximal ssize_t. Passing a

// larger |buflen| risks mistakenly reporting a truncated read.

CHECK(buflen <= GetMaxVal());

const ssize_t res = raw_os_->Recv(fd, buf, buflen, MSG_TRUNC);

if (res < 0) {

return {0, errno};

}

// Due to the MSG_TRUNC flag, |res| may reasonably be larger than

// |buflen|. In such cases, |res| indicates the full size of the datagram,

// before being truncated to fit our buffer. Hence, we omit the

// buffer-overflow CHECK that exists in Write().

return {res, 0};

}

std::tuple Os::Write(int fd, const void* buf,

size_t buflen) {

// write() takes a size_t, but returns an ssize_t. That means that the

// largest successful write that write() can report is the maximal ssize_t.

// Passing a larger |buflen| risks mistakenly reporting a truncated write.

CHECK(buflen <= GetMaxVal());

const ssize_t res = raw_os_->Write(fd, buf, buflen);

if (res < 0) {

return {0, errno};

}

CHECK(res <=

SAFELY_CLAMP(buflen, ssize_t, 0,

GetMaxVal())); // Abort on buffer overflow.

// Note that |res| may be less than buflen. However, a) a short write is

// not an error, and b) |errno| may be stale, as |errno| is only guaranteed to

// be set if an error occurred. Hence, we return Errno of 0 unconditionally.

// See http://yarchive.net/comp/linux/write_error_return.html

return {res, 0};

}

} // namespace wifilogd

} // namespace android

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值