[PATCH] Re: /proc/non-existent-pid/ (Resent)

Roderich Schupp (rsch@ExperTeam.de)
Sun, 15 Nov 1998 15:38:24 +0100


Sorry, my last mail got mangled by some f***ing Lotus Notes mailer.
Here it's again w/o MIME parts.

Koblinger Egmont <egmont@math.bme.hu> said:
> Several weeks ago I reported a bug. No answer came, and the bug is
> still present in 2.1.127. :-( So I send the report again.
> ...
> Here is the log file how I can invoke the bug:
> egmont $ sleep 10 &
> [1] 182
> egmont $ ls -l /proc/182/fd
> total 0
> lrwx------ 1 egmont pupil 64 Oct 20 20:50 0 -> /dev/ttyp0
> lrwx------ 1 egmont pupil 64 Oct 20 20:50 1 -> /dev/ttyp0
> lrwx------ 1 egmont pupil 64 Oct 20 20:50 2 -> /dev/ttyp0
> egmont $ ##### wait ten seconds
> [1]+ Done sleep 10
> egmont $ ls /proc | grep 182
> egmont $ ls -l /proc/182
> ls: /proc/182/exe: Permission denied
> ls: /proc/182/root: Permission denied
> ls: /proc/182/cwd: Permission denied
> total 0
> --r--r--r-- 1 root root 0 Oct 20 20:50 cmdline
> --r--r--r-- 1 root root 0 Oct 20 20:50 cpu
> ...

Reason for this strange behaviour is that the lookup inode_operation
for /proc/<pid>/fd directories, proc_lookupfd in fs/proc/fd.c, forgot
to set the procfs specific dentry_operations in the looked up dentry
(unlike proc_lookup and proc_root_lookup in fs/proc/root.c). Hence /
proc/<pid>/fd dentries have no d_delete operation and their inodes get
not reaped immediately when the process no longer exists.
Patch #1 (against 2.1.128) below fixes this.

Moreover, Koblinger Egmont <egmont@math.bme.hu> said:
> 2. When a file is deleted, the string "/tmp/x (deleted)" could rather
> be "deleted:/tmp/x". This is because it's more similar to the pipe:
> and socket: ones, and a normal file _can_ have the name
> "/tmp/x (deleted)" so it cannot be told whether this really
> means a deleted file. On the
> other hand, "deleted:/tmp/x" can not be the full name of a real
> file. So it would be more simplier and more consequent to parse the
> deleted:/tmp/x" notation within a program.

That's of course a matter of taste, but I think his point is valid.
Patch #2 below implements the "deleted:/tmp/x".

Cheers, Roderich

----------- patch #1 --------------
--- linux/fs/proc/root.c.~1~ Fri Nov 13 21:14:39 1998
+++ linux/fs/proc/root.c Sat Nov 14 16:02:54 1998
@@ -730,7 +730,7 @@
d_drop(dentry);
}

-static struct dentry_operations proc_dentry_operations =
+struct dentry_operations proc_dentry_operations =
{
NULL, /* revalidate */
NULL, /* d_hash */
--- linux/fs/proc/fd.c.~1~ Sat Nov 14 13:01:27 1998
+++ linux/fs/proc/fd.c Sat Nov 14 16:26:58 1998
@@ -18,6 +18,8 @@

#include <asm/uaccess.h>

+extern struct dentry_operations proc_dentry_operations;
+
static int proc_readfd(struct file *, void *, filldir_t);
static int proc_lookupfd(struct inode *, struct dentry *);

@@ -119,6 +121,7 @@
ino = (pid << 16) + PROC_PID_FD_DIR + fd;
inode = proc_get_inode(dir->i_sb, ino, NULL);
if (inode) {
+ dentry->d_op = &proc_dentry_operations;
d_add(dentry, inode);
err = 0;
}
----------- end of patch #1 --------------

----------- patch #2 --------------
--- linux/fs/dcache.c.~1~ Fri Nov 13 23:25:21 1998
+++ linux/fs/dcache.c Fri Nov 13 23:25:29 1998
@@ -775,14 +775,10 @@
char * end = buffer+buflen;
char * retval;
struct dentry * root = current->fs->root;
+ int deleted = dentry->d_parent != dentry &&
list_empty(&dentry->d_hash);

*--end = '\0';
buflen--;
- if (dentry->d_parent != dentry && list_empty(&dentry->d_hash)) {
- buflen -= 10;
- end -= 10;
- memcpy(end, " (deleted)", 10);
- }

/* Get '/' right */
retval = end-1;
@@ -807,6 +803,11 @@
*--end = '/';
retval = end;
dentry = parent;
+ }
+
+ if (deleted && buflen >= 8) {
+ retval -= 8;
+ memcpy(retval, "deleted:", 8);
}
return retval;
}
----------- end of patch #2 --------------

--
                "A man, a plan, a canal, Panama!"

Roderich Schupp mailto:rsch@ExperTeam.de ExperTeam GmbH http://www.experteam.de/ Munich, Germany linux:2.1.129

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/