{"id":3191,"date":"2018-07-30T10:26:22","date_gmt":"2018-07-29T22:26:22","guid":{"rendered":"http:\/\/www.zoyinc.com\/?p=3191"},"modified":"2018-07-30T10:29:43","modified_gmt":"2018-07-29T22:29:43","slug":"apache-doesnt-write-to-var-tmp","status":"publish","type":"post","link":"http:\/\/www.zoyinc.com\/?p=3191","title":{"rendered":"Apache doesn&#8217;t write to \/var\/tmp"},"content":{"rendered":"<p>I had a really strange problem, I was writing a tiny Python script to learn about Python and it was writing to \/var\/tmp. Only problem was the file never turned up. This script was run from within Apache 2 on Fedora 18.<\/p>\n<p>The file I was trying to create was &#8220;<em>webhook_current_json_request.json<\/em>&#8220;. Turns out the problem is Apache when run using systemd. This is what I found.<\/p>\n<p>Looking at &#8220;man system.unit&#8221; we see:<\/p>\n<blockquote><p>PrivateTmp=<br \/>\nTakes a boolean argument. If true sets up a new file system namespace for the executed processes and mounts private \/tmp and <span style=\"background-color: #ffff00;\">\/var\/tmp<\/span> directories inside it, that are not<br \/>\nshared by processes outside of the namespace. This is useful to secure access to temporary files of the process, but makes sharing between processes via \/tmp or \/var\/tmp<br \/>\nimpossible. All temporary data created by service will be removed after service is stopped. Defaults to false.<\/p><\/blockquote>\n<p>I found my systemctl .service file for Apache, &#8220;httpd&#8221; at:<\/p>\n<p style=\"padding-left: 30px;\">\/usr\/lib\/systemd\/system\/httpd.service<\/p>\n<p>It looked like:<\/p>\n<pre class=\"lang:sh decode:true \">[Unit]\r\nDescription=The Apache HTTP Server\r\nAfter=network.target remote-fs.target nss-lookup.target\r\n\r\n[Service]\r\nType=notify\r\nEnvironmentFile=\/etc\/sysconfig\/httpd\r\nExecStart=\/usr\/sbin\/httpd $OPTIONS -DFOREGROUND\r\nExecReload=\/usr\/sbin\/httpd $OPTIONS -k graceful\r\nExecStop=\/usr\/sbin\/httpd $OPTIONS -k graceful-stop\r\n# We want systemd to give httpd some time to finish gracefully, but still want\r\n# it to kill httpd after TimeoutStopSec if something went wrong during the\r\n# graceful stop. Normally, Systemd sends SIGTERM signal right after the\r\n# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give\r\n# httpd time to finish.\r\nKillSignal=SIGCONT\r\nPrivateTmp=true\r\n\r\n[Install]\r\nWantedBy=multi-user.target\r\n<\/pre>\n<p>As the man page states &#8220;<em>sets up a new file system namespace for the executed processes and mounts private \/tmp and \/var\/tmp directories inside it<\/em>&#8221; it transpires the &#8220;<em>new file system namespace<\/em>&#8221; is a somewhat random folder under \/var\/tmp. These look like:<\/p>\n<pre class=\"crayon:false;\">[root@vmvaliant tmp]# cd \/var\/tmp\r\n[root@vmvaliant tmp]# ll\r\ntotal 304\r\ndrwxr-xr-x 2 abrt abrt 4096 Jul 6 08:48 abrt\r\ndrwx------ 3 root root 4096 Jul 8 2015 systemd-private-0zBSJE\r\ndrwx------ 3 root root 4096 Apr 27 10:38 systemd-private-1aUPmp\r\ndrwx------ 3 root root 4096 Aug 20 2017 systemd-private-1T74Sw\r\ndrwx------ 3 root root 4096 Aug 29 2017 systemd-private-1VhcnW\r\ndrwx------ 3 root root 4096 Aug 20 2017 systemd-private-7BmxdZ\r\ndrwx------ 3 root root 4096 Dec 9 2017 systemd-private-8cXaI2<\/pre>\n<p>I did some digging and found the one which Apache was currently using &#8220;systemd-private-JgJxbB&#8221; and inside it I found my file<\/p>\n<p style=\"padding-left: 30px;\">\/var\/tmp\/systemd-private-JgJxbB\/tmp\/webhook_current_json_request.json<\/p>\n<p>Moral of the story seems to be not to write to \/tmp or \/var\/tmp when running in Apache.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had a really strange problem, I was writing a tiny Python script to learn about Python and it was writing to \/var\/tmp. Only problem was the file never turned up. This script was run from within Apache 2 on Fedora 18. The file I was trying to create was &#8220;webhook_current_json_request.json&#8220;. Turns out the problem [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1473,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[85,5,433],"tags":[435,436,305,434,437,430,439,440,441,438],"class_list":["post-3191","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apache","category-fedora","category-python","tag-tmp","tag-var-tmp","tag-apache","tag-httpd","tag-permissions","tag-python","tag-read","tag-systemd","tag-systemdtl","tag-write"],"_links":{"self":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts\/3191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3191"}],"version-history":[{"count":6,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts\/3191\/revisions"}],"predecessor-version":[{"id":3197,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/posts\/3191\/revisions\/3197"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=\/wp\/v2\/media\/1473"}],"wp:attachment":[{"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3191"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zoyinc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}