Change working directory to / under certain conditions.
authorTilghman Lesher <tilghman@meg.abyt.es>
Thu, 30 Apr 2009 06:47:13 +0000 (06:47 +0000)
committerTilghman Lesher <tilghman@meg.abyt.es>
Thu, 30 Apr 2009 06:47:13 +0000 (06:47 +0000)
If backgrounding and no core will be produced, then changing the directory
won't break anything; likewise, if the CWD isn't accessible by the current
user, then a core wasn't possible anyway.
(closes issue #14831)
 Reported by: chris-mac
 Patches:
       20090428__bug14831.diff.txt uploaded by tilghman (license 14)
       20090430__bug14831.diff.txt uploaded by tilghman (license 14)
 Tested by: chris-mac

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@191283 65c4cc65-6c06-0410-ace0-fbb531ad65f3

main/asterisk.c

index acba2de..3de44a7 100644 (file)
@@ -3435,6 +3435,23 @@ int main(int argc, char *argv[])
                        ast_log(LOG_WARNING, "Unable to set the process for core dumps after changing to a non-root user. %s\n", strerror(errno));
                }       
        }
+
+       {
+               char dir[PATH_MAX];
+               if (!getcwd(dir, sizeof(dir)) || eaccess(dir, R_OK | X_OK | F_OK)) {
+                       ast_log(LOG_ERROR, "Unable to access the running directory (%s).  Changing to '/' for compatibility.\n", strerror(errno));
+                       /* If we cannot access the CWD, then we couldn't dump core anyway,
+                        * so chdir("/") won't break anything. */
+                       if (chdir("/")) {
+                               ast_log(LOG_ERROR, "chdir() failed?!! %s\n", strerror(errno));
+                       }
+               } else if (!ast_opt_no_fork && !ast_opt_dump_core) {
+                       /* Backgrounding, but no cores, so chdir won't break anything. */
+                       if (chdir("/")) {
+                               ast_log(LOG_ERROR, "Unable to chdir(\"/\") ?!! %s\n", strerror(errno));
+                       }
+               }
+       }
 #endif
 
        ast_term_init();