summaryrefslogtreecommitdiff
path: root/include/common.h
blob: ec6ff45d2b524533edcd2d46d365ff2459dbbafc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#pragma once
#include "marsio.h"

#include <zlog.h>

#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#ifndef MR_SYMBOL_MAX
#define MR_SYMBOL_MAX 128
#endif

#define TELEMETRY_INDEX_START 8
#define TELEMETRY_INDEX_END DP_TRACE_JOB_NUM_MAX

#define TRACE_INDEX_START 0
#define TRACE_INDEX_END TELEMETRY_INDEX_START

#ifndef likely
#define likely(x) __builtin_expect(!!(x), 1)
#endif /* likely */

#ifndef unlikely
#define unlikely(x) __builtin_expect(!!(x), 0)
#endif /* unlikely */

#define TELEMETRY_DIM(a) (sizeof(a) / sizeof((a)[0]))

extern unsigned int zlog_env_is_init;
#define DP_TRACE_VERIFY(condition, fmt, ...)                                                                           \
    do                                                                                                                 \
    {                                                                                                                  \
        if (!(condition))                                                                                              \
        {                                                                                                              \
            if (zlog_env_is_init == 1)                                                                                 \
            {                                                                                                          \
                dzlog_error(fmt, ##__VA_ARGS__);                                                                       \
            }                                                                                                          \
            else                                                                                                       \
            {                                                                                                          \
                printf(fmt, ##__VA_ARGS__);                                                                            \
            }                                                                                                          \
            exit(EXIT_FAILURE);                                                                                        \
        }                                                                                                              \
    } while (0)

#define DP_TRACE_NO_ROLE 0

extern struct mr_instance * mr_instance;

static inline bool is_directory_exists(const char * path)
{
    struct stat info;
    if (stat(path, &info) != 0)
        return false;
    return S_ISDIR(info.st_mode) == 1;
}

static inline bool is_file_exists(const char * path)
{
    return access(path, F_OK) == 0;
}

// Combine relative paths and absolute paths into a new absolute path
static inline char * paths_combine(const char * restrict prog_absolute_path, const char * restrict file_relative_path,
                                   char * restrict resolve_path, size_t resolve_path_size)
{
    int prog_len = strlen(prog_absolute_path);
    int file_len = strlen(file_relative_path);
    if (prog_len + file_len + 1 > resolve_path_size)
    {
        return NULL;
    }

    if (file_relative_path[0] == '/')
    {
        strcpy(resolve_path, file_relative_path);
        return resolve_path;
    }

    char combine_path[PATH_MAX];
    strcpy(combine_path, prog_absolute_path);

    char * last_slash = strrchr(combine_path, '/');
    if (last_slash == NULL)
    {
        return NULL;
    }
    *last_slash = '\0';

    strcat(combine_path, "/");
    strcat(combine_path, file_relative_path);

    realpath(combine_path, resolve_path);

    return resolve_path;
}

// Special character conversion adapted to geedge CM.
// unescape only for bpf.
// Full string escaping is confusing and difficult to read.
// pcap-filter support backslash (\). but no '\b' in bpf.
static inline void bpf_str_unescape_for_cm(const char * input, char * output)
{
    int i, j;

    for (i = 0, j = 0; i < strlen(input) - 1; i++)
    {
        if (input[i] == '\\' && input[i + 1] == '\\')
        {
            i += 1;
            output[j++] = '\\';
        }
        else if (input[i] == '\\' && input[i + 1] == 'b')
        {
            i += 1;
            output[j++] = ' ';
        }
        else if (input[i] == '\\' && input[i + 1] == '&')
        {
            i += 1;
            output[j++] = '&';
        }
        else if (input[i] == '\\' && input[i + 1] == '^')
        {
            i += 1;
            output[j++] = '^';
        }
        else if (input[i] == '\\' && input[i + 1] == '$')
        {
            i += 1;
            output[j++] = '$';
        }
        else if (input[i] == '\\' && input[i + 1] == '|')
        {
            i += 1;
            output[j++] = '|';
        }
        else if (input[i] == '\\' && input[i + 1] == '(')
        {
            i += 1;
            output[j++] = '(';
        }
        else if (input[i] == '\\' && input[i + 1] == ')')
        {
            i += 1;
            output[j++] = ')';
        }
        else
        {
            output[j++] = input[i];
        }
    }
    output[j++] = input[i];

    output[j] = '\0';
}