summaryrefslogtreecommitdiff
path: root/indra/llvfs/llpidlock.cpp
blob: e64368e8d771e207323371f769b39de81ae300b1 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/** 
 * @file llformat.cpp
 * @date   January 2007
 * @brief string formatting utility
 *
 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "linden_common.h"

#include "llpidlock.h"
#include "lldir.h"
#include "llsd.h"
#include "llsdserialize.h"
#include "llnametable.h"
#include "llframetimer.h"

#if LL_WINDOWS   //For windows platform.

#include <windows.h>

bool isProcessAlive(U32 pid)
{
	return (bool) GetProcessVersion((DWORD)pid);
}

#else   //Everyone Else
bool isProcessAlive(U32 pid)
{   
	return (bool) kill( (pid_t)pid, 0);
}
#endif //Everyone else.


	
class LLPidLockFile
{
	public:
		LLPidLockFile( ) :
			mAutosave(false),
			mSaving(false),
			mWaiting(false),
			mPID(getpid()),
			mNameTable(NULL),
			mClean(true)
		{
			mLockName = gDirUtilp->getTempDir() + gDirUtilp->getDirDelimiter() + "savelock";
		}
		bool requestLock(LLNameTable<void *> *name_table, bool autosave,
						bool force_immediate=FALSE, F32 timeout=300.0);
		bool checkLock();
		void releaseLock();

	private:
		void writeLockFile(LLSD pids);
	public:
		static LLPidLockFile& instance(); // return the singleton black list file
			 
		bool mAutosave;
		bool mSaving;
		bool mWaiting;
		LLFrameTimer mTimer;
		U32  mPID;
		std::string mLockName;
		std::string mSaveName;
		LLSD mPIDS_sd;
		LLNameTable<void*> *mNameTable;
		bool mClean;
};

LLPidLockFile& LLPidLockFile::instance()
{   
	static LLPidLockFile the_file;
	return the_file;
}

void LLPidLockFile::writeLockFile(LLSD pids)
{
	llofstream ofile(mLockName);

	if (!LLSDSerialize::toXML(pids,ofile))
	{
		LL_WARNS() << "Unable to write concurrent save lock file." << LL_ENDL;
	}
	ofile.close();
}

bool LLPidLockFile::requestLock(LLNameTable<void *> *name_table, bool autosave,
								bool force_immediate, F32 timeout)
{
	bool readyToSave = FALSE;

	if (mSaving) return FALSE;	//Bail out if we're currently saving.  Will not queue another save.
	
	if (!mWaiting){
		mNameTable=name_table;
		mAutosave = autosave;
	}

	LLSD out_pids;
	out_pids.append( (LLSD::Integer)mPID );

	llifstream ifile(mLockName);

	if (ifile.is_open()) 
	{									//If file exists, we need to decide whether or not to continue.
		if ( force_immediate
			|| mTimer.hasExpired() )	//Only deserialize if we REALLY need to.
		{

			LLSD in_pids;

			LLSDSerialize::fromXML(in_pids, ifile);	

			//Clean up any dead PIDS that might be in there.
			for (LLSD::array_iterator i=in_pids.beginArray();
				i !=in_pids.endArray();
				++i)
			{
				U32 stored_pid=(*i).asInteger();

				if (isProcessAlive(stored_pid))
				{
					out_pids.append( (*i) );
				}
			}

			readyToSave=TRUE;
		}
		ifile.close();
	}
	else
	{
		readyToSave=TRUE;
	}

	if (!mWaiting)				//Not presently waiting to save.  Queue up.
	{
		mTimer.resetWithExpiry(timeout);
		mWaiting=TRUE;
	}

	if (readyToSave)
	{	//Potential race condition won't kill us. Ignore it.
		writeLockFile(out_pids);
		mSaving=TRUE;
	}
	
	return readyToSave;
}

bool LLPidLockFile::checkLock()
{
	return mWaiting;
}

void LLPidLockFile::releaseLock()
{
	llifstream ifile(mLockName);
	LLSD in_pids;
	LLSD out_pids;
	bool write_file=FALSE;

	LLSDSerialize::fromXML(in_pids, ifile);	

	//Clean up this PID and any dead ones.
	for (LLSD::array_iterator i=in_pids.beginArray();
		i !=in_pids.endArray();
		++i)
	{
		U32 stored_pid=(*i).asInteger();

		if (stored_pid != mPID && isProcessAlive(stored_pid))
		{
			out_pids.append( (*i) );
			write_file=TRUE;
		}
	}
	ifile.close();

	if (write_file)
	{
		writeLockFile(out_pids);
	}
	else
	{
		unlink(mLockName.c_str());
	}

	mSaving=FALSE;
	mWaiting=FALSE;
}

//LLPidLock

void LLPidLock::initClass() { 
	(void) LLPidLockFile::instance(); 
}

bool LLPidLock::checkLock() 
{
	return LLPidLockFile::instance().checkLock();
}

bool LLPidLock::requestLock(LLNameTable<void *> *name_table, bool autosave,
								bool force_immediate, F32 timeout)
{
	return LLPidLockFile::instance().requestLock(name_table,autosave,force_immediate,timeout);
}

void LLPidLock::releaseLock() 
{ 
	return LLPidLockFile::instance().releaseLock(); 
}

bool LLPidLock::isClean() 
{ 
	return LLPidLockFile::instance().mClean; 
}

//getters
LLNameTable<void *> * LLPidLock::getNameTable() 
{ 
    return LLPidLockFile::instance().mNameTable; 
}

bool LLPidLock::getAutosave() 
{ 
	return LLPidLockFile::instance().mAutosave; 
}

bool LLPidLock::getClean() 
{ 
	return LLPidLockFile::instance().mClean; 
}

std::string LLPidLock::getSaveName() 
{ 
	return LLPidLockFile::instance().mSaveName; 
}

//setters
void LLPidLock::setClean(bool clean) 
{ 
	LLPidLockFile::instance().mClean=clean; 
}

void LLPidLock::setSaveName(std::string savename) 
{ 
	LLPidLockFile::instance().mSaveName=savename; 
}

S32 LLPidLock::getPID()
{
    return (S32)getpid();
}