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     /* dont start an app that has no entry point */
49     if (app->entry == NULL) {
50         return;
51     }
52 
53     uint32_t stack_size = (app->flags & APP_FLAG_CUSTOM_STACK_SIZE) ? app->stack_size : DEFAULT_STACK_SIZE;
54 
55     printf("starting app %s\n", app->name);
56     thread_t *t = thread_create(app->name, &app_thread_entry, (void *)app, DEFAULT_PRIORITY, stack_size);
57     if (detach) {
58         thread_detach(t);
59         thread_resume(t);
60     } else {
61         thread_resume(t);
62         thread_join(t, NULL, INFINITE_TIME);
63     }
64 }
65 
app_start_by_name(const char * name,bool detached)66 status_t app_start_by_name(const char *name, bool detached) {
67     const struct app_descriptor *app;
68 
69     /* find the app and call it */
70     for (app = &__start_apps; app != &__stop_apps; app++) {
71         if (!strcmp(app->name, name)) {
72             start_app(app, detached);
73             return NO_ERROR;
74         }
75     }
76 
77     return ERR_NOT_FOUND;
78 }
79 
list_apps(void)80 static void list_apps(void) {
81     const struct app_descriptor *app;
82 
83     for (app = &__start_apps; app != &__stop_apps; app++) {
84         printf("%s\n", app->name);
85     }
86 }
87 
cmd_app(int argc,const console_cmd_args * argv)88 static int cmd_app(int argc, const console_cmd_args *argv) {
89     if (argc == 1) {
90 usage:
91         printf("%s subcommands:\n", argv[0].str);
92         printf("%s list         : list apps compiled into the system\n", argv[0].str);
93         printf("%s start <name> : run app\n", argv[0].str);
94         return ERR_INVALID_ARGS;
95     } else if (!strcmp(argv[1].str, "list")) {
96         list_apps();
97     } else if (!strcmp(argv[1].str, "start")) {
98         if (argc <= 2) {
99             printf("not enough args\n");
100             goto usage;
101         }
102 
103         app_start_by_name(argv[2].str, false);
104     } else {
105         printf("unknown subcommand\n");
106         goto usage;
107     }
108 
109     return NO_ERROR;
110 }
111 
cmd_start(int argc,const console_cmd_args * argv)112 static int cmd_start(int argc, const console_cmd_args *argv) {
113     if (argc == 1) {
114         printf("not enough args\n");
115         return ERR_INVALID_ARGS;
116     }
117 
118     return app_start_by_name(argv[1].str, false);
119 }
120 
121 STATIC_COMMAND_START
122 STATIC_COMMAND("app", "commands to operate on apps", &cmd_app)
123 STATIC_COMMAND("start", "shortcut for app start", &cmd_start)
124 STATIC_COMMAND_END(app);
125