blob: 8cc950803fb213e717496145bf28c22e5142a892 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
Glenn Kasten33df5292011-12-19 14:09:14 -080018#define LOG_TAG "mediaserver"
Glenn Kastenb4d30742012-03-13 14:46:23 -070019//#define LOG_NDEBUG 0
Glenn Kasten33df5292011-12-19 14:09:14 -080020
Glenn Kasten6f1c1912013-01-18 15:31:41 -080021#include <fcntl.h>
22#include <sys/prctl.h>
23#include <sys/wait.h>
Mathias Agopian75624082009-05-19 19:08:10 -070024#include <binder/IPCThreadState.h>
25#include <binder/ProcessState.h>
26#include <binder/IServiceManager.h>
Glenn Kasten6f1c1912013-01-18 15:31:41 -080027#include <cutils/properties.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080028#include <utils/Log.h>
Glenn Kastenfeb21792013-02-20 16:47:28 -080029#include "RegisterExtensions.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080030
Glenn Kastenb4d30742012-03-13 14:46:23 -070031// from LOCAL_C_INCLUDES
32#include "AudioFlinger.h"
33#include "CameraService.h"
Narayan Kamath2f340652015-06-09 17:42:34 +010034#include "IcuUtils.h"
Glenn Kasten6f1c1912013-01-18 15:31:41 -080035#include "MediaLogService.h"
Glenn Kastenb4d30742012-03-13 14:46:23 -070036#include "MediaPlayerService.h"
Ronghua Wu67e7f542015-03-13 10:47:08 -070037#include "ResourceManagerService.h"
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080038#include "service/AudioPolicyService.h"
Marco Nelissen6c1ad0d2016-05-13 10:39:23 -070039#include "MediaUtils.h"
Eric Laurentb7a11d82014-04-18 17:40:41 -070040#include "SoundTriggerHwService.h"
Eric Laurent4e090692015-03-05 15:12:40 -080041#include "RadioService.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080042
43using namespace android;
44
Glenn Kasten0f11b512014-01-31 16:18:54 -080045int main(int argc __unused, char** argv)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080046{
Marco Nelissen6c1ad0d2016-05-13 10:39:23 -070047 limitProcessMemory(
48 "ro.media.maxmem", /* property that defines limit */
49 SIZE_MAX, /* upper limit in bytes */
50 65 /* upper limit as percentage of physical RAM */);
51
Glenn Kasten879d5032012-10-17 12:16:50 -070052 signal(SIGPIPE, SIG_IGN);
Glenn Kasten6f1c1912013-01-18 15:31:41 -080053 char value[PROPERTY_VALUE_MAX];
54 bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1);
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 all the other real 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 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 }
Neil Fuller4b60b062015-06-10 15:12:38 +0100134 InitializeIcuOrDie();
Glenn Kasten6f1c1912013-01-18 15:31:41 -0800135 sp<ProcessState> proc(ProcessState::self());
136 sp<IServiceManager> sm = defaultServiceManager();
137 ALOGI("ServiceManager: %p", sm.get());
138 AudioFlinger::instantiate();
139 MediaPlayerService::instantiate();
Ronghua Wu67e7f542015-03-13 10:47:08 -0700140 ResourceManagerService::instantiate();
Glenn Kasten6f1c1912013-01-18 15:31:41 -0800141 CameraService::instantiate();
142 AudioPolicyService::instantiate();
Eric Laurentb7a11d82014-04-18 17:40:41 -0700143 SoundTriggerHwService::instantiate();
Eric Laurent4e090692015-03-05 15:12:40 -0800144 RadioService::instantiate();
Glenn Kastenfeb21792013-02-20 16:47:28 -0800145 registerExtensions();
Glenn Kasten6f1c1912013-01-18 15:31:41 -0800146 ProcessState::self()->startThreadPool();
147 IPCThreadState::self()->joinThreadPool();
148 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800149}