summaryrefslogtreecommitdiffstats
path: root/patches/procps-3.2.7/generic/40_ps_cgroup_display.dpatch
blob: 93f4a876724423d1a1e9509c6fda151c0b3a06e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#! /bin/sh /usr/share/dpatch/dpatch-run
## 40_ps_cgroup_display.dpatch by  <csmall@debian.org>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: ps displays cgroup #469669

@DPATCH@
diff -urNad procps-3.2.7~/proc/readproc.c procps-3.2.7/proc/readproc.c
--- procps-3.2.7~/proc/readproc.c	2009-01-07 18:20:19.000000000 +1100
+++ procps-3.2.7/proc/readproc.c	2009-01-07 18:24:51.000000000 +1100
@@ -598,6 +598,17 @@
 	p->environ = file2strvec(path, "environ");
     else
         p->environ = NULL;
+
+    if(linux_version_code>=LINUX_VERSION(2,6,24) && (flags & PROC_FILLCGROUP)) {
+	p->cgroup = file2strvec(path, "cgroup"); 	/* read /proc/#/cgroup */
+    	if(p->cgroup && *p->cgroup) {
+		int i = strlen(*p->cgroup);
+		if( (*p->cgroup)[i-1]=='\n' )
+			(*p->cgroup)[i-1] = ' '; //little hack to remove trailing \n
+	}
+    }
+    else
+	p->cgroup = NULL;
     
     return p;
 next_proc:
@@ -686,7 +697,7 @@
     t->cmdline = p->cmdline;  // better not free these until done with all threads!
     t->environ = p->environ;
 #endif
-
+    t->cgroup = p->cgroup;
     t->ppid = p->ppid;  // ought to put the per-task ppid somewhere
 
     return t;
@@ -896,6 +907,8 @@
 	free((void*)*p->cmdline);
     if (p->environ)
 	free((void*)*p->environ);
+    if (p->cgroup)
+	free((void*)*p->cgroup);
     free(p);
 }
 
diff -urNad procps-3.2.7~/proc/readproc.h procps-3.2.7/proc/readproc.h
--- procps-3.2.7~/proc/readproc.h	2006-06-16 18:01:21.000000000 +1000
+++ procps-3.2.7/proc/readproc.h	2009-01-07 18:24:56.000000000 +1100
@@ -139,6 +139,7 @@
 	tpgid,		// stat            terminal process group id
 	exit_signal,	// stat            might not be SIGCHLD
 	processor;      // stat            current (or most recent?) CPU
+    	char **cgroup;  // cgroup	   current cgroup, looks like a classic filepath
 } proc_t;
 
 // PROCTAB: data structure holding the persistent information readproc needs
@@ -239,6 +240,8 @@
 
 #define PROC_LOOSE_TASKS     0x0200 // threat threads as if they were processes
 
+#define PROC_FILLCGROUP      0x0400 // alloc and fill in `cgroup`
+
 // Obsolete, consider only processes with one of the passed:
 #define PROC_PID             0x1000  // process id numbers ( 0   terminated)
 #define PROC_UID             0x4000  // user id numbers    ( length needed )
diff -urNad procps-3.2.7~/ps/display.c procps-3.2.7/ps/display.c
--- procps-3.2.7~/ps/display.c	2005-01-06 10:44:37.000000000 +1100
+++ procps-3.2.7/ps/display.c	2009-01-07 18:24:32.000000000 +1100
@@ -223,8 +223,7 @@
 
 #define needs_for_format (proc_format_needs|task_format_needs)
 
-#define PROC_ONLY_FLAGS (PROC_FILLENV|PROC_FILLARG|PROC_FILLCOM|PROC_FILLMEM)
-
+#define PROC_ONLY_FLAGS (PROC_FILLENV|PROC_FILLARG|PROC_FILLCOM|PROC_FILLMEM|PROC_FILLCGROUP)
 /***** munge lists and determine openproc() flags */
 static void lists_and_needs(void){
   check_headers();
@@ -342,6 +341,7 @@
       }
       if(buf.cmdline) free((void*)*buf.cmdline); // ought to reuse
       if(buf.environ) free((void*)*buf.environ); // ought to reuse
