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