- All component locks moved to memory
[public/netxms.git] / src / server / core / locks.cpp
CommitLineData
c7ca9142
VK
1/*
2** NetXMS - Network Management System
3** Copyright (C) 2003, 2004 Victor Kirhenshtein
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
19** $module: locks.cpp
20**
21**/
22
23#include "nms_core.h"
24
25
b4895bbe
VK
26//
27// Constants
28//
29
30#define MAX_OWNER_INFO 256
31#define NUMBER_OF_LOCKS 5
32
33
34//
35// Lock structure
36//
37
38struct LOCK_INFO
39{
40 DWORD dwLockStatus;
41 TCHAR *pszName;
42 TCHAR szOwnerInfo[MAX_OWNER_INFO];
43};
44
45
b54b2b11
VK
46//
47// Static data
48//
49
b4895bbe
VK
50static MUTEX m_hMutexLockerAccess = NULL;
51static LOCK_INFO m_locks[NUMBER_OF_LOCKS] =
52{
53 { UNLOCKED, _T("Event Processing Policy"), _T("") },
54 { UNLOCKED, _T("User Database"), _T("") },
55 { UNLOCKED, _T("Event Configuration Database"), _T("") },
56 { UNLOCKED, _T("Action Configuration Database"), _T("") },
57 { UNLOCKED, _T("SNMP Trap Configuration"), _T("") }
58};
b54b2b11
VK
59
60
c7ca9142
VK
61//
62// Lock entire database and clear all other locks
63// Will return FALSE if someone already locked database
64//
65
66BOOL InitLocks(DWORD *pdwIpAddr, char *pszInfo)
67{
c7ca9142 68 BOOL bSuccess = FALSE;
b4895bbe 69 char szBuffer[256];
c7ca9142
VK
70
71 *pdwIpAddr = UNLOCKED;
72 pszInfo[0] = 0;
73
b4895bbe
VK
74 // Check current database lock status
75 ConfigReadStr("DBLockStatus", szBuffer, 256, "ERROR");
76 if (!strcmp(szBuffer, "UNLOCKED"))
c7ca9142 77 {
b4895bbe
VK
78 IpToStr(GetLocalIpAddr(), szBuffer);
79 ConfigWriteStr("DBLockStatus", szBuffer, FALSE);
80 GetSysInfoStr(szBuffer);
81 ConfigWriteStr("DBLockInfo", szBuffer, TRUE);
82 m_hMutexLockerAccess = MutexCreate();
83 bSuccess = TRUE;
c7ca9142 84 }
b4895bbe 85 else
b54b2b11 86 {
b4895bbe
VK
87 if (strcmp(szBuffer, "ERROR"))
88 {
89 *pdwIpAddr = ntohl(inet_addr(szBuffer));
90 ConfigReadStr("DBLockInfo", pszInfo, 256, "<error>");
91 }
b54b2b11 92 }
c7ca9142
VK
93
94 return bSuccess;
95}
96
97
b4895bbe
VK
98//
99// Unlock database
100//
101
102void UnlockDB(void)
103{
104 ConfigWriteStr("DBLockStatus", "UNLOCKED", FALSE);
105 ConfigWriteStr("DBLockInfo", "", FALSE);
106}
107
108
c7ca9142
VK
109//
110// Lock component
b54b2b11
VK
111// Function will try to lock specified component. On success, will return TRUE.
112// On failure, will return FALSE and pdwCurrentOwner will be set to the value of lock_status
113// field, and pszCurrentOwnerInfo will be filled with the value of owner_info field.
c7ca9142
VK
114//
115
b54b2b11
VK
116BOOL LockComponent(DWORD dwId, DWORD dwLockBy, char *pszOwnerInfo,
117 DWORD *pdwCurrentOwner, char *pszCurrentOwnerInfo)
c7ca9142 118{
b4895bbe 119 char szBuffer[256];
b54b2b11
VK
120 BOOL bSuccess = FALSE;
121 DWORD dwTemp;
122
123 if (pdwCurrentOwner == NULL)
124 pdwCurrentOwner = &dwTemp;
125 if (pszCurrentOwnerInfo == NULL)
126 pszCurrentOwnerInfo = szBuffer;
127
b4895bbe
VK
128 if (dwId >= NUMBER_OF_LOCKS)
129 {
130 *pdwCurrentOwner = UNLOCKED;
131 strcpy(pszCurrentOwnerInfo, "Unknown component");
132 return FALSE;
133 }
134
135 DbgPrintf(AF_DEBUG_LOCKS, "*Locks* Attempting to lock component \"%s\" by %d (%s)",
136 m_locks[dwId].pszName, dwLockBy, pszOwnerInfo != NULL ? pszOwnerInfo : "NULL");
b54b2b11 137 MutexLock(m_hMutexLockerAccess, INFINITE);
b4895bbe 138 if (m_locks[dwId].dwLockStatus == UNLOCKED)
b54b2b11 139 {
b4895bbe
VK
140 m_locks[dwId].dwLockStatus = dwLockBy;
141 strncpy(m_locks[dwId].szOwnerInfo, pszOwnerInfo, MAX_OWNER_INFO);
142 bSuccess = TRUE;
b54b2b11
VK
143 }
144 else
145 {
b4895bbe
VK
146 *pdwCurrentOwner = m_locks[dwId].dwLockStatus;
147 strcpy(pszCurrentOwnerInfo, m_locks[dwId].szOwnerInfo);
b54b2b11
VK
148 }
149 MutexUnlock(m_hMutexLockerAccess);
150 return bSuccess;
c7ca9142
VK
151}
152
153
154//
155// Unlock component
156//
157
158void UnlockComponent(DWORD dwId)
159{
b54b2b11 160 MutexLock(m_hMutexLockerAccess, INFINITE);
b4895bbe
VK
161 m_locks[dwId].dwLockStatus = UNLOCKED;
162 m_locks[dwId].szOwnerInfo[0] = 0;
b54b2b11 163 MutexUnlock(m_hMutexLockerAccess);
b4895bbe 164 DbgPrintf(AF_DEBUG_LOCKS, "*Locks* Component \"%s\" unlocked", m_locks[dwId].pszName);
c7ca9142 165}
54abbe0e
VK
166
167
168//
169// Unlock all locks for specific session
170//
171
172void RemoveAllSessionLocks(DWORD dwSessionId)
173{
b4895bbe 174 DWORD i;
54abbe0e
VK
175
176 MutexLock(m_hMutexLockerAccess, INFINITE);
b4895bbe
VK
177 for(i = 0; i < NUMBER_OF_LOCKS; i++)
178 if (m_locks[i].dwLockStatus == dwSessionId)
179 {
180 m_locks[i].dwLockStatus = UNLOCKED;
181 m_locks[i].szOwnerInfo[0] = 0;
182 }
54abbe0e 183 MutexUnlock(m_hMutexLockerAccess);
b1dd534d 184 DbgPrintf(AF_DEBUG_LOCKS, "*Locks* All locks for session %d removed", dwSessionId);
54abbe0e 185}