Add delta.ua - a binary to apply an upgrade of DELTA_FS type.
[platform/core/system/upgrade.git] / src / delta-ua / fota_util.c
1 /*
2  * delta-ua
3  *
4  * Copyright (c) 2017 - 2022 Samsung Electronics Co., Ltd.
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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/wait.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <errno.h>
28
29 #include "engine/fota_common.h"
30
31 int s_fd_stdin = -1;
32 int s_fd_stdout = -1;
33 int s_fd_stderr = -1;
34
35 /*-----------------------------------------------------------------------------
36   _exit_stdio
37  ----------------------------------------------------------------------------*/
38 void _exit_stdio(void)
39 {
40         if (s_fd_stdin >= 0)
41                 close(s_fd_stdin);
42
43         if (s_fd_stdout >= 0)
44                 close(s_fd_stdout);
45
46         if (s_fd_stderr >= 0)
47                 close(s_fd_stderr);
48 }
49
50 /*-----------------------------------------------------------------------------
51   _system_cmd_wait
52  ----------------------------------------------------------------------------*/
53 int _system_cmd_wait(const char *command)
54 {
55
56         int pid = 0;
57         int status = 0;
58         char* const environ[2] = {"DISPLAY=:0", NULL };
59
60         if (command == NULL)
61                 return -1;
62
63         pid = fork();
64
65         if (pid == -1)
66                 return -1;
67
68         if (pid == 0) {
69                 char *argv[4];
70                 argv[0] = "sh";
71                 argv[1] = "-c";
72                 argv[2] = (char*)command;
73                 argv[3] = 0;
74                 execve("/bin/sh", argv, environ);
75                 exit(127);
76         }
77
78         do {
79                 if (waitpid(pid, &status, 0) == -1) {
80                         if (errno != EINTR)
81                                 return -1;
82                 } else {
83                         return status;
84                 }
85         } while (1);
86 }
87
88 /*-----------------------------------------------------------------------------
89   _system_cmd_nowait
90  ----------------------------------------------------------------------------*/
91 int _system_cmd_nowait(const char *command)
92 {
93
94         int pid = 0;
95         char* const environ[2] = {"DISPLAY=:0", NULL };
96
97         if (command == NULL)
98                 return -1;
99
100         pid = fork();
101
102         if (pid == -1)
103                 return -1;
104
105         if (pid == 0) {
106                 char *argv[4];
107                 argv[0] = "sh";
108                 argv[1] = "-c";
109                 argv[2] = (char*)command;
110                 argv[3] = 0;
111                 execve("/bin/sh", argv, environ);
112                 exit(127);
113         }
114
115         return 0;
116 }