Do default action in RUI mode
[platform/core/system/initrd-recovery.git] / src / system-recovery / recovery-headless.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /*
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <libconfig.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include <errno.h>
25
26 #include "config.h"
27 #include "log.h"
28 #include "system-recovery.h"
29
30 static char *get_action_from_config(config_t *cfg)
31 {
32         config_setting_t *node;
33         const char *action;
34
35         node = config_lookup(cfg, "headless_action");
36         if (!node)
37                 return NULL;
38
39         action = config_setting_get_string(node);
40
41         return strdup(action);
42 }
43
44 int recovery_headless(config_t *cfg)
45 {
46         config_setting_t *node;
47
48         node = config_lookup(cfg, "action_handlers");
49         if (!node)
50                 return -ENOENT;
51
52         char *action = get_action_from_cmdline();
53         if (!action)
54                 action = get_action_from_config(cfg);
55         if (!action)
56                 return -ENOENT;
57
58         const char *handler;
59         int r = config_setting_lookup_string(node, action, &handler);
60         free(action);
61
62         if (r == CONFIG_FALSE)
63                 return -ENOENT;
64
65         return system(handler);
66 }