Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012-2013 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 | #include <dirent.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
Mark Salyzyn | 882f856 | 2013-12-26 15:13:36 -0800 | [diff] [blame] | 20 | #include <sched.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/capability.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 27 | #include <unistd.h> |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 28 | |
| 29 | #include <linux/prctl.h> |
| 30 | |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 31 | #include <cutils/properties.h> |
| 32 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 33 | #include "private/android_filesystem_config.h" |
| 34 | #include "CommandListener.h" |
| 35 | #include "LogBuffer.h" |
| 36 | #include "LogListener.h" |
| 37 | |
| 38 | static int drop_privs() { |
Mark Salyzyn | 882f856 | 2013-12-26 15:13:36 -0800 | [diff] [blame] | 39 | struct sched_param param; |
| 40 | memset(¶m, 0, sizeof(param)); |
| 41 | |
| 42 | if (sched_setscheduler((pid_t) 0, SCHED_BATCH, ¶m) < 0) { |
| 43 | return -1; |
| 44 | } |
| 45 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 46 | if (prctl(PR_SET_KEEPCAPS, 1) < 0) { |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | if (setgid(AID_LOGD) != 0) { |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | if (setuid(AID_LOGD) != 0) { |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | struct __user_cap_header_struct capheader; |
| 59 | struct __user_cap_data_struct capdata[2]; |
| 60 | memset(&capheader, 0, sizeof(capheader)); |
| 61 | memset(&capdata, 0, sizeof(capdata)); |
| 62 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 63 | capheader.pid = 0; |
| 64 | |
| 65 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG); |
| 66 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG); |
| 67 | capdata[0].inheritable = 0; |
| 68 | capdata[1].inheritable = 0; |
| 69 | |
| 70 | if (capset(&capheader, &capdata[0]) < 0) { |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | // Foreground waits for exit of the three main persistent threads that |
| 78 | // are started here. The three threads are created to manage UNIX |
| 79 | // domain client sockets for writing, reading and controlling the user |
| 80 | // space logger. Additional transitory per-client threads are created |
| 81 | // for each reader once they register. |
| 82 | int main() { |
| 83 | if (drop_privs() != 0) { |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | // Serves the purpose of managing the last logs times read on a |
| 88 | // socket connection, and as a reader lock on a range of log |
| 89 | // entries. |
| 90 | |
| 91 | LastLogTimes *times = new LastLogTimes(); |
| 92 | |
| 93 | // LogBuffer is the object which is responsible for holding all |
| 94 | // log entries. |
| 95 | |
| 96 | LogBuffer *logBuf = new LogBuffer(times); |
| 97 | |
Mark Salyzyn | e457b74 | 2014-02-19 17:18:31 -0800 | [diff] [blame] | 98 | char dgram_qlen_statistics[PROPERTY_VALUE_MAX]; |
| 99 | property_get("logd.dgram_qlen.statistics", dgram_qlen_statistics, ""); |
| 100 | if (atol(dgram_qlen_statistics)) { |
| 101 | logBuf->enableDgramQlenStatistics(); |
| 102 | } |
| 103 | |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 104 | // LogReader listens on /dev/socket/logdr. When a client |
| 105 | // connects, log entries in the LogBuffer are written to the client. |
| 106 | |
| 107 | LogReader *reader = new LogReader(logBuf); |
| 108 | if (reader->startListener()) { |
| 109 | exit(1); |
| 110 | } |
| 111 | |
| 112 | // LogListener listens on /dev/socket/logdw for client |
| 113 | // initiated log messages. New log entries are added to LogBuffer |
| 114 | // and LogReader is notified to send updates to connected clients. |
| 115 | |
| 116 | LogListener *swl = new LogListener(logBuf, reader); |
Mark Salyzyn | 581edc1 | 2013-11-20 13:38:52 -0800 | [diff] [blame] | 117 | // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value |
| 118 | if (swl->startListener(300)) { |
Mark Salyzyn | 0175b07 | 2014-02-26 09:50:16 -0800 | [diff] [blame] | 119 | exit(1); |
| 120 | } |
| 121 | |
| 122 | // Command listener listens on /dev/socket/logd for incoming logd |
| 123 | // administrative commands. |
| 124 | |
| 125 | CommandListener *cl = new CommandListener(logBuf, reader, swl); |
| 126 | if (cl->startListener()) { |
| 127 | exit(1); |
| 128 | } |
| 129 | |
| 130 | pause(); |
| 131 | exit(0); |
| 132 | } |
| 133 | |