blob: 667e5bbfcf29c4ca5ccde917f09fe71ed5f80af1 [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
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>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/capability.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
27#include <linux/prctl.h>
28
29#include "private/android_filesystem_config.h"
30#include "CommandListener.h"
31#include "LogBuffer.h"
32#include "LogListener.h"
33
34static int drop_privs() {
35 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
36 return -1;
37 }
38
39 if (setgid(AID_LOGD) != 0) {
40 return -1;
41 }
42
43 if (setuid(AID_LOGD) != 0) {
44 return -1;
45 }
46
47 struct __user_cap_header_struct capheader;
48 struct __user_cap_data_struct capdata[2];
49 memset(&capheader, 0, sizeof(capheader));
50 memset(&capdata, 0, sizeof(capdata));
51 capheader.version = _LINUX_CAPABILITY_VERSION_3;
52 capheader.pid = 0;
53
54 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
55 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
56 capdata[0].inheritable = 0;
57 capdata[1].inheritable = 0;
58
59 if (capset(&capheader, &capdata[0]) < 0) {
60 return -1;
61 }
62
63 return 0;
64}
65
66// Foreground waits for exit of the three main persistent threads that
67// are started here. The three threads are created to manage UNIX
68// domain client sockets for writing, reading and controlling the user
69// space logger. Additional transitory per-client threads are created
70// for each reader once they register.
71int main() {
72 if (drop_privs() != 0) {
73 return -1;
74 }
75
76 // Serves the purpose of managing the last logs times read on a
77 // socket connection, and as a reader lock on a range of log
78 // entries.
79
80 LastLogTimes *times = new LastLogTimes();
81
82 // LogBuffer is the object which is responsible for holding all
83 // log entries.
84
85 LogBuffer *logBuf = new LogBuffer(times);
86
87 // LogReader listens on /dev/socket/logdr. When a client
88 // connects, log entries in the LogBuffer are written to the client.
89
90 LogReader *reader = new LogReader(logBuf);
91 if (reader->startListener()) {
92 exit(1);
93 }
94
95 // LogListener listens on /dev/socket/logdw for client
96 // initiated log messages. New log entries are added to LogBuffer
97 // and LogReader is notified to send updates to connected clients.
98
99 LogListener *swl = new LogListener(logBuf, reader);
100 if (swl->startListener()) {
101 exit(1);
102 }
103
104 // Command listener listens on /dev/socket/logd for incoming logd
105 // administrative commands.
106
107 CommandListener *cl = new CommandListener(logBuf, reader, swl);
108 if (cl->startListener()) {
109 exit(1);
110 }
111
112 pause();
113 exit(0);
114}
115