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> |
| 21 | |
Elliott Hughes | a567cfa | 2015-01-28 12:15:15 -0800 | [diff] [blame] | 22 | #include <string.h> |
| 23 | |
Pirama Arumuga Nainar | 2fa8a23 | 2015-03-25 17:21:40 -0700 | [diff] [blame] | 24 | #ifndef RS_COMPATIBILITY_LIB |
| 25 | #include <sys/wait.h> |
| 26 | #endif |
| 27 | |
Jason Sams | f313dc3 | 2013-07-09 14:29:39 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace renderscript { |
| 30 | |
| 31 | const char * rsuCopyString(const char *name) { |
| 32 | return rsuCopyString(name, strlen(name)); |
| 33 | } |
| 34 | |
| 35 | const 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 Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 42 | const 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 Sams | f313dc3 | 2013-07-09 14:29:39 -0700 | [diff] [blame] | 52 | |
Pirama Arumuga Nainar | 2fa8a23 | 2015-03-25 17:21:40 -0700 | [diff] [blame] | 53 | #ifndef RS_COMPATIBILITY_LIB |
| 54 | bool 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()); |
| 66 | execv(exe, (char * const *)args); |
| 67 | |
| 68 | ALOGE("execv() failed: %s", strerror(errno)); |
| 69 | abort(); |
| 70 | return false; |
| 71 | } |
| 72 | default: { // Parent process (actual driver) |
| 73 | // Wait on child process to finish execution. |
| 74 | int status = 0; |
| 75 | pid_t w = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 76 | if (w == -1) { |
| 77 | ALOGE("Waitpid of \"%s\" failed with error %s", exe, |
| 78 | strerror(errno)); |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | ALOGE("Child process \"%s\" terminated with status %d", exe, status); |
| 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | #endif // RS_COMPATIBILITY_LIB |
| 92 | |
Jason Sams | f313dc3 | 2013-07-09 14:29:39 -0700 | [diff] [blame] | 93 | } |
| 94 | } |