Skip to contentSkip to conversation

rlim in proc/<pid>/status

PATCH In review Clifford Wolf 3 participants 7 messages Jan 15, 2008 – Jan 16, 2008 Proc filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rlim in proc/<pid>/status

Hi,

because I needed it already twice in two different projects this week: the following patch adds rlim (ulimits) output to /proc/<pid>/status.

Please let me know if there is another (already existing) way of accessing this information easy (i.e. connecting with gdb to the process in question and 'injecting' a getrlimit() call does not count.. ;-).

yours,
- clifford

fs/proc/array.c +50 -0
@@ -239,6 +239,55 @@
 	}
 }
 
+static char *rlim_names[RLIM_NLIMITS] = {
+	[RLIMIT_CPU]        = "CPU",
+	[RLIMIT_FSIZE]      = "FSize",
+	[RLIMIT_DATA]       = "Data",
+	[RLIMIT_STACK]      = "Stack",
+	[RLIMIT_CORE]       = "Core",
+	[RLIMIT_RSS]        = "RSS",
+	[RLIMIT_NPROC]      = "NProc",
+	[RLIMIT_NOFILE]     = "NoFile",
+	[RLIMIT_MEMLOCK]    = "MemLock",
+	[RLIMIT_AS]         = "AddrSpace",
+	[RLIMIT_LOCKS]      = "Locks",
+	[RLIMIT_SIGPENDING] = "SigPending",
+	[RLIMIT_MSGQUEUE]   = "MsgQueue",
+	[RLIMIT_NICE]       = "Nice",
+	[RLIMIT_RTPRIO]     = "RTPrio"
+};
+
+static inline char *task_rlim(struct task_struct *p, char *buffer)
+{
+	unsigned long flags;
+	struct rlimit rlim[RLIM_NLIMITS];
+	int i;
+	
+	rcu_read_lock();
+	if (lock_task_sighand(p, &flags)) {
+		for (i=0; i<RLIM_NLIMITS; i++)
+			rlim[i] = p->signal->rlim[i];
+	}
+	rcu_read_unlock();
+
+	for (i=0; i<RLIM_NLIMITS; i++) {
+		if (rlim_names[i])
+			buffer += sprintf(buffer, "Rlim%s:\t", rlim_names[i]);
+		else
+			buffer += sprintf(buffer, "Rlim%d:\t", i);
+		if (rlim[i].rlim_cur != ~0)
+			buffer += sprintf(buffer, "%lu\t", rlim[i].rlim_cur);
+		else
+			buffer += sprintf(buffer, "-\t");
+		if (rlim[i].rlim_max != ~0)
+			buffer += sprintf(buffer, "%lu\n", rlim[i].rlim_max);
+		else
+			buffer += sprintf(buffer, "-\n");
+	}
+
+	return buffer;
+}
+
 static inline char *task_sig(struct task_struct *p, char *buffer)
 {
 	unsigned long flags;
@@ -310,6 +359,7 @@
 		buffer = task_mem(mm, buffer);
 		mmput(mm);
 	}
+	buffer = task_rlim(task, buffer);
 	buffer = task_sig(task, buffer);
 	buffer = task_cap(task, buffer);
 	buffer = cpuset_task_status_allowed(task, buffer);
Signature · 2 lines
[..] If it still doesn't work, re-write it in assembler. This won't fix the
bug, but it will make sure no one else finds it and makes you look bad.
#1 lore raw

Hi

sound good for me.
a few question please.

fs/proc/array.c line 277

Quoted 5 lines
fs/proc/array.c +5 -0
+	for (i=0; i<RLIM_NLIMITS; i++) {
+		if (rlim_names[i])
+			buffer += sprintf(buffer, "Rlim%s:\t", rlim_names[i]);
+		else
+			buffer += sprintf(buffer, "Rlim%d:\t", i);

this else is really necessary?

fs/proc/array.c line 285

Quoted 8 lines
fs/proc/array.c +8 -0
+		if (rlim[i].rlim_cur != ~0)
+			buffer += sprintf(buffer, "%lu\t", rlim[i].rlim_cur);
+		else
+			buffer += sprintf(buffer, "-\t");
+		if (rlim[i].rlim_max != ~0)
+			buffer += sprintf(buffer, "%lu\n", rlim[i].rlim_max);
+		else
+			buffer += sprintf(buffer, "-\n");

Why do you don't use RLIM_INFINITY?

- kosaki

#2 lore raw

Hi,

Quoted 10 lines · KOSAKI Motohiro

sound good for me.
a few question please.

fs/proc/array.c line 277

Quoted 5 lines
fs/proc/array.c +5 -0
+	for (i=0; i<RLIM_NLIMITS; i++) {
+		if (rlim_names[i])
+			buffer += sprintf(buffer, "Rlim%s:\t", rlim_names[i]);
+		else
+			buffer += sprintf(buffer, "Rlim%d:\t", i);

this else is really necessary?

no. not with the current sources. maybe something like the following would be better:

#if RLIM_NLIMITS != 15
# error New RLIM_NLIMITS add mising entries to rlim_names[] #endif

Quoted 10 lines

fs/proc/array.c line 285

Quoted 8 lines
fs/proc/array.c +8 -0
+		if (rlim[i].rlim_cur != ~0)
+			buffer += sprintf(buffer, "%lu\t", rlim[i].rlim_cur);
+		else
+			buffer += sprintf(buffer, "-\t");
+		if (rlim[i].rlim_max != ~0)
+			buffer += sprintf(buffer, "%lu\n", rlim[i].rlim_max);
+		else
+			buffer += sprintf(buffer, "-\n");

Why do you don't use RLIM_INFINITY?

because I'm blind and didn't see it... ;-)

maybe it would also be better to output 'inf' instead of '-' in this case?

yours,
- clifford

Signature · 1 line
The number of the beast - vi vi vi.
#3 lore raw
In reply to Clifford Wolf
Quoted 38 lines · Clifford Wolf

Hi,

because I needed it already twice in two different projects this week: the following patch adds rlim (ulimits) output to /proc/<pid>/status.

Please let me know if there is another (already existing) way of accessing this information easy (i.e. connecting with gdb to the process in question and 'injecting' a getrlimit() call does not count.. ;-).

yours,
- clifford

fs/proc/array.c +18 -0
@@ -239,6 +239,55 @@
 	}
 }
 
