Remove liblogcat.

Parsing logs isn't an API, and even if you want to do that, popen(3)
already exists.

Bug: N/A
Test: ran tests
Change-Id: I53c40be49141483da0a844a7af47da0b38d29781
This commit is contained in:
Elliott Hughes 2018-06-15 15:16:20 -07:00
parent 8c6f7ec060
commit 61b580e15e
13 changed files with 108 additions and 1065 deletions

View file

@ -1,11 +0,0 @@
BasedOnStyle: Google
AllowShortFunctionsOnASingleLine: false
CommentPragmas: NOLINT:.*
DerivePointerAlignment: false
IndentWidth: 4
PointerAlignment: Left
TabWidth: 4
PenaltyExcessCharacter: 32
Cpp11BracedListStyle: false

View file

@ -1,5 +1,5 @@
//
// Copyright (C) 2006-2017 The Android Open Source Project
// Copyright (C) 2006 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.
@ -31,25 +31,13 @@ cc_defaults {
logtags: ["event.logtags"],
}
cc_library {
name: "liblogcat",
defaults: ["logcat_defaults"],
srcs: [
"logcat.cpp",
"getopt_long.cpp",
"logcat_system.cpp",
],
export_include_dirs: ["include"],
}
cc_binary {
name: "logcat",
defaults: ["logcat_defaults"],
shared_libs: ["liblogcat"],
srcs: [
"logcat_main.cpp",
"logcat.cpp",
],
}
@ -57,9 +45,9 @@ cc_binary {
name: "logcatd",
defaults: ["logcat_defaults"],
shared_libs: ["liblogcat"],
srcs: [
"logcatd_main.cpp",
"logcat.cpp",
],
}

View file

