blob: 5a9d1bb2993cdf75f37b85a672a2157039cc1d7e [file] [log] [blame]
Marco Nelissendcb346b2015-09-09 10:47:29 -07001/*
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 Kastenae0cff12016-02-24 13:57:49 -080020#include <fcntl.h>
21#include <sys/prctl.h>
22#include <sys/wait.h>
23#include <cutils/properties.h>
24
Marco Nelissendcb346b2015-09-09 10:47:29 -070025#include <binder/IPCThreadState.h>
26#include <binder/ProcessState.h>
27#include <binder/IServiceManager.h>
Mikhail Naganov00a21a32017-11-16 08:57:44 -080028#include <hidl/HidlTransportSupport.h>
Marco Nelissendcb346b2015-09-09 10:47:29 -070029#include <utils/Log.h>
30
31// from LOCAL_C_INCLUDES
Phil Burk5b38eb32017-09-29 15:06:40 -070032#include "aaudio/AAudioTesting.h"
Marco Nelissendcb346b2015-09-09 10:47:29 -070033#include "AudioFlinger.h"
34#include "AudioPolicyService.h"
Phil Burk7f6b40d2017-02-09 13:18:38 -080035#include "AAudioService.h"
Phil Burk5b38eb32017-09-29 15:06:40 -070036#include "utility/AAudioUtilities.h"
Glenn Kastenae0cff12016-02-24 13:57:49 -080037#include "MediaLogService.h"
Marco Nelissendcb346b2015-09-09 10:47:29 -070038#include "SoundTriggerHwService.h"
39
40using namespace android;
41
Glenn Kastenae0cff12016-02-24 13:57:49 -080042int main(int argc __unused, char **argv)
Marco Nelissendcb346b2015-09-09 10:47:29 -070043{
44 signal(SIGPIPE, SIG_IGN);
45
Glenn Kastenae0cff12016-02-24 13:57:49 -080046 bool doLog = (bool) property_get_bool("ro.test_harness", 0);
Marco Nelissendcb346b2015-09-09 10:47:29 -070047
Glenn Kastenae0cff12016-02-24 13:57:49 -080048 pid_t childPid;
49 // FIXME The advantage of making the process containing media.log service the parent process of
50 // the process that contains the other audio services, is that it allows us to collect more
51 // detailed information such as signal numbers, stop and continue, resource usage, etc.
52 // But it is also more complex. Consider replacing this by independent processes, and using
53 // binder on death notification instead.
54 if (doLog && (childPid = fork()) != 0) {
55 // media.log service
56 //prctl(PR_SET_NAME, (unsigned long) "media.log", 0, 0, 0);
57 // unfortunately ps ignores PR_SET_NAME for the main thread, so use this ugly hack
58 strcpy(argv[0], "media.log");
59 sp<ProcessState> proc(ProcessState::self());
60 MediaLogService::instantiate();
61 ProcessState::self()->startThreadPool();
Eric Laurent9c0b3a32016-03-24 12:08:27 -070062 IPCThreadState::self()->joinThreadPool();
Glenn Kastenae0cff12016-02-24 13:57:49 -080063 for (;;) {
64 siginfo_t info;
65 int ret = waitid(P_PID, childPid, &info, WEXITED | WSTOPPED | WCONTINUED);
66 if (ret == EINTR) {
67 continue;
68 }
69 if (ret < 0) {
70 break;
71 }
72 char buffer[32];
73 const char *code;
74 switch (info.si_code) {
75 case CLD_EXITED:
76 code = "CLD_EXITED";
77 break;
78 case CLD_KILLED:
79 code = "CLD_KILLED";
80 break;
81 case CLD_DUMPED:
82 code = "CLD_DUMPED";
83 break;
84 case CLD_STOPPED:
85 code = "CLD_STOPPED";
86 break;
87 case CLD_TRAPPED:
88 code = "CLD_TRAPPED";
89 break;
90 case CLD_CONTINUED:
91 code = "CLD_CONTINUED";
92 break;
93 default:
94 snprintf(buffer, sizeof(buffer), "unknown (%d)", info.si_code);
95 code = buffer;
96 break;
97 }
98 struct rusage usage;
99 getrusage(RUSAGE_CHILDREN, &usage);
100 ALOG(LOG_ERROR, "media.log", "pid %d status %d code %s user %ld.%03lds sys %ld.%03lds",
101 info.si_pid, info.si_status, code,
102 usage.ru_utime.tv_sec, usage.ru_utime.tv_usec / 1000,
103 usage.ru_stime.tv_sec, usage.ru_stime.tv_usec / 1000);
104 sp<IServiceManager> sm = defaultServiceManager();
105 sp<IBinder> binder = sm->getService(String16("media.log"));
106 if (binder != 0) {
107 Vector<String16> args;
108 binder->dump(-1, args);
109 }
110 switch (info.si_code) {
111 case CLD_EXITED:
112 case CLD_KILLED:
113 case CLD_DUMPED: {
114 ALOG(LOG_INFO, "media.log", "exiting");
115 _exit(0);
116 // not reached
117 }
118 default:
119 break;
120 }
121 }
122 } else {
123 // all other services
124 if (doLog) {
125 prctl(PR_SET_PDEATHSIG, SIGKILL); // if parent media.log dies before me, kill me also
126 setpgid(0, 0); // but if I die first, don't kill my parent
127 }
Mikhail Naganov00a21a32017-11-16 08:57:44 -0800128 android::hardware::configureRpcThreadpool(4, false /*callerWillJoin*/);
Glenn Kastenae0cff12016-02-24 13:57:49 -0800129 sp<ProcessState> proc(ProcessState::self());
130 sp<IServiceManager> sm = defaultServiceManager();
131 ALOGI("ServiceManager: %p", sm.get());
132 AudioFlinger::instantiate();
133 AudioPolicyService::instantiate();
Phil Burk5b38eb32017-09-29 15:06:40 -0700134
135 // AAudioService should only be used in OC-MR1 and later.
136 // And only enable the AAudioService if the system MMAP policy explicitly allows it.
137 // This prevents a client from misusing AAudioService when it is not supported.
138 aaudio_policy_t mmapPolicy = property_get_int32(AAUDIO_PROP_MMAP_POLICY,
139 AAUDIO_POLICY_NEVER);
140 if (mmapPolicy == AAUDIO_POLICY_AUTO || mmapPolicy == AAUDIO_POLICY_ALWAYS) {
141 AAudioService::instantiate();
142 }
143
Glenn Kastenae0cff12016-02-24 13:57:49 -0800144 SoundTriggerHwService::instantiate();
145 ProcessState::self()->startThreadPool();
146 IPCThreadState::self()->joinThreadPool();
147 }
Marco Nelissendcb346b2015-09-09 10:47:29 -0700148}