Jason Sams | f313dc3 | 2013-07-09 14:29:39 -0700 | [diff] [blame] | 1 | /* |
| 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 Hines | 574854b | 2013-07-10 08:29:51 -0700 | [diff] [blame] | 17 | #include "rsUtils.h" |
Jason Sams | f313dc3 | 2013-07-09 14:29:39 -0700 | [diff] [blame] | 18 | #include "rsCppUtils.h" |
| 19 | |
Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 20 | #include <string> |
Miao Wang | 2a61168 | 2017-02-27 17:36:40 -0800 | [diff] [blame^] | 21 | #include <unistd.h> |
Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 22 | |
Miao Wang | 2a61168 | 2017-02-27 17:36:40 -0800 | [diff] [blame^] | 23 | #include <sys/system_properties.h> |
Elliott Hughes | a567cfa | 2015-01-28 12:15:15 -0800 | [diff] [blame] | 24 | |
Pirama Arumuga Nainar | 2fa8a23 | 2015-03-25 17:21:40 -0700 | [diff] [blame] | 25 | #ifndef RS_COMPATIBILITY_LIB |
| 26 | #include <sys/wait.h> |
| 27 | #endif |
| 28 | |
Mark Salyzyn | f5ffbf2 | 2016-10-17 09:42:09 -0700 | [diff] [blame] | 29 | |
Jason Sams | f313dc3 | 2013-07-09 14:29:39 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace renderscript { |
| 32 | |
| 33 | const char * rsuCopyString(const char *name) { |
| 34 | return rsuCopyString(name, strlen(name)); |
| 35 | } |
| 36 | |
| 37 | const char * rsuCopyString(const char *name, size_t len) { |
| 38 | char *n = new char[len+1]; |
| 39 | memcpy(n, name, len); |
| 40 | n[len] = 0; |
| 41 | return n; |
| 42 | } |
| 43 | |
Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 44 | const char* rsuJoinStrings(int n, const char* const* strs) { |
| 45 | std::string tmp; |
| 46 | for (int i = 0; i < n; i++) { |
| 47 | if (i > 0) { |
| 48 | tmp.append(" "); |
| 49 | } |
| 50 | tmp.append(strs[i]); |
| 51 | } |
| 52 | return strndup(tmp.c_str(), tmp.size()); |
| 53 | } |
Jason Sams | f313dc3 | 2013-07-09 14:29:39 -0700 | [diff] [blame] | 54 | |
Pirama Arumuga Nainar | 2fa8a23 | 2015-03-25 17:21:40 -0700 | [diff] [blame] | 55 | #ifndef RS_COMPATIBILITY_LIB |
| 56 | bool rsuExecuteCommand(const char *exe, int nArgs, const char * const *args) { |
| 57 | std::unique_ptr<const char> joined(rsuJoinStrings(nArgs, args)); |
Pirama Arumuga Nainar | 7b29f5a | 2015-04-13 12:05:21 -0700 | [diff] [blame] | 58 | ALOGV("Invoking %s with args '%s'", exe, joined.get()); |
Pirama Arumuga Nainar | 2fa8a23 | 2015-03-25 17:21:40 -0700 | [diff] [blame] | 59 | |
| 60 | pid_t pid = fork(); |
| 61 | |
| 62 | switch (pid) { |
| 63 | case -1: { // Error occurred (we attempt no recovery) |
| 64 | ALOGE("Fork of \"%s\" failed with error %s", exe, strerror(errno)); |
| 65 | return false; |
| 66 | } |
| 67 | case 0: { // Child process |
Pirama Arumuga Nainar | 7b29f5a | 2015-04-13 12:05:21 -0700 | [diff] [blame] | 68 | // No (direct or indirect) call to malloc between fork and exec. It is |
| 69 | // possible that a different thread holds the heap lock before the fork. |
Pirama Arumuga Nainar | 90ea8d3 | 2015-03-27 18:24:43 -0700 | [diff] [blame] | 70 | |
| 71 | // ProcessManager in libcore can reap unclaimed SIGCHLDs in its process |
| 72 | // group. To ensure that the exit signal is not caught by |
| 73 | // ProcessManager and instead sent to libRS, set the child's PGID to its |
| 74 | // PID. |
| 75 | setpgid(0, 0); |
| 76 | |
Pirama Arumuga Nainar | 2fa8a23 | 2015-03-25 17:21:40 -0700 | [diff] [blame] | 77 | execv(exe, (char * const *)args); |
| 78 | |
| 79 | ALOGE("execv() failed: %s", strerror(errno)); |
| 80 | abort(); |
| 81 | return false; |
| 82 | } |
| 83 | default: { // Parent process (actual driver) |
| 84 | // Wait on child process to finish execution. |
| 85 | int status = 0; |
| 86 | pid_t w = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 87 | if (w == -1) { |
| 88 | ALOGE("Waitpid of \"%s\" failed with error %s", exe, |
| 89 | strerror(errno)); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | ALOGE("Child process \"%s\" terminated with status %d", exe, status); |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | #endif // RS_COMPATIBILITY_LIB |
| 103 | |
Miao Wang | 2a61168 | 2017-02-27 17:36:40 -0800 | [diff] [blame^] | 104 | // Implementation of property_get from libcutils |
| 105 | int property_get(const char *key, char *value, const char *default_value) { |
| 106 | int len; |
| 107 | len = __system_property_get(key, value); |
| 108 | if (len > 0) { |
| 109 | return len; |
| 110 | } |
| 111 | |
| 112 | if (default_value) { |
| 113 | len = strlen(default_value); |
| 114 | memcpy(value, default_value, len + 1); |
| 115 | } |
| 116 | return len; |
| 117 | } |
| 118 | |
Rahul Chaudhry | 7974fc0 | 2017-02-09 12:33:28 -0800 | [diff] [blame] | 119 | } // namespace renderscript |
| 120 | } // namespace android |