@ -1,401 +0,0 @@
/* $OpenBSD: getopt_long.c,v 1.26 2013/06/08 22:47:56 millert Exp $ */
/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
/*
* Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Sponsored in part by the Defense Advanced Research Projects
* Agency (DARPA) and Air Force Research Laboratory, Air Force
* Materiel Command, USAF, under agreement number F39502-99-1-0512.
*/
/*-
* Copyright (c) 2000 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Dieter Baron and Thomas Klausner.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/cdefs.h>
#include <log/getopt.h>
#define PRINT_ERROR ((context->opterr) && (*options != ':'))
#define FLAG_PERMUTE 0x01 // permute non-options to the end of argv
#define FLAG_ALLARGS 0x02 // treat non-options as args to option "-1"
// return values
#define BADCH (int)'?'
#define BADARG ((*options == ':') ? (int)':' : (int)'?')
#define INORDER (int)1
#define D_PREFIX 0
#define DD_PREFIX 1
#define W_PREFIX 2
// Compute the greatest common divisor of a and b.
static int gcd(int a, int b) {
int c = a % b;
while (c) {
a = b;
b = c;
c = a % b;
}
return b;
}
// Exchange the block from nonopt_start to nonopt_end with the block from
// nonopt_end to opt_end (keeping the same order of arguments in each block).
// Returns optind - (nonopt_end - nonopt_start) for convenience.
static int permute_args(getopt_context* context, char* const* nargv) {
// compute lengths of blocks and number and size of cycles
int nnonopts = context->nonopt_end - context->nonopt_start;
int nopts = context->optind - context->nonopt_end;
int ncycle = gcd(nnonopts, nopts);
int cyclelen = (context->optind - context->nonopt_start) / ncycle;
for (int i = 0; i < ncycle; i++) {
int cstart = context->nonopt_end + i;
int pos = cstart;
for (int j = 0; j < cyclelen; j++) {
if (pos >= context->nonopt_end) {
pos -= nnonopts;
} else {
pos += nopts;
}
char* swap = nargv[pos];
const_cast<char**>(nargv)[pos] = nargv[cstart];
const_cast<char**>(nargv)[cstart] = swap;
}
}
return context->optind - (context->nonopt_end - context->nonopt_start);
}
// parse_long_options_r --
// Parse long options in argc/argv argument vector.
// Returns -1 if short_too is set and the option does not match long_options.
static int parse_long_options_r(char* const* nargv, const char* options,
const struct option* long_options, int* idx,
bool short_too, struct getopt_context* context) {
const char* current_argv = context->place;
const char* current_dash;
switch (context->dash_prefix) {
case D_PREFIX:
current_dash = "-";
break;
case DD_PREFIX:
current_dash = "--";
break;
case W_PREFIX:
current_dash = "-W ";
break;
default:
current_dash = "";
break;
}
context->optind++;
const char* has_equal;
size_t current_argv_len;
if (!!(has_equal = strchr(current_argv, '='))) {
// argument found (--option=arg)
current_argv_len = has_equal - current_argv;
has_equal++;
} else {
current_argv_len = strlen(current_argv);
}
int match = -1;
bool exact_match = false;
bool second_partial_match = false;
for (int i = 0; long_options[i].name; i++) {
// find matching long option
if (strncmp(current_argv, long_options[i].name, current_argv_len)) {
continue;
}
if (strlen(long_options[i].name) == current_argv_len) {
// exact match
match = i;
exact_match = true;
break;
}
// If this is a known short option, don't allow
// a partial match of a single character.
if (short_too && current_argv_len == 1) continue;
if (match == -1) { // first partial match
match = i;
} else if (long_options[i].has_arg != long_options[match].has_arg ||
long_options[i].flag != long_options[match].flag ||
long_options[i].val != long_options[match].val) {
second_partial_match = true;
}
}
if (!exact_match && second_partial_match) {
// ambiguous abbreviation
if (PRINT_ERROR) {
fprintf(context->optstderr ?: stderr,
"option `%s%.*s' is ambiguous", current_dash,
(int)current_argv_len, current_argv);
}
context->optopt = 0;
return BADCH;
}
if (match != -1) { // option found
if (long_options[match].has_arg == no_argument && has_equal) {
if (PRINT_ERROR) {
fprintf(context->optstderr ?: stderr,
"option `%s%.*s' doesn't allow an argument",
current_dash, (int)current_argv_len, current_argv);
}
// XXX: GNU sets optopt to val regardless of flag
context->optopt =
long_options[match].flag ? 0 : long_options[match].val;
return BADCH;
}
if (long_options[match].has_arg == required_argument ||
long_options[match].has_arg == optional_argument) {
if (has_equal) {
context->optarg = has_equal;
} else if (long_options[match].has_arg == required_argument) {
// optional argument doesn't use next nargv
context->optarg = nargv[context->optind++];
}
}
if ((long_options[match].has_arg == required_argument) &&
!context->optarg) {
// Missing argument; leading ':' indicates no error
// should be generated.
if (PRINT_ERROR) {
fprintf(context->optstderr ?: stderr,
"option `%s%s' requires an argument", current_dash,
current_argv);
}
// XXX: GNU sets optopt to val regardless of flag
context->optopt =
long_options[match].flag ? 0 : long_options[match].val;
context->optind--;
return BADARG;
}
} else { // unknown option
if (short_too) {
context->optind--;
return -1;
}
if (PRINT_ERROR) {
fprintf(context->optstderr ?: stderr, "unrecognized option `%s%s'",
current_dash, current_argv);
}
context->optopt = 0;
return BADCH;
}
if (idx) *idx = match;
if (long_options[match].flag) {
*long_options[match].flag = long_options[match].val;
return 0;
}
return long_options[match].val;
}
// getopt_long_r --
// Parse argc/argv argument vector.
int getopt_long_r(int nargc, char* const* nargv, const char* options,
const struct option* long_options, int* idx,
struct getopt_context* context) {
if (!options) return -1;
// XXX Some GNU programs (like cvs) set optind to 0 instead of
// XXX using optreset. Work around this braindamage.
if (!context->optind) context->optind = context->optreset = 1;
// Disable GNU extensions if options string begins with a '+'.
int flags = FLAG_PERMUTE;
if (*options == '-') {
flags |= FLAG_ALLARGS;
} else if (*options == '+') {
flags &= ~FLAG_PERMUTE;
}
if (*options == '+' || *options == '-') options++;
context->optarg = nullptr;
if (context->optreset) context->nonopt_start = context->nonopt_end = -1;
start:
if (context->optreset || !*context->place) { // update scanning pointer
context->optreset = 0;
if (context->optind >= nargc) { // end of argument vector
context->place = EMSG;
if (context->nonopt_end != -1) {
// do permutation, if we have to
context->optind = permute_args(context, nargv);
} else if (context->nonopt_start != -1) {
// If we skipped non-options, set optind to the first of them.
context->optind = context->nonopt_start;
}
context->nonopt_start = context->nonopt_end = -1;
return -1;
}
if (*(context->place = nargv[context->optind]) != '-' ||
context->place[1] == '\0') {
context->place = EMSG; // found non-option
if (flags & FLAG_ALLARGS) {
// GNU extension: return non-option as argument to option 1
context->optarg = nargv[context->optind++];
return INORDER;
}
if (!(flags & FLAG_PERMUTE)) {
// If no permutation wanted, stop parsing at first non-option.
return -1;
}
// do permutation
if (context->nonopt_start == -1) {
context->nonopt_start = context->optind;
} else if (context->nonopt_end != -1) {
context->nonopt_start = permute_args(context, nargv);
context->nonopt_end = -1;
}
context->optind++;
// process next argument
goto start;
}
if (context->nonopt_start != -1 && context->nonopt_end == -1) {
context->nonopt_end = context->optind;
}
// If we have "-" do nothing, if "--" we are done.
if (context->place[1] != '\0' && *++(context->place) == '-' &&
context->place[1] == '\0') {
context->optind++;
context->place = EMSG;
// We found an option (--), so if we skipped
// non-options, we have to permute.
if (context->nonopt_end != -1) {
context->optind = permute_args(context, nargv);
}
context->nonopt_start = context->nonopt_end = -1;
return -1;
}
}
int optchar;
// Check long options if:
// 1) we were passed some
// 2) the arg is not just "-"
// 3) either the arg starts with -- we are getopt_long_only()
if (long_options && context->place != nargv[context->optind] &&
(*context->place == '-')) {
bool short_too = false;
context->dash_prefix = D_PREFIX;
if (*context->place == '-') {
context->place++; // --foo long option
context->dash_prefix = DD_PREFIX;
} else if (*context->place != ':' && strchr(options, *context->place)) {
short_too = true; // could be short option too
}
optchar = parse_long_options_r(nargv, options, long_options, idx,
short_too, context);
if (optchar != -1) {
context->place = EMSG;
return optchar;
}
}
const char* oli; // option letter list index
if ((optchar = (int)*(context->place)++) == (int)':' ||
(optchar == (int)'-' && *context->place != '\0') ||
!(oli = strchr(options, optchar))) {
// If the user specified "-" and '-' isn't listed in
// options, return -1 (non-option) as per POSIX.
// Otherwise, it is an unknown option character (or ':').
if (optchar == (int)'-' && *context->place == '\0') return -1;
if (!*context->place) context->optind++;
if (PRINT_ERROR) {
fprintf(context->optstderr ?: stderr, "invalid option -- %c",
optchar);
}
context->optopt = optchar;
return BADCH;
}
static const char recargchar[] = "option requires an argument -- %c";
if (long_options && optchar == 'W' && oli[1] == ';') {
// -W long-option
if (*context->place) { // no space
; // NOTHING
} else if (++(context->optind) >= nargc) { // no arg
context->place = EMSG;
if (PRINT_ERROR) {
fprintf(context->optstderr ?: stderr, recargchar, optchar);
}
context->optopt = optchar;
return BADARG;
} else { // white space
context->place = nargv[context->optind];
}
context->dash_prefix = W_PREFIX;
optchar = parse_long_options_r(nargv, options, long_options, idx, false,
context);
context->place = EMSG;
return optchar;
}
if (*++oli != ':') { // doesn't take argument
if (!*context->place) context->optind++;
} else { // takes (optional) argument
context->optarg = nullptr;
if (*context->place) { // no white space
context->optarg = context->place;
} else if (oli[1] != ':') { // arg not optional
if (++(context->optind) >= nargc) { // no arg
context->place = EMSG;
if (PRINT_ERROR) {
fprintf(context->optstderr ?: stderr, recargchar, optchar);
}
context->optopt = optchar;
return BADARG;
}
context->optarg = nargv[context->optind];
}
context->place = EMSG;
context->optind++;
}
// dump back option letter
return optchar;
}

View file

@ -1,67 +0,0 @@
/*
* Copyright (C) 2017 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.
*/
#ifndef _LOG_GETOPT_H_
#define _LOG_GETOPT_H_
#ifndef __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE
#ifndef __ANDROID_API__
#define __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE 1
#elif __ANDROID_API__ > 24 /* > Nougat */
#define __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE 1
#else
#define __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE 0
#endif
#endif
#if __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE
#include <getopt.h>
#include <sys/cdefs.h>
struct getopt_context {
int opterr;
int optind;
int optopt;
int optreset;
const char* optarg;
FILE* optstderr; /* NULL defaults to stderr */
/* private */
const char* place;
int nonopt_start;
int nonopt_end;
int dash_prefix;
/* expansion space */
int __extra__;
void* __stuff__;
};
#define EMSG ""
#define NO_PREFIX (-1)
#define INIT_GETOPT_CONTEXT(context) \
context = { 1, 1, '?', 0, NULL, NULL, EMSG, -1, -1, NO_PREFIX, 0, NULL }
__BEGIN_DECLS
int getopt_long_r(int nargc, char* const* nargv, const char* options,
const struct option* long_options, int* idx,
struct getopt_context* context);
__END_DECLS
#endif /* __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE */
#endif /* !_LOG_GETOPT_H_ */

