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 | |
| 30 | // from LOCAL_C_INCLUDES |
| 31 | #include "AudioFlinger.h" |
| 32 | #include "AudioPolicyService.h" |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame^] | 33 | #include "MediaLogService.h" |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 34 | #include "RadioService.h" |
| 35 | #include "SoundTriggerHwService.h" |
| 36 | |
| 37 | using namespace android; |
| 38 | |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame^] | 39 | int main(int argc __unused, char **argv) |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 40 | { |
| 41 | signal(SIGPIPE, SIG_IGN); |
| 42 | |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame^] | 43 | bool doLog = (bool) property_get_bool("ro.test_harness", 0); |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 44 | |
Glenn Kasten | ae0cff1 | 2016-02-24 13:57:49 -0800 | [diff] [blame^] | 45 | pid_t childPid; |
| 46 | // FIXME The advantage of making the process containing media.log service the parent process of |
| 47 | // the process that contains the other audio services, is that it allows us to collect more |
| 48 | // detailed information such as signal numbers, stop and continue, resource usage, etc. |
| 49 | // But it is also more complex. Consider replacing this by independent processes, and using |
| 50 | // binder on death notification instead. |
| 51 | if (doLog && (childPid = fork()) != 0) { |
| 52 | // media.log service |
| 53 | //prctl(PR_SET_NAME, (unsigned long) "media.log", 0, 0, 0); |
| 54 | // unfortunately ps ignores PR_SET_NAME for the main thread, so use this ugly hack |
| 55 | strcpy(argv[0], "media.log"); |
| 56 | sp<ProcessState> proc(ProcessState::self()); |
| 57 | MediaLogService::instantiate(); |
| 58 | ProcessState::self()->startThreadPool(); |
| 59 | for (;;) { |
| 60 | siginfo_t info; |
| 61 | int ret = waitid(P_PID, childPid, &info, WEXITED | WSTOPPED | WCONTINUED); |
| 62 | if (ret == EINTR) { |
| 63 | continue; |
| 64 | } |
| 65 | if (ret < 0) { |
| 66 | break; |
| 67 | } |
| 68 | char buffer[32]; |
| 69 | const char *code; |
| 70 | switch (info.si_code) { |
| 71 | case CLD_EXITED: |
| 72 | code = "CLD_EXITED"; |
| 73 | break; |
| 74 | case CLD_KILLED: |
| 75 | code = "CLD_KILLED"; |
| 76 | break; |
| 77 | case CLD_DUMPED: |
| 78 | code = "CLD_DUMPED"; |
| 79 | break; |
| 80 | case CLD_STOPPED: |
| 81 | code = "CLD_STOPPED"; |
| 82 | break; |
| 83 | case CLD_TRAPPED: |
| 84 | code = "CLD_TRAPPED"; |
| 85 | break; |
| 86 | case CLD_CONTINUED: |
| 87 | code = "CLD_CONTINUED"; |
| 88 | break; |
| 89 | default: |
| 90 | snprintf(buffer, sizeof(buffer), "unknown (%d)", info.si_code); |
| 91 | code = buffer; |
| 92 | break; |
| 93 | } |
| 94 | struct rusage usage; |
| 95 | getrusage(RUSAGE_CHILDREN, &usage); |
| 96 | ALOG(LOG_ERROR, "media.log", "pid %d status %d code %s user %ld.%03lds sys %ld.%03lds", |
| 97 | info.si_pid, info.si_status, code, |
| 98 | usage.ru_utime.tv_sec, usage.ru_utime.tv_usec / 1000, |
| 99 | usage.ru_stime.tv_sec, usage.ru_stime.tv_usec / 1000); |
| 100 | sp<IServiceManager> sm = defaultServiceManager(); |
| 101 | sp<IBinder> binder = sm->getService(String16("media.log")); |
| 102 | if (binder != 0) { |
| 103 | Vector<String16> args; |
| 104 | binder->dump(-1, args); |
| 105 | } |
| 106 | switch (info.si_code) { |
| 107 | case CLD_EXITED: |
| 108 | case CLD_KILLED: |
| 109 | case CLD_DUMPED: { |
| 110 | ALOG(LOG_INFO, "media.log", "exiting"); |
| 111 | _exit(0); |
| 112 | // not reached |
| 113 | } |
| 114 | default: |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | } else { |
| 119 | // all other services |
| 120 | if (doLog) { |
| 121 | prctl(PR_SET_PDEATHSIG, SIGKILL); // if parent media.log dies before me, kill me also |
| 122 | setpgid(0, 0); // but if I die first, don't kill my parent |
| 123 | } |
| 124 | sp<ProcessState> proc(ProcessState::self()); |
| 125 | sp<IServiceManager> sm = defaultServiceManager(); |
| 126 | ALOGI("ServiceManager: %p", sm.get()); |
| 127 | AudioFlinger::instantiate(); |
| 128 | AudioPolicyService::instantiate(); |
| 129 | RadioService::instantiate(); |
| 130 | SoundTriggerHwService::instantiate(); |
| 131 | ProcessState::self()->startThreadPool(); |
| 132 | IPCThreadState::self()->joinThreadPool(); |
| 133 | } |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 134 | } |