{"id":233,"date":"2011-02-26T10:07:51","date_gmt":"2011-02-26T09:07:51","guid":{"rendered":"http:\/\/locallost.net\/?p=233"},"modified":"2022-11-27T13:37:19","modified_gmt":"2022-11-27T12:37:19","slug":"identifying-running-processes-that-uses-deleted-shared-librairies","status":"publish","type":"post","link":"https:\/\/locallost.net\/?p=233","title":{"rendered":"Identifying running processes that uses deleted shared librairies"},"content":{"rendered":"<p>Updates:<\/p>\n<ul>\n<li><strong>2022-11-27: The correct solution to this problem is to use the <a href=\"https:\/\/github.com\/liske\/needrestart\">needrestart<\/a> tool (available on Ubuntu\/Debian\/etc.)<\/strong><\/li>\n<li>2015-03-03: Changed <em>ps xh<\/em> to <em>ps axh<\/em> to search in all processes.<\/li>\n<li>2015-02-11: The format of \/proc\/%pid\/maps seems different between Ubuntu 12.04 and Ubuntu 14.04 (the \u00ab\u00a0(deleted)\u00a0\u00bb string occurs before or after the library name). So I updated the script to handle both cases. Also addded a one-liner version of the script.<\/li>\n<\/ul>\n<p>Sometimes, when you log into your Ubuntu server, you get a message warning you that the system requires a reboot :<\/p>\n<pre>*** System restart required ***<\/pre>\n<p>This happens after a kernel upgrade, or after upgrading a shared library like openssl (libssl, libcrypto, etc.).<\/p>\n<p>In the case of a kernel upgrade, it&rsquo;s pretty obvious that a restart is required in order to boot on the new kernel.<\/p>\n<p>But in the case of a library upgrade I don&rsquo;t think such reboots are absolutely required.<\/p>\n<p><!--more--><\/p>\n<p>What you need, is to stop and re-run the processes\/services that use this library. They have been loaded in memory when the processs got started, and your new library is in fact not used by the running processes until they die.<\/p>\n<p>So, instead of restarting the system, it may be more suitable to only restart the set of processes that use this library, isn&rsquo;t it?<\/p>\n<p>You could go with restarting all your \/etc\/init.d\/* services, but here is a script I made to identify the running processes that are now using a dead\/deleted shared library :<\/p>\n<pre>ps axh -o pid \\\n| while read PROCID; do\n        grep '(deleted)' \/proc\/$PROCID\/maps 2&gt; \/dev\/null | grep '\\.so';\n        if [ $? -eq 0 ]; then\n                CMDLINE=$(sed -e 's\/\\x00\/ \/g' &lt; \/proc\/$PROCID\/cmdline)\n                echo -e \"\\tPID $PROCID $CMDLINE\\n\"\n        fi\ndone\n<\/pre>\n<p>One-liner version for easy execution on remote machines by copy\/paste (it only show processes that need to be reloaded) :<\/p>\n<pre>( ps xh -o pid | while read PROCID; do grep '(deleted)' \/proc\/$PROCID\/maps 2&gt; \/dev\/null | grep '\\.so'; if [ $? -eq 0 ]; then CMDLINE=$(sed -e 's\/\\x00\/ \/g' &lt; \/proc\/$PROCID\/cmdline); echo -e \"\\tPID $PROCID $CMDLINE\\n\"; fi; done ) | grep PID\n<\/pre>\n<p>The script inspect the <code>maps' file, in the<\/code>\/proc&rsquo; process space, for files marked as <code>(deleted)'. The<\/code>maps&rsquo; file\u00a0contains the list of all the memory mapped files of a process.<\/p>\n<p>So, if you have processes using deleted shared libraries, you shoud get an output looking like this :<\/p>\n<pre>7f6d7a5ce000-7f6d7a736000 r-xp 00000000 08:02 9437350 \/lib\/libcrypto.so.0.9.8 (deleted)\n7f6d7a736000-7f6d7a935000 ---p 00168000 08:02 9437350 \/lib\/libcrypto.so.0.9.8 (deleted)\n7f6d7a935000-7f6d7a942000 r--p 00167000 08:02 9437350 \/lib\/libcrypto.so.0.9.8 (deleted)\n7f6d7a942000-7f6d7a95a000 rw-p 00174000 08:02 9437350 \/lib\/libcrypto.so.0.9.8 (deleted)\n    PID 725 \/usr\/sbin\/ntpd\n\n7fa48360d000-7fa483775000 r-xp 00000000 08:02 9437350 \/lib\/libcrypto.so.0.9.8 (deleted)\n7fa483775000-7fa483974000 ---p 00168000 08:02 9437350 \/lib\/libcrypto.so.0.9.8 (deleted)\n7fa483974000-7fa483981000 r--p 00167000 08:02 9437350 \/lib\/libcrypto.so.0.9.8 (deleted)\n7fa483981000-7fa483999000 rw-p 00174000 08:02 9437350 \/lib\/libcrypto.so.0.9.8 (deleted)\n    PID 735 \/usr\/sbin\/sshd<\/pre>\n<p>From this output, you know that <code>ntpd' and<\/code>sshd&rsquo; are using the `\/lib\/libcrypto.so.0.9.8&prime; which as been removed, or changed (inode change), on disk.<\/p>\n<p>Now you can schedule the restart of these services.<\/p>\n<p>Some more informations.<\/p>\n<p>The \u00ab\u00a0System restart required\u00a0\u00bb message is triggered by the presence of a <code>\/var\/run\/reboot-required' which is set by the upgraded library. You can find who set this file by inspecting the<\/code>\/var\/run\/reboot-required.pkgs&rsquo; file :<\/p>\n<pre># cat \/var\/run\/reboot-required\n*** System restart required ***\n# cat \/var\/run\/reboot-required.pkgs\nlibssl0.9.8<\/pre>\n<p>So, it&rsquo;s the upgrade of libssl which triggered the message.<\/p>\n<p>After restarting your processes, you can then remove the `\/var\/run\/reboot-required&rsquo; file, and get back your usual \u00ab\u00a0clean\u00a0\u00bb motd.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Updates: 2022-11-27: The correct solution to this problem is to use the needrestart tool (available on Ubuntu\/Debian\/etc.) 2015-03-03: Changed ps xh to ps axh to search in all processes. 2015-02-11: The format of \/proc\/%pid\/maps seems different between Ubuntu 12.04 and &hellip; <a href=\"https:\/\/locallost.net\/?p=233\">Continuer la lecture <span class=\"meta-nav\">&rarr;<\/span><\/a><\/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":[17],"tags":[],"class_list":["post-233","post","type-post","status-publish","format-standard","hentry","category-system"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2Bei9-3L","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/locallost.net\/index.php?rest_route=\/wp\/v2\/posts\/233","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=233"}],"version-history":[{"count":22,"href":"https:\/\/locallost.net\/index.php?rest_route=\/wp\/v2\/posts\/233\/revisions"}],"predecessor-version":[{"id":1735,"href":"https:\/\/locallost.net\/index.php?rest_route=\/wp\/v2\/posts\/233\/revisions\/1735"}],"wp:attachment":[{"href":"https:\/\/locallost.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/locallost.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/locallost.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}