1 /*
2  * Copyright (C) 2006-2017 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 <arpa/inet.h>
18 #include <assert.h>
19 #include <ctype.h>
20 #include <dirent.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <math.h>
24 #include <pthread.h>
25 #include <sched.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/cdefs.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <time.h>
36 #include <unistd.h>
37 
38 #include <atomic>
39 #include <memory>
40 #include <string>
41 #include <vector>
42 
43 #include <android-base/file.h>
44 #include <android-base/properties.h>
45 #include <android-base/stringprintf.h>
46 #include <android-base/strings.h>
47 #include <cutils/sched_policy.h>
48 #include <cutils/sockets.h>
49 #include <log/event_tag_map.h>
50 #include <log/getopt.h>
51 #include <log/logcat.h>
52 #include <log/logprint.h>
53 #include <private/android_logger.h>
54 #include <system/thread_defs.h>
55 
56 #include <pcrecpp.h>
57 
58 #define DEFAULT_MAX_ROTATED_LOGS 4
59 
60 struct log_device_t {
61     const char* device;
62     bool binary;
63     struct logger* logger;
64     struct logger_list* logger_list;
65     bool printed;
66 
67     log_device_t* next;
68 
log_device_tlog_device_t69     log_device_t(const char* d, bool b) {
70         device = d;
71         binary = b;
72         next = nullptr;
73         printed = false;
74         logger = nullptr;
75         logger_list = nullptr;
76     }
77 };
78 
79 struct android_logcat_context_internal {
80     // status
81     volatile std::atomic_int retval;  // valid if thread_stopped set
82     // Arguments passed in, or copies and storage thereof if a thread.
83     int argc;
84     char* const* argv;
85     char* const* envp;
86     std::vector<std::string> args;
87     std::vector<const char*> argv_hold;
88     std::vector<std::string> envs;
89     std::vector<const char*> envp_hold;
90     int output_fd;  // duplication of fileno(output) (below)
91     int error_fd;   // duplication of fileno(error) (below)
92 
93     // library
94     int fds[2];    // From popen call
95     FILE* output;  // everything writes to fileno(output), buffer unused
96     FILE* error;   // unless error == output.
97     pthread_t thr;
98     volatile std::atomic_bool stop;  // quick exit flag
99     volatile std::atomic_bool thread_stopped;
100     bool stderr_null;    // shell "2>/dev/null"
101     bool stderr_stdout;  // shell "2>&1"
102 
103     // global variables
104     AndroidLogFormat* logformat;
105     const char* outputFileName;
106     // 0 means "no log rotation"
107     size_t logRotateSizeKBytes;
108     // 0 means "unbounded"
109     size_t maxRotatedLogs;
110     size_t outByteCount;
111     int printBinary;
112     int devCount;  // >1 means multiple
113     pcrecpp::RE* regex;
114     log_device_t* devices;
115     EventTagMap* eventTagMap;
116     // 0 means "infinite"
117     size_t maxCount;
118     size_t printCount;
119 
120     bool printItAnyways;
121     bool debug;
122     bool hasOpenedEventTagMap;
123 };
124 
125 // Creates a context associated with this logcat instance
create_android_logcat()126 android_logcat_context create_android_logcat() {
127     android_logcat_context_internal* context;
128 
129     context = (android_logcat_context_internal*)calloc(
130         1, sizeof(android_logcat_context_internal));
131     if (!context) return nullptr;
132 
133     context->fds[0] = -1;
134     context->fds[1] = -1;
135     context->output_fd = -1;
136     context->error_fd = -1;
137     context->maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS;
138 
139     context->argv_hold.clear();
140     context->args.clear();
141     context->envp_hold.clear();
142     context->envs.clear();
143 
144     return (android_logcat_context)context;
145 }
146 
147 // logd prefixes records with a length field
148 #define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t)
149 
150 namespace android {
151 
152 enum helpType { HELP_FALSE, HELP_TRUE, HELP_FORMAT };
153 
154 // if showHelp is set, newline required in fmt statement to transition to usage
155 static void logcat_panic(android_logcat_context_internal* context,
156                          enum helpType showHelp, const char* fmt, ...)
157     __printflike(3, 4);
158 
openLogFile(const char * pathname)159 static int openLogFile(const char* pathname) {
160     return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
161 }
162 
close_output(android_logcat_context_internal * context)163 static void close_output(android_logcat_context_internal* context) {
164     // split output_from_error
165     if (context->error == context->output) {
166         context->output = nullptr;
167         context->output_fd = -1;
168     }
169     if (context->error && (context->output_fd == fileno(context->error))) {
170         context->output_fd = -1;
171     }
172     if (context->output_fd == context->error_fd) {
173         context->output_fd = -1;
174     }
175     // close output channel
176     if (context->output) {
177         if (context->output != stdout) {
178             if (context->output_fd == fileno(context->output)) {
179                 context->output_fd = -1;
180             }
181             if (context->fds[1] == fileno(context->output)) {
182                 context->fds[1] = -1;
183             }
184             fclose(context->output);
185         }
186         context->output = nullptr;
187     }
188     if (context->output_fd >= 0) {
189         if (context->output_fd != fileno(stdout)) {
190             if (context->fds[1] == context->output_fd) {
191                 context->fds[1] = -1;
192             }
193             close(context->output_fd);
194         }
195         context->output_fd = -1;
196     }
197 }
198 
close_error(android_logcat_context_internal * context)199 static void close_error(android_logcat_context_internal* context) {
200     // split error_from_output
201     if (context->output == context->error) {
202         context->error = nullptr;
203         context->error_fd = -1;
204     }
205     if (context->output && (context->error_fd == fileno(context->output))) {
206         context->error_fd = -1;
207     }
208     if (context->error_fd == context->output_fd) {
209         context->error_fd = -1;
210     }
211     // close error channel
212     if (context->error) {
213         if ((context->error != stderr) && (context->error != stdout)) {
214             if (context->error_fd == fileno(context->error)) {
215                 context->error_fd = -1;
216             }
217             if (context->fds[1] == fileno(context->error)) {
218                 context->fds[1] = -1;
219             }
220             fclose(context->error);
221         }
222         context->error = nullptr;
223     }
224     if (context->error_fd >= 0) {
225         if ((context->error_fd != fileno(stdout)) &&
226             (context->error_fd != fileno(stderr))) {
227             if (context->fds[1] == context->error_fd) context->fds[1] = -1;
228             close(context->error_fd);
229         }
230         context->error_fd = -1;
231     }
232 }
233 
rotateLogs(android_logcat_context_internal * context)234 static void rotateLogs(android_logcat_context_internal* context) {
235     int err;
236 
237     // Can't rotate logs if we're not outputting to a file
238     if (!context->outputFileName) return;
239 
240     close_output(context);
241 
242     // Compute the maximum number of digits needed to count up to
243     // maxRotatedLogs in decimal.  eg:
244     // maxRotatedLogs == 30
245     //   -> log10(30) == 1.477
246     //   -> maxRotationCountDigits == 2
247     int maxRotationCountDigits =
248         (context->maxRotatedLogs > 0)
249             ? (int)(floor(log10(context->maxRotatedLogs) + 1))
250             : 0;
251 
252     for (int i = context->maxRotatedLogs; i > 0; i--) {
253         std::string file1 = android::base::StringPrintf(
254             "%s.%.*d", context->outputFileName, maxRotationCountDigits, i);
255 
256         std::string file0;
257         if (!(i - 1)) {
258             file0 = android::base::StringPrintf("%s", context->outputFileName);
259         } else {
260             file0 =
261                 android::base::StringPrintf("%s.%.*d", context->outputFileName,
262                                             maxRotationCountDigits, i - 1);
263         }
264 
265         if (!file0.length() || !file1.length()) {
266             perror("while rotating log files");
267             break;
268         }
269 
270         err = rename(file0.c_str(), file1.c_str());
271 
272         if (err < 0 && errno != ENOENT) {
273             perror("while rotating log files");
274         }
275     }
276 
277     context->output_fd = openLogFile(context->outputFileName);
278 
279     if (context->output_fd < 0) {
280         logcat_panic(context, HELP_FALSE, "couldn't open output file");
281         return;
282     }
283     context->output = fdopen(context->output_fd, "web");
284     if (!context->output) {
285         logcat_panic(context, HELP_FALSE, "couldn't fdopen output file");
286         return;
287     }
288     if (context->stderr_stdout) {
289         close_error(context);
290         context->error = context->output;
291         context->error_fd = context->output_fd;
292     }
293 
294     context->outByteCount = 0;
295 }
296 
printBinary(android_logcat_context_internal * context,struct log_msg * buf)297 void printBinary(android_logcat_context_internal* context, struct log_msg* buf) {
298     size_t size = buf->len();
299 
300     TEMP_FAILURE_RETRY(write(context->output_fd, buf, size));
301 }
302 
regexOk(android_logcat_context_internal * context,const AndroidLogEntry & entry)303 static bool regexOk(android_logcat_context_internal* context,
304                     const AndroidLogEntry& entry) {
305     if (!context->regex) return true;
306 
307     std::string messageString(entry.message, entry.messageLen);
308 
309     return context->regex->PartialMatch(messageString);
310 }
311 
processBuffer(android_logcat_context_internal * context,log_device_t * dev,struct log_msg * buf)312 static void processBuffer(android_logcat_context_internal* context,
313                           log_device_t* dev, struct log_msg* buf) {
314     int bytesWritten = 0;
315     int err;
316     AndroidLogEntry entry;
317     char binaryMsgBuf[1024];
318 
319     if (dev->binary) {
320         if (!context->eventTagMap && !context->hasOpenedEventTagMap) {
321             context->eventTagMap = android_openEventTagMap(nullptr);
322             context->hasOpenedEventTagMap = true;
323         }
324         err = android_log_processBinaryLogBuffer(
325             &buf->entry_v1, &entry, context->eventTagMap, binaryMsgBuf,
326             sizeof(binaryMsgBuf));
327         // printf(">>> pri=%d len=%d msg='%s'\n",
328         //    entry.priority, entry.messageLen, entry.message);
329     } else {
330         err = android_log_processLogBuffer(&buf->entry_v1, &entry);
331     }
332     if ((err < 0) && !context->debug) return;
333 
334     if (android_log_shouldPrintLine(
335             context->logformat, std::string(entry.tag, entry.tagLen).c_str(),
336             entry.priority)) {
337         bool match = regexOk(context, entry);
338 
339         context->printCount += match;
340         if (match || context->printItAnyways) {
341             bytesWritten = android_log_printLogLine(context->logformat,
342                                                     context->output_fd, &entry);
343 
344             if (bytesWritten < 0) {
345                 logcat_panic(context, HELP_FALSE, "output error");
346                 return;
347             }
348         }
349     }
350 
351     context->outByteCount += bytesWritten;
352 
353     if (context->logRotateSizeKBytes > 0 &&
354         (context->outByteCount / 1024) >= context->logRotateSizeKBytes) {
355         rotateLogs(context);
356     }
357 }
358 
maybePrintStart(android_logcat_context_internal * context,log_device_t * dev,bool printDividers)359 static void maybePrintStart(android_logcat_context_internal* context,
360                             log_device_t* dev, bool printDividers) {
361     if (!dev->printed || printDividers) {
362         if (context->devCount > 1 && !context->printBinary) {
363             char buf[1024];
364             snprintf(buf, sizeof(buf), "--------- %s %s\n",
365                      dev->printed ? "switch to" : "beginning of", dev->device);
366             if (write(context->output_fd, buf, strlen(buf)) < 0) {
367                 logcat_panic(context, HELP_FALSE, "output error");
368                 return;
369             }
370         }
371         dev->printed = true;
372     }
373 }
374 
setupOutputAndSchedulingPolicy(android_logcat_context_internal * context,bool blocking)375 static void setupOutputAndSchedulingPolicy(
376     android_logcat_context_internal* context, bool blocking) {
377     if (!context->outputFileName) return;
378 
379     if (blocking) {
380         // Lower priority and set to batch scheduling if we are saving
381         // the logs into files and taking continuous content.
382         if ((set_sched_policy(0, SP_BACKGROUND) < 0) && context->error) {
383             fprintf(context->error,
384                     "failed to set background scheduling policy\n");
385         }
386 
387         struct sched_param param;
388         memset(&param, 0, sizeof(param));
389         if (sched_setscheduler((pid_t)0, SCHED_BATCH, &param) < 0) {
390             fprintf(stderr, "failed to set to batch scheduler\n");
391         }
392 
393         if ((setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) &&
394             context->error) {
395             fprintf(context->error, "failed set to priority\n");
396         }
397     }
398 
399     close_output(context);
400 
401     context->output_fd = openLogFile(context->outputFileName);
402 
403     if (context->output_fd < 0) {
404         logcat_panic(context, HELP_FALSE, "couldn't open output file");
405         return;
406     }
407 
408     struct stat statbuf;
409     if (fstat(context->output_fd, &statbuf) == -1) {
410         close_output(context);
411         logcat_panic(context, HELP_FALSE, "couldn't get output file stat\n");
412         return;
413     }
414 
415     if ((size_t)statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) {
416         close_output(context);
417         logcat_panic(context, HELP_FALSE, "invalid output file stat\n");
418         return;
419     }
420 
421     context->output = fdopen(context->output_fd, "web");
422 
423     context->outByteCount = statbuf.st_size;
424 }
425 
426 // clang-format off
show_help(android_logcat_context_internal * context)427 static void show_help(android_logcat_context_internal* context) {
428     if (!context->error) return;
429 
430     const char* cmd = strrchr(context->argv[0], '/');
431     cmd = cmd ? cmd + 1 : context->argv[0];
432 
433     fprintf(context->error, "Usage: %s [options] [filterspecs]\n", cmd);
434 
435     fprintf(context->error, "options include:\n"
436                     "  -s              Set default filter to silent. Equivalent to filterspec '*:S'\n"
437                     "  -f <file>, --file=<file>               Log to file. Default is stdout\n"
438                     "  -r <kbytes>, --rotate-kbytes=<kbytes>\n"
439                     "                  Rotate log every kbytes. Requires -f option\n"
440                     "  -n <count>, --rotate-count=<count>\n"
441                     "                  Sets max number of rotated logs to <count>, default 4\n"
442                     "  --id=<id>       If the signature id for logging to file changes, then clear\n"
443                     "                  the fileset and continue\n"
444                     "  -v <format>, --format=<format>\n"
445                     "                  Sets log print format verb and adverbs, where <format> is:\n"
446                     "                    brief help long process raw tag thread threadtime time\n"
447                     "                  and individually flagged modifying adverbs can be added:\n"
448                     "                    color descriptive epoch monotonic printable uid\n"
449                     "                    usec UTC year zone\n"
450                     "                  Multiple -v parameters or comma separated list of format and\n"
451                     "                  format modifiers are allowed.\n"
452                     // private and undocumented nsec, no signal, too much noise
453                     // useful for -T or -t <timestamp> accurate testing though.
454                     "  -D, --dividers  Print dividers between each log buffer\n"
455                     "  -c, --clear     Clear (flush) the entire log and exit\n"
456                     "                  if Log to File specified, clear fileset instead\n"
457                     "  -d              Dump the log and then exit (don't block)\n"
458                     "  -e <expr>, --regex=<expr>\n"
459                     "                  Only print lines where the log message matches <expr>\n"
460                     "                  where <expr> is a Perl-compatible regular expression\n"
461                     // Leave --head undocumented as alias for -m
462                     "  -m <count>, --max-count=<count>\n"
463                     "                  Quit after printing <count> lines. This is meant to be\n"
464                     "                  paired with --regex, but will work on its own.\n"
465                     "  --print         Paired with --regex and --max-count to let content bypass\n"
466                     "                  regex filter but still stop at number of matches.\n"
467                     // Leave --tail undocumented as alias for -t
468                     "  -t <count>      Print only the most recent <count> lines (implies -d)\n"
469                     "  -t '<time>'     Print most recent lines since specified time (implies -d)\n"
470                     "  -T <count>      Print only the most recent <count> lines (does not imply -d)\n"
471                     "  -T '<time>'     Print most recent lines since specified time (not imply -d)\n"
472                     "                  count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n"
473                     "                  'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n"
474                     "  -g, --buffer-size                      Get the size of the ring buffer.\n"
475                     "  -G <size>, --buffer-size=<size>\n"
476                     "                  Set size of log ring buffer, may suffix with K or M.\n"
477                     "  -L, --last      Dump logs from prior to last reboot\n"
478                     // Leave security (Device Owner only installations) and
479                     // kernel (userdebug and eng) buffers undocumented.
480                     "  -b <buffer>, --buffer=<buffer>         Request alternate ring buffer, 'main',\n"
481                     "                  'system', 'radio', 'events', 'crash', 'default' or 'all'.\n"
482                     "                  Multiple -b parameters or comma separated list of buffers are\n"
483                     "                  allowed. Buffers interleaved. Default -b main,system,crash.\n"
484                     "  -B, --binary    Output the log in binary.\n"
485                     "  -S, --statistics                       Output statistics.\n"
486                     "  -p, --prune     Print prune white and ~black list. Service is specified as\n"
487                     "                  UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n"
488                     "                  with ~, otherwise weighed for longevity if unadorned. All\n"
489                     "                  other pruning activity is oldest first. Special case ~!\n"
490                     "                  represents an automatic quicker pruning for the noisiest\n"
491                     "                  UID as determined by the current statistics.\n"
492                     "  -P '<list> ...', --prune='<list> ...'\n"
493                     "                  Set prune white and ~black list, using same format as\n"
494                     "                  listed above. Must be quoted.\n"
495                     "  --pid=<pid>     Only prints logs from the given pid.\n"
496                     // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours
497                     "  --wrap          Sleep for 2 hours or when buffer about to wrap whichever\n"
498                     "                  comes first. Improves efficiency of polling by providing\n"
499                     "                  an about-to-wrap wakeup.\n");
500 
501     fprintf(context->error, "\nfilterspecs are a series of \n"
502                    "  <tag>[:priority]\n\n"
503                    "where <tag> is a log component tag (or * for all) and priority is:\n"
504                    "  V    Verbose (default for <tag>)\n"
505                    "  D    Debug (default for '*')\n"
506                    "  I    Info\n"
507                    "  W    Warn\n"
508                    "  E    Error\n"
509                    "  F    Fatal\n"
510                    "  S    Silent (suppress all output)\n"
511                    "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n"
512                    "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n"
513                    "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n"
514                    "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n"
515                    "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n"
516                    "or defaults to \"threadtime\"\n\n");
517 }
518 
show_format_help(android_logcat_context_internal * context)519 static void show_format_help(android_logcat_context_internal* context) {
520     if (!context->error) return;
521     fprintf(context->error,
522         "-v <format>, --format=<format> options:\n"
523         "  Sets log print format verb and adverbs, where <format> is:\n"
524         "    brief long process raw tag thread threadtime time\n"
525         "  and individually flagged modifying adverbs can be added:\n"
526         "    color descriptive epoch monotonic printable uid usec UTC year zone\n"
527         "\nSingle format verbs:\n"
528         "  brief      — Display priority/tag and PID of the process issuing the message.\n"
529         "  long       — Display all metadata fields, separate messages with blank lines.\n"
530         "  process    — Display PID only.\n"
531         "  raw        — Display the raw log message, with no other metadata fields.\n"
532         "  tag        — Display the priority/tag only.\n"
533         "  thread     — Display priority, PID and TID of process issuing the message.\n"
534         "  threadtime — Display the date, invocation time, priority, tag, and the PID\n"
535         "               and TID of the thread issuing the message. (the default format).\n"
536         "  time       — Display the date, invocation time, priority/tag, and PID of the\n"
537         "             process issuing the message.\n"
538         "\nAdverb modifiers can be used in combination:\n"
539         "  color       — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n"
540         "                \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n"
541         "  descriptive — events logs only, descriptions from event-log-tags database.\n"
542         "  epoch       — Display time as seconds since Jan 1 1970.\n"
543         "  monotonic   — Display time as cpu seconds since last boot.\n"
544         "  printable   — Ensure that any binary logging content is escaped.\n"
545         "  uid         — If permitted, display the UID or Android ID of logged process.\n"
546         "  usec        — Display time down the microsecond precision.\n"
547         "  UTC         — Display time as UTC.\n"
548         "  year        — Add the year to the displayed time.\n"
549         "  zone        — Add the local timezone to the displayed time.\n"
550         "  \"<zone>\"    — Print using this public named timezone (experimental).\n\n"
551     );
552 }
553 // clang-format on
554 
setLogFormat(android_logcat_context_internal * context,const char * formatString)555 static int setLogFormat(android_logcat_context_internal* context,
556                         const char* formatString) {
557     AndroidLogPrintFormat format;
558 
559     format = android_log_formatFromString(formatString);
560 
561     // invalid string?
562     if (format == FORMAT_OFF) return -1;
563 
564     return android_log_setPrintFormat(context->logformat, format);
565 }
566 
567 static const char multipliers[][2] = { { "" }, { "K" }, { "M" }, { "G" } };
568 
value_of_size(unsigned long value)569 static unsigned long value_of_size(unsigned long value) {
570     for (unsigned i = 0;
571          (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
572          value /= 1024, ++i)
573         ;
574     return value;
575 }
576 
multiplier_of_size(unsigned long value)577 static const char* multiplier_of_size(unsigned long value) {
578     unsigned i;
579     for (i = 0;
580          (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024);
581          value /= 1024, ++i)
582         ;
583     return multipliers[i];
584 }
585 
586 // String to unsigned int, returns -1 if it fails
getSizeTArg(const char * ptr,size_t * val,size_t min=0,size_t max=SIZE_MAX)587 static bool getSizeTArg(const char* ptr, size_t* val, size_t min = 0,
588                         size_t max = SIZE_MAX) {
589     if (!ptr) return false;
590 
591     char* endp;
592     errno = 0;
593     size_t ret = (size_t)strtoll(ptr, &endp, 0);
594 
595     if (endp[0] || errno) return false;
596 
597     if ((ret > max) || (ret < min)) return false;
598 
599     *val = ret;
600     return true;
601 }
602 
logcat_panic(android_logcat_context_internal * context,enum helpType showHelp,const char * fmt,...)603 static void logcat_panic(android_logcat_context_internal* context,
604                          enum helpType showHelp, const char* fmt, ...) {
605     context->retval = EXIT_FAILURE;
606     if (!context->error) {
607         context->stop = true;
608         return;
609     }
610 
611     va_list args;
612     va_start(args, fmt);
613     vfprintf(context->error, fmt, args);
614     va_end(args);
615 
616     switch (showHelp) {
617         case HELP_TRUE:
618             show_help(context);
619             break;
620         case HELP_FORMAT:
621             show_format_help(context);
622             break;
623         case HELP_FALSE:
624         default:
625             break;
626     }
627 
628     context->stop = true;
629 }
630 
parseTime(log_time & t,const char * cp)631 static char* parseTime(log_time& t, const char* cp) {
632     char* ep = t.strptime(cp, "%m-%d %H:%M:%S.%q");
633     if (ep) return ep;
634     ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q");
635     if (ep) return ep;
636     return t.strptime(cp, "%s.%q");
637 }
638 
639 // Find last logged line in <outputFileName>, or <outputFileName>.1
lastLogTime(const char * outputFileName)640 static log_time lastLogTime(const char* outputFileName) {
641     log_time retval(log_time::EPOCH);
642     if (!outputFileName) return retval;
643 
644     std::string directory;
645     const char* file = strrchr(outputFileName, '/');
646     if (!file) {
647         directory = ".";
648         file = outputFileName;
649     } else {
650         directory = std::string(outputFileName, file - outputFileName);
651         ++file;
652     }
653 
654     std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(directory.c_str()),
655                                             closedir);
656     if (!dir.get()) return retval;
657 
658     log_time now(android_log_clockid());
659 
660     size_t len = strlen(file);
661     log_time modulo(0, NS_PER_SEC);
662     struct dirent* dp;
663 
664     while (!!(dp = readdir(dir.get()))) {
665         if ((dp->d_type != DT_REG) || !!strncmp(dp->d_name, file, len) ||
666             (dp->d_name[len] && ((dp->d_name[len] != '.') ||
667                                  (strtoll(dp->d_name + 1, nullptr, 10) != 1)))) {
668             continue;
669         }
670 
671         std::string file_name = directory;
672         file_name += "/";
673         file_name += dp->d_name;
674         std::string file;
675         if (!android::base::ReadFileToString(file_name, &file)) continue;
676 
677         bool found = false;
678         for (const auto& line : android::base::Split(file, "\n")) {
679             log_time t(log_time::EPOCH);
680             char* ep = parseTime(t, line.c_str());
681             if (!ep || (*ep != ' ')) continue;
682             // determine the time precision of the logs (eg: msec or usec)
683             for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) {
684                 if (t.tv_nsec % (mod * 10)) {
685                     modulo.tv_nsec = mod;
686                     break;
687                 }
688             }
689             // We filter any times later than current as we may not have the
690             // year stored with each log entry. Also, since it is possible for
691             // entries to be recorded out of order (very rare) we select the
692             // maximum we find just in case.
693             if ((t < now) && (t > retval)) {
694                 retval = t;
695                 found = true;
696             }
697         }
698         // We count on the basename file to be the definitive end, so stop here.
699         if (!dp->d_name[len] && found) break;
700     }
701     if (retval == log_time::EPOCH) return retval;
702     // tail_time prints matching or higher, round up by the modulo to prevent
703     // a replay of the last entry we have just checked.
704     retval += modulo;
705     return retval;
706 }
707 
getenv(android_logcat_context_internal * context,const char * name)708 const char* getenv(android_logcat_context_internal* context, const char* name) {
709     if (!context->envp || !name || !*name) return nullptr;
710 
711     for (size_t len = strlen(name), i = 0; context->envp[i]; ++i) {
712         if (strncmp(context->envp[i], name, len)) continue;
713         if (context->envp[i][len] == '=') return &context->envp[i][len + 1];
714     }
715     return nullptr;
716 }
717 
718 }  // namespace android
719 
reportErrorName(const char ** current,const char * name,bool blockSecurity)720 void reportErrorName(const char** current, const char* name,
721                      bool blockSecurity) {
722     if (*current) return;
723     if (!blockSecurity || (android_name_to_log_id(name) != LOG_ID_SECURITY)) {
724         *current = name;
725     }
726 }
727 
__logcat(android_logcat_context_internal * context)728 static int __logcat(android_logcat_context_internal* context) {
729     using namespace android;
730     int err;
731     bool hasSetLogFormat = false;
732     bool clearLog = false;
733     bool allSelected = false;
734     bool getLogSize = false;
735     bool getPruneList = false;
736     bool printStatistics = false;
737     bool printDividers = false;
738     unsigned long setLogSize = 0;
739     const char* setPruneList = nullptr;
740     const char* setId = nullptr;
741     int mode = ANDROID_LOG_RDONLY;
742     std::string forceFilters;
743     log_device_t* dev;
744     struct logger_list* logger_list;
745     size_t tail_lines = 0;
746     log_time tail_time(log_time::EPOCH);
747     size_t pid = 0;
748     bool got_t = false;
749 
750     // object instantiations before goto's can happen
751     log_device_t unexpected("unexpected", false);
752     const char* openDeviceFail = nullptr;
753     const char* clearFail = nullptr;
754     const char* setSizeFail = nullptr;
755     const char* getSizeFail = nullptr;
756     int argc = context->argc;
757     char* const* argv = context->argv;
758 
759     context->output = stdout;
760     context->error = stderr;
761 
762     for (int i = 0; i < argc; ++i) {
763         // Simulate shell stderr redirect parsing
764         if ((argv[i][0] != '2') || (argv[i][1] != '>')) continue;
765 
766         // Append to file not implemented, just open file
767         size_t skip = (argv[i][2] == '>') + 2;
768         if (!strcmp(&argv[i][skip], "/dev/null")) {
769             context->stderr_null = true;
770         } else if (!strcmp(&argv[i][skip], "&1")) {
771             context->stderr_stdout = true;
772         } else {
773             // stderr file redirections are not supported
774             fprintf(context->stderr_stdout ? stdout : stderr,
775                     "stderr redirection to file %s unsupported, skipping\n",
776                     &argv[i][skip]);
777         }
778         // Only the first one
779         break;
780     }
781 
782     const char* filename = nullptr;
783     for (int i = 0; i < argc; ++i) {
784         // Simulate shell stdout redirect parsing
785         if (argv[i][0] != '>') continue;
786 
787         // Append to file not implemented, just open file
788         filename = &argv[i][(argv[i][1] == '>') + 1];
789         // Only the first one
790         break;
791     }
792 
793     // Deal with setting up file descriptors and FILE pointers
794     if (context->error_fd >= 0) {  // Is an error file descriptor supplied?
795         if (context->error_fd == context->output_fd) {
796             context->stderr_stdout = true;
797         } else if (context->stderr_null) {  // redirection told us to close it
798             close(context->error_fd);
799             context->error_fd = -1;
800         } else {  // All Ok, convert error to a FILE pointer
801             context->error = fdopen(context->error_fd, "web");
802             if (!context->error) {
803                 context->retval = -errno;
804                 fprintf(context->stderr_stdout ? stdout : stderr,
805                         "Failed to fdopen(error_fd=%d) %s\n", context->error_fd,
806                         strerror(errno));
807                 goto exit;
808             }
809         }
810     }
811     if (context->output_fd >= 0) {  // Is an output file descriptor supplied?
812         if (filename) {  // redirect to file, close supplied file descriptor.
813             close(context->output_fd);
814             context->output_fd = -1;
815         } else {  // All Ok, convert output to a FILE pointer
816             context->output = fdopen(context->output_fd, "web");
817             if (!context->output) {
818                 context->retval = -errno;
819                 fprintf(context->stderr_stdout ? stdout : context->error,
820                         "Failed to fdopen(output_fd=%d) %s\n",
821                         context->output_fd, strerror(errno));
822                 goto exit;
823             }
824         }
825     }
826     if (filename) {  // We supplied an output file redirected in command line
827         context->output = fopen(filename, "web");
828     }
829     // Deal with 2>&1
830     if (context->stderr_stdout) context->error = context->output;
831     // Deal with 2>/dev/null
832     if (context->stderr_null) {
833         context->error_fd = -1;
834         context->error = nullptr;
835     }
836     // Only happens if output=stdout or output=filename
837     if ((context->output_fd < 0) && context->output) {
838         context->output_fd = fileno(context->output);
839     }
840     // Only happens if error=stdout || error=stderr
841     if ((context->error_fd < 0) && context->error) {
842         context->error_fd = fileno(context->error);
843     }
844 
845     context->logformat = android_log_format_new();
846 
847     if (argc == 2 && !strcmp(argv[1], "--help")) {
848         show_help(context);
849         context->retval = EXIT_SUCCESS;
850         goto exit;
851     }
852 
853     // meant to catch comma-delimited values, but cast a wider
854     // net for stability dealing with possible mistaken inputs.
855     static const char delimiters[] = ",:; \t\n\r\f";
856 
857     struct getopt_context optctx;
858     INIT_GETOPT_CONTEXT(optctx);
859     optctx.opterr = !!context->error;
860     optctx.optstderr = context->error;
861 
862     for (;;) {
863         int ret;
864 
865         int option_index = 0;
866         // list of long-argument only strings for later comparison
867         static const char pid_str[] = "pid";
868         static const char debug_str[] = "debug";
869         static const char id_str[] = "id";
870         static const char wrap_str[] = "wrap";
871         static const char print_str[] = "print";
872         // clang-format off
873         static const struct option long_options[] = {
874           { "binary",        no_argument,       nullptr, 'B' },
875           { "buffer",        required_argument, nullptr, 'b' },
876           { "buffer-size",   optional_argument, nullptr, 'g' },
877           { "clear",         no_argument,       nullptr, 'c' },
878           { debug_str,       no_argument,       nullptr, 0 },
879           { "dividers",      no_argument,       nullptr, 'D' },
880           { "file",          required_argument, nullptr, 'f' },
881           { "format",        required_argument, nullptr, 'v' },
882           // hidden and undocumented reserved alias for --regex
883           { "grep",          required_argument, nullptr, 'e' },
884           // hidden and undocumented reserved alias for --max-count
885           { "head",          required_argument, nullptr, 'm' },
886           { "help",          no_argument,       nullptr, 'h' },
887           { id_str,          required_argument, nullptr, 0 },
888           { "last",          no_argument,       nullptr, 'L' },
889           { "max-count",     required_argument, nullptr, 'm' },
890           { pid_str,         required_argument, nullptr, 0 },
891           { print_str,       no_argument,       nullptr, 0 },
892           { "prune",         optional_argument, nullptr, 'p' },
893           { "regex",         required_argument, nullptr, 'e' },
894           { "rotate-count",  required_argument, nullptr, 'n' },
895           { "rotate-kbytes", required_argument, nullptr, 'r' },
896           { "statistics",    no_argument,       nullptr, 'S' },
897           // hidden and undocumented reserved alias for -t
898           { "tail",          required_argument, nullptr, 't' },
899           // support, but ignore and do not document, the optional argument
900           { wrap_str,        optional_argument, nullptr, 0 },
901           { nullptr,         0,                 nullptr, 0 }
902         };
903         // clang-format on
904 
905         ret = getopt_long_r(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:",
906                             long_options, &option_index, &optctx);
907         if (ret < 0) break;
908 
909         switch (ret) {
910             case 0:
911                 // only long options
912                 if (long_options[option_index].name == pid_str) {
913                     // ToDo: determine runtime PID_MAX?
914                     if (!getSizeTArg(optctx.optarg, &pid, 1)) {
915                         logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
916                                      long_options[option_index].name,
917                                      optctx.optarg);
918                         goto exit;
919                     }
920                     break;
921                 }
922                 if (long_options[option_index].name == wrap_str) {
923                     mode |= ANDROID_LOG_WRAP | ANDROID_LOG_RDONLY |
924                             ANDROID_LOG_NONBLOCK;
925                     // ToDo: implement API that supports setting a wrap timeout
926                     size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT;
927                     if (optctx.optarg &&
928                         !getSizeTArg(optctx.optarg, &dummy, 1)) {
929                         logcat_panic(context, HELP_TRUE, "%s %s out of range\n",
930                                      long_options[option_index].name,
931                                      optctx.optarg);
932                         goto exit;
933                     }
934                     if ((dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) &&
935                         context->error) {
936                         fprintf(context->error,
937                                 "WARNING: %s %u seconds, ignoring %zu\n",
938                                 long_options[option_index].name,
939                                 ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy);
940                     }
941                     break;
942                 }
943                 if (long_options[option_index].name == print_str) {
944                     context->printItAnyways = true;
945                     break;
946                 }
947                 if (long_options[option_index].name == debug_str) {
948                     context->debug = true;
949                     break;
950                 }
951                 if (long_options[option_index].name == id_str) {
952                     setId = (optctx.optarg && optctx.optarg[0]) ? optctx.optarg
953                                                                 : nullptr;
954                 }
955                 break;
956 
957             case 's':
958                 // default to all silent
959                 android_log_addFilterRule(context->logformat, "*:s");
960                 break;
961 
962             case 'c':
963                 clearLog = true;
964                 mode |= ANDROID_LOG_WRONLY;
965                 break;
966 
967             case 'L':
968                 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE |
969                         ANDROID_LOG_NONBLOCK;
970                 break;
971 
972             case 'd':
973                 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
974                 break;
975 
976             case 't':
977                 got_t = true;
978                 mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
979             // FALLTHRU
980             case 'T':
981                 if (strspn(optctx.optarg, "0123456789") !=
982                     strlen(optctx.optarg)) {
983                     char* cp = parseTime(tail_time, optctx.optarg);
984                     if (!cp) {
985                         logcat_panic(context, HELP_FALSE,
986                                      "-%c \"%s\" not in time format\n", ret,
987                                      optctx.optarg);
988                         goto exit;
989                     }
990                     if (*cp) {
991                         char c = *cp;
992                         *cp = '\0';
993                         if (context->error) {
994                             fprintf(
995                                 context->error,
996                                 "WARNING: -%c \"%s\"\"%c%s\" time truncated\n",
997                                 ret, optctx.optarg, c, cp + 1);
998                         }
999                         *cp = c;
1000                     }
1001                 } else {
1002                     if (!getSizeTArg(optctx.optarg, &tail_lines, 1)) {
1003                         if (context->error) {
1004                             fprintf(context->error,
1005                                     "WARNING: -%c %s invalid, setting to 1\n",
1006                                     ret, optctx.optarg);
1007                         }
1008                         tail_lines = 1;
1009                     }
1010                 }
1011                 break;
1012 
1013             case 'D':
1014                 printDividers = true;
1015                 break;
1016 
1017             case 'e':
1018                 context->regex = new pcrecpp::RE(optctx.optarg);
1019                 break;
1020 
1021             case 'm': {
1022                 if (!getSizeTArg(optctx.optarg, &context->maxCount)) {
1023                     logcat_panic(context, HELP_FALSE,
1024                                  "-%c \"%s\" isn't an "
1025                                  "integer greater than zero\n",
1026                                  ret, optctx.optarg);
1027                     goto exit;
1028                 }
1029             } break;
1030 
1031             case 'g':
1032                 if (!optctx.optarg) {
1033                     getLogSize = true;
1034                     break;
1035                 }
1036             // FALLTHRU
1037 
1038             case 'G': {
1039                 char* cp;
1040                 if (strtoll(optctx.optarg, &cp, 0) > 0) {
1041                     setLogSize = strtoll(optctx.optarg, &cp, 0);
1042                 } else {
1043                     setLogSize = 0;
1044                 }
1045 
1046                 switch (*cp) {
1047                     case 'g':
1048                     case 'G':
1049                         setLogSize *= 1024;
1050                     // FALLTHRU
1051                     case 'm':
1052                     case 'M':
1053                         setLogSize *= 1024;
1054                     // FALLTHRU
1055                     case 'k':
1056                     case 'K':
1057                         setLogSize *= 1024;
1058                     // FALLTHRU
1059                     case '\0':
1060                         break;
1061 
1062                     default:
1063                         setLogSize = 0;
1064                 }
1065 
1066                 if (!setLogSize) {
1067                     logcat_panic(context, HELP_FALSE,
1068                                  "ERROR: -G <num><multiplier>\n");
1069                     goto exit;
1070                 }
1071             } break;
1072 
1073             case 'p':
1074                 if (!optctx.optarg) {
1075                     getPruneList = true;
1076                     break;
1077                 }
1078             // FALLTHRU
1079 
1080             case 'P':
1081                 setPruneList = optctx.optarg;
1082                 break;
1083 
1084             case 'b': {
1085                 std::unique_ptr<char, void (*)(void*)> buffers(
1086                     strdup(optctx.optarg), free);
1087                 char* arg = buffers.get();
1088                 unsigned idMask = 0;
1089                 char* sv = nullptr;  // protect against -ENOMEM above
1090                 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1091                     if (!strcmp(arg, "default")) {
1092                         idMask |= (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) |
1093                                   (1 << LOG_ID_CRASH);
1094                     } else if (!strcmp(arg, "all")) {
1095                         allSelected = true;
1096                         idMask = (unsigned)-1;
1097                     } else {
1098                         log_id_t log_id = android_name_to_log_id(arg);
1099                         const char* name = android_log_id_to_name(log_id);
1100 
1101                         if (!!strcmp(name, arg)) {
1102                             logcat_panic(context, HELP_TRUE,
1103                                          "unknown buffer %s\n", arg);
1104                             goto exit;
1105                         }
1106                         if (log_id == LOG_ID_SECURITY) allSelected = false;
1107                         idMask |= (1 << log_id);
1108                     }
1109                     arg = nullptr;
1110                 }
1111 
1112                 for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) {
1113                     const char* name = android_log_id_to_name((log_id_t)i);
1114                     log_id_t log_id = android_name_to_log_id(name);
1115 
1116                     if (log_id != (log_id_t)i) continue;
1117                     if (!(idMask & (1 << i))) continue;
1118 
1119                     bool found = false;
1120                     for (dev = context->devices; dev; dev = dev->next) {
1121                         if (!strcmp(name, dev->device)) {
1122                             found = true;
1123                             break;
1124                         }
1125                         if (!dev->next) break;
1126                     }
1127                     if (found) continue;
1128 
1129                     bool binary = !strcmp(name, "events") ||
1130                                   !strcmp(name, "security") ||
1131                                   !strcmp(name, "stats");
1132                     log_device_t* d = new log_device_t(name, binary);
1133 
1134                     if (dev) {
1135                         dev->next = d;
1136                         dev = d;
1137                     } else {
1138                         context->devices = dev = d;
1139                     }
1140                     context->devCount++;
1141                 }
1142             } break;
1143 
1144             case 'B':
1145                 context->printBinary = 1;
1146                 break;
1147 
1148             case 'f':
1149                 if ((tail_time == log_time::EPOCH) && !tail_lines) {
1150                     tail_time = lastLogTime(optctx.optarg);
1151                 }
1152                 // redirect output to a file
1153                 context->outputFileName = optctx.optarg;
1154                 break;
1155 
1156             case 'r':
1157                 if (!getSizeTArg(optctx.optarg, &context->logRotateSizeKBytes,
1158                                  1)) {
1159                     logcat_panic(context, HELP_TRUE,
1160                                  "Invalid parameter \"%s\" to -r\n",
1161                                  optctx.optarg);
1162                     goto exit;
1163                 }
1164                 break;
1165 
1166             case 'n':
1167                 if (!getSizeTArg(optctx.optarg, &context->maxRotatedLogs, 1)) {
1168                     logcat_panic(context, HELP_TRUE,
1169                                  "Invalid parameter \"%s\" to -n\n",
1170                                  optctx.optarg);
1171                     goto exit;
1172                 }
1173                 break;
1174 
1175             case 'v': {
1176                 if (!strcmp(optctx.optarg, "help") ||
1177                     !strcmp(optctx.optarg, "--help")) {
1178                     show_format_help(context);
1179                     context->retval = EXIT_SUCCESS;
1180                     goto exit;
1181                 }
1182                 std::unique_ptr<char, void (*)(void*)> formats(
1183                     strdup(optctx.optarg), free);
1184                 char* arg = formats.get();
1185                 char* sv = nullptr;  // protect against -ENOMEM above
1186                 while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1187                     err = setLogFormat(context, arg);
1188                     if (err < 0) {
1189                         logcat_panic(context, HELP_FORMAT,
1190                                      "Invalid parameter \"%s\" to -v\n", arg);
1191                         goto exit;
1192                     }
1193                     arg = nullptr;
1194                     if (err) hasSetLogFormat = true;
1195                 }
1196             } break;
1197 
1198             case 'Q':
1199 #define LOGCAT_FILTER "androidboot.logcat="
1200 #define CONSOLE_PIPE_OPTION "androidboot.consolepipe="
1201 #define CONSOLE_OPTION "androidboot.console="
1202 #define QEMU_PROPERTY "ro.kernel.qemu"
1203 #define QEMU_CMDLINE "qemu.cmdline"
1204                 // This is a *hidden* option used to start a version of logcat
1205                 // in an emulated device only.  It basically looks for
1206                 // androidboot.logcat= on the kernel command line.  If
1207                 // something is found, it extracts a log filter and uses it to
1208                 // run the program. The logcat output will go to consolepipe if
1209                 // androiboot.consolepipe (e.g. qemu_pipe) is given, otherwise,
1210                 // it goes to androidboot.console (e.g. tty)
1211                 {
1212                     // if not in emulator, exit quietly
1213                     if (false == android::base::GetBoolProperty(QEMU_PROPERTY, false)) {
1214                         context->retval = EXIT_SUCCESS;
1215                         goto exit;
1216                     }
1217 
1218                     std::string cmdline = android::base::GetProperty(QEMU_CMDLINE, "");
1219                     if (cmdline.empty()) {
1220                         android::base::ReadFileToString("/proc/cmdline", &cmdline);
1221                     }
1222 
1223                     const char* logcatFilter = strstr(cmdline.c_str(), LOGCAT_FILTER);
1224                     // if nothing found or invalid filters, exit quietly
1225                     if (!logcatFilter) {
1226                         context->retval = EXIT_SUCCESS;
1227                         goto exit;
1228                     }
1229 
1230                     const char* p = logcatFilter + strlen(LOGCAT_FILTER);
1231                     const char* q = strpbrk(p, " \t\n\r");
1232                     if (!q) q = p + strlen(p);
1233                     forceFilters = std::string(p, q);
1234 
1235                     // redirect our output to the emulator console pipe or console
1236                     const char* consolePipe =
1237                         strstr(cmdline.c_str(), CONSOLE_PIPE_OPTION);
1238                     const char* console =
1239                         strstr(cmdline.c_str(), CONSOLE_OPTION);
1240 
1241                     if (consolePipe) {
1242                         p = consolePipe + strlen(CONSOLE_PIPE_OPTION);
1243                     } else if (console) {
1244                         p = console + strlen(CONSOLE_OPTION);
1245                     } else {
1246                         context->retval = EXIT_FAILURE;
1247                         goto exit;
1248                     }
1249 
1250                     q = strpbrk(p, " \t\n\r");
1251                     int len = q ? q - p : strlen(p);
1252                     std::string devname = "/dev/" + std::string(p, len);
1253                     std::string pipePurpose("pipe:logcat");
1254                     if (consolePipe) {
1255                         // example: "qemu_pipe,pipe:logcat"
1256                         // upon opening of /dev/qemu_pipe, the "pipe:logcat"
1257                         // string with trailing '\0' should be written to the fd
1258                         size_t pos = devname.find(',');
1259                         if (pos != std::string::npos) {
1260                             pipePurpose = devname.substr(pos + 1);
1261                             devname = devname.substr(0, pos);
1262                         }
1263                     }
1264                     cmdline.erase();
1265 
1266                     if (context->error) {
1267                         fprintf(context->error, "logcat using %s\n",
1268                                 devname.c_str());
1269                     }
1270 
1271                     FILE* fp = fopen(devname.c_str(), "web");
1272                     devname.erase();
1273                     if (!fp) break;
1274 
1275                     if (consolePipe) {
1276                         // need the trailing '\0'
1277                         if(!android::base::WriteFully(fileno(fp), pipePurpose.c_str(),
1278                                     pipePurpose.size() + 1)) {
1279                             fclose(fp);
1280                             context->retval = EXIT_FAILURE;
1281                             goto exit;
1282                         }
1283                     }
1284 
1285                     // close output and error channels, replace with console
1286                     android::close_output(context);
1287                     android::close_error(context);
1288                     context->stderr_stdout = true;
1289                     context->output = fp;
1290                     context->output_fd = fileno(fp);
1291                     if (context->stderr_null) break;
1292                     context->stderr_stdout = true;
1293                     context->error = fp;
1294                     context->error_fd = fileno(fp);
1295                 }
1296                 break;
1297 
1298             case 'S':
1299                 printStatistics = true;
1300                 break;
1301 
1302             case ':':
1303                 logcat_panic(context, HELP_TRUE,
1304                              "Option -%c needs an argument\n", optctx.optopt);
1305                 goto exit;
1306 
1307             case 'h':
1308                 show_help(context);
1309                 show_format_help(context);
1310                 goto exit;
1311 
1312             default:
1313                 logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n",
1314                              optctx.optopt);
1315                 goto exit;
1316         }
1317     }
1318 
1319     if (context->maxCount && got_t) {
1320         logcat_panic(context, HELP_TRUE,
1321                      "Cannot use -m (--max-count) and -t together\n");
1322         goto exit;
1323     }
1324     if (context->printItAnyways && (!context->regex || !context->maxCount)) {
1325         // One day it would be nice if --print -v color and --regex <expr>
1326         // could play with each other and show regex highlighted content.
1327         // clang-format off
1328         if (context->error) {
1329             fprintf(context->error, "WARNING: "
1330                             "--print ignored, to be used in combination with\n"
1331                                 "         "
1332                             "--regex <expr> and --max-count <N>\n");
1333         }
1334         context->printItAnyways = false;
1335     }
1336 
1337     if (!context->devices) {
1338         dev = context->devices = new log_device_t("main", false);
1339         context->devCount = 1;
1340         if (android_name_to_log_id("system") == LOG_ID_SYSTEM) {
1341             dev = dev->next = new log_device_t("system", false);
1342             context->devCount++;
1343         }
1344         if (android_name_to_log_id("crash") == LOG_ID_CRASH) {
1345             dev = dev->next = new log_device_t("crash", false);
1346             context->devCount++;
1347         }
1348     }
1349 
1350     if (!!context->logRotateSizeKBytes && !context->outputFileName) {
1351         logcat_panic(context, HELP_TRUE, "-r requires -f as well\n");
1352         goto exit;
1353     }
1354 
1355     if (!!setId) {
1356         if (!context->outputFileName) {
1357             logcat_panic(context, HELP_TRUE,
1358                          "--id='%s' requires -f as well\n", setId);
1359             goto exit;
1360         }
1361 
1362         std::string file_name = android::base::StringPrintf(
1363                                         "%s.id", context->outputFileName);
1364         std::string file;
1365         bool file_ok = android::base::ReadFileToString(file_name, &file);
1366         android::base::WriteStringToFile(setId, file_name, S_IRUSR | S_IWUSR,
1367                                          getuid(), getgid());
1368         if (!file_ok || !file.compare(setId)) setId = nullptr;
1369     }
1370 
1371     if (!hasSetLogFormat) {
1372         const char* logFormat = android::getenv(context, "ANDROID_PRINTF_LOG");
1373 
1374         if (!!logFormat) {
1375             std::unique_ptr<char, void (*)(void*)> formats(strdup(logFormat),
1376                                                            free);
1377             char* sv = nullptr;  // protect against -ENOMEM above
1378             char* arg = formats.get();
1379             while (!!(arg = strtok_r(arg, delimiters, &sv))) {
1380                 err = setLogFormat(context, arg);
1381                 // environment should not cause crash of logcat
1382                 if ((err < 0) && context->error) {
1383                     fprintf(context->error,
1384                             "invalid format in ANDROID_PRINTF_LOG '%s'\n", arg);
1385                 }
1386                 arg = nullptr;
1387                 if (err > 0) hasSetLogFormat = true;
1388             }
1389         }
1390         if (!hasSetLogFormat) {
1391             setLogFormat(context, "threadtime");
1392         }
1393     }
1394 
1395     if (forceFilters.size()) {
1396         err = android_log_addFilterString(context->logformat,
1397                                           forceFilters.c_str());
1398         if (err < 0) {
1399             logcat_panic(context, HELP_FALSE,
1400                          "Invalid filter expression in logcat args\n");
1401             goto exit;
1402         }
1403     } else if (argc == optctx.optind) {
1404         // Add from environment variable
1405         const char* env_tags_orig = android::getenv(context, "ANDROID_LOG_TAGS");
1406 
1407         if (!!env_tags_orig) {
1408             err = android_log_addFilterString(context->logformat,
1409                                               env_tags_orig);
1410 
1411             if (err < 0) {
1412                 logcat_panic(context, HELP_TRUE,
1413                             "Invalid filter expression in ANDROID_LOG_TAGS\n");
1414                 goto exit;
1415             }
1416         }
1417     } else {
1418         // Add from commandline
1419         for (int i = optctx.optind ; i < argc ; i++) {
1420             // skip stderr redirections of _all_ kinds
1421             if ((argv[i][0] == '2') && (argv[i][1] == '>')) continue;
1422             // skip stdout redirections of _all_ kinds
1423             if (argv[i][0] == '>') continue;
1424 
1425             err = android_log_addFilterString(context->logformat, argv[i]);
1426             if (err < 0) {
1427                 logcat_panic(context, HELP_TRUE,
1428                              "Invalid filter expression '%s'\n", argv[i]);
1429                 goto exit;
1430             }
1431         }
1432     }
1433 
1434     dev = context->devices;
1435     if (tail_time != log_time::EPOCH) {
1436         logger_list = android_logger_list_alloc_time(mode, tail_time, pid);
1437     } else {
1438         logger_list = android_logger_list_alloc(mode, tail_lines, pid);
1439     }
1440     // We have three orthogonal actions below to clear, set log size and
1441     // get log size. All sharing the same iteration loop.
1442     while (dev) {
1443         dev->logger_list = logger_list;
1444         dev->logger = android_logger_open(logger_list,
1445                                           android_name_to_log_id(dev->device));
1446         if (!dev->logger) {
1447             reportErrorName(&openDeviceFail, dev->device, allSelected);
1448             dev = dev->next;
1449             continue;
1450         }
1451 
1452         if (clearLog || setId) {
1453             if (context->outputFileName) {
1454                 int maxRotationCountDigits =
1455                     (context->maxRotatedLogs > 0) ?
1456                         (int)(floor(log10(context->maxRotatedLogs) + 1)) :
1457                         0;
1458 
1459                 for (int i = context->maxRotatedLogs ; i >= 0 ; --i) {
1460                     std::string file;
1461 
1462                     if (!i) {
1463                         file = android::base::StringPrintf(
1464                             "%s", context->outputFileName);
1465                     } else {
1466                         file = android::base::StringPrintf("%s.%.*d",
1467                             context->outputFileName, maxRotationCountDigits, i);
1468                     }
1469 
1470                     if (!file.length()) {
1471                         perror("while clearing log files");
1472                         reportErrorName(&clearFail, dev->device, allSelected);
1473                         break;
1474                     }
1475 
1476                     err = unlink(file.c_str());
1477 
1478                     if (err < 0 && errno != ENOENT && !clearFail) {
1479                         perror("while clearing log files");
1480                         reportErrorName(&clearFail, dev->device, allSelected);
1481                     }
1482                 }
1483             } else if (android_logger_clear(dev->logger)) {
1484                 reportErrorName(&clearFail, dev->device, allSelected);
1485             }
1486         }
1487 
1488         if (setLogSize) {
1489             if (android_logger_set_log_size(dev->logger, setLogSize)) {
1490                 reportErrorName(&setSizeFail, dev->device, allSelected);
1491             }
1492         }
1493 
1494         if (getLogSize) {
1495             long size = android_logger_get_log_size(dev->logger);
1496             long readable = android_logger_get_log_readable_size(dev->logger);
1497 
1498             if ((size < 0) || (readable < 0)) {
1499                 reportErrorName(&getSizeFail, dev->device, allSelected);
1500             } else {
1501                 std::string str = android::base::StringPrintf(
1502                        "%s: ring buffer is %ld%sb (%ld%sb consumed),"
1503                          " max entry is %db, max payload is %db\n",
1504                        dev->device,
1505                        value_of_size(size), multiplier_of_size(size),
1506                        value_of_size(readable), multiplier_of_size(readable),
1507                        (int)LOGGER_ENTRY_MAX_LEN,
1508                        (int)LOGGER_ENTRY_MAX_PAYLOAD);
1509                 TEMP_FAILURE_RETRY(write(context->output_fd,
1510                                          str.data(), str.length()));
1511             }
1512         }
1513 
1514         dev = dev->next;
1515     }
1516 
1517     context->retval = EXIT_SUCCESS;
1518 
1519     // report any errors in the above loop and exit
1520     if (openDeviceFail) {
1521         logcat_panic(context, HELP_FALSE,
1522                      "Unable to open log device '%s'\n", openDeviceFail);
1523         goto close;
1524     }
1525     if (clearFail) {
1526         logcat_panic(context, HELP_FALSE,
1527                      "failed to clear the '%s' log\n", clearFail);
1528         goto close;
1529     }
1530     if (setSizeFail) {
1531         logcat_panic(context, HELP_FALSE,
1532                      "failed to set the '%s' log size\n", setSizeFail);
1533         goto close;
1534     }
1535     if (getSizeFail) {
1536         logcat_panic(context, HELP_FALSE,
1537                      "failed to get the readable '%s' log size", getSizeFail);
1538         goto close;
1539     }
1540 
1541     if (setPruneList) {
1542         size_t len = strlen(setPruneList);
1543         // extra 32 bytes are needed by android_logger_set_prune_list
1544         size_t bLen = len + 32;
1545         char* buf = nullptr;
1546         if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) {
1547             buf[len] = '\0';
1548             if (android_logger_set_prune_list(logger_list, buf, bLen)) {
1549                 logcat_panic(context, HELP_FALSE,
1550                              "failed to set the prune list");
1551             }
1552             free(buf);
1553         } else {
1554             logcat_panic(context, HELP_FALSE,
1555                          "failed to set the prune list (alloc)");
1556         }
1557         goto close;
1558     }
1559 
1560     if (printStatistics || getPruneList) {
1561         size_t len = 8192;
1562         char* buf;
1563 
1564         for (int retry = 32; (retry >= 0) && ((buf = new char[len]));
1565              delete[] buf, buf = nullptr, --retry) {
1566             if (getPruneList) {
1567                 android_logger_get_prune_list(logger_list, buf, len);
1568             } else {
1569                 android_logger_get_statistics(logger_list, buf, len);
1570             }
1571             buf[len - 1] = '\0';
1572             if (atol(buf) < 3) {
1573                 delete[] buf;
1574                 buf = nullptr;
1575                 break;
1576             }
1577             size_t ret = atol(buf) + 1;
1578             if (ret <= len) {
1579                 len = ret;
1580                 break;
1581             }
1582             len = ret;
1583         }
1584 
1585         if (!buf) {
1586             logcat_panic(context, HELP_FALSE, "failed to read data");
1587             goto close;
1588         }
1589 
1590         // remove trailing FF
1591         char* cp = buf + len - 1;
1592         *cp = '\0';
1593         bool truncated = *--cp != '\f';
1594         if (!truncated) *cp = '\0';
1595 
1596         // squash out the byte count
1597         cp = buf;
1598         if (!truncated) {
1599             while (isdigit(*cp)) ++cp;
1600             if (*cp == '\n') ++cp;
1601         }
1602 
1603         len = strlen(cp);
1604         TEMP_FAILURE_RETRY(write(context->output_fd, cp, len));
1605         delete[] buf;
1606         goto close;
1607     }
1608 
1609     if (getLogSize || setLogSize || clearLog) goto close;
1610 
1611     setupOutputAndSchedulingPolicy(context, !(mode & ANDROID_LOG_NONBLOCK));
1612     if (context->stop) goto close;
1613 
1614     // LOG_EVENT_INT(10, 12345);
1615     // LOG_EVENT_LONG(11, 0x1122334455667788LL);
1616     // LOG_EVENT_STRING(0, "whassup, doc?");
1617 
1618     dev = nullptr;
1619 
1620     while (!context->stop &&
1621            (!context->maxCount || (context->printCount < context->maxCount))) {
1622         struct log_msg log_msg;
1623         int ret = android_logger_list_read(logger_list, &log_msg);
1624         if (!ret) {
1625             logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1626             break;
1627         }
1628 
1629         if (ret < 0) {
1630             if (ret == -EAGAIN) break;
1631 
1632             if (ret == -EIO) {
1633                 logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n");
1634                 break;
1635             }
1636             if (ret == -EINVAL) {
1637                 logcat_panic(context, HELP_FALSE, "read: unexpected length.\n");
1638                 break;
1639             }
1640             logcat_panic(context, HELP_FALSE, "logcat read failure\n");
1641             break;
1642         }
1643 
1644         log_device_t* d;
1645         for (d = context->devices; d; d = d->next) {
1646             if (android_name_to_log_id(d->device) == log_msg.id()) break;
1647         }
1648         if (!d) {
1649             context->devCount = 2; // set to Multiple
1650             d = &unexpected;
1651             d->binary = log_msg.id() == LOG_ID_EVENTS;
1652         }
1653 
1654         if (dev != d) {
1655             dev = d;
1656             maybePrintStart(context, dev, printDividers);
1657             if (context->stop) break;
1658         }
1659         if (context->printBinary) {
1660             printBinary(context, &log_msg);
1661         } else {
1662             processBuffer(context, dev, &log_msg);
1663         }
1664     }
1665 
1666 close:
1667     // Short and sweet. Implemented generic version in android_logcat_destroy.
1668     while (!!(dev = context->devices)) {
1669         context->devices = dev->next;
1670         delete dev;
1671     }
1672     android_logger_list_free(logger_list);
1673 
1674 exit:
1675     // close write end of pipe to help things along
1676     if (context->output_fd == context->fds[1]) {
1677         android::close_output(context);
1678     }
1679     if (context->error_fd == context->fds[1]) {
1680         android::close_error(context);
1681     }
1682     if (context->fds[1] >= 0) {
1683         // NB: should be closed by the above
1684         int save_errno = errno;
1685         close(context->fds[1]);
1686         errno = save_errno;
1687         context->fds[1] = -1;
1688     }
1689     context->thread_stopped = true;
1690     return context->retval;
1691 }
1692 
1693 // Can block
android_logcat_run_command(android_logcat_context ctx,int output,int error,int argc,char * const * argv,char * const * envp)1694 int android_logcat_run_command(android_logcat_context ctx,
1695                                int output, int error,
1696                                int argc, char* const* argv,
1697                                char* const* envp) {
1698     android_logcat_context_internal* context = ctx;
1699 
1700     context->output_fd = output;
1701     context->error_fd = error;
1702     context->argc = argc;
1703     context->argv = argv;
1704     context->envp = envp;
1705     context->stop = false;
1706     context->thread_stopped = false;
1707     return __logcat(context);
1708 }
1709 
1710 // starts a thread, opens a pipe, returns reading end.
android_logcat_run_command_thread(android_logcat_context ctx,int argc,char * const * argv,char * const * envp)1711 int android_logcat_run_command_thread(android_logcat_context ctx,
1712                                       int argc, char* const* argv,
1713                                       char* const* envp) {
1714     android_logcat_context_internal* context = ctx;
1715 
1716     int save_errno = EBUSY;
1717     if ((context->fds[0] >= 0) || (context->fds[1] >= 0)) goto exit;
1718 
1719     if (pipe(context->fds) < 0) {
1720         save_errno = errno;
1721         goto exit;
1722     }
1723 
1724     pthread_attr_t attr;
1725     if (pthread_attr_init(&attr)) {
1726         save_errno = errno;
1727         goto close_exit;
1728     }
1729 
1730     struct sched_param param;
1731     memset(&param, 0, sizeof(param));
1732     pthread_attr_setschedparam(&attr, &param);
1733     pthread_attr_setschedpolicy(&attr, SCHED_BATCH);
1734     if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) {
1735         save_errno = errno;
1736         goto pthread_attr_exit;
1737     }
1738 
1739     context->stop = false;
1740     context->thread_stopped = false;
1741     context->output_fd = context->fds[1];
1742     // save off arguments so they remain while thread is active.
1743     for (int i = 0; i < argc; ++i) {
1744         context->args.push_back(std::string(argv[i]));
1745     }
1746     // save off environment so they remain while thread is active.
1747     if (envp) for (size_t i = 0; envp[i]; ++i) {
1748         context->envs.push_back(std::string(envp[i]));
1749     }
1750 
1751     for (auto& str : context->args) {
1752         context->argv_hold.push_back(str.c_str());
1753     }
1754     context->argv_hold.push_back(nullptr);
1755     for (auto& str : context->envs) {
1756         context->envp_hold.push_back(str.c_str());
1757     }
1758     context->envp_hold.push_back(nullptr);
1759 
1760     context->argc = context->argv_hold.size() - 1;
1761     context->argv = (char* const*)&context->argv_hold[0];
1762     context->envp = (char* const*)&context->envp_hold[0];
1763 
1764 #ifdef DEBUG
1765     fprintf(stderr, "argv[%d] = {", context->argc);
1766     for (auto str : context->argv_hold) {
1767         fprintf(stderr, " \"%s\"", str ?: "nullptr");
1768     }
1769     fprintf(stderr, " }\n");
1770     fflush(stderr);
1771 #endif
1772     context->retval = EXIT_SUCCESS;
1773     if (pthread_create(&context->thr, &attr,
1774                        (void*(*)(void*))__logcat, context)) {
1775         save_errno = errno;
1776         goto argv_exit;
1777     }
1778     pthread_attr_destroy(&attr);
1779 
1780     return context->fds[0];
1781 
1782 argv_exit:
1783     context->argv_hold.clear();
1784     context->args.clear();
1785     context->envp_hold.clear();
1786     context->envs.clear();
1787 pthread_attr_exit:
1788     pthread_attr_destroy(&attr);
1789 close_exit:
1790     close(context->fds[0]);
1791     context->fds[0] = -1;
1792     close(context->fds[1]);
1793     context->fds[1] = -1;
1794 exit:
1795     errno = save_errno;
1796     context->stop = true;
1797     context->thread_stopped = true;
1798     context->retval = EXIT_FAILURE;
1799     return -1;
1800 }
1801 
1802 // test if the thread is still doing 'stuff'
android_logcat_run_command_thread_running(android_logcat_context ctx)1803 int android_logcat_run_command_thread_running(android_logcat_context ctx) {
1804     android_logcat_context_internal* context = ctx;
1805 
1806     return context->thread_stopped == false;
1807 }
1808 
1809 // Finished with context
android_logcat_destroy(android_logcat_context * ctx)1810 int android_logcat_destroy(android_logcat_context* ctx) {
1811     android_logcat_context_internal* context = *ctx;
1812 
1813     if (!context) return -EBADF;
1814 
1815     *ctx = nullptr;
1816 
1817     context->stop = true;
1818 
1819     while (context->thread_stopped == false) {
1820         // Makes me sad, replace thread_stopped with semaphore.  Short lived.
1821         sched_yield();
1822     }
1823 
1824     delete context->regex;
1825     context->argv_hold.clear();
1826     context->args.clear();
1827     context->envp_hold.clear();
1828     context->envs.clear();
1829     if (context->fds[0] >= 0) {
1830         close(context->fds[0]);
1831         context->fds[0] = -1;
1832     }
1833     android::close_output(context);
1834     android::close_error(context);
1835 
1836     if (context->fds[1] >= 0) {
1837         // NB: this should be closed by close_output, but just in case...
1838         close(context->fds[1]);
1839         context->fds[1] = -1;
1840     }
1841 
1842     android_closeEventTagMap(context->eventTagMap);
1843 
1844     // generic cleanup of devices list to handle all possible dirty cases
1845     log_device_t* dev;
1846     while (!!(dev = context->devices)) {
1847         struct logger_list* logger_list = dev->logger_list;
1848         if (logger_list) {
1849             for (log_device_t* d = dev; d; d = d->next) {
1850                 if (d->logger_list == logger_list) d->logger_list = nullptr;
1851             }
1852             android_logger_list_free(logger_list);
1853         }
1854         context->devices = dev->next;
1855         delete dev;
1856     }
1857 
1858     int retval = context->retval;
1859 
1860     free(context);
1861 
1862     return retval;
1863 }
1864