From 78e08066f272c2adb41a1c80a7d566bd8b31d607 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Thu, 28 Jan 2016 16:08:08 -0800 Subject: [PATCH] Always have ps output to stdout at least one line. There is at least one app that assumes that you will always get at least one line of output in stdout from ps. To fix this, move error output to stdout, and move the check of whether /proc can be opened until after the ps header is printed. Bug: 26554285 Change-Id: I6d9342aafd5c6f728735507cdd87a48a8e0373ac --- toolbox/ps.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/toolbox/ps.c b/toolbox/ps.c index ecc1c9f81..7e70c71c0 100644 --- a/toolbox/ps.c +++ b/toolbox/ps.c @@ -264,9 +264,6 @@ int ps_main(int argc, char **argv) int pidfilter = 0; int threads = 0; - d = opendir("/proc"); - if(d == 0) return -1; - while(argc > 1){ if(!strcmp(argv[1],"-t")) { threads = 1; @@ -287,7 +284,10 @@ int ps_main(int argc, char **argv) } else if(!strcmp(argv[1],"--ppid")) { ppid_filter = atoi(argv[2]); if (ppid_filter == 0) { - fprintf(stderr, "bad ppid '%s'\n", argv[2]); + /* Bug 26554285: Use printf because some apps require at least + * one line of output to stdout even for errors. + */ + printf("bad ppid '%s'\n", argv[2]); return 1; } argc--; @@ -295,7 +295,10 @@ int ps_main(int argc, char **argv) } else { pidfilter = atoi(argv[1]); if (pidfilter == 0) { - fprintf(stderr, "bad pid '%s'\n", argv[1]); + /* Bug 26554285: Use printf because some apps require at least + * one line of output to stdout even for errors. + */ + printf("bad pid '%s'\n", argv[1]); return 1; } } @@ -313,6 +316,9 @@ int ps_main(int argc, char **argv) (int) PC_WIDTH, "PC", (display_flags&SHOW_ABI)?"ABI " : ""); + d = opendir("/proc"); + if(d == 0) return -1; + while((de = readdir(d)) != 0){ if(isdigit(de->d_name[0])){ int pid = atoi(de->d_name);