Bug 1345413 - Fallback to getpwuid() info when LOGNAME is not set when creating the XRemote mutex. r?froydnj draft
authorMike Hommey <mh+mozilla@glandium.org>
Tue, 14 Mar 2017 14:11:55 +0900
changeset 498026 df4489d11d3c921d3e5a8e32a409a944d7b616bf
parent 498025 e6113871af3df68c43bcdaa0afaa759b7abc7c72
child 549057 80011fcbc849578180f121cbd470b158ea07cf81
push id49083
push userbmo:mh+mozilla@glandium.org
push dateTue, 14 Mar 2017 05:24:50 +0000
reviewersfroydnj
bugs1345413
milestone55.0a1
Bug 1345413 - Fallback to getpwuid() info when LOGNAME is not set when creating the XRemote mutex. r?froydnj
toolkit/xre/nsAppRunner.cpp
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -3815,21 +3815,37 @@ XREMain::XRE_mainStartup(bool* aExitFlag
     const char* username = getenv("LOGNAME");
     const char* profile  = nullptr;
 
     RemoteResult rr = ParseRemoteCommandLine(program, &profile, &username);
     if (rr == REMOTE_ARG_BAD) {
       return 1;
     }
 
+    if (!username) {
+      struct passwd *pw = getpwuid(geteuid());
+      if (pw && pw->pw_name) {
+        // Beware that another call to getpwent/getpwname/getpwuid will overwrite
+        // pw, but we don't have such another call between here and when username
+        // is used last.
+        username = pw->pw_name;
+      }
+    }
+
     nsCOMPtr<nsIFile> mutexDir;
     rv = GetSpecialSystemDirectory(OS_TemporaryDirectory, getter_AddRefs(mutexDir));
     if (NS_SUCCEEDED(rv)) {
-      nsAutoCString mutexPath =
-        program + NS_LITERAL_CSTRING("_") + nsDependentCString(username);
+      nsAutoCString mutexPath = program + NS_LITERAL_CSTRING("_");
+      // In the unlikely even that LOGNAME is not set and getpwuid failed, just
+      // don't put the username in the mutex directory. It will conflict with
+      // other users mutex, but the worst that can happen is that they wait for
+      // MOZ_XREMOTE_START_TIMEOUT_SEC during startup in that case.
+      if (username) {
+        mutexPath.Append(username);
+      }
       if (profile) {
         mutexPath.Append(NS_LITERAL_CSTRING("_") + nsDependentCString(profile));
       }
       mutexDir->AppendNative(mutexPath);
 
       rv = mutexDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
       if (NS_SUCCEEDED(rv) || rv == NS_ERROR_FILE_ALREADY_EXISTS) {
         mRemoteLockDir = mutexDir;