1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 #include <sys/types.h>
31 #include "process-util.h"
32 #include "system-recovery.h" /* For LOGD */
34 int process_exec(process_exec_type sync, char *argv[])
37 struct sigaction act, oldact;
43 /* Use default signal handler */
44 act.sa_handler = SIG_DFL;
46 sigemptyset(&act.sa_mask);
48 if (sigaction(SIGCHLD, &act, &oldact) < 0)
52 if (pid < 0) { /* Error */
53 LOGD("Failed to fork\n");
55 } else if (!pid) { /* Child */
58 for (i = 0; i < _NSIG; i++)
61 r = execv(argv[0], (char **)argv);
67 if (sync == PROCESS_NON_WAIT)
71 if (waitpid(pid, &status, 0) != -1) {
72 if (WIFEXITED(status)) { /* Terminated normally */
73 LOGD("%d terminated by exit(%d)\n", pid,
75 return WEXITSTATUS(status);
76 } else if (WIFSIGNALED(status))
77 LOGD("%d terminated by signal %d\n", pid,
79 else if (WIFSTOPPED(status))
80 LOGD("%d stopped by signal %d\n", pid,
83 LOGD("%d waitpid() failed - %d\n", pid, errno);
86 if (sigaction(SIGCHLD, &oldact, NULL) < 0)
87 LOGD("failed to restore sigaction\n");