summaryrefslogtreecommitdiffstats
path: root/toolkit/profile/nsProfileLock.h
blob: 932ce567bfb25a5251ace29eb56f87c12b2c0f9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef __nsProfileLock_h___
#define __nsProfileLock_h___

#include "nsIFile.h"

class nsIProfileUnlocker;

#if defined(XP_WIN)
#  include <windows.h>
#endif

#if defined(XP_UNIX)
#  include <signal.h>
#  include "prclist.h"
#endif

class nsProfileLock
#if defined(XP_UNIX)
    : public PRCList
#endif
{
 public:
  nsProfileLock();
  nsProfileLock(nsProfileLock& src);

  ~nsProfileLock();

  nsProfileLock& operator=(nsProfileLock& rhs);

  /**
   * Attempt to lock a profile directory.
   *
   * @param aProfileDir  [in] The profile directory to lock.
   * @param aUnlocker    [out] Optional. This is only returned when locking
   *                     fails with NS_ERROR_FILE_ACCESS_DENIED, and may not
   *                     be returned at all.
   * @throws NS_ERROR_FILE_ACCESS_DENIED if the profile is locked.
   */
  nsresult Lock(nsIFile* aProfileDir, nsIProfileUnlocker** aUnlocker);

  /**
   * Unlock a profile directory.  If you're unlocking the directory because
   * the application is in the process of shutting down because of a fatal
   * signal, set aFatalSignal to true.
   */
  nsresult Unlock(bool aFatalSignal = false);

  /**
   * Checks, if the given file has a name that matches potential lock file
   * names. It does not check, if it actually is the currently active lock.
   */
  static bool IsMaybeLockFile(nsIFile* aFile);

  /**
   * Clean up any left over files in the directory.
   */
  nsresult Cleanup();

  /**
   * Get the modification time of a replaced profile lock, otherwise 0.
   */
  nsresult GetReplacedLockTime(PRTime* aResult);

 private:
  bool mHaveLock;
  PRTime mReplacedLockTime;
  nsCOMPtr<nsIFile> mLockFile;

#if defined(XP_WIN)
  HANDLE mLockFileHandle;
#elif defined(XP_UNIX)

  struct RemovePidLockFilesExiting {
    RemovePidLockFilesExiting() = default;
    ~RemovePidLockFilesExiting() { RemovePidLockFiles(false); }
  };

  static void RemovePidLockFiles(bool aFatalSignal);
  static void FatalSignalHandler(int signo
#  ifdef SA_SIGINFO
                                 ,
                                 siginfo_t* info, void* context
#  endif
  );
  static PRCList mPidLockList;

  nsresult LockWithFcntl(nsIFile* aLockFile,
                         nsIProfileUnlocker** aUnlocker = nullptr);

  /**
   * @param aHaveFcntlLock if true, we've already acquired an fcntl lock so this
   * lock is merely an "obsolete" lock to keep out old Firefoxes
   */
  nsresult LockWithSymlink(nsIFile* aLockFile, bool aHaveFcntlLock);

  char* mPidLockFileName;
  int mLockFileDesc;
#endif
};

#endif /* __nsProfileLock_h___ */