Skip to content

Commit f230b98

Browse files
committed
[misc] git requests and testing
1 parent 251a20a commit f230b98

17 files changed

Lines changed: 316 additions & 404 deletions

File tree

common.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ BUILD_DIR := ./build
1414
COMMON_FLAGS ?= -ffreestanding -nostdlib -fno-exceptions -fno-unwind-tables \
1515
-fno-asynchronous-unwind-tables -g -O0 -Wall -Wextra \
1616
-Wno-unused-parameter -Wno-address-of-packed-member \
17-
#-Werror \
17+
-Werror \
1818
-Wno-unused-function
1919

2020
ifeq ($(ARCH), aarch64-none-elf-)

kernel/bin/ping.c

Lines changed: 26 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ typedef struct {
2626
const char *host;
2727
} ping_opts_t;
2828

29-
static void help(file *fd) {
30-
const char *a = "usage: ping [-4/-6] [-n times] [-w timeout] [-i interval] [-t TTL] [-s src_local_ip] host";
31-
write_file(fd, a, strlen_max(a, STRING_MAX_LEN));
32-
write_file(fd, "\n", 1);
33-
}
34-
3529
static bool parse_args(int argc, char *argv[], ping_opts_t *o) {
3630
o->ver = IP_VER4;
3731
o->count = 4;
@@ -94,7 +88,7 @@ static const char *status_to_msg(uint8_t st) {
9488
}
9589
}
9690

