/src/fluent-bit/src/flb_kernel.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* Fluent Bit |
4 | | * ========== |
5 | | * Copyright (C) 2015-2022 The Fluent Bit Authors |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <fluent-bit/flb_info.h> |
21 | | #include <fluent-bit/flb_mem.h> |
22 | | #include <fluent-bit/flb_kernel.h> |
23 | | #include <fluent-bit/flb_utils.h> |
24 | | |
25 | | #ifdef _WIN32 |
26 | | |
27 | | /* Dummy function for Windows environment */ |
28 | | struct flb_kernel *flb_kernel_info() |
29 | | { |
30 | | int len; |
31 | | struct flb_kernel *kernel; |
32 | | |
33 | | kernel = flb_malloc(sizeof(struct flb_kernel)); |
34 | | if (!kernel) { |
35 | | flb_errno(); |
36 | | return NULL; |
37 | | } |
38 | | |
39 | | kernel->minor = 0; |
40 | | kernel->major = 0; |
41 | | kernel->patch = 0; |
42 | | kernel->s_version.data = flb_malloc(16); |
43 | | |
44 | | if (!kernel->s_version.data) { |
45 | | flb_errno(); |
46 | | flb_free(kernel); |
47 | | return NULL; |
48 | | } |
49 | | |
50 | | |
51 | | len = snprintf(kernel->s_version.data, 16, "0.0.0"); |
52 | | if (len == -1) { |
53 | | perror("snprintf"); |
54 | | return NULL; |
55 | | } |
56 | | kernel->s_version.len = len; |
57 | | kernel->n_version = 0; |
58 | | |
59 | | return kernel; |
60 | | } |
61 | | #else |
62 | | |
63 | | #include <ctype.h> |
64 | | #include <sys/utsname.h> |
65 | | |
66 | | /* |
67 | | * Routine taken from Monkey Project, Eduardo says it's ok ;) |
68 | | */ |
69 | | struct flb_kernel *flb_kernel_info() |
70 | 396 | { |
71 | | |
72 | 396 | int a, b, c; |
73 | 396 | int len; |
74 | 396 | int pos; |
75 | 396 | char *p, *t; |
76 | 396 | char *tmp; |
77 | 396 | struct utsname uts; |
78 | 396 | struct flb_kernel *kernel; |
79 | | |
80 | 396 | if (uname(&uts) == -1) { |
81 | 0 | flb_errno(); |
82 | 0 | return NULL; |
83 | 0 | } |
84 | 396 | len = strlen(uts.release); |
85 | | |
86 | | /* Fixme: this don't support Linux Kernel 10.x.x :P */ |
87 | 396 | a = (*uts.release - '0'); |
88 | | |
89 | | /* Second number */ |
90 | 396 | p = (uts.release) + 2; |
91 | 396 | pos = mk_string_char_search(p, '.', len - 2); |
92 | 396 | if (pos <= 0) { |
93 | | /* Some Debian systems uses a different notation, e.g: 3.14-2-amd64 */ |
94 | 0 | pos = mk_string_char_search(p, '-', len - 2); |
95 | 0 | if (pos <= 0) { |
96 | 0 | return NULL; |
97 | 0 | } |
98 | 0 | } |
99 | | |
100 | 396 | tmp = mk_string_copy_substr(p, 0, pos); |
101 | 396 | if (!tmp) { |
102 | 0 | return NULL; |
103 | 0 | } |
104 | 396 | b = atoi(tmp); |
105 | 396 | mk_mem_free(tmp); |
106 | | |
107 | | /* Last number (it needs filtering) */ |
108 | 396 | t = p = p + pos + 1; |
109 | 396 | do { |
110 | 396 | t++; |
111 | 396 | } while (isdigit(*t)); |
112 | | |
113 | 396 | tmp = mk_string_copy_substr(p, 0, t - p); |
114 | 396 | if (!tmp) { |
115 | 0 | return NULL; |
116 | 0 | } |
117 | 396 | c = atoi(tmp); |
118 | 396 | mk_mem_free(tmp); |
119 | | |
120 | 396 | kernel = flb_malloc(sizeof(struct flb_kernel)); |
121 | 396 | if (!kernel) { |
122 | 0 | flb_errno(); |
123 | 0 | return NULL; |
124 | 0 | } |
125 | 396 | kernel->minor = a; |
126 | 396 | kernel->major = b; |
127 | 396 | kernel->patch = c; |
128 | 396 | kernel->s_version.data = flb_malloc(16); |
129 | | |
130 | 396 | if (!kernel->s_version.data) { |
131 | 0 | flb_errno(); |
132 | 0 | flb_free(kernel); |
133 | 0 | return NULL; |
134 | 0 | } |
135 | | |
136 | 396 | len = snprintf(kernel->s_version.data, 16, "%i.%i.%i", a, b, c); |
137 | 396 | if (len == -1) { |
138 | 0 | flb_errno(); |
139 | 0 | flb_free(kernel->s_version.data); |
140 | 0 | flb_free(kernel); |
141 | 0 | return NULL; |
142 | 0 | } |
143 | 396 | kernel->s_version.len = len; |
144 | 396 | kernel->n_version = FLB_KERNEL_VERSION(a, b, c); |
145 | | |
146 | 396 | return kernel; |
147 | 396 | } |
148 | | |
149 | | #endif |