am afe5bbef: am a67914e4: Merge "Fix top(1) SIGPIPE behavior."

* commit 'afe5bbefac2cd09a2a2e46777f6bba7afa9c2966':
  Fix top(1) SIGPIPE behavior.
This commit is contained in:
Elliott Hughes 2014-04-21 13:45:30 +00:00 committed by Android Git Automerger
commit 0888e52fa8
2 changed files with 17 additions and 12 deletions

View file

@ -95,6 +95,7 @@ LOCAL_SRC_FILES := \
LOCAL_C_INCLUDES := bionic/libc/bionic LOCAL_C_INCLUDES := bionic/libc/bionic
LOCAL_CFLAGS += \ LOCAL_CFLAGS += \
-std=gnu99 \
-Wno-unused-parameter \ -Wno-unused-parameter \
-include bsd-compatibility.h \ -include bsd-compatibility.h \

View file

@ -9,7 +9,7 @@
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in * notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the * the documentation and/or other materials provided with the
* distribution. * distribution.
* * Neither the name of Google, Inc. nor the names of its contributors * * Neither the name of Google, Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this * may be used to endorse or promote products derived from this
@ -22,7 +22,7 @@
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
@ -108,16 +108,20 @@ static int proc_thr_cmp(const void *a, const void *b);
static int numcmp(long long a, long long b); static int numcmp(long long a, long long b);
static void usage(char *cmd); static void usage(char *cmd);
int top_main(int argc, char *argv[]) { static void exit_top(int signal) {
int i; exit(EXIT_FAILURE);
}
int top_main(int argc, char *argv[]) {
num_used_procs = num_free_procs = 0; num_used_procs = num_free_procs = 0;
signal(SIGPIPE, exit_top);
max_procs = 0; max_procs = 0;
delay = 3; delay = 3;
iterations = -1; iterations = -1;
proc_cmp = &proc_cpu_cmp; proc_cmp = &proc_cpu_cmp;
for (i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-m")) { if (!strcmp(argv[i], "-m")) {
if (i + 1 >= argc) { if (i + 1 >= argc) {
fprintf(stderr, "Option -m expects an argument.\n"); fprintf(stderr, "Option -m expects an argument.\n");
@ -249,9 +253,9 @@ static void read_procs(void) {
continue; continue;
pid = atoi(pid_dir->d_name); pid = atoi(pid_dir->d_name);
struct proc_info cur_proc; struct proc_info cur_proc;
if (!threads) { if (!threads) {
proc = alloc_proc(); proc = alloc_proc();
@ -275,7 +279,7 @@ static void read_procs(void) {
sprintf(filename, "/proc/%d/status", pid); sprintf(filename, "/proc/%d/status", pid);
read_status(filename, &cur_proc); read_status(filename, &cur_proc);
proc = NULL; proc = NULL;
} }
@ -310,7 +314,7 @@ static void read_procs(void) {
} }
closedir(task_dir); closedir(task_dir);
if (!threads) if (!threads)
add_proc(proc_num++, proc); add_proc(proc_num++, proc);
} }
@ -339,7 +343,7 @@ static int read_stat(char *filename, struct proc_info *proc) {
*open_paren = *close_paren = '\0'; *open_paren = *close_paren = '\0';
strncpy(proc->tname, open_paren + 1, THREAD_NAME_LEN); strncpy(proc->tname, open_paren + 1, THREAD_NAME_LEN);
proc->tname[THREAD_NAME_LEN-1] = 0; proc->tname[THREAD_NAME_LEN-1] = 0;
/* Scan rest of string. */ /* Scan rest of string. */
sscanf(close_paren + 1, " %c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " sscanf(close_paren + 1, " %c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
"%lu %lu %*d %*d %*d %*d %*d %*d %*d %lu %ld " "%lu %lu %*d %*d %*d %*d %*d %*d %*d %lu %ld "
@ -452,7 +456,7 @@ static void print_procs(void) {
new_cpu.sirqtime - old_cpu.sirqtime, new_cpu.sirqtime - old_cpu.sirqtime,
total_delta_time); total_delta_time);
printf("\n"); printf("\n");
if (!threads) if (!threads)
printf("%5s %2s %4s %1s %5s %7s %7s %3s %-8s %s\n", "PID", "PR", "CPU%", "S", "#THR", "VSS", "RSS", "PCY", "UID", "Name"); printf("%5s %2s %4s %1s %5s %7s %7s %3s %-8s %s\n", "PID", "PR", "CPU%", "S", "#THR", "VSS", "RSS", "PCY", "UID", "Name");
else else
printf("%5s %5s %2s %4s %1s %7s %7s %3s %-8s %-15s %s\n", "PID", "TID", "PR", "CPU%", "S", "VSS", "RSS", "PCY", "UID", "Thread", "Proc"); printf("%5s %5s %2s %4s %1s %7s %7s %3s %-8s %-15s %s\n", "PID", "TID", "PR", "CPU%", "S", "VSS", "RSS", "PCY", "UID", "Thread", "Proc");
@ -476,7 +480,7 @@ static void print_procs(void) {
snprintf(group_buf, 20, "%d", proc->gid); snprintf(group_buf, 20, "%d", proc->gid);
group_str = group_buf; group_str = group_buf;
} }
if (!threads) if (!threads)
printf("%5d %2d %3ld%% %c %5d %6ldK %6ldK %3s %-8.8s %s\n", proc->pid, proc->prs, proc->delta_time * 100 / total_delta_time, proc->state, proc->num_threads, printf("%5d %2d %3ld%% %c %5d %6ldK %6ldK %3s %-8.8s %s\n", proc->pid, proc->prs, proc->delta_time * 100 / total_delta_time, proc->state, proc->num_threads,
proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->name[0] != 0 ? proc->name : proc->tname); proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->name[0] != 0 ? proc->name : proc->tname);
else else