+static char *rlim_names[RLIM_NLIMITS] = {
+	[RLIMIT_CPU]        = "CPU",
+	[RLIMIT_FSIZE]      = "FSize",
+	[RLIMIT_DATA]       = "Data",
+	[RLIMIT_STACK]      = "Stack",
+	[RLIMIT_CORE]       = "Core",
+	[RLIMIT_RSS]        = "RSS",
+	[RLIMIT_NPROC]      = "NProc",
+	[RLIMIT_NOFILE]     = "NoFile",
+	[RLIMIT_MEMLOCK]    = "MemLock",
+	[RLIMIT_AS]         = "AddrSpace",
+	[RLIMIT_LOCKS]      = "Locks",
+	[RLIMIT_SIGPENDING] = "SigPending",
+	[RLIMIT_MSGQUEUE]   = "MsgQueue",
+	[RLIMIT_NICE]       = "Nice",
+	[RLIMIT_RTPRIO]     = "RTPrio"
+};
+

fs/proc/array.c line 269

Quoted 10 lines
fs/proc/array.c +10 -0
+static inline char *task_rlim(struct task_struct *p, char *buffer)
+{
+	unsigned long flags;
+	struct rlimit rlim[RLIM_NLIMITS];
+	int i;
+	
+	rcu_read_lock();
+	if (lock_task_sighand(p, &flags)) {
+		for (i=0; i<RLIM_NLIMITS; i++)
+			rlim[i] = p->signal->rlim[i];

I'm confused - where do you unlock_task_sighand()?

Quoted 41 lines
fs/proc/array.c +22 -0
+	}
+	rcu_read_unlock();
+
+	for (i=0; i<RLIM_NLIMITS; i++) {
+		if (rlim_names[i])
+			buffer += sprintf(buffer, "Rlim%s:\t", rlim_names[i]);
+		else
+			buffer += sprintf(buffer, "Rlim%d:\t", i);
+		if (rlim[i].rlim_cur != ~0)
+			buffer += sprintf(buffer, "%lu\t", rlim[i].rlim_cur);
+		else
+			buffer += sprintf(buffer, "-\t");
+		if (rlim[i].rlim_max != ~0)
+			buffer += sprintf(buffer, "%lu\n", rlim[i].rlim_max);
+		else
+			buffer += sprintf(buffer, "-\n");
+	}
+
+	return buffer;
+}
+
 static inline char *task_sig(struct task_struct *p, char *buffer)
 {
 	unsigned long flags;
@@ -310,6 +359,7 @@
 		buffer = task_mem(mm, buffer);
 		mmput(mm);
 	}
+	buffer = task_rlim(task, buffer);
 	buffer = task_sig(task, buffer);
 	buffer = task_cap(task, buffer);
 	buffer = cpuset_task_status_allowed(task, buffer);
Signature · 8 lines
-- 
[..] If it still doesn't work, re-write it in assembler. This won't fix the
bug, but it will make sure no one else finds it and makes you look bad.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
#4 lore raw

Re: [PATCH] rlim in proc/<pid>/status (2nd rev.)

Hi,

Quoted 6 lines · On Tue, Jan 15, 2008 at 02:36:59PM -0600, [email protected] wrote:

Review

Quoted 4 lines
(quoted diff) +4 -0
+	rcu_read_lock();
+	if (lock_task_sighand(p, &flags)) {
+		for (i=0; i<RLIM_NLIMITS; i++)
+			rlim[i] = p->signal->rlim[i];

I'm confused - where do you unlock_task_sighand()?

oh fsck! thanks for that pointer..

Here is a new version of the patch which solves this issue and the issues adressed earlier in this thread by kosaki.

yours,
- clifford

fs/proc/array.c +53 -0
@@ -239,6 +239,58 @@
 	}
 }
 
