Skip to content

Commit f6069ec

Browse files
committed
Merge pull request #1622 from pguyot/w15/system-info-additions
Add `erlang:system_info/1` additions for remote shell These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 4bd3f8d + 68e6bd6 commit f6069ec

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

src/libAtomVM/defaultatoms.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,7 @@ X(TIMEOUT_ATOM, "\x7", "timeout")
181181
X(DIST_DATA_ATOM, "\x9", "dist_data")
182182
X(REQUEST_ATOM, "\x7", "request")
183183
X(CONNECT_ATOM, "\x7", "connect")
184+
185+
X(SYSTEM_VERSION_ATOM, "\xE", "system_version")
186+
X(OTP_RELEASE_ATOM, "\xB", "otp_release")
187+
X(BREAK_IGNORED_ATOM, "\xD", "break_ignored")

src/libAtomVM/dist_nifs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
extern "C" {
3535
#endif
3636

37+
#define DIST_OTP_RELEASE "27"
38+
3739
extern const ErlNifResourceTypeInit dist_connection_resource_type_init;
3840

3941
extern const struct Nif setnode_3_nif;

src/libAtomVM/nifs.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,6 +2898,19 @@ static term nif_erlang_system_info(Context *ctx, int argc, term argv[])
28982898
if (key == ATOMVM_VERSION_ATOM) {
28992899
return term_from_literal_binary((const uint8_t *) ATOMVM_VERSION, strlen(ATOMVM_VERSION), &ctx->heap, ctx->global);
29002900
}
2901+
if (key == SYSTEM_VERSION_ATOM) {
2902+
char system_version[256];
2903+
int len;
2904+
#ifndef AVM_NO_SMP
2905+
len = snprintf(system_version, sizeof(system_version), "AtomVM %s [%d-bit] [smp:%d:%d]\n", ATOMVM_VERSION, TERM_BYTES * 8, ctx->global->online_schedulers, smp_get_online_processors());
2906+
#else
2907+
len = snprintf(system_version, sizeof(system_version), "AtomVM %s [%d-bit] [nosmp]\n", ATOMVM_VERSION, TERM_BYTES * 8);
2908+
#endif
2909+
if (UNLIKELY(memory_ensure_free_opt(ctx, len * CONS_SIZE, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
2910+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
2911+
}
2912+
return interop_bytes_to_list(system_version, len, &ctx->heap);
2913+
}
29012914
if (key == REFC_BINARY_INFO_ATOM) {
29022915
fprintf(stderr, "WARNING: The refc_binary_info system info tag is deprecated. Use erlang:memory(binary) instead.\n");
29032916
term ret = refc_binary_create_binary_info(ctx);
@@ -2906,6 +2919,16 @@ static term nif_erlang_system_info(Context *ctx, int argc, term argv[])
29062919
}
29072920
return ret;
29082921
}
2922+
// For distribution, we report a gullible OTP version
2923+
if (key == OTP_RELEASE_ATOM) {
2924+
if (UNLIKELY(memory_ensure_free_opt(ctx, strlen(DIST_OTP_RELEASE) * CONS_SIZE, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
2925+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
2926+
}
2927+
return interop_bytes_to_list(DIST_OTP_RELEASE, strlen(DIST_OTP_RELEASE), &ctx->heap);
2928+
}
2929+
if (key == BREAK_IGNORED_ATOM) {
2930+
return TRUE_ATOM;
2931+
}
29092932
if (key == SCHEDULERS_ATOM) {
29102933
#ifndef AVM_NO_SMP
29112934
return term_from_int32(smp_get_online_processors());

tests/erlang_tests/test_system_info.erl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,31 @@ start() ->
3535
_ ->
3636
assert(is_binary(erlang:system_info(system_architecture)))
3737
end,
38+
SystemVersion = erlang:system_info(system_version),
39+
true = is_list(SystemVersion),
40+
case Machine of
41+
"BEAM" ->
42+
"Erlang/OTP " ++ _ = SystemVersion;
43+
_ ->
44+
"AtomVM " ++ _ = SystemVersion
45+
end,
46+
OTPRelease = erlang:system_info(otp_release),
47+
true = is_list(OTPRelease),
48+
OTPReleaseInt = list_to_integer(OTPRelease),
49+
true = OTPReleaseInt > 20,
3850
case Machine of
3951
"BEAM" ->
4052
% beam raises badarg, and probably so should AtomVM.
4153
ok =
4254
try
43-
erlang:system_info(some_wierd_unused_key),
55+
erlang:system_info(some_weird_unused_key),
4456
unexpected
4557
catch
4658
error:badarg ->
4759
ok
4860
end;
4961
_ ->
50-
assert(erlang:system_info(some_wierd_unused_key) =:= undefined)
62+
assert(erlang:system_info(some_weird_unused_key) =:= undefined)
5163
end,
5264
0.
5365

0 commit comments

Comments
 (0)