blob: 863af74a94f63a9b2089e59a002522cff272bb1a [file] [log] [blame]
Jason Samsf313dc32013-07-09 14:29:39 -07001/*
2 * Copyright (C) 2013 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
Stephen Hines574854b2013-07-10 08:29:51 -070017#include "rsUtils.h"
Jason Samsf313dc32013-07-09 14:29:39 -070018#include "rsCppUtils.h"
19
Yang Ni2abfcc62015-02-17 16:05:19 -080020#include <string>
21
Elliott Hughesa567cfa2015-01-28 12:15:15 -080022#include <string.h>
23
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -070024#ifndef RS_COMPATIBILITY_LIB
25#include <sys/wait.h>
26#endif
27
Jason Samsf313dc32013-07-09 14:29:39 -070028namespace android {
29namespace renderscript {
30
31const char * rsuCopyString(const char *name) {
32 return rsuCopyString(name, strlen(name));
33}
34
35const char * rsuCopyString(const char *name, size_t len) {
36 char *n = new char[len+1];
37 memcpy(n, name, len);
38 n[len] = 0;
39 return n;
40}
41
Yang Ni2abfcc62015-02-17 16:05:19 -080042const char* rsuJoinStrings(int n, const char* const* strs) {
43 std::string tmp;
44 for (int i = 0; i < n; i++) {
45 if (i > 0) {
46 tmp.append(" ");
47 }
48 tmp.append(strs[i]);
49 }
50 return strndup(tmp.c_str(), tmp.size());
51}
Jason Samsf313dc32013-07-09 14:29:39 -070052
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -070053#ifndef RS_COMPATIBILITY_LIB
54bool rsuExecuteCommand(const char *exe, int nArgs, const char * const *args) {
55 std::unique_ptr<const char> joined(rsuJoinStrings(nArgs, args));
56
57 pid_t pid = fork();
58
59 switch (pid) {
60 case -1: { // Error occurred (we attempt no recovery)
61 ALOGE("Fork of \"%s\" failed with error %s", exe, strerror(errno));
62 return false;
63 }
64 case 0: { // Child process
65 ALOGV("Invoking %s with args '%s'", exe, joined.get());
Pirama Arumuga Nainar90ea8d32015-03-27 18:24:43 -070066
67 // ProcessManager in libcore can reap unclaimed SIGCHLDs in its process
68 // group. To ensure that the exit signal is not caught by
69 // ProcessManager and instead sent to libRS, set the child's PGID to its
70 // PID.
71 setpgid(0, 0);
72
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -070073 execv(exe, (char * const *)args);
74
75 ALOGE("execv() failed: %s", strerror(errno));
76 abort();
77 return false;
78 }
79 default: { // Parent process (actual driver)
80 // Wait on child process to finish execution.
81 int status = 0;
82 pid_t w = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
83 if (w == -1) {
84 ALOGE("Waitpid of \"%s\" failed with error %s", exe,
85 strerror(errno));
86 return false;
87 }
88
89 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
90 return true;
91 }
92
93 ALOGE("Child process \"%s\" terminated with status %d", exe, status);
94 return false;
95 }
96 }
97}
98#endif // RS_COMPATIBILITY_LIB
99
Jason Samsf313dc32013-07-09 14:29:39 -0700100}
101}