rsCppUtils.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include "rsUtils.h"
  17. #include "rsCppUtils.h"
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <string>
  22. #ifndef RS_COMPATIBILITY_LIB
  23. #include <sys/wait.h>
  24. #endif
  25. namespace android {
  26. namespace renderscript {
  27. const char * rsuCopyString(const char *name) {
  28. return rsuCopyString(name, strlen(name));
  29. }
  30. const char * rsuCopyString(const char *name, size_t len) {
  31. char *n = new char[len+1];
  32. memcpy(n, name, len);
  33. n[len] = 0;
  34. return n;
  35. }
  36. const char* rsuJoinStrings(int n, const char* const* strs) {
  37. std::string tmp;
  38. for (int i = 0; i < n; i++) {
  39. if (i > 0) {
  40. tmp.append(" ");
  41. }
  42. tmp.append(strs[i]);
  43. }
  44. return strndup(tmp.c_str(), tmp.size());
  45. }
  46. #ifndef RS_COMPATIBILITY_LIB
  47. bool rsuExecuteCommand(const char *exe, int nArgs, const char * const *args) {
  48. std::unique_ptr<const char> joined(rsuJoinStrings(nArgs, args));
  49. ALOGV("Invoking %s with args '%s'", exe, joined.get());
  50. pid_t pid = fork();
  51. switch (pid) {
  52. case -1: { // Error occurred (we attempt no recovery)
  53. ALOGE("Fork of \"%s\" failed with error %s", exe, strerror(errno));
  54. return false;
  55. }
  56. case 0: { // Child process
  57. // No (direct or indirect) call to malloc between fork and exec. It is
  58. // possible that a different thread holds the heap lock before the fork.
  59. // ProcessManager in libcore can reap unclaimed SIGCHLDs in its process
  60. // group. To ensure that the exit signal is not caught by
  61. // ProcessManager and instead sent to libRS, set the child's PGID to its
  62. // PID.
  63. setpgid(0, 0);
  64. execv(exe, (char * const *)args);
  65. ALOGE("execv() failed: %s", strerror(errno));
  66. abort();
  67. return false;
  68. }
  69. default: { // Parent process (actual driver)
  70. // Wait on child process to finish execution.
  71. int status = 0;
  72. pid_t w = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
  73. if (w == -1) {
  74. ALOGE("Waitpid of \"%s\" failed with error %s", exe,
  75. strerror(errno));
  76. return false;
  77. }
  78. if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
  79. return true;
  80. }
  81. ALOGE("Child process \"%s\" terminated with status %d", exe, status);
  82. return false;
  83. }
  84. }
  85. }
  86. #endif // RS_COMPATIBILITY_LIB
  87. // Implementation of property_get from libcutils
  88. int property_get(const char *key, char *value, const char *default_value) {
  89. int len;
  90. len = __system_property_get(key, value);
  91. if (len > 0) {
  92. return len;
  93. }
  94. if (default_value) {
  95. len = strlen(default_value);
  96. memcpy(value, default_value, len + 1);
  97. }
  98. return len;
  99. }
  100. } // namespace renderscript
  101. } // namespace android