Do default action in RUI mode
[platform/core/system/initrd-recovery.git] / src / system-recovery / recovery-main.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /*
4  * Copyright (c) 2014 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 #define _POSIX_SOURCE
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <signal.h>
25 #include <unistd.h>
26 #include <stdbool.h>
27 #include <libconfig.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <sys/reboot.h>
35
36 #include <vconf/vconf-keys.h>
37 #include <asm-generic/setup.h> // for COMMAND_LINE_SIZE
38
39 #include "system-recovery.h"
40
41 void sys_power_reboot(void)
42 {
43         reboot(RB_AUTOBOOT);
44 }
45
46 #define KERNEL_CMDLINE_KEY "tizen.recovery"
47
48 // looks for tizen.recovery= key in kernel command line
49 char *get_action_from_cmdline(void)
50 {
51         FILE *fp;
52         char cmdline[COMMAND_LINE_SIZE];
53         int len;
54
55         fp = fopen("/proc/cmdline", "r");
56         if (!fp)
57                 return NULL;
58
59         char *p = fgets(cmdline, sizeof cmdline, fp);
60         fclose(fp);
61         if (!p)
62                 return NULL;
63
64         const char *prefix = KERNEL_CMDLINE_KEY "=";
65         p = strstr(cmdline, prefix);
66         if (!p)
67                 return NULL;
68         p += strlen(prefix);
69
70         for (len = 0; *(p + len) != 0 && !isspace(*(p + len)); ++len)
71                 ; /* skip */
72
73         return strndup(p, len);
74 }
75
76
77 int main(void)
78 {
79         config_t cfg;
80         int ret;
81
82         LOGD("[main] recovery started.\n");
83
84         config_init(&cfg);
85         ret = config_read_file(&cfg, SYSTEM_RECOVERY_CONFIG_FILE);
86         if (ret == CONFIG_FALSE) {
87                 LOGD("Can't read config file");
88                 return 1;
89         }
90
91 #ifdef RECOVERY_GUI
92         ret = recovery_gui(&cfg);
93 #else
94         ret = recovery_headless(&cfg);
95 #endif
96
97         LOGD("[main] recovery finished.\n");
98         config_destroy(&cfg);
99
100         return ret;
101 }