PATCH: FEATURE_PS_LONG: A new 'l' option to ps:

Denys Vlasenko vda.linux at googlemail.com
Mon Sep 26 00:54:21 UTC 2011


On Tuesday 20 September 2011 20:37, Flemming Madsen wrote:
> 02-ps-long-output.patch
> ----------
> 
> FEATURE_PS_LONG: A new 'l' option to ps:
> 
>    PID  PPID USER   VSZ   RSS STAT START     TIME   TTY COMMAND
>      1     0 root  1236   416 S    Sep15  0:00:05    -  init
>      2     0 root     0     0 SW<  Sep15  0:00:00    -  [kthreadd]
>   1227     1 root  1236   376 S    Sep15  0:00:00 ttyS4 /sbin/getty -L
> ttyS4 115200 vt100
>   2196   521 root  1388   752 S    16:04  0:00:09    -  /usr/sbin/dropbear -s
>   2197  2196 root  1248   484 S    16:04  0:00:00  pts3 -sh
>   2201  2197 root  1228   412 R    16:04  0:00:00  pts3 ps l
> 
> I often find myself needing the START and CPU time of a process.
> Parent PID and TTY are also nice info.
> RSS gives a better hint at memory usage than VSZ
> 
> This will normally require a desktop build for the '-o' option I dont need
> every combination, but honestly thinks that the above are useful, and would
> be to others as well.


@@ -648,60 +656,95 @@ int ps_main(int argc UNUSED_PARAM, char.
 # else
        /* -w is not supported, only -Z and/or -T */
        opt_complementary = "-";
-       opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T"));
+       opts = getopt32(argv, IF_SELINUX("Z")IF_FEATURE_SHOW_THREADS("T")IF_FEATURE_PS_LONG("l"));
 # endif
-#endif
 
 #if ENABLE_SELINUX
-       if ((opts & OPT_Z) && is_selinux_enabled()) {
-               psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT
-                               | PSSCAN_STATE | PSSCAN_COMM;
-               puts("  PID CONTEXT                          STAT COMMAND");
-       } else
+               if ((opts & OPT_Z) && is_selinux_enabled()) {
+                       psscan_flags = PSSCAN_PID | PSSCAN_CONTEXT
+                                       | PSSCAN_STATE | PSSCAN_COMM;
+                       puts("  PID CONTEXT                          STAT COMMAND");
+               } else
+#endif
+               {
+                       if (opts & OPT_T) {
+                               psscan_flags |= PSSCAN_TASKS;
+                       }
...

Indentation is broken by this hunk, for no apparent reason.

Also, it broke -ZT on selinux. Not -ZT ignores -T.



@@ -687,8 +699,37 @@ int ps_main(int argc UNUSED_PARAM, char.
                                char buf6[6];
                                smart_ulltoa5(p->vsz, buf6, " mgtpezy");
                                buf6[5] = '\0';
-                               len = printf("%5u %-8.8s %s %s  ",
-                                       p->pid, user, buf6, p->state);
+#if ENABLE_FEATURE_PS_LONG
+                               if (opts & OPT_l) {
+                                       char bufr[6], strt[6], tty[20];
+                                       struct tm *tm;
+                                       int sut = (p->stime + p->utime) / 100;
+                                       time_t now = time(NULL);
+                                       struct sysinfo info;
+                                       int elap;
+
+                                       sysinfo(&info);
+                                       elap = info.uptime - (p->start_time / 100);
+                                       now -= elap;
+                                       tm = localtime((time_t *) &now);

This calls sysinfo(), time() on every loop iteration.
Casting (time_t *) to (time_t *) - ??


                                char buf6[6];
                                smart_ulltoa5(p->vsz, buf6, " mgtpezy");
...
+                                       smart_ulltoa5(p->vsz, buf6, "kmgtpezy");

(1) You do smart_ulltoa5 for VSZ twice?
(2) "kmgtpezy" format is inconsistent with non-long output. ps from procps3 assumes kbytes,
thus format should be " mgtpezy".


+                                       strcat(tty, p->tty_minor >= 64 ? "S" : "");
+                                       if (tty[2])
+                                               strcat(tty, itoa(p->tty_minor % 64));

strcat() is inefficient. strcat("") is doubly so.


The fields shown are nowhere near what is mandated by
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html


I propose attached patch instead.

-- 
vda

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 5.patch
Type: text/x-diff
Size: 6092 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20110926/022b4571/attachment.bin>


More information about the busybox mailing list