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