{"id":420,"date":"2011-05-02T16:38:35","date_gmt":"2011-05-02T14:38:35","guid":{"rendered":"http:\/\/locallost.net\/?p=420"},"modified":"2011-05-02T16:38:35","modified_gmt":"2011-05-02T14:38:35","slug":"fuser-in-php","status":"publish","type":"post","link":"https:\/\/locallost.net\/?p=420","title":{"rendered":"fuser in PHP"},"content":{"rendered":"<p><a href=\"http:\/\/manpages.ubuntu.com\/manpages\/lucid\/en\/man1\/fuser.1posix.html\">fuser<\/a> is a handy command when you need to check if a file is currently opened by processes.\u00a0Here is an implementation in PHP which will work on Linux by scanning the `<tt>\/proc\/&lt;pids&gt;\/fd<\/tt>&lsquo; sub-directories.<\/p>\n<p><!--more--><\/p>\n<pre>function fuser($file) {\r\n  $pidList = array();\r\n  if( ! is_dir('\/proc') || ! is_readable('\/proc') ) { return array(); }\r\n  if( ($pDir = opendir('\/proc')) === false ) { return array(); }\r\n  while( ($pid = readdir($pDir)) !== false ) {\r\n    if( ! preg_match('\/^\\d+$\/', $pid) ) { continue; }\r\n    $dir = sprintf('\/proc\/%s\/fd', $pid);\r\n    if( ! is_dir($dir) || ! is_readable($dir) ) { continue; }\r\n    if( ($fdDir = opendir($dir)) === false ) { continue; }\r\n    while( ($link = readdir($fdDir)) !== false ) {\r\n      $link = sprintf('%s\/%s', $dir, $link);\r\n      if( is_link($link) &amp;&amp; readlink($link) == $file ) { $pidList[] = $pid; break; }\r\n    }\r\n  }\r\n  return $pidList;\r\n}<\/pre>\n<p>This is specific to Linux, as on BSD (and Mac OS X) the information do not seems to be available in clear-form and requires some specific syscalls to get the information from the kernel VM\u2026<\/p>\n","protected":false},"excerpt":{"rendered":"<p>fuser is a handy command when you need to check if a file is currently opened by processes.\u00a0Here is an implementation in PHP which will work on Linux by scanning the `\/proc\/&lt;pids&gt;\/fd&lsquo; sub-directories.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[],"class_list":["post-420","post","type-post","status-publish","format-standard","hentry","category-code"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2Bei9-6M","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/locallost.net\/index.php?rest_route=\/wp\/v2\/posts\/420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/locallost.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/locallost.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/locallost.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/locallost.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=420"}],"version-history":[{"count":11,"href":"https:\/\/locallost.net\/index.php?rest_route=\/wp\/v2\/posts\/420\/revisions"}],"predecessor-version":[{"id":431,"href":"https:\/\/locallost.net\/index.php?rest_route=\/wp\/v2\/posts\/420\/revisions\/431"}],"wp:attachment":[{"href":"https:\/\/locallost.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/locallost.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/locallost.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}