1 /*
2 * Copyright (C) 2023, 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 <errno.h>
20 #include <time.h>
21
22 #include "stats_statsdsocketlog.h"
23
get_elapsed_realtime_ns()24 int64_t get_elapsed_realtime_ns() {
25 struct timespec t;
26 t.tv_sec = t.tv_nsec = 0;
27 clock_gettime(CLOCK_BOOTTIME, &t);
28 return (int64_t)t.tv_sec * 1000000000LL + t.tv_nsec;
29 }
30
toSocketLossError(int errno_code)31 int toSocketLossError(int errno_code) {
32 using namespace android::os::statsdsocket;
33
34 // compile time checks
35 static_assert(STATS_SOCKET_LOSS_REPORTED__ERRORS__SOCKET_LOSS_ERROR_ON_WRITE_EPERM == -EPERM,
36 "Socket Loss Error codes mapping function needs to be updated");
37 static_assert(STATS_SOCKET_LOSS_REPORTED__ERRORS__SOCKET_LOSS_ERROR_ON_WRITE_EINTR == -EINTR,
38 "Socket Loss Error codes mapping function needs to be updated");
39 static_assert(STATS_SOCKET_LOSS_REPORTED__ERRORS__SOCKET_LOSS_ERROR_ON_WRITE_EIO == -EIO,
40 "Socket Loss Error codes mapping function needs to be updated");
41 static_assert(STATS_SOCKET_LOSS_REPORTED__ERRORS__SOCKET_LOSS_ERROR_ON_WRITE_EBADF == -EBADF,
42 "Socket Loss Error codes mapping function needs to be updated");
43 static_assert(
44 STATS_SOCKET_LOSS_REPORTED__ERRORS__SOCKET_LOSS_ERROR_ON_WRITE_EAGAIN == -EAGAIN,
45 "Socket Loss Error codes mapping function needs to be updated"); // same as EWOULDBLOCK
46 static_assert(STATS_SOCKET_LOSS_REPORTED__ERRORS__SOCKET_LOSS_ERROR_ON_WRITE_EFAULT == -EFAULT,
47 "Socket Loss Error codes mapping function needs to be updated");
48 static_assert(STATS_SOCKET_LOSS_REPORTED__ERRORS__SOCKET_LOSS_ERROR_ON_WRITE_ENODEV == -ENODEV,
49 "Socket Loss Error codes mapping function needs to be updated");
50 static_assert(STATS_SOCKET_LOSS_REPORTED__ERRORS__SOCKET_LOSS_ERROR_ON_WRITE_EINVAL == -EINVAL,
51 "Socket Loss Error codes mapping function needs to be updated");
52 static_assert(STATS_SOCKET_LOSS_REPORTED__ERRORS__SOCKET_LOSS_ERROR_ON_WRITE_EFBIG == -EFBIG,
53 "Socket Loss Error codes mapping function needs to be updated");
54 static_assert(STATS_SOCKET_LOSS_REPORTED__ERRORS__SOCKET_LOSS_ERROR_ON_WRITE_ENOSPC == -ENOSPC,
55 "Socket Loss Error codes mapping function needs to be updated");
56 static_assert(STATS_SOCKET_LOSS_REPORTED__ERRORS__SOCKET_LOSS_ERROR_ON_WRITE_EPIPE == -EPIPE,
57 "Socket Loss Error codes mapping function needs to be updated");
58 static_assert(STATS_SOCKET_LOSS_REPORTED__ERRORS__SOCKET_LOSS_ERROR_ON_WRITE_EDESTADDRREQ ==
59 -EDESTADDRREQ,
60 "Socket Loss Error codes mapping function needs to be updated");
61 static_assert(STATS_SOCKET_LOSS_REPORTED__ERRORS__SOCKET_LOSS_ERROR_ON_WRITE_EDQUOT == -EDQUOT,
62 "Socket Loss Error codes mapping function needs to be updated");
63
64 switch (errno_code) {
65 case EPERM:
66 case EINTR:
67 case EIO:
68 case EBADF:
69 case EAGAIN:
70 case EFAULT:
71 case ENODEV:
72 case EINVAL:
73 case EFBIG:
74 case ENOSPC:
75 case EPIPE:
76 case EDESTADDRREQ:
77 case EDQUOT:
78 return -errno_code;
79 default:
80 return STATS_SOCKET_LOSS_REPORTED__ERRORS__SOCKET_LOSS_ERROR_UNKNOWN;
81 }
82 }
83