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