View file

@ -14,12 +14,15 @@
* limitations under the License.
*/
#include "logcat.h"
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <math.h>
#include <pthread.h>
#include <sched.h>
@ -47,8 +50,6 @@
#include <cutils/sched_policy.h>
#include <cutils/sockets.h>
#include <log/event_tag_map.h>
#include <log/getopt.h>
#include <log/logcat.h>
#include <log/logprint.h>
#include <private/android_logger.h>
#include <system/thread_defs.h>
@ -854,14 +855,8 @@ static int __logcat(android_logcat_context_internal* context) {
// net for stability dealing with possible mistaken inputs.
static const char delimiters[] = ",:; \t\n\r\f";
struct getopt_context optctx;
INIT_GETOPT_CONTEXT(optctx);
optctx.opterr = !!context->error;
optctx.optstderr = context->error;
for (;;) {
int ret;
optind = 0;
while (true) {
int option_index = 0;
// list of long-argument only strings for later comparison
static const char pid_str[] = "pid";
@ -902,19 +897,18 @@ static int __logcat(android_logcat_context_internal* context) {
};
// clang-format on
ret = getopt_long_r(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:",
long_options, &option_index, &optctx);
if (ret < 0) break;
int c = getopt_long(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", long_options,
&option_index);
if (c == -1) break;
switch (ret) {
switch (c) {
case 0:
// only long options
if (long_options[option_index].name == pid_str) {
// ToDo: determine runtime PID_MAX?
if (!getSizeTArg(optctx.optarg, &pid, 1)) {
if (!getSizeTArg(optarg, &pid, 1)) {
logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
long_options[option_index].name,
optctx.optarg);
long_options[option_index].name, optarg);
goto exit;
}
break;
@ -924,11 +918,9 @@ static int __logcat(android_logcat_context_internal* context) {
ANDROID_LOG_NONBLOCK;
// ToDo: implement API that supports setting a wrap timeout
size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
if (optctx.optarg &&
!getSizeTArg(optctx.optarg, &dummy, 1)) {
if (optarg && !getSizeTArg(optarg, &dummy, 1)) {
logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
long_options[option_index].name,
optctx.optarg);
long_options[option_index].name, optarg);
goto exit;
}
if ((dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) &&
@ -949,8 +941,7 @@ static int __logcat(android_logcat_context_internal* context) {
break;
}
if (long_options[option_index].name == id_str) {
setId = (optctx.optarg && optctx.optarg[0]) ? optctx.optarg
: nullptr;
setId = (optarg && optarg[0]) ? optarg : nullptr;
}
break;
@ -978,32 +969,27 @@ static int __logcat(android_logcat_context_internal* context) {
mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
// FALLTHRU
case 'T':
if (strspn(optctx.optarg, "0123456789") !=
strlen(optctx.optarg)) {
char* cp = parseTime(tail_time, optctx.optarg);
if (strspn(optarg, "0123456789") != strlen(optarg)) {
char* cp = parseTime(tail_time, optarg);
if (!cp) {
logcat_panic(context, HELP_FALSE,
"-%c \"%s\" not in time format\n", ret,
optctx.optarg);
logcat_panic(context, HELP_FALSE, "-%c \"%s\" not in time format\n", c,
optarg);
goto exit;
}
if (*cp) {
char c = *cp;
char ch = *cp;
*cp = '\0';
if (context->error) {
fprintf(
context->error,
"WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
ret, optctx.optarg, c, cp + 1);
fprintf(context->error, "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
c, optarg, ch, cp + 1);
}
*cp = c;
*cp = ch;
}
} else {
if (!getSizeTArg(optctx.optarg, &tail_lines, 1)) {
if (!getSizeTArg(optarg, &tail_lines, 1)) {
if (context->error) {
fprintf(context->error,
"WARNING: -%c %s invalid, setting to 1\n",
ret, optctx.optarg);
fprintf(context->error, "WARNING: -%c %s invalid, setting to 1\n", c,
optarg);
}
tail_lines = 1;
}
@ -1015,21 +1001,19 @@ static int __logcat(android_logcat_context_internal* context) {
break;
case 'e':
context->regex = new pcrecpp::RE(optctx.optarg);
context->regex = new pcrecpp::RE(optarg);
break;
case 'm': {
if (!getSizeTArg(optctx.optarg, &context->maxCount)) {
if (!getSizeTArg(optarg, &context->maxCount)) {
logcat_panic(context, HELP_FALSE,
"-%c \"%s\" isn't an "
"integer greater than zero\n",
ret, optctx.optarg);
"-%c \"%s\" isn't an integer greater than zero\n", c, optarg);
goto exit;
}
} break;
case 'g':
if (!optctx.optarg) {
if (!optarg) {
getLogSize = true;
break;
}
@ -1037,8 +1021,8 @@ static int __logcat(android_logcat_context_internal* context) {
case 'G': {
char* cp;
if (strtoll(optctx.optarg, &cp, 0) > 0) {
setLogSize = strtoll(optctx.optarg, &cp, 0);
if (strtoll(optarg, &cp, 0) > 0) {
setLogSize = strtoll(optarg, &cp, 0);
} else {
setLogSize = 0;
}
@ -1071,19 +1055,18 @@ static int __logcat(android_logcat_context_internal* context) {
} break;
case 'p':
if (!optctx.optarg) {
if (!optarg) {
getPruneList = true;
break;
}
// FALLTHRU
case 'P':
setPruneList = optctx.optarg;
setPruneList = optarg;
break;
case 'b': {
std::unique_ptr<char, void (*)(void*)> buffers(
strdup(optctx.optarg), free);
std::unique_ptr<char, void (*)(void*)> buffers(strdup(optarg), free);
char* arg = buffers.get();
unsigned idMask = 0;
char* sv = nullptr; // protect against -ENOMEM above
@ -1147,40 +1130,33 @@ static int __logcat(android_logcat_context_internal* context) {
case 'f':
if ((tail_time == log_time::EPOCH) && !tail_lines) {
tail_time = lastLogTime(optctx.optarg);
tail_time = lastLogTime(optarg);
}
// redirect output to a file
context->outputFileName = optctx.optarg;
context->outputFileName = optarg;
break;
case 'r':
if (!getSizeTArg(optctx.optarg, &context->logRotateSizeKBytes,
1)) {
logcat_panic(context, HELP_TRUE,
"Invalid parameter \"%s\" to -r\n",
optctx.optarg);
if (!getSizeTArg(optarg, &context->logRotateSizeKBytes, 1)) {
logcat_panic(context, HELP_TRUE, "Invalid parameter \"%s\" to -r\n", optarg);
goto exit;
}
break;
case 'n':
if (!getSizeTArg(optctx.optarg, &context->maxRotatedLogs, 1)) {
logcat_panic(context, HELP_TRUE,
"Invalid parameter \"%s\" to -n\n",
optctx.optarg);
if (!getSizeTArg(optarg, &context->maxRotatedLogs, 1)) {
logcat_panic(context, HELP_TRUE, "Invalid parameter \"%s\" to -n\n", optarg);
goto exit;
}
break;
case 'v': {
if (!strcmp(optctx.optarg, "help") ||
!strcmp(optctx.optarg, "--help")) {
if (!strcmp(optarg, "help") || !strcmp(optarg, "--help")) {
show_format_help(context);
context->retval = EXIT_SUCCESS;
goto exit;
}
std::unique_ptr<char, void (*)(void*)> formats(
strdup(optctx.optarg), free);
std::unique_ptr<char, void (*)(void*)> formats(strdup(optarg), free);
char* arg = formats.get();
char* sv = nullptr; // protect against -ENOMEM above
while (!!(arg = strtok_r(arg, delimiters, &sv))) {
@ -1300,8 +1276,7 @@ static int __logcat(android_logcat_context_internal* context) {
break;
case ':':
logcat_panic(context, HELP_TRUE,
"Option -%c needs an argument\n", optctx.optopt);
logcat_panic(context, HELP_TRUE, "Option -%c needs an argument\n", optopt);
goto exit;
case 'h':
@ -1310,8 +1285,7 @@ static int __logcat(android_logcat_context_internal* context) {
goto exit;
default:
logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n",
optctx.optopt);
logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n", optopt);
goto exit;
}
}
@ -1400,7 +1374,7 @@ static int __logcat(android_logcat_context_internal* context) {
"Invalid filter expression in logcat args\n");
goto exit;
}
} else if (argc == optctx.optind) {
} else if (argc == optind) {
// Add from environment variable
const char* env_tags_orig = android::getenv(context, "ANDROID_LOG_TAGS");
@ -1416,7 +1390,7 @@ static int __logcat(android_logcat_context_internal* context) {
}
} else {
// Add from commandline
for (int i = optctx.optind ; i < argc ; i++) {
for (int i = optind ; i < argc ; i++) {
// skip stderr redirections of _all_ kinds
if ((argv[i][0] == '2') && (argv[i][1] == '>')) continue;
// skip stdout redirections of _all_ kinds
@ -1707,105 +1681,6 @@ int android_logcat_run_command(android_logcat_context ctx,
return __logcat(context);
}
// starts a thread, opens a pipe, returns reading end.
int android_logcat_run_command_thread(android_logcat_context ctx,
int argc, char* const* argv,
char* const* envp) {
android_logcat_context_internal* context = ctx;
int save_errno = EBUSY;
if ((context->fds[0] >= 0) || (context->fds[1] >= 0)) goto exit;
if (pipe(context->fds) < 0) {
save_errno = errno;
goto exit;
}
pthread_attr_t attr;
if (pthread_attr_init(&attr)) {
save_errno = errno;
goto close_exit;
}
struct sched_param param;
memset(&param, 0, sizeof(param));
pthread_attr_setschedparam(&attr, &param);
pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
save_errno = errno;
goto pthread_attr_exit;
}
context->stop = false;
context->thread_stopped = false;
context->output_fd = context->fds[1];
// save off arguments so they remain while thread is active.
for (int i = 0; i < argc; ++i) {
context->args.push_back(std::string(argv[i]));
}
// save off environment so they remain while thread is active.
if (envp) for (size_t i = 0; envp[i]; ++i) {
context->envs.push_back(std::string(envp[i]));
}
for (auto& str : context->args) {
context->argv_hold.push_back(str.c_str());
}
context->argv_hold.push_back(nullptr);
for (auto& str : context->envs) {
context->envp_hold.push_back(str.c_str());
}
context->envp_hold.push_back(nullptr);
context->argc = context->argv_hold.size() - 1;
context->argv = (char* const*)&context->argv_hold[0];
context->envp = (char* const*)&context->envp_hold[0];
#ifdef DEBUG
fprintf(stderr, "argv[%d] = {", context->argc);
for (auto str : context->argv_hold) {
fprintf(stderr, " \"%s\"", str ?: "nullptr");
}
fprintf(stderr, " }\n");
fflush(stderr);
#endif
context->retval = EXIT_SUCCESS;
if (pthread_create(&context->thr, &attr,
(void*(*)(void*))__logcat, context)) {
save_errno = errno;
goto argv_exit;
}
pthread_attr_destroy(&attr);
return context->fds[0];
argv_exit:
context->argv_hold.clear();
context->args.clear();
context->envp_hold.clear();
context->envs.clear();
pthread_attr_exit:
pthread_attr_destroy(&attr);
close_exit:
close(context->fds[0]);
context->fds[0] = -1;
close(context->fds[1]);
context->fds[1] = -1;
exit:
errno = save_errno;
context->stop = true;
context->thread_stopped = true;
context->retval = EXIT_FAILURE;
return -1;
}
// test if the thread is still doing 'stuff'
int android_logcat_run_command_thread_running(android_logcat_context ctx) {
android_logcat_context_internal* context = ctx;
return context->thread_stopped == false;
}
// Finished with context
int android_logcat_destroy(android_logcat_context* ctx) {
android_logcat_context_internal* context = *ctx;

View file

@ -75,18 +75,8 @@ android_logcat_context create_android_logcat();
*
* Return value is 0 for success, non-zero for errors.
*/
int android_logcat_run_command(android_logcat_context ctx, int output, int error,
int argc, char* const* argv, char* const* envp);
/* Will not block, performed in-process
*
* Starts a thread, opens a pipe, returns reading end fd, saves off argv.
* The command supports 2>&1 (mix content) and 2>/dev/null (drop content) for
* scripted error (stderr) redirection.
*/
int android_logcat_run_command_thread(android_logcat_context ctx, int argc,
char* const* argv, char* const* envp);
int android_logcat_run_command_thread_running(android_logcat_context ctx);
int android_logcat_run_command(android_logcat_context ctx, int output, int error, int argc,
char* const* argv, char* const* envp);
/* Finished with context
*
@ -97,22 +87,6 @@ int android_logcat_run_command_thread_running(android_logcat_context ctx);
*/
int android_logcat_destroy(android_logcat_context* ctx);
/* derived helpers */
/*
* In-process thread that acts like somewhat like libc-like system and popen
* respectively. Can not handle shell scripting, only pure calls to the
* logcat operations. The android_logcat_system is a wrapper for the
* create_android_logcat, android_logcat_run_command and android_logcat_destroy
* API above. The android_logcat_popen is a wrapper for the
* android_logcat_run_command_thread API above. The android_logcat_pclose is
* a wrapper for a reasonable wait until output has subsided for command
* completion, fclose on the FILE pointer and the android_logcat_destroy API.
*/
int android_logcat_system(const char* command);
FILE* android_logcat_popen(android_logcat_context* ctx, const char* command);
int android_logcat_pclose(android_logcat_context* ctx, FILE* output);
#endif /* __ANDROID_USE_LIBLOG_LOGCAT_INTERFACE */
#ifdef __cplusplus

View file

@ -17,7 +17,7 @@
#include <signal.h>
#include <stdlib.h>
#include <log/logcat.h>
#include "logcat.h"
int main(int argc, char** argv, char** envp) {
android_logcat_context ctx = create_android_logcat();

View file

@ -1,153 +0,0 @@
/*
* Copyright (C) 2017 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.
*/
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <string>
#include <vector>
#include <log/logcat.h>
static std::string unquote(const char*& cp, const char*& delim) {
if ((*cp == '\'') || (*cp == '"')) {
// KISS: Simple quotes. Do not handle the case
// of concatenation like "blah"foo'bar'
char quote = *cp++;
delim = strchr(cp, quote);
if (!delim) delim = cp + strlen(cp);
std::string str(cp, delim);
if (*delim) ++delim;
return str;
}
delim = strpbrk(cp, " \t\f\r\n");
if (!delim) delim = cp + strlen(cp);
return std::string(cp, delim);
}
static bool __android_logcat_parse(const char* command,
std::vector<std::string>& args,
std::vector<std::string>& envs) {
for (const char *delim, *cp = command; cp && *cp; cp = delim) {
while (isspace(*cp)) ++cp;
if ((args.size() == 0) && (*cp != '=') && !isdigit(*cp)) {
const char* env = cp;
while (isalnum(*cp) || (*cp == '_')) ++cp;
if (cp && (*cp == '=')) {
std::string str(env, ++cp);
str += unquote(cp, delim);
envs.push_back(str);
continue;
}
cp = env;
}
args.push_back(unquote(cp, delim));
if ((args.size() == 1) && (args[0] != "logcat") &&
(args[0] != "/system/bin/logcat")) {
return false;
}
}
return args.size() != 0;
}
FILE* android_logcat_popen(android_logcat_context* ctx, const char* command) {
*ctx = NULL;
std::vector<std::string> args;
std::vector<std::string> envs;
if (!__android_logcat_parse(command, args, envs)) return NULL;
std::vector<const char*> argv;
for (auto& str : args) {
argv.push_back(str.c_str());
}
argv.push_back(NULL);
std::vector<const char*> envp;
for (auto& str : envs) {
envp.push_back(str.c_str());
}
envp.push_back(NULL);
*ctx = create_android_logcat();
if (!*ctx) return NULL;
int fd = android_logcat_run_command_thread(
*ctx, argv.size() - 1, (char* const*)&argv[0], (char* const*)&envp[0]);
argv.clear();
args.clear();
envp.clear();
envs.clear();
if (fd < 0) {
android_logcat_destroy(ctx);
return NULL;
}
int duped = dup(fd);
FILE* retval = fdopen(duped, "reb");
if (!retval) {
close(duped);
android_logcat_destroy(ctx);
}
return retval;
}
int android_logcat_pclose(android_logcat_context* ctx, FILE* output) {
if (*ctx) {
static const useconds_t wait_sample = 20000;
// Wait two seconds maximum
for (size_t retry = ((2 * 1000000) + wait_sample - 1) / wait_sample;
android_logcat_run_command_thread_running(*ctx) && retry; --retry) {
usleep(wait_sample);
}
}
if (output) fclose(output);
return android_logcat_destroy(ctx);
}
int android_logcat_system(const char* command) {
std::vector<std::string> args;
std::vector<std::string> envs;
if (!__android_logcat_parse(command, args, envs)) return -1;
std::vector<const char*> argv;
for (auto& str : args) {
argv.push_back(str.c_str());
}
argv.push_back(NULL);
std::vector<const char*> envp;
for (auto& str : envs) {
envp.push_back(str.c_str());
}
envp.push_back(NULL);
android_logcat_context ctx = create_android_logcat();
if (!ctx) return -1;
/* Command return value */
int retval = android_logcat_run_command(ctx, -1, -1, argv.size() - 1,
(char* const*)&argv[0],
(char* const*)&envp[0]);
/* destroy return value */
int ret = android_logcat_destroy(&ctx);
/* Paranoia merging any discrepancies between the two return values */
if (!ret) ret = retval;
return ret;
}

View file

@ -21,7 +21,7 @@
#include <string>
#include <vector>
#include <log/logcat.h>
#include "logcat.h"
int main(int argc, char** argv, char** envp) {
android_logcat_context ctx = create_android_logcat();

View file

@ -32,7 +32,6 @@ test_c_flags := \
benchmark_src_files := \
logcat_benchmark.cpp \
exec_benchmark.cpp \
# Build benchmarks for the device. Run with:
# adb shell /data/nativetest/logcat-benchmarks/logcat-benchmarks
@ -41,7 +40,7 @@ LOCAL_MODULE := $(test_module_prefix)benchmarks
LOCAL_MODULE_TAGS := $(test_tags)
LOCAL_CFLAGS += $(test_c_flags)
LOCAL_SRC_FILES := $(benchmark_src_files)
LOCAL_SHARED_LIBRARIES := libbase liblogcat
LOCAL_SHARED_LIBRARIES := libbase
include $(BUILD_NATIVE_BENCHMARK)
# -----------------------------------------------------------------------------
@ -51,7 +50,6 @@ include $(BUILD_NATIVE_BENCHMARK)
test_src_files := \
logcat_test.cpp \
logcatd_test.cpp \
liblogcat_test.cpp \
# Build tests for the device (with .so). Run with:
# adb shell /data/nativetest/logcat-unit-tests/logcat-unit-tests
@ -59,6 +57,6 @@ include $(CLEAR_VARS)
LOCAL_MODULE := $(test_module_prefix)unit-tests
LOCAL_MODULE_TAGS := $(test_tags)
LOCAL_CFLAGS += $(test_c_flags)
LOCAL_SHARED_LIBRARIES := liblog libbase liblogcat
LOCAL_SHARED_LIBRARIES := liblog libbase
LOCAL_SRC_FILES := $(test_src_files)
include $(BUILD_NATIVE_TEST)

View file

@ -1,96 +0,0 @@
/*
* Copyright (C) 2017 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.
*/
#include <stdio.h>
#include <android-base/file.h>
#include <benchmark/benchmark.h>
#include <log/logcat.h>
// Dump the statistics and report results
static void logcat_popen_libc(benchmark::State& state, const char* cmd) {
while (state.KeepRunning()) {
FILE* fp = popen(cmd, "r");
std::string ret;
android::base::ReadFdToString(fileno(fp), &ret);
pclose(fp);
}
}
static void BM_logcat_stat_popen_libc(benchmark::State& state) {
logcat_popen_libc(state, "logcat -b all -S");
}
BENCHMARK(BM_logcat_stat_popen_libc);
static void logcat_popen_liblogcat(benchmark::State& state, const char* cmd) {
while (state.KeepRunning()) {
android_logcat_context ctx;
FILE* fp = android_logcat_popen(&ctx, cmd);
std::string ret;
android::base::ReadFdToString(fileno(fp), &ret);
android_logcat_pclose(&ctx, fp);
}
}
static void BM_logcat_stat_popen_liblogcat(benchmark::State& state) {
logcat_popen_liblogcat(state, "logcat -b all -S");
}
BENCHMARK(BM_logcat_stat_popen_liblogcat);
static void logcat_system_libc(benchmark::State& state, const char* cmd) {
while (state.KeepRunning()) {
system(cmd);
}
}
static void BM_logcat_stat_system_libc(benchmark::State& state) {
logcat_system_libc(state, "logcat -b all -S >/dev/null 2>/dev/null");
}
BENCHMARK(BM_logcat_stat_system_libc);
static void logcat_system_liblogcat(benchmark::State& state, const char* cmd) {
while (state.KeepRunning()) {
android_logcat_system(cmd);
}
}
static void BM_logcat_stat_system_liblogcat(benchmark::State& state) {
logcat_system_liblogcat(state, "logcat -b all -S >/dev/null 2>/dev/null");
}
BENCHMARK(BM_logcat_stat_system_liblogcat);
// Dump the logs and report results
static void BM_logcat_dump_popen_libc(benchmark::State& state) {
logcat_popen_libc(state, "logcat -b all -d");
}
BENCHMARK(BM_logcat_dump_popen_libc);
static void BM_logcat_dump_popen_liblogcat(benchmark::State& state) {
logcat_popen_liblogcat(state, "logcat -b all -d");
}
BENCHMARK(BM_logcat_dump_popen_liblogcat);
static void BM_logcat_dump_system_libc(benchmark::State& state) {
logcat_system_libc(state, "logcat -b all -d >/dev/null 2>/dev/null");
}
BENCHMARK(BM_logcat_dump_system_libc);
static void BM_logcat_dump_system_liblogcat(benchmark::State& state) {
logcat_system_liblogcat(state, "logcat -b all -d >/dev/null 2>/dev/null");
}
BENCHMARK(BM_logcat_dump_system_liblogcat);

View file

@ -1,25 +0,0 @@
/*
* Copyright (C) 2017 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.
*/
#include <log/logcat.h>
#define logcat_define(context) android_logcat_context context
#define logcat_popen(context, command) android_logcat_popen(&(context), command)
#define logcat_pclose(context, fp) android_logcat_pclose(&(context), fp)
#define logcat_system(command) android_logcat_system(command)
#define logcat liblogcat
#include "logcat_test.cpp"

View file

@ -37,12 +37,6 @@
#include <log/log.h>
#include <log/log_event_list.h>
#ifndef logcat_popen
#define logcat_define(context)
#define logcat_popen(context, command) popen((command), "r")
#define logcat_pclose(context, fp) pclose(fp)
#define logcat_system(command) system(command)
#endif
#ifndef logcat_executable
#define USING_LOGCAT_EXECUTABLE_DEFAULT
#define logcat_executable "logcat"
@ -78,7 +72,6 @@ static const char begin[] = "--------- beginning of ";
TEST(logcat, buckets) {
FILE* fp;
logcat_define(ctx);
#undef LOG_TAG
#define LOG_TAG "inject.buckets"
@ -90,10 +83,9 @@ TEST(logcat, buckets) {
__android_log_bswrite(0, logcat_executable ".inject.buckets");
rest();
ASSERT_TRUE(NULL !=
(fp = logcat_popen(
ctx, logcat_executable
" -b radio -b events -b system -b main -d 2>/dev/null")));
ASSERT_TRUE(NULL != (fp = popen(logcat_executable
" -b radio -b events -b system -b main -d 2>/dev/null",
"r")));
char buffer[BIG_BUFFER];
@ -111,7 +103,7 @@ TEST(logcat, buckets) {
}
}
logcat_pclose(ctx, fp);
pclose(fp);
EXPECT_EQ(ids, 15);
@ -120,7 +112,6 @@ TEST(logcat, buckets) {
TEST(logcat, event_tag_filter) {
FILE* fp;
logcat_define(ctx);
#undef LOG_TAG
#define LOG_TAG "inject.filter"
@ -135,7 +126,7 @@ TEST(logcat, event_tag_filter) {
logcat_executable
" -b radio -b system -b main --pid=%d -d -s inject.filter 2>/dev/null",
getpid());
ASSERT_TRUE(NULL != (fp = logcat_popen(ctx, command.c_str())));
ASSERT_TRUE(NULL != (fp = popen(command.c_str(), "r")));
char buffer[BIG_BUFFER];
@ -145,7 +136,7 @@ TEST(logcat, event_tag_filter) {
if (strncmp(begin, buffer, sizeof(begin) - 1)) ++count;
}
logcat_pclose(ctx, fp);
pclose(fp);
// logcat, liblogcat and logcatd test instances result in the progression
// of 3, 6 and 9 for our counts as each round is performed.
@ -191,7 +182,6 @@ TEST(logcat, year) {
do {
FILE* fp;
logcat_define(ctx);
char needle[32];
time_t now;
@ -205,9 +195,8 @@ TEST(logcat, year) {
#endif
strftime(needle, sizeof(needle), "[ %Y-", ptm);
ASSERT_TRUE(NULL != (fp = logcat_popen(
ctx, logcat_executable
" -v long -v year -b all -t 3 2>/dev/null")));
ASSERT_TRUE(NULL !=
(fp = popen(logcat_executable " -v long -v year -b all -t 3 2>/dev/null", "r")));
char buffer[BIG_BUFFER];
@ -218,7 +207,7 @@ TEST(logcat, year) {
++count;
}
}
logcat_pclose(ctx, fp);
pclose(fp);
} while ((count < 3) && --tries && inject(3 - count));
@ -268,12 +257,10 @@ TEST(logcat, tz) {
do {
FILE* fp;
logcat_define(ctx);
ASSERT_TRUE(NULL !=
(fp = logcat_popen(ctx, logcat_executable
" -v long -v America/Los_Angeles "
"-b all -t 3 2>/dev/null")));
ASSERT_TRUE(NULL != (fp = popen(logcat_executable
" -v long -v America/Los_Angeles -b all -t 3 2>/dev/null",
"r")));
char buffer[BIG_BUFFER];
@ -287,7 +274,7 @@ TEST(logcat, tz) {
}
}
logcat_pclose(ctx, fp);
pclose(fp);
} while ((count < 3) && --tries && inject(3 - count));
@ -296,11 +283,11 @@ TEST(logcat, tz) {
TEST(logcat, ntz) {
FILE* fp;
logcat_define(ctx);
ASSERT_TRUE(NULL != (fp = logcat_popen(ctx, logcat_executable
" -v long -v America/Los_Angeles -v "
"zone -b all -t 3 2>/dev/null")));
ASSERT_TRUE(NULL !=
(fp = popen(logcat_executable
" -v long -v America/Los_Angeles -v zone -b all -t 3 2>/dev/null",
"r")));
char buffer[BIG_BUFFER];
@ -312,7 +299,7 @@ TEST(logcat, ntz) {
}
}
logcat_pclose(ctx, fp);
pclose(fp);
ASSERT_EQ(0, count);
}
@ -330,8 +317,7 @@ static void do_tail(int num) {
"ANDROID_PRINTF_LOG=long logcat -b all -t %d 2>/dev/null", num);
FILE* fp;
logcat_define(ctx);
ASSERT_TRUE(NULL != (fp = logcat_popen(ctx, buffer)));
ASSERT_TRUE(NULL != (fp = popen(buffer, "r")));
count = 0;
@ -339,7 +325,7 @@ static void do_tail(int num) {
++count;
}
logcat_pclose(ctx, fp);
pclose(fp);
} while ((count < num) && --tries && inject(num - count));
@ -377,8 +363,7 @@ static void do_tail_time(const char* cmd) {
do {
snprintf(buffer, sizeof(buffer), "%s -t 10 2>&1", cmd);
logcat_define(ctx);
ASSERT_TRUE(NULL != (fp = logcat_popen(ctx, buffer)));
ASSERT_TRUE(NULL != (fp = popen(buffer, "r")));
count = 0;
while ((input = fgetLongTime(buffer, sizeof(buffer), fp))) {
@ -391,7 +376,7 @@ static void do_tail_time(const char* cmd) {
free(last_timestamp);
last_timestamp = strdup(input);
}
logcat_pclose(ctx, fp);
pclose(fp);
} while ((count < 10) && --tries && inject(10 - count));
@ -401,8 +386,7 @@ static void do_tail_time(const char* cmd) {
EXPECT_TRUE(second_timestamp != NULL);
snprintf(buffer, sizeof(buffer), "%s -t '%s' 2>&1", cmd, first_timestamp);
logcat_define(ctx);
ASSERT_TRUE(NULL != (fp = logcat_popen(ctx, buffer)));
ASSERT_TRUE(NULL != (fp = popen(buffer, "r")));
int second_count = 0;
int last_timestamp_count = -1;
@ -442,7 +426,7 @@ static void do_tail_time(const char* cmd) {
last_timestamp_count = second_count;
}
}
logcat_pclose(ctx, fp);
pclose(fp);
EXPECT_TRUE(found);
if (!found) {
@ -483,10 +467,8 @@ TEST(logcat, End_to_End) {
ASSERT_LT(0, __android_log_btwrite(0, EVENT_TYPE_LONG, &ts, sizeof(ts)));
FILE* fp;
logcat_define(ctx);
ASSERT_TRUE(NULL !=
(fp = logcat_popen(ctx, logcat_executable
" -v brief -b events -t 100 2>/dev/null")));
(fp = popen(logcat_executable " -v brief -b events -t 100 2>/dev/null", "r")));
char buffer[BIG_BUFFER];
@ -507,7 +489,7 @@ TEST(logcat, End_to_End) {
}
}
logcat_pclose(ctx, fp);
pclose(fp);
ASSERT_EQ(1, count);
}
@ -521,12 +503,9 @@ TEST(logcat, End_to_End_multitude) {
FILE* fp[256]; // does this count as a multitude!
memset(fp, 0, sizeof(fp));
logcat_define(ctx[sizeof(fp) / sizeof(fp[0])]);
size_t num = 0;
do {
EXPECT_TRUE(NULL !=
(fp[num] = logcat_popen(ctx[num], logcat_executable
" -v brief -b events -t 100")));
EXPECT_TRUE(NULL != (fp[num] = popen(logcat_executable " -v brief -b events -t 100", "r")));
if (!fp[num]) {
fprintf(stderr,
"WARNING: limiting to %zu simultaneous logcat operations\n",
@ -556,7 +535,7 @@ TEST(logcat, End_to_End_multitude) {
}
}
logcat_pclose(ctx[idx], fp[idx]);
pclose(fp[idx]);
}
ASSERT_EQ(num, count);
@ -564,10 +543,9 @@ TEST(logcat, End_to_End_multitude) {
static int get_groups(const char* cmd) {
FILE* fp;
logcat_define(ctx);
// NB: crash log only available in user space
EXPECT_TRUE(NULL != (fp = logcat_popen(ctx, cmd)));
EXPECT_TRUE(NULL != (fp = popen(cmd, "r")));
if (fp == NULL) {
return 0;
@ -631,7 +609,7 @@ static int get_groups(const char* cmd) {
}
}
logcat_pclose(ctx, fp);
pclose(fp);
return count;
}
@ -815,7 +793,7 @@ TEST(logcat, logrotate) {
snprintf(command, sizeof(command), comm, buf);
int ret;
EXPECT_FALSE(IsFalse(ret = logcat_system(command), command));
EXPECT_FALSE(IsFalse(ret = system(command), command));
if (!ret) {
snprintf(command, sizeof(command), "ls -s %s 2>/dev/null", buf);
@ -861,7 +839,7 @@ TEST(logcat, logrotate_suffix) {
snprintf(command, sizeof(command), logcat_cmd, tmp_out_dir);
int ret;
EXPECT_FALSE(IsFalse(ret = logcat_system(command), command));
EXPECT_FALSE(IsFalse(ret = system(command), command));
if (!ret) {
snprintf(command, sizeof(command), "ls %s 2>/dev/null", tmp_out_dir);
@ -920,7 +898,7 @@ TEST(logcat, logrotate_continue) {
snprintf(command, sizeof(command), logcat_cmd, tmp_out_dir, log_filename);
int ret;
EXPECT_FALSE(IsFalse(ret = logcat_system(command), command));
EXPECT_FALSE(IsFalse(ret = system(command), command));
if (ret) {
snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir);
EXPECT_FALSE(IsFalse(system(command), command));
@ -969,7 +947,7 @@ TEST(logcat, logrotate_continue) {
// re-run the command, it should only add a few lines more content if it
// continues where it left off.
snprintf(command, sizeof(command), logcat_cmd, tmp_out_dir, log_filename);
EXPECT_FALSE(IsFalse(ret = logcat_system(command), command));
EXPECT_FALSE(IsFalse(ret = system(command), command));
if (ret) {
snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir);
EXPECT_FALSE(IsFalse(system(command), command));
@ -1052,7 +1030,7 @@ TEST(logcat, logrotate_clear) {
tmp_out_dir, log_filename, num_val);
int ret;
EXPECT_FALSE(IsFalse(ret = logcat_system(command), command));
EXPECT_FALSE(IsFalse(ret = system(command), command));
if (ret) {
snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir);
EXPECT_FALSE(IsFalse(system(command), command));
@ -1082,7 +1060,7 @@ TEST(logcat, logrotate_clear) {
strcat(command, clear_cmd);
int ret;
EXPECT_FALSE(IsFalse(ret = logcat_system(command), command));
EXPECT_FALSE(IsFalse(ret = system(command), command));
if (ret) {
snprintf(command, sizeof(command), cleanup_cmd, tmp_out_dir);
EXPECT_FALSE(system(command));
@ -1120,7 +1098,7 @@ static int logrotate_count_id(const char* logcat_cmd, const char* tmp_out_dir) {
snprintf(command, sizeof(command), logcat_cmd, tmp_out_dir, log_filename);
int ret = logcat_system(command);
int ret = system(command);
if (ret) {
fprintf(stderr, "system(\"%s\")=%d", command, ret);
return -1;
@ -1194,7 +1172,7 @@ TEST(logcat, logrotate_nodir) {
" -b all -d"
" -f /das/nein/gerfingerpoken/logcat/log.txt"
" -n 256 -r 1024";
EXPECT_FALSE(IsFalse(0 == logcat_system(command), command));
EXPECT_FALSE(IsFalse(0 == system(command), command));
}
#ifndef logcat
@ -1329,10 +1307,7 @@ TEST(logcat, blocking_clear) {
#endif
static bool get_white_black(char** list) {
FILE* fp;
logcat_define(ctx);
fp = logcat_popen(ctx, logcat_executable " -p 2>/dev/null");
FILE* fp = popen(logcat_executable " -p 2>/dev/null", "r");
if (fp == NULL) {
fprintf(stderr, "ERROR: logcat -p 2>/dev/null\n");
return false;
@ -1360,19 +1335,15 @@ static bool get_white_black(char** list) {
asprintf(list, "%s", buf);
}
}
logcat_pclose(ctx, fp);
pclose(fp);
return *list != NULL;
}
static bool set_white_black(const char* list) {
FILE* fp;
logcat_define(ctx);
char buffer[BIG_BUFFER];
snprintf(buffer, sizeof(buffer), logcat_executable " -P '%s' 2>&1",
list ? list : "");
fp = logcat_popen(ctx, buffer);
FILE* fp = popen(buffer, "r");
if (fp == NULL) {
fprintf(stderr, "ERROR: %s\n", buffer);
return false;
@ -1391,10 +1362,10 @@ static bool set_white_black(const char* list) {
continue;
}
fprintf(stderr, "%s\n", buf);
logcat_pclose(ctx, fp);
pclose(fp);
return false;
}
return logcat_pclose(ctx, fp) == 0;
return pclose(fp) == 0;
}
TEST(logcat, white_black_adjust) {
@ -1429,7 +1400,6 @@ TEST(logcat, white_black_adjust) {
TEST(logcat, regex) {
FILE* fp;
logcat_define(ctx);
int count = 0;
char buffer[BIG_BUFFER];
@ -1450,7 +1420,7 @@ TEST(logcat, regex) {
// Let the logs settle
rest();
ASSERT_TRUE(NULL != (fp = logcat_popen(ctx, buffer)));
ASSERT_TRUE(NULL != (fp = popen(buffer, "r")));
while (fgets(buffer, sizeof(buffer), fp)) {
if (!strncmp(begin, buffer, sizeof(begin) - 1)) {
@ -1462,14 +1432,13 @@ TEST(logcat, regex) {
count++;
}
logcat_pclose(ctx, fp);
pclose(fp);
ASSERT_EQ(2, count);
}
TEST(logcat, maxcount) {
FILE* fp;
logcat_define(ctx);
int count = 0;
char buffer[BIG_BUFFER];
@ -1488,7 +1457,7 @@ TEST(logcat, maxcount) {
rest();
ASSERT_TRUE(NULL != (fp = logcat_popen(ctx, buffer)));
ASSERT_TRUE(NULL != (fp = popen(buffer, "r")));
while (fgets(buffer, sizeof(buffer), fp)) {
if (!strncmp(begin, buffer, sizeof(begin) - 1)) {
@ -1498,7 +1467,7 @@ TEST(logcat, maxcount) {
count++;
}
logcat_pclose(ctx, fp);
pclose(fp);
ASSERT_EQ(3, count);
}
@ -1510,13 +1479,7 @@ static bool End_to_End(const char* tag, const char* fmt, ...)
;
static bool End_to_End(const char* tag, const char* fmt, ...) {
logcat_define(ctx);
FILE* fp = logcat_popen(ctx, logcat_executable
" -v brief"
" -b events"
" -v descriptive"
" -t 100"
" 2>/dev/null");
FILE* fp = popen(logcat_executable " -v brief -b events -v descriptive -t 100 2>/dev/null", "r");
if (!fp) {
fprintf(stderr, "End_to_End: popen failed");
return false;
@ -1551,7 +1514,7 @@ static bool End_to_End(const char* tag, const char* fmt, ...) {
}
}
logcat_pclose(ctx, fp);
pclose(fp);
if ((count == 0) && (lastMatch.length() > 0)) {
// Help us pinpoint where things went wrong ...
@ -1741,13 +1704,12 @@ TEST(logcat, descriptive) {
}
static bool reportedSecurity(const char* command) {
logcat_define(ctx);
FILE* fp = logcat_popen(ctx, command);
FILE* fp = popen(command, "r");
if (!fp) return true;
std::string ret;
bool val = android::base::ReadFdToString(fileno(fp), &ret);
logcat_pclose(ctx, fp);
pclose(fp);
if (!val) return true;
return std::string::npos != ret.find("'security'");
@ -1762,13 +1724,12 @@ TEST(logcat, security) {
}
static size_t commandOutputSize(const char* command) {
logcat_define(ctx);
FILE* fp = logcat_popen(ctx, command);
FILE* fp = popen(command, "r");
if (!fp) return 0;
std::string ret;
if (!android::base::ReadFdToString(fileno(fp), &ret)) return 0;
if (logcat_pclose(ctx, fp) != 0) return 0;
if (pclose(fp) != 0) return 0;
return ret.size();
}