+static char *rlim_names[RLIM_NLIMITS] = {
+	[RLIMIT_CPU]        = "CPU",
+	[RLIMIT_FSIZE]      = "FSize",
+	[RLIMIT_DATA]       = "Data",
+	[RLIMIT_STACK]      = "Stack",
+	[RLIMIT_CORE]       = "Core",
+	[RLIMIT_RSS]        = "RSS",
+	[RLIMIT_NPROC]      = "NProc",
+	[RLIMIT_NOFILE]     = "NoFile",
+	[RLIMIT_MEMLOCK]    = "MemLock",
+	[RLIMIT_AS]         = "AddrSpace",
+	[RLIMIT_LOCKS]      = "Locks",
+	[RLIMIT_SIGPENDING] = "SigPending",
+	[RLIMIT_MSGQUEUE]   = "MsgQueue",
+	[RLIMIT_NICE]       = "Nice",
+	[RLIMIT_RTPRIO]     = "RTPrio"
+};
+
+#if RLIM_NLIMITS != 15
+#  error Value of RLIM_NLIMITS changed. \
+         Please update rlim_names in fs/proc/array.c
+#endif
+
+static inline char *task_rlim(struct task_struct *p, char *buffer)
+{
+	unsigned long flags;
+	struct rlimit rlim[RLIM_NLIMITS];
+	int i;
+	
+	rcu_read_lock();
+	if (lock_task_sighand(p, &flags)) {
+		for (i=0; i<RLIM_NLIMITS; i++)
+			rlim[i] = p->signal->rlim[i];
+		unlock_task_sighand(p, &flags);
+	}
+	rcu_read_unlock();
+
+	for (i=0; i<RLIM_NLIMITS; i++) {
+		buffer += sprintf(buffer, "Rlim%s:\t", rlim_names[i]);
+		if (rlim[i].rlim_cur != RLIM_INFINITY)
+			buffer += sprintf(buffer, "%lu\t", rlim[i].rlim_cur);
+		else
+			buffer += sprintf(buffer, "inf\t");
+		if (rlim[i].rlim_max != RLIM_INFINITY)
+			buffer += sprintf(buffer, "%lu\n", rlim[i].rlim_max);
+		else
+			buffer += sprintf(buffer, "inf\n");
+	}
+
+	return buffer;
+}
+
 static inline char *task_sig(struct task_struct *p, char *buffer)
 {
 	unsigned long flags;
@@ -310,6 +362,7 @@
 		buffer = task_mem(mm, buffer);
 		mmput(mm);
 	}
+	buffer = task_rlim(task, buffer);
 	buffer = task_sig(task, buffer);
 	buffer = task_cap(task, buffer);
 	buffer = cpuset_task_status_allowed(task, buffer);
Signature · 1 line
perl -le '$_=1;(1x$_)!~/^(11+)\1+$/&&print${_}while$_++<1000'|fmt
#5 lore raw

Re: [PATCH] rlim in proc/<pid>/status (2nd rev.)

Hi Clifford,

fs/proc/array.c line 276

Quoted 12 lines
fs/proc/array.c +12 -0
+static inline char *task_rlim(struct task_struct *p, char *buffer)
+{
+	unsigned long flags;
+	struct rlimit rlim[RLIM_NLIMITS];
+	int i;
+	
+	rcu_read_lock();
+	if (lock_task_sighand(p, &flags)) {
+		for (i=0; i<RLIM_NLIMITS; i++)
+			rlim[i] = p->signal->rlim[i];
+		unlock_task_sighand(p, &flags);
+	}

lock_task_sighand is possible return NULL?
if so, rlim is uninitialized when NULL.

- kosaki

#6 lore raw

Re: [PATCH] rlim in proc/<pid>/status (2nd rev.)

Hi,

Quoted 17 lines · KOSAKI Motohiro

Hi Clifford,

fs/proc/array.c line 276

Quoted 12 lines
fs/proc/array.c +12 -0
+static inline char *task_rlim(struct task_struct *p, char *buffer)
+{
+	unsigned long flags;
+	struct rlimit rlim[RLIM_NLIMITS];
+	int i;
+	
+	rcu_read_lock();
+	if (lock_task_sighand(p, &flags)) {
+		for (i=0; i<RLIM_NLIMITS; i++)
+			rlim[i] = p->signal->rlim[i];
+		unlock_task_sighand(p, &flags);
+	}

lock_task_sighand is possible return NULL?
if so, rlim is uninitialized when NULL.

hmm.. can p->sighand be NULL here? If so, there also is a problem in the task_sig() function in the same file.

In fact that code is copy&paste from task_sig(). In fact I'm not even sure if I actually need to lock anything to access p->signal->rlim.. I've searched the kernel code a bit and it looks like most acesses to the rlimits are done unlocked, which is no problem imo given the typical change-pattern of this values..

anyone a clue on this issues?

yours,
- clifford

Signature · 1 line
2B OR (NOT 2B) That is the question. The answer is FF.