Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | #define LOG_TAG "audioserver" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <sys/prctl.h> |
| 22 | #include <sys/wait.h> |
| 23 | #include <cutils/properties.h> |
| 24 | |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 25 | #include <binder/IPCThreadState.h> |
| 26 | #include <binder/ProcessState.h> |
| 27 | #include <binder/IServiceManager.h> |
| 28 | #include <utils/Log.h> |
| 29 | |
Eric Laurent | e2afa1e | 2016-12-16 16:30:28 -0800 | [diff] [blame] | 30 | // FIXME: remove when BUG 31748996 is fixed |
| 31 | #include <hwbinder/IPCThreadState.h> |
| 32 | #include <hwbinder/ProcessState.h> |
| 33 | |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 34 | // from LOCAL_C_INCLUDES |
Phil Burk | 5b38eb3 | 2017-09-29 15:06:40 -0700 | [diff] [blame^] | 35 | #include "aaudio/AAudioTesting.h" |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 36 | #include "AudioFlinger.h" |
| 37 | #include "AudioPolicyService.h" |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 38 | #include "AAudioService.h" |
Phil Burk | 5b38eb3 | 2017-09-29 15:06:40 -0700 | [diff] [blame^] | 39 | #include "utility/AAudioUtilities.h" |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 40 | #include "MediaLogService.h" |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 41 | #include "SoundTriggerHwService.h" |
| 42 | |
| 43 | using namespace android; |
| 44 | |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 45 | int main(int argc __unused, char **argv) |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 46 | { |
| 47 | signal(SIGPIPE, SIG_IGN); |
| 48 | |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 49 | bool doLog = (bool) property_get_bool("ro.test_harness", 0); |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 50 | |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 51 | pid_t childPid; |
| 52 | // FIXME The advantage of making the process containing media.log service the parent process of |
| 53 | // the process that contains the other audio services, is that it allows us to collect more |
| 54 | // detailed information such as signal numbers, stop and continue, resource usage, etc. |
| 55 | // But it is also more complex. Consider replacing this by independent processes, and using |
| 56 | // binder on death notification instead. |
| 57 | if (doLog && (childPid = fork()) != 0) { |
| 58 | // media.log service |
| 59 | //prctl(PR_SET_NAME, (unsigned long) "media.log", 0, 0, 0); |
| 60 | // unfortunately ps ignores PR_SET_NAME for the main thread, so use this ugly hack |
| 61 | strcpy(argv[0], "media.log"); |
| 62 | sp<ProcessState> proc(ProcessState::self()); |
| 63 | MediaLogService::instantiate(); |
| 64 | ProcessState::self()->startThreadPool(); |
Eric Laurent | 9c0b3a3 | 2016-03-24 12:08:27 -0700 | [diff] [blame] | 65 | IPCThreadState::self()->joinThreadPool(); |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 66 | for (;;) { |
| 67 | siginfo_t info; |
| 68 | int ret = waitid(P_PID, childPid, &info, WEXITED | WSTOPPED | WCONTINUED); |
| 69 | if (ret == EINTR) { |
| 70 | continue; |
| 71 | } |
| 72 | if (ret < 0) { |
| 73 | break; |
| 74 | } |
| 75 | char buffer[32]; |
| 76 | const char *code; |
| 77 | switch (info.si_code) { |
| 78 | case CLD_EXITED: |
| 79 | code = "CLD_EXITED"; |
| 80 | break; |
| 81 | case CLD_KILLED: |
| 82 | code = "CLD_KILLED"; |
| 83 | break; |
| 84 | case CLD_DUMPED: |
| 85 | code = "CLD_DUMPED"; |
| 86 | break; |
| 87 | case CLD_STOPPED: |
| 88 | code = "CLD_STOPPED"; |
| 89 | break; |
| 90 | case CLD_TRAPPED: |
| 91 | code = "CLD_TRAPPED"; |
| 92 | break; |
| 93 | case CLD_CONTINUED: |
| 94 | code = "CLD_CONTINUED"; |
| 95 | break; |
| 96 | default: |
| 97 | snprintf(buffer, sizeof(buffer), "unknown (%d)", info.si_code); |
| 98 | code = buffer; |
| 99 | break; |
| 100 | } |
| 101 | struct rusage usage; |
| 102 | getrusage(RUSAGE_CHILDREN, &usage); |
| 103 | ALOG(LOG_ERROR, "media.log", "pid %d status %d code %s user %ld.%03lds sys %ld.%03lds", |
| 104 | info.si_pid, info.si_status, code, |
| 105 | usage.ru_utime.tv_sec, usage.ru_utime.tv_usec / 1000, |
| 106 | usage.ru_stime.tv_sec, usage.ru_stime.tv_usec / 1000); |
| 107 | sp<IServiceManager> sm = defaultServiceManager(); |
| 108 | sp<IBinder> binder = sm->getService(String16("media.log")); |
| 109 | if (binder != 0) { |
| 110 | Vector<String16> args; |
| 111 | binder->dump(-1, args); |
| 112 | } |
| 113 | switch (info.si_code) { |
| 114 | case CLD_EXITED: |
| 115 | case CLD_KILLED: |
| 116 | case CLD_DUMPED: { |
| 117 | ALOG(LOG_INFO, "media.log", "exiting"); |
| 118 | _exit(0); |
| 119 | // not reached |
| 120 | } |
| 121 | default: |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | } else { |
| 126 | // all other services |
| 127 | if (doLog) { |
| 128 | prctl(PR_SET_PDEATHSIG, SIGKILL); // if parent media.log dies before me, kill me also |
| 129 | setpgid(0, 0); // but if I die first, don't kill my parent |
| 130 | } |
| 131 | sp<ProcessState> proc(ProcessState::self()); |
| 132 | sp<IServiceManager> sm = defaultServiceManager(); |
| 133 | ALOGI("ServiceManager: %p", sm.get()); |
| 134 | AudioFlinger::instantiate(); |
| 135 | AudioPolicyService::instantiate(); |
Phil Burk | 5b38eb3 | 2017-09-29 15:06:40 -0700 | [diff] [blame^] | 136 | |
| 137 | // AAudioService should only be used in OC-MR1 and later. |
| 138 | // And only enable the AAudioService if the system MMAP policy explicitly allows it. |
| 139 | // This prevents a client from misusing AAudioService when it is not supported. |
| 140 | aaudio_policy_t mmapPolicy = property_get_int32(AAUDIO_PROP_MMAP_POLICY, |
| 141 | AAUDIO_POLICY_NEVER); |
| 142 | if (mmapPolicy == AAUDIO_POLICY_AUTO || mmapPolicy == AAUDIO_POLICY_ALWAYS) { |
| 143 | AAudioService::instantiate(); |
| 144 | } |
| 145 | |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 146 | SoundTriggerHwService::instantiate(); |
| 147 | ProcessState::self()->startThreadPool(); |
Eric Laurent | e2afa1e | 2016-12-16 16:30:28 -0800 | [diff] [blame] | 148 | |
| 149 | // FIXME: remove when BUG 31748996 is fixed |
| 150 | android::hardware::ProcessState::self()->startThreadPool(); |
| 151 | |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame] | 152 | IPCThreadState::self()->joinThreadPool(); |
| 153 | } |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 154 | } |