|
17 | 17 |
|
18 | 18 | #include <os> |
19 | 19 |
|
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 |
27 | 21 |
|
| 22 | +// name and binary of current service (built from another module) |
| 23 | +extern const char* service_binary_name__; |
| 24 | +extern const char* service_name__; |
28 | 25 |
|
29 | | -std::string Service::binary_name() { |
| 26 | +const char* Service::binary_name() { |
30 | 27 | return service_binary_name__; |
31 | 28 | } |
32 | | - |
33 | | -std::string Service::name() { |
| 29 | +const char* Service::name() { |
34 | 30 | return service_name__; |
35 | 31 | } |
36 | 32 |
|
37 | | - |
38 | 33 | // functions that we can override if we want to |
39 | 34 | __attribute__((weak)) |
40 | 35 | void Service::start() |
41 | 36 | { |
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); |
43 | 78 | } |
44 | 79 |
|
45 | 80 | __attribute__((weak)) |
|
0 commit comments