Joe Onorato 1754d744a7 First checkin of incident reporting.
There are a few major pieces here:

incidentd
---------
This daemon (started by init) runs and accepts incoming requests to take
incident reports.  When prompted, it calls into various system services
and fills in an IncidentProto data structure, and then writes the report
into dropbox.

The next steps for incidentd:
    - Security review of SELinux policies. These will be a subset of
      the dumpstate permissions.  Until this is done, incidentd is
      not started at boot time.

incident
--------
This shell command calls into incidentd, and can initiate an incident
report and either capture the output or leave for dropbox.

incident_report
---------------
This host side tool can call adb shell with the correct parameters
and also format the incident report as text.  This formatting code
was left of the device on purpose.  Right now it's pretty small, but
as the number of fields increases, the metadata and code to do the
formatting will start to grow.

The incident_report command also contains a workaround to let it
work before incidentd is turned on by default.  Right now, it is
implemented to call adb shell dumpsys <service> --proto directly,
whereas in the future it will go through the full incidentd flow.

incident_section_gen
--------------------
A build-time tool that generates a stripped down set of information
about the fields that are available.

libincident
-----------
This library contains the code to connect to incidentd, and the
meta proto definitions that are used by the framework protos.
The basics are here now, but they are not fully fleshed out yet.
The privacy.proto file contains annotations that can go in the
proto file that we will later use to filter which fields are
uploaded, and which are used by local sources.  For example, a
device in a test lab is safe to upload much much more information
than a real user.  These will share the same mechanism, but the
user's output will be filtered according to these annotations.

frameworks/core/proto
---------------------
These .proto files contain the definitions of the system's
output.  There is one master android.os.IncidentProto file that
is the top level of an incident report, but some other services
(notification, fingerprint, batterystats, etc) will have others
that are used directly by the logging mechanism.

Other files which are shared by several of the services also go
here, such as ComponentName, Locale, Configuration, etc.  There
will be many more.

There is also a first iplementation of a dump method handling
--proto in the fingerprint service.

IncidentManager
---------------
The java API to trigger an incident report.

Test: Not written yet
Change-Id: I59568b115ac7fcf73af70c946c95752bf33ae67f
2016-12-15 11:23:05 -08:00

237 lines
6.3 KiB
C++

/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "incident"
#include "incident_sections.h"
#include <android/os/BnIncidentReportStatusListener.h>
#include <android/os/IIncidentManager.h>
#include <android/os/IncidentReportArgs.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <utils/Looper.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
using namespace android;
using namespace android::base;
using namespace android::binder;
using namespace android::os;
// ================================================================================
class StatusListener : public BnIncidentReportStatusListener {
public:
StatusListener();
virtual ~StatusListener();
virtual Status onReportStarted();
virtual Status onReportSectionStatus(int32_t section, int32_t status);
virtual Status onReportServiceStatus(const String16& service, int32_t status);
virtual Status onReportFinished();
virtual Status onReportFailed();
};
StatusListener::StatusListener()
{
}
StatusListener::~StatusListener()
{
}
Status
StatusListener::onReportStarted()
{
return Status::ok();
}
Status
StatusListener::onReportSectionStatus(int32_t section, int32_t status)
{
fprintf(stderr, "section %d status %d\n", section, status);
return Status::ok();
}
Status
StatusListener::onReportServiceStatus(const String16& service, int32_t status)
{
fprintf(stderr, "service '%s' status %d\n", String8(service).string(), status);
return Status::ok();
}
Status
StatusListener::onReportFinished()
{
fprintf(stderr, "done\n");
exit(0);
return Status::ok();
}
Status
StatusListener::onReportFailed()
{
fprintf(stderr, "failed\n");
exit(1);
return Status::ok();
}
// ================================================================================
static IncidentSection const*
find_section(const char* name)
{
size_t low = 0;
size_t high = INCIDENT_SECTION_COUNT - 1;
while (low <= high) {
size_t mid = (low + high) >> 1;
IncidentSection const* section = INCIDENT_SECTIONS + mid;
int cmp = strcmp(section->name, name);
if (cmp < 0) {
low = mid + 1;
} else if (cmp > 0) {
high = mid - 1;
} else {
return section;
}
}
return NULL;
}
// ================================================================================
static void
usage(FILE* out)
{
fprintf(out, "usage: incident OPTIONS [SECTION...]\n");
fprintf(out, "\n");
fprintf(out, "Takes an incident report.\n");
fprintf(out, "\n");
fprintf(out, "OPTIONS\n");
fprintf(out, " -b (default) print the report to stdout (in proto format)\n");
fprintf(out, " -d send the report into dropbox\n");
fprintf(out, "\n");
fprintf(out, " SECTION the field numbers of the incident report fields to include\n");
fprintf(out, "\n");
}
int
main(int argc, char** argv)
{
Status status;
IncidentReportArgs args;
enum { DEST_DROPBOX, DEST_STDOUT } destination = DEST_STDOUT;
// Parse the args
int opt;
while ((opt = getopt(argc, argv, "bhd")) != -1) {
switch (opt) {
case 'b':
destination = DEST_STDOUT;
break;
case 'h':
usage(stdout);
return 0;
case 'd':
destination = DEST_DROPBOX;
break;
default:
usage(stderr);
return 1;
}
}
if (optind == argc) {
args.setAll(true);
} else {
for (int i=optind; i<argc; i++) {
const char* arg = argv[i];
char* end;
if (arg[0] != '\0') {
int section = strtol(arg, &end, 0);
if (*end == '\0') {
args.addSection(section);
} else {
IncidentSection const* ic = find_section(arg);
if (ic == NULL) {
fprintf(stderr, "Invalid section: %s\n", arg);
return 1;
}
args.addSection(ic->id);
}
}
}
}
// Start the thread pool.
sp<ProcessState> ps(ProcessState::self());
ps->startThreadPool();
ps->giveThreadPoolName();
// Look up the service
sp<IIncidentManager> service = interface_cast<IIncidentManager>(
defaultServiceManager()->getService(android::String16("incident")));
if (service == NULL) {
fprintf(stderr, "Couldn't look up the incident service\n");
return 1;
}
// Construct the stream
int fds[2];
pipe(fds);
unique_fd readEnd(fds[0]);
unique_fd writeEnd(fds[1]);
if (destination == DEST_STDOUT) {
// Call into the service
sp<StatusListener> listener(new StatusListener());
status = service->reportIncidentToStream(args, listener, writeEnd);
if (!status.isOk()) {
fprintf(stderr, "reportIncident returned \"%s\"\n", status.toString8().string());
}
// Wait for the result and print out the data they send.
//IPCThreadState::self()->joinThreadPool();
while (true) {
int amt = splice(fds[0], NULL, STDOUT_FILENO, NULL, 4096, 0);
fprintf(stderr, "spliced %d bytes\n", amt);
if (amt < 0) {
return errno;
} else if (amt == 0) {
return 0;
}
}
} else {
status = service->reportIncident(args);
if (!status.isOk()) {
fprintf(stderr, "reportIncident returned \"%s\"\n", status.toString8().string());
return 1;
} else {
return 0;
}
}
}