Skip to content

Commit

Permalink
Add comments: logic behind comparison compare_mntns()
Browse files Browse the repository at this point in the history
Add comments:
Compare the inode number of mount namespace, to see if target process is in a
different mount namespace from lsof process: return 0 (false) means mount
namespace is the same, and return 1 (true) means mount namespace is different.
Note that legacy linux kernel (e.g. linux-2.6) might not have this mount
namespace path, so makes this case return 0 (false) and acts as old days.

Signed-off-by: Jones Syue <[email protected]>
  • Loading branch information
jones2024gh committed Jan 9, 2025
1 parent ce96381 commit 904534a
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions lib/dialects/linux/dproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1361,27 +1361,52 @@ static int process_id(struct lsof_context *ctx, /* context */
return (0);
}

/* compare mount namespace of this lsof process and the target process */
/*
* compare_mntns() - compare mount namespace of this lsof process and the
* target process
*
* Note: mount namespace path might not be found with legacy linux kernel (e.g.
* linux-2.6) which does not have path "/proc/self/ns" or "/proc/${pid}/ns",
* see Commit 6b4e306aa3dc ("ns: proc files for namespace naming policy.")
*
* return: 0 == mount namespace is the same, or path is not found.
* 1 == mount namespace is different.
*/

static int compare_mntns(int pid) /* pid of the target process */
{
char nspath[NS_PATH_LENGTH];
struct stat sb_self, sb_target;
int ret;

/*
* Get mount namespace of this lsof process, early return if path is not
* found.
*/
if (stat("/proc/self/ns/mnt", &sb_self))
return 0;

/*
* Get mount namespace of the target process, early return if path is not
* found.
*/
ret = snprintf(nspath, sizeof(nspath), "/proc/%d/ns/mnt", pid);
if (ret >= sizeof(nspath) || ret <= 0)
return 0;

if (stat(nspath, &sb_target))
return 0;

/*
* Compare the inode number of mount namespace, to see if target process
* is in a different mount namespace from lsof process.
*/
if (sb_self.st_ino != sb_target.st_ino)
return -1;
return 1;

/*
* mount namespace is the same.
*/
return 0;
}

Expand Down

0 comments on commit 904534a

Please sign in to comment.