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