97-
static int ping_v4(file *fd, const ping_opts_t *o) {
91+
static int ping_v4(const ping_opts_t *o) {
9892
const char *host = o->host;
9993

10094
uint32_t dst_ip_be = 0;
@@ -103,10 +97,7 @@ static int ping_v4(file *fd, const ping_opts_t *o) {
10397
uint32_t r = 0;
10498
dns_result_t dr = dns_resolve_a(host, &r, DNS_USE_BOTH, o->timeout_ms);
10599
if (dr != DNS_OK) {
106-
string m = string_format("ping: dns lookup failed (%d) for '%s'", (int)dr, host);
107-
write_file(fd, m.data, m.length);
108-
write_file(fd, "\n", 1);
109-
string_free(m);
100+
print("ping: dns lookup failed (%d) for '%s'\n", (int)dr, host);
110101
return 2;
111102
}
112103
dst_ip_be = r;
@@ -115,11 +106,7 @@ static int ping_v4(file *fd, const ping_opts_t *o) {
115106
char ipstr[16];
116107
ipv4_to_string(dst_ip_be, ipstr);
117108

118-
write_file(fd, "PING ", 5);
119-
write_file(fd, host, strlen_max(host, STRING_MAX_LEN));
120-
write_file(fd, " (", 2);
121-
write_file(fd, ipstr, strlen_max(ipstr, STRING_MAX_LEN));
122-
write_file(fd, ") with 32 bytes of data:\n", 26);
109+
print("PING %s (%s) with 32 bytes of data:\n", host, ipstr);
123110

124111
uint32_t sent = 0, received = 0, min_ms = UINT32_MAX, max_ms = 0;
125112
uint64_t sum_ms = 0;
@@ -131,8 +118,7 @@ static int ping_v4(file *fd, const ping_opts_t *o) {
131118
if (o->src_set) {
132119
l3_ipv4_interface_t *l3 = l3_ipv4_find_by_ip(o->src_ip);
133120
if (!l3) {
134-
const char *em = "ping: invalid source (no local ip match)\n";
135-
write_file(fd, em, strlen_max(em, STRING_MAX_LEN));
121+
print("ping: invalid source (no local ip match)\n");
136122
return 2;
137123
}
138124
txo.index = (uint8_t)l3->l3_id;
@@ -153,71 +139,48 @@ static int ping_v4(file *fd, const ping_opts_t *o) {
153139
if (rtt < min_ms) min_ms = rtt;
154140
if (rtt > max_ms) max_ms = rtt;
155141
sum_ms += rtt;
156-
string ln = string_format("Reply from %s: bytes=32 time=%ums", ipstr, (uint32_t)rtt);
157-
write_file(fd, ln.data, ln.length);
158-
write_file(fd, "\n", 1);
159-
string_free(ln);
142+
print("Reply from %s: bytes=32 time=%ums\n", ipstr, rtt);
160143
} else {
161-
const char *msg = status_to_msg(res.status);
162-
write_file(fd, msg, strlen(msg));
163-
write_file(fd, "\n", 1);
144+
print("%s\n", status_to_msg(res.status));
164145
}
165146

166147
if (i + 1 < o->count) msleep(o->interval_ms);
167148
}
168149

169-
write_file(fd, "\n", 1);
170-
171-
string h = string_format("--- %s ping statistics ---", host);
172-
write_file(fd, h.data, h.length);
173-
write_file(fd, "\n", 1);
174-
string_free(h);
150+
print("\n");
151+
print("--- %s ping statistics ---\n", host);
175152

176153
uint32_t loss = (sent == 0) ? 0 : (uint32_t)((((uint64_t)(sent - received)) * 100) / sent);
177154
uint32_t total_time = (o->count > 0) ? (o->count - 1) * o->interval_ms : 0;
178155

179-
string s = string_format("%u packets transmitted, %u received, %u%% packet loss, time %ums", sent, received, loss, total_time);
180-
write_file(fd, s.data, s.length);
181-
write_file(fd, "\n", 1);
182-
string_free(s);
156+
print("%u packets transmitted, %u received, %u%% packet loss, time %ums\n", sent, received, loss, total_time);
183157

184158
if (received > 0) {
185159
uint32_t avg = (uint32_t)(sum_ms / received);
186160
if (min_ms == UINT32_MAX) min_ms = avg;
187-
string r = string_format("rtt min/avg/max = %u/%u/%u ms", min_ms, avg, max_ms);
188-
write_file(fd, r.data, r.length);
189-
write_file(fd, "\n", 1);
190-
string_free(r);
161+
print("rtt min/avg/max = %u/%u/%u ms\n", min_ms, avg, max_ms);
191162
}
192163

193164
return (received > 0) ? 0 : 1;
194165
}
195166

196-
static int ping_v6(file *fd, const ping_opts_t *o) {
167+
static int ping_v6(const ping_opts_t *o) {
197168
const char *host = o->host;
198169

199170
uint8_t dst6[16] ={0};
200171
bool is_lit = ipv6_parse(host, dst6);
201172
if (!is_lit) {
202173
dns_result_t dr = dns_resolve_aaaa(host, dst6, DNS_USE_BOTH, o->timeout_ms);
203174
if (dr != DNS_OK) {
204-
string m = string_format("ping: dns lookup failed (%d) for '%s'",(int)dr, host);
205-
write_file(fd, m.data, m.length);
206-
write_file(fd, "\n", 1);
207-
string_free(m);
175+
print("ping: dns lookup failed (%d) for '%s'\n",(int)dr, host);
208176
return 2;
209177
}
210178
}
211179

212180
char ipstr[64];
213181
ipv6_to_string(dst6, ipstr, (int)sizeof(ipstr));
214182

215-
write_file(fd, "PING ", 5);
216-
write_file(fd, host, strlen(host));
217-
write_file(fd, " (", 2);
218-
write_file(fd, ipstr, strlen(ipstr));
219-
write_file(fd, ") with 32 bytes of data:", 25);
220-
write_file(fd, "\n", 1);
183+
print("PING %s (%s) with 32 bytes of data:\n", host, ipstr);
221184

222185
uint32_t sent = 0, received = 0, min_ms = UINT32_MAX, max_ms = 0;
223186
uint64_t sum_ms = 0;
@@ -237,60 +200,41 @@ static int ping_v6(file *fd, const ping_opts_t *o) {
237200
if (rtt < min_ms) min_ms = rtt;
238201
if (rtt > max_ms) max_ms = rtt;
239202
sum_ms += rtt;
240-
241-
string ln = string_format("Reply from %s: bytes=32 time=%ums", ipstr, (uint32_t)rtt);
242-
write_file(fd, ln.data, ln.length);
243-
write_file(fd, "\n", 1);
244-
string_free(ln);
203+
print("Reply from %s: bytes=32 time=%ums\n", ipstr, rtt);
245204
} else {
246-
const char *msg = status_to_msg(res.status);
247-
write_file(fd, msg, strlen(msg));
248-
write_file(fd, "\n", 1);
205+
print("%s\n", status_to_msg(res.status));
249206
}
250207

251208
if (i + 1 < o->count) msleep(o->interval_ms);
252209
}
253210

254-
write_file(fd, "\n", 1);
255-
256-
string h = string_format("--- %s ping statistics ---", host);
257-
write_file(fd, h.data, h.length);
258-
write_file(fd, "\n", 1);
259-
string_free(h);
211+
print("\n");
260212

213+
print("--- %s ping statistics ---\n", host);
261214

262215
uint32_t loss = (sent == 0) ? 0 : (uint32_t)((((uint64_t)(sent - received)) * 100) / sent);
263216
uint32_t total_time = (o->count > 0) ? (o->count - 1) * o->interval_ms : 0;
264217

265-
string s = string_format("%u packets transmitted, %u received, %u%% packet loss, time %ums", sent, received, loss, total_time);
266-
write_file(fd, s.data, s.length);
267-
write_file(fd, "\n", 1);
268-
string_free(s);
218+
print("%u packets transmitted, %u received, %u%% packet loss, time %ums\n", sent, received, loss, total_time);
269219

270220
if (received > 0) {
271221
uint32_t avg = (uint32_t)(sum_ms / received);
272222
if (min_ms == UINT32_MAX) min_ms = avg;
273-
string r = string_format("rtt min/avg/max = %u/%u/%u ms", min_ms, avg, max_ms);
274-
write_file(fd, r.data, r.length);
275-
write_file(fd, "\n", 1);
276-
string_free(r);
223+
print("rtt min/avg/max = %u/%u/%u ms\n", min_ms, avg, max_ms);
277224
}
278225

279226
return (received > 0) ? 0 : 1;
280227
}
281228

282229
int run_ping(int argc, char *argv[]) {
283-
file fd = (file){.id = FD_OUT};
284-
285230
ping_opts_t opts;
286231
if (!parse_args(argc, argv, &opts)) {
287-
help(&fd);
232+
print("usage: ping [-4/-6] [-n times] [-w timeout] [-i interval] [-t TTL] [-s src_local_ip] host\n");
288233
return 2;
289234
}
290235

291236
if (opts.ver == IP_VER6 && opts.src_set) {
292-
const char *em = "ping: -s is only supported for IPv4\n";
293-
write_file(&fd, em, strlen(em));
237+
print("ping: -s is only supported for IPv4\n");
294238
return 2;
295239
}
296240

@@ -299,18 +243,14 @@ int run_ping(int argc, char *argv[]) {
299243
if (!l3) {
300244
char ssrc[16];
301245
ipv4_to_string(opts.src_ip, ssrc);
302-
string em = string_format("ping: invalid source %s (no local ip match)", ssrc);
303-
write_file(&fd, em.data, em.length);
304-
write_file(&fd, "\n", 1);
305-
string_free(em);
246+
print("ping: invalid source %s (no local ip match)\n", ssrc);
306247
return 2;
307248
}
308249
}
309250

310-
int rc = 0;
311-
if (opts.ver == IP_VER4) rc = ping_v4(&fd, &opts);
312-
else if (opts.ver == IP_VER6) rc = ping_v6(&fd, &opts);
313-
else { help(&fd); rc = 2; }
251+
if (opts.ver == IP_VER4) return ping_v4(&opts);
252+
if (opts.ver == IP_VER6) return ping_v6(&opts);
253+
print("usage: ping [-4/-6] [-n times] [-w timeout] [-i interval] [-t TTL] [-s src_local_ip] host\n");
314254

315-
return rc;
255+
return 2;
316256
}

kernel/bin/shutdown.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ int run_shutdown(int argc, char* argv[]){
1111
const char *u = "usage: shutdown [-r|-p]\n -r reboot\n -p power off\n";
1212

1313

14-
file out = (file){.id = FD_OUT};
1514
if (argc <= 0){
16-
write_file(&out, u,strlen(u));
17-
return 0;
15+
print("%s", u);
16+
return 0;
1817
}
1918

2019
int mode = -1;
@@ -26,20 +25,20 @@ int run_shutdown(int argc, char* argv[]){
2625
if (strcmp(a, "-r") == 0) mode = SHUTDOWN_REBOOT;
2726
else if (strcmp(a, "-p") == 0) mode = SHUTDOWN_POWEROFF;
2827
else{
29-
write_file(&out, u,strlen(u));
28+
print("%s", u);
3029
msleep(100);
3130
return 2;
3231
}
3332
}
3433

3534
if (mode == -1){
36-
write_file(&out, u,strlen(u));
35+
print("%s", u);
3736
msleep(100);
3837
return 2;
3938
}
4039

41-
if (mode == SHUTDOWN_REBOOT) write_file(&out, "Rebooting...\n", 13);
42-
else write_file(&out, "Powering off...\n", 16);
40+
if (mode == SHUTDOWN_REBOOT) print("Rebooting...\n");
41+
else print("Powering off...\n");
4342

4443
msleep(100);
4544
hw_shutdown(mode);

0 commit comments

Comments
 (0)