+      if(buf.cgroup)  free((void*)*buf.cgroup);
     }
     break;
   case TF_show_proc|TF_loose_tasks:    // H option
@@ -354,6 +354,7 @@
       }
       if(buf.cmdline) free((void*)*buf.cmdline); // ought to reuse
       if(buf.environ) free((void*)*buf.environ); // ought to reuse
+      if(buf.cgroup)  free((void*)*buf.cgroup);
     }
     break;
   case TF_show_proc|TF_show_task:      // m and -m options
@@ -366,7 +367,8 @@
       }
       if(buf.cmdline) free((void*)*buf.cmdline); // ought to reuse
       if(buf.environ) free((void*)*buf.environ); // ought to reuse
-    }
+      if(buf.cgroup)  free((void*)*buf.cgroup);
+     }
     break;
   case TF_show_task:                   // -L and -T options
     while(readproc(ptp,&buf)){
@@ -377,7 +379,8 @@
       }
       if(buf.cmdline) free((void*)*buf.cmdline); // ought to reuse
       if(buf.environ) free((void*)*buf.environ); // ought to reuse
-    }
+      if(buf.cgroup)  free((void*)*buf.cgroup);
+   }
     break;
   }
   closeproc(ptp);
diff -urNad procps-3.2.7~/ps/output.c procps-3.2.7/ps/output.c
--- procps-3.2.7~/ps/output.c	2006-06-19 09:27:02.000000000 +1000
+++ procps-3.2.7/ps/output.c	2009-01-07 18:24:43.000000000 +1100
@@ -359,6 +359,26 @@
   return max_rightward-rightward;
 }
 
+static int pr_cgroup(char *restrict const outbuf,const proc_t *restrict const pp) {
+ if(pp->cgroup && *pp->cgroup) {
+   char *endp = outbuf;
+   int rightward=max_rightward;	
+   if(forest_prefix){
+       int fh = forest_helper(outbuf);
+       endp += fh;
+       rightward -= fh;
+   }
+   if(rightward>1){
+     *endp++ = ' ';
+     rightward--;
+     endp += escape_str(endp, *pp->cgroup, OUTBUF_SIZE, &rightward);
+   }
+   return max_rightward-rightward;
+ }	
+ else
+   return pr_nop(outbuf,pp);
+}
+
 /* "ucomm" is the same thing: short unless -f */
 static int pr_comm(char *restrict const outbuf, const proc_t *restrict const pp){
   char *endp = outbuf;
@@ -1248,7 +1268,7 @@
 #define GRP PROC_FILLGRP     /* gid_t -> group names */
 #define WCH PROC_FILLWCHAN   /* do WCHAN lookup */
 
-
+#define CGRP PROC_FILLCGROUP /* read cgroup */
 /* TODO
  *      pull out annoying BSD aliases into another table (to macro table?)
  *      add sorting functions here (to unify names)
@@ -1284,6 +1304,7 @@
 {"bsdtime",   "TIME",    pr_bsdtime,  sr_nop,     6,   0,    LNX, ET|RIGHT},
 {"c",         "C",       pr_c,        sr_pcpu,    2,   0,    SUN, ET|RIGHT},
 {"caught",    "CAUGHT",  pr_sigcatch, sr_nop,     9,   0,    BSD, TO|SIGNAL}, /*sigcatch*/
+{"cgroup",    "CGROUP",  pr_cgroup,     sr_nop,     27, CGRP,  LNX, PO|UNLIMITED},
 {"class",     "CLS",     pr_class,    sr_sched,   3,   0,    XXX, TO|LEFT},
 {"cls",       "CLS",     pr_class,    sr_sched,   3,   0,    HPU, TO|RIGHT}, /*says HPUX or RT*/
 {"cmaj_flt",  "-",       pr_nop,      sr_cmaj_flt, 1,  0,    LNX, AN|RIGHT},