@@ -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
13661376static 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 ))
1373- return - 1 ;
1387+ 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 )
1377- return - 1 ;
1395+ return 0 ;
13781396
13791397 if (stat (nspath , & sb_target ))
1380- return - 1 ;
1398+ 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