blob: 333f3a17a8df651291fbb1bca44dbfab93880b29 [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>
Miao Wang2a611682017-02-27 17:36:40 -080021#include <unistd.h>
Yang Ni2abfcc62015-02-17 16:05:19 -080022
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -070023#ifndef RS_COMPATIBILITY_LIB
24#include <sys/wait.h>
25#endif
26
Mark Salyzynf5ffbf22016-10-17 09:42:09 -070027
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));
Pirama Arumuga Nainar7b29f5a2015-04-13 12:05:21 -070056 ALOGV("Invoking %s with args '%s'", exe, joined.get());
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -070057
58 pid_t pid = fork();
59
60 switch (pid) {
61 case -1: { // Error occurred (we attempt no recovery)
62 ALOGE("Fork of \"%s\" failed with error %s", exe, strerror(errno));
63 return false;
64 }
65 case 0: { // Child process
Pirama Arumuga Nainar7b29f5a2015-04-13 12:05:21 -070066 // No (direct or indirect) call to malloc between fork and exec. It is
67 // possible that a different thread holds the heap lock before the fork.
Pirama Arumuga Nainar90ea8d32015-03-27 18:24:43 -070068
69 // ProcessManager in libcore can reap unclaimed SIGCHLDs in its process
70 // group. To ensure that the exit signal is not caught by
71 // ProcessManager and instead sent to libRS, set the child's PGID to its
72 // PID.
73 setpgid(0, 0);
74
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -070075 execv(exe, (char * const *)args);
76
77 ALOGE("execv() failed: %s", strerror(errno));
78 abort();
79 return false;
80 }
81 default: { // Parent process (actual driver)
82 // Wait on child process to finish execution.
83 int status = 0;
84 pid_t w = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
85 if (w == -1) {
86 ALOGE("Waitpid of \"%s\" failed with error %s", exe,
87 strerror(errno));
88 return false;
89 }
90
91 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
92 return true;
93 }
94
95 ALOGE("Child process \"%s\" terminated with status %d", exe, status);
96 return false;
97 }
98 }
99}
100#endif // RS_COMPATIBILITY_LIB
101
Miao Wang2a611682017-02-27 17:36:40 -0800102// Implementation of property_get from libcutils
103int property_get(const char *key, char *value, const char *default_value) {
104 int len;
105 len = __system_property_get(key, value);
106 if (len > 0) {
107 return len;
108 }
109
110 if (default_value) {
111 len = strlen(default_value);
112 memcpy(value, default_value, len + 1);
113 }
114 return len;
115}
116
Rahul Chaudhry7974fc02017-02-09 12:33:28 -0800117} // namespace renderscript
118} // namespace android