Skip to content

Commit 7bc0bf4

Browse files
committed
kernel: Change commandline, service name and binary name to const char*
1 parent 1aa2df7 commit 7bc0bf4

14 files changed

Lines changed: 84 additions & 85 deletions

File tree

api/kernel/os.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class OS {
5959
* other mechanisms. The first argument is always
6060
* the binary name.
6161
**/
62-
static const std::string& cmdline_args() noexcept;
62+
static const char* cmdline_args() noexcept;
6363

6464
/** Clock cycles since boot. */
6565
static uint64_t cycles_since_boot() {
@@ -257,7 +257,6 @@ class OS {
257257
static bool boot_sequence_passed_;
258258
static MHz cpu_mhz_;
259259

260-
// XXX: Only used by solo5
261260
static RTC::timestamp_t booted_at_;
262261
static uintptr_t liveupdate_loc_;
263262
static std::string version_str_;
@@ -266,7 +265,7 @@ class OS {
266265
static uintptr_t memory_end_;
267266
static uintptr_t heap_max_;
268267
static const uintptr_t elf_binary_size_;
269-
static std::string cmdline;
268+
static const char* cmdline;
270269

271270
// Prohibit copy and move operations
272271
OS(OS&) = delete;

api/kernel/service.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
88
// You may obtain a copy of the License at
9-
//
9+
//
1010
// http://www.apache.org/licenses/LICENSE-2.0
11-
//
11+
//
1212
// Unless required by applicable law or agreed to in writing, software
1313
// distributed under the License is distributed on an "AS IS" BASIS,
1414
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -30,12 +30,12 @@ class Service {
3030
/**
3131
* @return: The (descriptive) name of the service
3232
*/
33-
static std::string name();
33+
static const char* name();
3434

3535
/**
3636
* @return: The name of the service binary
3737
*/
38-
static std::string binary_name();
38+
static const char* binary_name();
3939

4040

4141
/**

src/kernel/main_call.cpp

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,3 @@
1717

1818
#include <service>
1919
#include <info>
20-
#define ARGS_MAX 64
21-
22-
extern "C" {
23-
__attribute__((weak))
24-
int main(int, const char*[]);
25-
}
26-
27-
__attribute__((weak))
28-
void Service::start(const std::string& cmd)
29-
{
30-
std::string st(cmd); // mangled copy
31-
int argc = 0;
32-
const char* argv[ARGS_MAX];
33-
34-
// Get pointers to null-terminated string
35-
char* word = (char*) st.c_str();
36-
char* end = word + st.size() + 1;
37-
bool new_word = false;
38-
39-
for (char* ptr = word; ptr < end; ptr++) {
40-
41-
// Replace all spaces with 0
42-
if(std::isspace(*ptr)) {
43-
*ptr = 0;
44-
new_word = true;
45-
continue;
46-
}
47-
48-
// At the start of each word, or last byte, add previous pointer to array
49-
if (new_word or ptr == end - 1) {
50-
argv[argc++] = word;
51-
word = ptr; // next arg
52-
if (argc >= ARGS_MAX) break;
53-
new_word = false;
54-
}
55-
}
56-
57-
int exit_status = main(argc, argv);
58-
INFO("main","returned with status %d", exit_status);
59-
60-
}

src/kernel/multiboot.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void OS::multiboot(uint32_t boot_addr)
116116
if (bootinfo_->flags & MULTIBOOT_INFO_CMDLINE) {
117117
INFO2("* Booted with parameters @ 0x%x: %s", bootinfo_->cmdline,
118118
reinterpret_cast<const char*>(bootinfo_->cmdline));
119-
cmdline = reinterpret_cast<const char*>(bootinfo_->cmdline);
119+
OS::cmdline = reinterpret_cast<const char*>(bootinfo_->cmdline);
120120
}
121121

122122
/*

src/kernel/os.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ uintptr_t OS::liveupdate_loc_ = 0;
5252
uintptr_t OS::memory_end_ = 0;
5353
uintptr_t OS::heap_max_ = (uintptr_t) -1;
5454
const uintptr_t OS::elf_binary_size_ {(uintptr_t)&_ELF_END_ - (uintptr_t)&_ELF_START_};
55-
std::string OS::cmdline{Service::binary_name()};
5655

5756
// stdout redirection
5857
using Print_vec = fixedvector<OS::print_func, 8>;
@@ -73,8 +72,8 @@ void* OS::liveupdate_storage_area() noexcept
7372
return (void*) OS::liveupdate_loc_;
7473
}
7574

76-
const std::string& OS::cmdline_args() noexcept
77-
{
75+
const char* OS::cmdline = nullptr;
76+
const char* OS::cmdline_args() noexcept {
7877
return cmdline;
7978
}
8079

@@ -135,7 +134,7 @@ void OS::post_start()
135134
printf(" IncludeOS %s (%s / %i-bit)\n",
136135
version().c_str(), arch().c_str(),
137136
static_cast<int>(sizeof(uintptr_t)) * 8);
138-
printf(" +--> Running [ %s ]\n", Service::name().c_str());
137+
printf(" +--> Running [ %s ]\n", Service::name());
139138
FILLINE('~');
140139

141140
Service::start();

src/kernel/service_stub.cpp

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,64 @@
1717

1818
#include <os>
1919

20-
// the name of the current service (built from another module)
21-
extern "C" {
22-
__attribute__((weak))
23-
const char* service_name__ = "(missing service name)";
24-
__attribute__((weak))
25-
const char* service_binary_name__ = "(missing binary name)";
26-
}
20+
#define ARGS_MAX 64
2721

22+
// name and binary of current service (built from another module)
23+
extern const char* service_binary_name__;
24+
extern const char* service_name__;
2825

29-
std::string Service::binary_name() {
26+
const char* Service::binary_name() {
3027
return service_binary_name__;
3128
}
32-
33-
std::string Service::name() {
29+
const char* Service::name() {
3430
return service_name__;
3531
}
3632

37-
3833
// functions that we can override if we want to
3934
__attribute__((weak))
4035
void Service::start()
4136
{
42-
Service::start(OS::cmdline_args());
37+
const std::string args(OS::cmdline_args());
38+
Service::start(args);
39+
}
40+
41+
extern "C" {
42+
__attribute__((weak))
43+
int main(int, const char*[]) {}
44+
}
45+
46+
__attribute__((weak))
47+
void Service::start(const std::string& cmd)
48+
{
49+
std::string st(cmd); // mangled copy
50+
int argc = 0;
51+
const char* argv[ARGS_MAX];
52+
53+
// Get pointers to null-terminated string
54+
char* word = (char*) st.c_str();
55+
char* end = word + st.size() + 1;
56+
bool new_word = false;
57+
58+
for (char* ptr = word; ptr < end; ptr++) {
59+
60+
// Replace all spaces with 0
61+
if(std::isspace(*ptr)) {
62+
*ptr = 0;
63+
new_word = true;
64+
continue;
65+
}
66+
67+
// At the start of each word, or last byte, add previous pointer to array
68+
if (new_word or ptr == end - 1) {
69+
argv[argc++] = word;
70+
word = ptr; // next arg
71+
if (argc >= ARGS_MAX) break;
72+
new_word = false;
73+
}
74+
}
75+
76+
int exit_status = main(argc, argv);
77+
INFO("main","returned with status %d", exit_status);
4378
}
4479

4580
__attribute__((weak))

src/platform/x86_pc/os.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ void OS::default_stdout(const char* str, const size_t len)
9696

9797
void OS::start(uint32_t boot_magic, uint32_t boot_addr)
9898
{
99+
OS::cmdline = Service::binary_name();
99100
// Initialize stdout handlers
100101
OS::add_stdout(&OS::default_stdout);
101102

@@ -112,6 +113,10 @@ void OS::start(uint32_t boot_magic, uint32_t boot_addr)
112113
/// initialize on page 7, 3 pages in size
113114
Statman::get().init(0x6000, 0x3000);
114115

116+
// Call global ctors
117+
PROFILE("Global constructors");
118+
__libc_init_array();
119+
115120
// BOOT METHOD //
116121
PROFILE("Multiboot / legacy");
117122
OS::memory_end_ = 0;
@@ -129,10 +134,6 @@ void OS::start(uint32_t boot_magic, uint32_t boot_addr)
129134
// Give the rest of physical memory to heap
130135
OS::heap_max_ = OS::memory_end_;
131136

132-
// Call global ctors
133-
PROFILE("Global constructors");
134-
__libc_init_array();
135-
136137
PROFILE("Memory map");
137138
// Assign memory ranges used by the kernel
138139
auto& memmap = memory_map();

src/platform/x86_solo5/os.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void OS::start(char* _cmdline, uintptr_t mem_size)
8686
/// initialize on page 7, 2 pages in size
8787
Statman::get().init(0x6000, 0x3000);
8888

89-
OS::cmdline = reinterpret_cast<char*>(_cmdline);
89+
OS::cmdline = _cmdline;
9090

9191
// setup memory and heap end
9292
OS::memory_end_ = mem_size;

src/plugins/syslogd.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void Syslog::syslog(const int priority, const char* buf) {
102102
message += std::string{timebuf} + " " + net::Inet4::stack().ip_addr().str() + " ";
103103

104104
// Fourth: App-name, PROCID (LOG_PID) and MSGID
105-
message += Service::binary_name() + " " + std::to_string(getpid()) + " UDPOUT ";
105+
message += std::string(Service::binary_name()) + " " + std::to_string(getpid()) + " UDPOUT ";
106106

107107
/* Structured data: SD-element (SD-ID PARAM-NAME=PARAM-VALUE) */
108108
message += "- "; // NILVALUE

src/util/syslogd.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void Syslog::syslog(const int priority, const char* buf) {
8282
message += std::string{timebuf} + " ";
8383

8484
// Fourth: App-name
85-
message += Service::binary_name();
85+
message += std::string(Service::binary_name());
8686

8787
// Fifth: Add ident if is set (through openlog)
8888
if (fac_->ident_is_set())

0 commit comments

Comments
 (0)