22
22
#include <unistd.h>
23
23
#include <errno.h>
24
24
#include <fcntl.h>
25
+ #include <libgen.h>
25
26
26
27
#include "private/android_filesystem_config.h"
27
28
#include "cutils/log.h"
@@ -34,13 +35,13 @@ void fatal(const char *msg) {
34
35
35
36
void usage () {
36
37
fatal (
37
- "Usage: logwrapper [-x ] BINARY [ARGS ...]\n"
38
+ "Usage: logwrapper [-d ] BINARY [ARGS ...]\n"
38
39
"\n"
39
40
"Forks and executes BINARY ARGS, redirecting stdout and stderr to\n"
40
41
"the Android logging system. Tag is set to BINARY, priority is\n"
41
42
"always LOG_INFO.\n"
42
43
"\n"
43
- "-x : Causes logwrapper to SIGSEGV when BINARY terminates\n"
44
+ "-d : Causes logwrapper to SIGSEGV when BINARY terminates\n"
44
45
" fault address is set to the status of wait()\n" );
45
46
}
46
47
@@ -51,6 +52,10 @@ void parent(const char *tag, int seg_fault_on_exit, int parent_read) {
51
52
int a = 0 ; // start index of unprocessed data
52
53
int b = 0 ; // end index of unprocessed data
53
54
int sz ;
55
+
56
+ char * btag = basename (tag );
57
+ if (!btag ) btag = (char * ) tag ;
58
+
54
59
while ((sz = read (parent_read , & buffer [b ], sizeof (buffer ) - 1 - b )) > 0 ) {
55
60
56
61
sz += b ;
@@ -60,15 +65,15 @@ void parent(const char *tag, int seg_fault_on_exit, int parent_read) {
60
65
buffer [b ] = '\0' ;
61
66
} else if (buffer [b ] == '\n' ) {
62
67
buffer [b ] = '\0' ;
63
- ALOG (LOG_INFO , tag , "%s" , & buffer [a ]);
68
+ ALOG (LOG_INFO , btag , "%s" , & buffer [a ]);
64
69
a = b + 1 ;
65
70
}
66
71
}
67
72
68
73
if (a == 0 && b == sizeof (buffer ) - 1 ) {
69
74
// buffer is full, flush
70
75
buffer [b ] = '\0' ;
71
- ALOG (LOG_INFO , tag , "%s" , & buffer [a ]);
76
+ ALOG (LOG_INFO , btag , "%s" , & buffer [a ]);
72
77
b = 0 ;
73
78
} else if (a != b ) {
74
79
// Keep left-overs
@@ -84,11 +89,11 @@ void parent(const char *tag, int seg_fault_on_exit, int parent_read) {
84
89
// Flush remaining data
85
90
if (a != b ) {
86
91
buffer [b ] = '\0' ;
87
- ALOG (LOG_INFO , tag , "%s" , & buffer [a ]);
92
+ ALOG (LOG_INFO , btag , "%s" , & buffer [a ]);
88
93
}
89
94
status = 0xAAAA ;
90
95
if (wait (& status ) != -1 ) { // Wait for child
91
- if (WIFEXITED (status ))
96
+ if (WIFEXITED (status ) && WEXITSTATUS ( status ) )
92
97
ALOG (LOG_INFO , "logwrapper" , "%s terminated by exit(%d)" , tag ,
93
98
WEXITSTATUS (status ));
94
99
else if (WIFSIGNALED (status ))
0 commit comments