1 /*
2 * Copyright (c) 2009 Travis Geiselbrecht
3 *
4 * Use of this source code is governed by a MIT-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/MIT
7 */
8 #include "app.h"
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <lk/err.h>
13 #include <lk/console_cmd.h>
14 #include <kernel/thread.h>
15
16 extern const struct app_descriptor __start_apps __WEAK;
17 extern const struct app_descriptor __stop_apps __WEAK;
18
19 static void start_app(const struct app_descriptor *app, bool detach);
20
21 /* one time setup */
apps_init(void)22 void apps_init(void) {
23 const struct app_descriptor *app;
24
25 /* call all the init routines */
26 for (app = &__start_apps; app != &__stop_apps; app++) {
27 if (app->init)
28 app->init(app);
29 }
30
31 /* start any that want to start on boot */
32 for (app = &__start_apps; app != &__stop_apps; app++) {
33 if (app->entry && (app->flags & APP_FLAG_NO_AUTOSTART) == 0) {
34 start_app(app, true);
35 }
36 }
37 }
38
app_thread_entry(void * arg)39 static int app_thread_entry(void *arg) {
40 const struct app_descriptor *app = (const struct app_descriptor *)arg;
41
42 app->entry(app, NULL);
43
44 return 0;
45 }
46
start_app(const struct app_descriptor * app,bool detach)47 static void start_app(const struct app_descriptor *app, bool detach) {
48 uint32_t stack_size = (app->flags & APP_FLAG_CUSTOM_STACK_SIZE) ? app->stack_size : DEFAULT_STACK_SIZE;
49
50 printf("starting app %s\n", app->name);
51 thread_t *t = thread_create(app->name, &app_thread_entry, (void *)app, DEFAULT_PRIORITY, stack_size);
52 if (detach) {
53 thread_detach(t);
54 thread_resume(t);
55 } else {
56 thread_resume(t);
57 thread_join(t, NULL, INFINITE_TIME);
58 }
59 }
60
app_start_by_name(const char * name,bool detached)61 status_t app_start_by_name(const char *name, bool detached) {
62 const struct app_descriptor *app;
63
64 /* find the app and call it */
65 for (app = &__start_apps; app != &__stop_apps; app++) {
66 if (!strcmp(app->name, name)) {
67 start_app(app, detached);
68 return NO_ERROR;
69 }
70 }
71
72 return ERR_NOT_FOUND;
73 }
74
list_apps(void)75 static void list_apps(void) {
76 const struct app_descriptor *app;
77
78 for (app = &__start_apps; app != &__stop_apps; app++) {
79 printf("%s\n", app->name);
80 }
81 }
82
cmd_app(int argc,const console_cmd_args * argv)83 static int cmd_app(int argc, const console_cmd_args *argv) {
84 if (argc == 1) {
85 usage:
86 printf("%s subcommands:\n", argv[0].str);
87 printf("%s list : list apps compiled into the system\n", argv[0].str);
88 printf("%s start <name> : run app\n", argv[0].str);
89 return ERR_INVALID_ARGS;
90 } else if (!strcmp(argv[1].str, "list")) {
91 list_apps();
92 } else if (!strcmp(argv[1].str, "start")) {
93 if (argc <= 2) {
94 printf("not enough args\n");
95 goto usage;
96 }
97
98 app_start_by_name(argv[2].str, false);
99 } else {
100 printf("unknown subcommand\n");
101 goto usage;
102 }
103
104 return NO_ERROR;
105 }
106
cmd_start(int argc,const console_cmd_args * argv)107 static int cmd_start(int argc, const console_cmd_args *argv) {
108 if (argc == 1) {
109 printf("not enough args\n");
110 return ERR_INVALID_ARGS;
111 }
112
113 return app_start_by_name(argv[1].str, false);
114 }
115
116 STATIC_COMMAND_START
117 STATIC_COMMAND("app", "commands to operate on apps", &cmd_app)
118 STATIC_COMMAND("start", "shortcut for app start", &cmd_start)
119 STATIC_COMMAND_END(app);
120