1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG     "pkvm_enabler"
18 
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <unistd.h>
24 
25 #include <log/log.h>
26 
27 #define MISC_WRITER "/vendor/bin/misc_writer"
28 
main()29 int main() {
30     char *newargv[] = { MISC_WRITER, "--set-enable-pkvm", NULL };
31     char *newenvp[] = { NULL };
32     pid_t pid;
33     int ret, wstatus;
34 
35     /* Run misc_writer and return FAILURE to force a reboot. */
36     pid = fork();
37     if (pid == -1) {
38         ALOGE("Could not fork: %d", errno);
39         exit(EXIT_FAILURE);
40     }
41 
42     if (pid == 0) {
43         execve(MISC_WRITER, newargv, newenvp);
44         ALOGE("Could not execute " MISC_WRITER ": %d", errno);
45         _exit(EXIT_FAILURE);
46     }
47 
48     waitpid(pid, &wstatus, 0);
49     if (WIFEXITED(wstatus)) {
50         ret = WEXITSTATUS(wstatus);
51         if (ret) {
52             ALOGE(MISC_WRITER " exit status: %d", ret);
53         }
54     } else {
55         ALOGE(MISC_WRITER " terminated unexpectedly: %d", wstatus);
56     }
57 
58     exit(EXIT_FAILURE);
59 }
60