The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Kasten | 33df529 | 2011-12-19 14:09:14 -0800 | [diff] [blame] | 18 | #define LOG_TAG "mediaserver" |
Glenn Kasten | b4d3074 | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
Glenn Kasten | 33df529 | 2011-12-19 14:09:14 -0800 | [diff] [blame] | 20 | |
Glenn Kasten | 6f1c191 | 2013-01-18 15:31:41 -0800 | [diff] [blame^] | 21 | #include <fcntl.h> |
| 22 | #include <sys/prctl.h> |
| 23 | #include <sys/wait.h> |
Mathias Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 24 | #include <binder/IPCThreadState.h> |
| 25 | #include <binder/ProcessState.h> |
| 26 | #include <binder/IServiceManager.h> |
Glenn Kasten | 6f1c191 | 2013-01-18 15:31:41 -0800 | [diff] [blame^] | 27 | #include <cutils/properties.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include <utils/Log.h> |
| 29 | |
Glenn Kasten | b4d3074 | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 30 | // from LOCAL_C_INCLUDES |
| 31 | #include "AudioFlinger.h" |
| 32 | #include "CameraService.h" |
Glenn Kasten | 6f1c191 | 2013-01-18 15:31:41 -0800 | [diff] [blame^] | 33 | #include "MediaLogService.h" |
Glenn Kasten | b4d3074 | 2012-03-13 14:46:23 -0700 | [diff] [blame] | 34 | #include "MediaPlayerService.h" |
| 35 | #include "AudioPolicyService.h" |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
| 37 | using namespace android; |
| 38 | |
| 39 | int main(int argc, char** argv) |
| 40 | { |
Glenn Kasten | 879d503 | 2012-10-17 12:16:50 -0700 | [diff] [blame] | 41 | signal(SIGPIPE, SIG_IGN); |
Glenn Kasten | 6f1c191 | 2013-01-18 15:31:41 -0800 | [diff] [blame^] | 42 | char value[PROPERTY_VALUE_MAX]; |
| 43 | bool doLog = (property_get("ro.test_harness", value, "0") > 0) && (atoi(value) == 1); |
| 44 | pid_t childPid; |
| 45 | // FIXME The advantage of making the process containing media.log service the parent process of |
| 46 | // the process that contains all the other real services, is that it allows us to collect more |
| 47 | // detailed information such as signal numbers, stop and continue, resource usage, etc. |
| 48 | // But it is also more complex. Consider replacing this by independent processes, and using |
| 49 | // binder on death notification instead. |
| 50 | if (doLog && (childPid = fork()) != 0) { |
| 51 | // media.log service |
| 52 | //prctl(PR_SET_NAME, (unsigned long) "media.log", 0, 0, 0); |
| 53 | // unfortunately ps ignores PR_SET_NAME for the main thread, so use this ugly hack |
| 54 | strcpy(argv[0], "media.log"); |
| 55 | sp<ProcessState> proc(ProcessState::self()); |
| 56 | MediaLogService::instantiate(); |
| 57 | ProcessState::self()->startThreadPool(); |
| 58 | for (;;) { |
| 59 | siginfo_t info; |
| 60 | int ret = waitid(P_PID, childPid, &info, WEXITED | WSTOPPED | WCONTINUED); |
| 61 | if (ret == EINTR) { |
| 62 | continue; |
| 63 | } |
| 64 | if (ret < 0) { |
| 65 | break; |
| 66 | } |
| 67 | char buffer[32]; |
| 68 | const char *code; |
| 69 | switch (info.si_code) { |
| 70 | case CLD_EXITED: |
| 71 | code = "CLD_EXITED"; |
| 72 | break; |
| 73 | case CLD_KILLED: |
| 74 | code = "CLD_KILLED"; |
| 75 | break; |
| 76 | case CLD_DUMPED: |
| 77 | code = "CLD_DUMPED"; |
| 78 | break; |
| 79 | case CLD_STOPPED: |
| 80 | code = "CLD_STOPPED"; |
| 81 | break; |
| 82 | case CLD_TRAPPED: |
| 83 | code = "CLD_TRAPPED"; |
| 84 | break; |
| 85 | case CLD_CONTINUED: |
| 86 | code = "CLD_CONTINUED"; |
| 87 | break; |
| 88 | default: |
| 89 | snprintf(buffer, sizeof(buffer), "unknown (%d)", info.si_code); |
| 90 | code = buffer; |
| 91 | break; |
| 92 | } |
| 93 | struct rusage usage; |
| 94 | getrusage(RUSAGE_CHILDREN, &usage); |
| 95 | ALOG(LOG_ERROR, "media.log", "pid %d status %d code %s user %ld.%03lds sys %ld.%03lds", |
| 96 | info.si_pid, info.si_status, code, |
| 97 | usage.ru_utime.tv_sec, usage.ru_utime.tv_usec / 1000, |
| 98 | usage.ru_stime.tv_sec, usage.ru_stime.tv_usec / 1000); |
| 99 | sp<IServiceManager> sm = defaultServiceManager(); |
| 100 | sp<IBinder> binder = sm->getService(String16("media.log")); |
| 101 | if (binder != 0) { |
| 102 | Vector<String16> args; |
| 103 | binder->dump(-1, args); |
| 104 | } |
| 105 | switch (info.si_code) { |
| 106 | case CLD_EXITED: |
| 107 | case CLD_KILLED: |
| 108 | case CLD_DUMPED: { |
| 109 | ALOG(LOG_INFO, "media.log", "exiting"); |
| 110 | _exit(0); |
| 111 | // not reached |
| 112 | } |
| 113 | default: |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | } else { |
| 118 | // all other services |
| 119 | if (doLog) { |
| 120 | prctl(PR_SET_PDEATHSIG, SIGKILL); // if parent media.log dies before me, kill me also |
| 121 | setpgid(0, 0); // but if I die first, don't kill my parent |
| 122 | } |
| 123 | sp<ProcessState> proc(ProcessState::self()); |
| 124 | sp<IServiceManager> sm = defaultServiceManager(); |
| 125 | ALOGI("ServiceManager: %p", sm.get()); |
| 126 | AudioFlinger::instantiate(); |
| 127 | MediaPlayerService::instantiate(); |
| 128 | CameraService::instantiate(); |
| 129 | AudioPolicyService::instantiate(); |
| 130 | ProcessState::self()->startThreadPool(); |
| 131 | IPCThreadState::self()->joinThreadPool(); |
| 132 | } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | } |