Skip to content

Commit 904534a

Browse files
committed
Add comments: logic behind comparison compare_mntns()
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 <jones_syue@askey.com>
1 parent ce96381 commit 904534a

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

lib/dialects/linux/dproc.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,27 +1361,52 @@ static int process_id(struct lsof_context *ctx, /* context */
13611361
return (0);
13621362
}
13631363

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

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

1382+
/*
1383+
* Get mount namespace of this lsof process, early return if path is not
1384+
* found.
1385+
*/
13721386
if (stat("/proc/self/ns/mnt", &sb_self))
13731387
return 0;
13741388

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

13791397
if (stat(nspath, &sb_target))
13801398
return 0;
13811399

1400+
/*
1401+
* Compare the inode number of mount namespace, to see if target process
1402+
* is in a different mount namespace from lsof process.
1403+
*/
13821404
if (sb_self.st_ino != sb_target.st_ino)
1383-
return -1;
1405+
return 1;
13841406

1407+
/*
1408+
* mount namespace is the same.
1409+
*/
13851410
return 0;
13861411
}
13871412

0 commit comments

Comments
 (0)